From fbda6e9c03906a9f6541e1ba4a5ed61bf27c88c0 Mon Sep 17 00:00:00 2001 From: Lennart Purucker Date: Wed, 16 Oct 2024 15:57:53 +0200 Subject: [PATCH 1/2] add: loose init model from run --- openml/runs/functions.py | 6 ++++-- openml/setups/functions.py | 6 ++++-- tests/test_runs/test_run_functions.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/openml/runs/functions.py b/openml/runs/functions.py index c6af4a481..b16af0b80 100644 --- a/openml/runs/functions.py +++ b/openml/runs/functions.py @@ -364,7 +364,7 @@ def get_run_trace(run_id: int) -> OpenMLRunTrace: return OpenMLRunTrace.trace_from_xml(trace_xml) -def initialize_model_from_run(run_id: int) -> Any: +def initialize_model_from_run(run_id: int, *, strict_version: bool = True) -> Any: """ Initialized a model based on a run_id (i.e., using the exact same parameter settings) @@ -373,6 +373,8 @@ def initialize_model_from_run(run_id: int) -> Any: ---------- run_id : int The Openml run_id + strict_version: bool (default=True) + See `flow_to_model` strict_version. Returns ------- @@ -382,7 +384,7 @@ def initialize_model_from_run(run_id: int) -> Any: # TODO(eddiebergman): I imagine this is None if it's not published, # might need to raise an explicit error for that assert run.setup_id is not None - return initialize_model(run.setup_id) + return initialize_model(setup_id=run.setup_id, strict_version=strict_version) def initialize_model_from_trace( diff --git a/openml/setups/functions.py b/openml/setups/functions.py index 0bcd2b4e2..1f4703899 100644 --- a/openml/setups/functions.py +++ b/openml/setups/functions.py @@ -265,7 +265,7 @@ def __list_setups( return setups -def initialize_model(setup_id: int) -> Any: +def initialize_model(setup_id: int, *, strict_version: bool) -> Any: """ Initialized a model based on a setup_id (i.e., using the exact same parameter settings) @@ -274,6 +274,8 @@ def initialize_model(setup_id: int) -> Any: ---------- setup_id : int The Openml setup_id + strict_version: bool (default=True) + See `flow_to_model` strict_version. Returns ------- @@ -294,7 +296,7 @@ def initialize_model(setup_id: int) -> Any: subflow = flow subflow.parameters[hyperparameter.parameter_name] = hyperparameter.value - return flow.extension.flow_to_model(flow) + return flow.extension.flow_to_model(flow, strict_version=strict_version) def _to_dict( diff --git a/tests/test_runs/test_run_functions.py b/tests/test_runs/test_run_functions.py index d43a8bab5..2bd9ee0ed 100644 --- a/tests/test_runs/test_run_functions.py +++ b/tests/test_runs/test_run_functions.py @@ -1905,6 +1905,16 @@ def test_delete_run(self): _run_id = run.run_id assert delete_run(_run_id) + @unittest.skipIf( + Version(sklearn.__version__) < Version("0.20"), + reason="SimpleImputer doesn't handle mixed type DataFrame as input", + ) + def test_initialize_model_from_run_nonstrict(self): + # We cannot guarantee that a run with an older version exists on the server. + # Thus, we test it simply with a run that we know exists that might not be loose. + # This tests all lines of code for OpenML but not the initialization, which we do not want to guarantee anyhow. + _ = openml.runs.initialize_model_from_run(run_id=1, strict_version=False) + @mock.patch.object(requests.Session, "delete") def test_delete_run_not_owned(mock_delete, test_files_directory, test_api_key): From e6cc89f36436509d5b8320d0e7aff15c3c7ea061 Mon Sep 17 00:00:00 2001 From: Lennart Purucker Date: Wed, 16 Oct 2024 17:32:12 +0200 Subject: [PATCH 2/2] fix: add default option for strict --- openml/setups/functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openml/setups/functions.py b/openml/setups/functions.py index 1f4703899..877384636 100644 --- a/openml/setups/functions.py +++ b/openml/setups/functions.py @@ -265,7 +265,7 @@ def __list_setups( return setups -def initialize_model(setup_id: int, *, strict_version: bool) -> Any: +def initialize_model(setup_id: int, *, strict_version: bool = True) -> Any: """ Initialized a model based on a setup_id (i.e., using the exact same parameter settings)