-
Notifications
You must be signed in to change notification settings - Fork 45
Separation of data preparation and plotting for criterion_plot()
#600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
🚀 New features to boost your workflow:
|
7fb293a
to
839ca0c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very nice already. I have a few remarks.
Let me know if you have any questions; thank you!!
I have made the suggested changes and I believe that the failing test is unrelated. |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes. Regarding the type-check errors, I had a look locally.
- You can ignore (on a per-line basis) the mypy errors that are introduced by passing "invalid" data to the
History
class - There seem to be a few bugs that are caught by mypy for which we do not have tests. For example this line is flagged
fun = res.multistart_info.exploration_results.tolist()[::-1] + stacked.fun
by mypy, saying the[...].exploration_results
has no methodtolist()
. This seems to be correct. For these cases, can you check whether it is possible to write a simple test case that would be triggered in the current version of the code, and then fix it? - As mentioned elsewhere, for the problem where mypy thinks
multistart_info
is None, you can doif res.multistart_info
directly, which should work. We will have to see if this is as readable as before.
I have a few more comments on _OptimizeData
:
- I would like to see it defined before it is used in the return type hint of
_retrieve_optimization_data
. This also applies for the other dataclasses and functions. - Is there no way to get the
start_params
in the case of_retrieve_optimization_data_from_results_object
? - Although I proposed it, I am not entirely happy with the class name
_OptimizeData
. It is too closely related toOptimizeResult
. Can you propose some alternatives that make clear that it is simply a data container with intermediate results containing the histories? - Given that it is a data container it should be a frozen. In the current setup this is problematic with the
name
field. You could just pass thename
argument to the retrieval functions. Alternatively, instead of returning a list of optimize data, you can also just return a dict[str, _OptimizeData].
|
||
Returns: | ||
plotly.graph_objs._figure.Figure: The figure. | ||
|
||
""" | ||
# ================================================================================== | ||
# Process inputs | ||
# ================================================================================== | ||
|
||
results = _harmonize_inputs_to_dict(results, names) | ||
|
||
if not isinstance(palette, list): | ||
palette = [palette] | ||
palette = itertools.cycle(palette) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
palette = itertools.cycle(palette) | |
palette_cycle = itertools.cycle(palette) |
def _extract_criterion_plot_data( | ||
data: list["_OptimizeData"], | ||
max_evaluations: int | None, | ||
palette: Iterator[str], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
palette: Iterator[str], | |
palette: itertools.cycle[str], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes a TypeError: 'type' object is not subscriptable
. The problem is that we can't subscript [str]
for itertools.cycle
. So, I think that we could:
- Use as
palette_cycle: itertools.cycle
. However, this doesn't convey that it is of typestr
. - Leave as
palette_cycle: Iterator[str]
Personally I believe using Iterator[str]
would be better.
cb9f65b
to
baeae7a
Compare
Thanks for the review. I have pushed the suggested changes and mypy seems to be passing.
How about
optimagic/src/optimagic/optimization/multistart.py Lines 291 to 296 in 9a40583
According to the type hints of I believe this is not a bug and the usage of diff --git a/src/optimagic/optimization/optimize_result.py b/src/optimagic/optimization/optimize_result.py
index f2895cf..65860b2 100644
@@ -218,7 +219,7 @@ class MultistartInfo:
start_parameters: list[PyTree]
local_optima: list[OptimizeResult]
exploration_sample: list[PyTree]
- exploration_results: list[float]
+ exploration_results: NDArray[np.float64] |
Summary of changes
Separated the logic for data preparation and plotting in
criterion_plot()
. This is done to easily enable the addition of plotting backends.LineData
stores required data for plotting a single line.CriterionPlotData
stores all backend agnostic data needed forcriterion_plot()
[lines and multistart_lines if applicable]PlotConfig
stores global settings of plotsAdditionally, we could add a new parameter such as
return_data
- which would return theplot_data
instead of plotting. This can be discussed, if it is worth looking into.