Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion openml/tasks/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ def create_task(
return task_cls(
task_id=None,
task_type_id=task_type,
task_type="None", # TODO: refactor to get task type string from ID.
task_type=task_type.type_str,
data_set_id=dataset_id,
target_name=target_name, # type: ignore
estimation_procedure_id=estimation_procedure_id,
Expand Down
18 changes: 17 additions & 1 deletion openml/tasks/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ class TaskType(Enum):
SUBGROUP_DISCOVERY = 8
MULTITASK_REGRESSION = 9

@property
def type_str(self) -> str:
"""Return the OpenML string representation of the task type."""
mapping = {
TaskType.SUPERVISED_CLASSIFICATION: "Supervised Classification",
TaskType.SUPERVISED_REGRESSION: "Supervised Regression",
TaskType.LEARNING_CURVE: "Learning Curve",
TaskType.SUPERVISED_DATASTREAM_CLASSIFICATION: "Supervised Data Stream Classification",
TaskType.CLUSTERING: "Clustering",
TaskType.MACHINE_LEARNING_CHALLENGE: "Machine Learning Challenge",
TaskType.SURVIVAL_ANALYSIS: "Survival Analysis",
TaskType.SUBGROUP_DISCOVERY: "Subgroup Discovery",
TaskType.MULTITASK_REGRESSION: "Multi-task Regression",
}
return mapping[self]


class _EstimationProcedure(TypedDict):
type: str | None
Expand Down Expand Up @@ -90,7 +106,7 @@ def __init__( # noqa: PLR0913
):
self.task_id = int(task_id) if task_id is not None else None
self.task_type_id = task_type_id
self.task_type = task_type
self.task_type = task_type if task_type is not None else task_type_id.type_str
self.dataset_id = int(data_set_id)
self.target_name = target_name
resolved_estimation_procedure_id = self._resolve_estimation_procedure_id(
Expand Down
53 changes: 53 additions & 0 deletions tests/test_tasks/test_task_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,56 @@ def test_delete_unknown_task(mock_delete, test_files_directory, test_server_v1,
task_url = test_server_v1 + "task/9999999"
assert task_url == mock_delete.call_args.args[0]
assert test_apikey_v1 == mock_delete.call_args.kwargs.get("params", {}).get("api_key")


def test_create_task_sets_task_type() -> None:
task = openml.tasks.create_task(
TaskType.SUPERVISED_CLASSIFICATION,
dataset_id=1,
target_name="class",
estimation_procedure_id=1,
)
assert task.task_type == "Supervised Classification"
assert task.task_type_id == TaskType.SUPERVISED_CLASSIFICATION

task_reg = openml.tasks.create_task(
TaskType.SUPERVISED_REGRESSION,
dataset_id=1,
target_name="target",
estimation_procedure_id=7,
)
assert task_reg.task_type == "Supervised Regression"
assert task_reg.task_type_id == TaskType.SUPERVISED_REGRESSION

task_cluster = openml.tasks.create_task(
TaskType.CLUSTERING,
dataset_id=1,
estimation_procedure_id=17,
)
assert task_cluster.task_type == "Clustering"
assert task_cluster.task_type_id == TaskType.CLUSTERING

task_lc = openml.tasks.create_task(
TaskType.LEARNING_CURVE,
dataset_id=1,
target_name="class",
estimation_procedure_id=13,
)
assert task_lc.task_type == "Learning Curve"
assert task_lc.task_type_id == TaskType.LEARNING_CURVE


def test_task_type_str() -> None:
expected = {
TaskType.SUPERVISED_CLASSIFICATION: "Supervised Classification",
TaskType.SUPERVISED_REGRESSION: "Supervised Regression",
TaskType.LEARNING_CURVE: "Learning Curve",
TaskType.SUPERVISED_DATASTREAM_CLASSIFICATION: "Supervised Data Stream Classification",
TaskType.CLUSTERING: "Clustering",
TaskType.MACHINE_LEARNING_CHALLENGE: "Machine Learning Challenge",
TaskType.SURVIVAL_ANALYSIS: "Survival Analysis",
TaskType.SUBGROUP_DISCOVERY: "Subgroup Discovery",
TaskType.MULTITASK_REGRESSION: "Multi-task Regression",
}
for task_type, expected_str in expected.items():
assert task_type.type_str == expected_str
Loading