Skip to content

Commit

Permalink
Dummy optimize can ignore warning now. Closes #107
Browse files Browse the repository at this point in the history
  • Loading branch information
AKuederle committed May 23, 2024
1 parent ee93e22 commit 02fb4fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 6 additions & 0 deletions tests/test_pipelines/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,12 @@ def test_warning(self):

assert len(w.list) == 0

def test_warning_suppression(self):
with pytest.warns(None) as w:
DummyOptimize(DummyOptimizablePipeline(), ignore_potential_user_error_warning=True).optimize(dataset=None)

assert len(w.list) == 0


class TestOptimizeBase:
optimizer: BaseOptimize
Expand Down
16 changes: 12 additions & 4 deletions tpcp/optimize/_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class DummyOptimize(BaseOptimize[PipelineT, DatasetT]):
pipeline
The pipeline to wrap.
It will not be optimized in any way, but simply copied to `self.optimized_pipeline_` if `optimize` is called.
ignore_potential_user_error_warning
If True, the warning about using a pipeline that implements `self_optimize` with `DummyOptimize` will be
ignored.
Only use this, if you are sure about what you are doing.
Other Parameters
----------------
Expand All @@ -82,11 +86,13 @@ class DummyOptimize(BaseOptimize[PipelineT, DatasetT]):
"""

pipeline: Parameter[PipelineT]
ignore_potential_user_error_warning: Parameter[bool]

optimized_pipeline_: PipelineT

def __init__(self, pipeline: PipelineT) -> None:
def __init__(self, pipeline: PipelineT, *, ignore_potential_user_error_warning: bool = False) -> None:
self.pipeline = pipeline
self.ignore_potential_user_error_warning = ignore_potential_user_error_warning

def optimize(self, dataset: DatasetT, **optimize_params: Any) -> Self: # noqa: ARG002
"""Run the "dummy" optimization.
Expand All @@ -105,14 +111,16 @@ def optimize(self, dataset: DatasetT, **optimize_params: Any) -> Self: # noqa:
"""
self.dataset = dataset
if hasattr(self.pipeline, "self_optimize"):
if not self.ignore_potential_user_error_warning and hasattr(self.pipeline, "self_optimize"):
warnings.warn(
"You are using `DummyOptimize` with a pipeline that implements `self_optimize` and, hence, indicates "
"that the pipeline can be optimized. "
"`DummyOptimize` does never call this method and skips any optimization steps! "
"Use `Optimize` if you actually want to optimize your pipeline.",
"Use `Optimize` if you actually want to optimize your pipeline.\n\n "
"If you are sure that you want to use `DummyOptimize` with this pipeline, set "
"``ignore_potential_user_error_warning=True`` to hide this warning in the future.",
PotentialUserErrorWarning,
stacklevel=2,
stacklevel=1,
)
self.optimized_pipeline_ = self.pipeline.clone()
return self
Expand Down

0 comments on commit 02fb4fe

Please sign in to comment.