Skip to content

Commit

Permalink
Fix mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
timmens committed Dec 1, 2024
1 parent df8bb95 commit e72608b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 13 additions & 8 deletions src/optimagic/optimization/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def fun_data(self, cost_model: CostModel, monotone: bool) -> pd.DataFrame:
"""
if monotone:
fun = self.monotone_fun
fun: list[float | None] | NDArray[np.float64] = self.monotone_fun
else:
fun = self.fun
task = _task_as_categorical(self.task)
Expand Down Expand Up @@ -213,10 +213,13 @@ def _get_time_per_task(
) -> NDArray[np.float64]:
dummy_task = np.array([1 if t == task else 0 for t in self.task])
if cost_factor is None:
cost_factor = np.array(self.stop_time, dtype=np.float64) - np.array(
self.start_time, dtype=np.float64
)
return np.cumsum(cost_factor * dummy_task)
factor: float | NDArray[np.float64] = np.array(
self.stop_time, dtype=np.float64
) - np.array(self.start_time, dtype=np.float64)
else:
factor = cost_factor

return np.cumsum(factor * dummy_task)

@property
def start_time(self) -> list[float]:
Expand Down Expand Up @@ -326,13 +329,15 @@ def _calculate_monotone_sequence(
# ======================================================================================


def _validate_args_are_all_none_or_lists_of_same_length(*args):
def _validate_args_are_all_none_or_lists_of_same_length(
*args: list[Any] | None,
) -> None:
all_none = all(arg is None for arg in args)
all_list = all(isinstance(arg, list) for arg in args)

if not all_none:
if all_list:
unique_list_lengths = set(map(len, args))
unique_list_lengths = set(map(len, args)) # type: ignore[arg-type]

if len(unique_list_lengths) != 1:
raise ValueError("All list arguments must have the same length.")
Expand All @@ -341,7 +346,7 @@ def _validate_args_are_all_none_or_lists_of_same_length(*args):
raise ValueError("All arguments must be lists of the same length or None.")


def _task_as_categorical(task: list[EvalTask]) -> pd.Series:
def _task_as_categorical(task: list[EvalTask]) -> pd.Categorical:
return pd.Categorical(
[t.value for t in task], categories=[t.value for t in EvalTask]
)
4 changes: 2 additions & 2 deletions src/optimagic/optimization/process_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from optimagic.optimization.history import History
from optimagic.optimization.optimize_result import MultistartInfo, OptimizeResult
from optimagic.parameters.conversion import Converter
from optimagic.typing import AggregationLevel, Direction, PyTree
from optimagic.typing import AggregationLevel, Direction, EvalTask, PyTree
from optimagic.utilities import isscalar


Expand Down Expand Up @@ -115,7 +115,7 @@ def process_multistart_result(
start_time=[np.nan for _ in info.local_optima],
stop_time=[np.nan for _ in info.local_optima],
batches=list(range(len(info.local_optima))),
task=len(info.local_optima) * [None],
task=len(info.local_optima) * [EvalTask.FUN],
)
conv_report = get_convergence_report(report_history)

Expand Down

0 comments on commit e72608b

Please sign in to comment.