Skip to content

Commit

Permalink
Add /estimationprocedure/list (#121)
Browse files Browse the repository at this point in the history
* Add /estimationprocedure/list

* [no ci] Add changes for 'Others'

specifically, estimationprocedure/list and evaluationmeasure/list
  • Loading branch information
PGijsbers authored Dec 1, 2023
1 parent 8d78792 commit 5bbe7c3
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ includes datasets which are private.

The `limit` and `offset` parameters can now be used independently, you no longer need
to provide both if you wish to set only one.

## Others

### `GET /estimationprocedure/list`
The `ttid` field has been renamed to `task_type_id`.
All values are now typed.
Outer levels of nesting have been removed.

#### `GET /evaluationmeasures/list`
Outer levels of nesting have been removed.
9 changes: 9 additions & 0 deletions src/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
from core.errors import DatasetError


def _str_to_bool(string: str) -> bool:
if string.casefold() in ["true", "1", "yes"]:
return True
if string.casefold() in ["false", "0", "no"]:
return False
msg = f"Could not parse {string=} as bool."
raise ValueError(msg)


def _format_error(*, code: DatasetError, message: str) -> dict[str, str]:
"""Formatter for JSON bodies of OpenML error codes."""
return {"code": str(code), "message": message}
Expand Down
24 changes: 23 additions & 1 deletion src/database/evaluations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Any
from typing import Any, Iterable

from core.formatting import _str_to_bool
from schemas.datasets.openml import EstimationProcedure
from sqlalchemy import Connection, CursorResult, text


Expand All @@ -14,3 +16,23 @@ def get_math_functions(function_type: str, connection: Connection) -> CursorResu
),
parameters={"function_type": function_type},
)


def get_estimation_procedures(connection: Connection) -> Iterable[EstimationProcedure]:
rows = connection.execute(
text(
"""
SELECT `id` as 'id_', `ttid` as 'task_type_id', `name`, `type` as 'type_',
`repeats`, `folds`, `stratified_sampling`, `percentage`
FROM estimation_procedure
""",
),
)
typed_rows = [
{
k: v if k != "stratified_sampling" or v is None else _str_to_bool(v)
for k, v in row.items()
}
for row in rows.mappings()
]
return [EstimationProcedure(**typed_row) for typed_row in typed_rows]
2 changes: 2 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fastapi import FastAPI
from routers.mldcat_ap.dataset import router as mldcat_ap_router
from routers.openml.datasets import router as datasets_router
from routers.openml.estimation_procedure import router as estimationprocedure_router
from routers.openml.evaluations import router as evaluationmeasures_router
from routers.openml.qualities import router as qualities_router
from routers.openml.tasktype import router as ttype_router
Expand Down Expand Up @@ -43,6 +44,7 @@ def create_api() -> FastAPI:
app.include_router(mldcat_ap_router)
app.include_router(ttype_router)
app.include_router(evaluationmeasures_router)
app.include_router(estimationprocedure_router)
return app


Expand Down
17 changes: 17 additions & 0 deletions src/routers/openml/estimation_procedure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Annotated, Iterable

from database.evaluations import get_estimation_procedures as db_get_estimation_procedures
from fastapi import APIRouter, Depends
from schemas.datasets.openml import EstimationProcedure
from sqlalchemy import Connection

from routers.dependencies import expdb_connection

router = APIRouter(prefix="/estimationprocedure", tags=["estimationprocedure"])


@router.get("/list", response_model_exclude_none=True)
def get_estimation_procedures(
expdb: Annotated[Connection, Depends(expdb_connection)],
) -> Iterable[EstimationProcedure]:
return db_get_estimation_procedures(expdb)
11 changes: 11 additions & 0 deletions src/schemas/datasets/openml.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ class Feature(BaseModel):
nominal_values: list[str] | None


class EstimationProcedure(BaseModel):
id_: int = Field(serialization_alias="id")
task_type_id: int
name: str
type_: str = Field(serialization_alias="type")
percentage: int | None
repeats: int | None
folds: int | None
stratified_sampling: bool | None


class DatasetMetadata(BaseModel):
id_: int = Field(json_schema_extra={"example": 1}, alias="id")
visibility: Visibility = Field(json_schema_extra={"example": Visibility.PUBLIC})
Expand Down
Loading

0 comments on commit 5bbe7c3

Please sign in to comment.