From 1dcb6429e36606d3e0fea0e0efbe6456d9798fb0 Mon Sep 17 00:00:00 2001 From: Janos Gabler Date: Tue, 12 Nov 2024 19:10:18 +0100 Subject: [PATCH] Do not use .__algo_info__ when .algo_info is available. --- .tools/create_algo_selection_code.py | 33 +++++++++---------- src/optimagic/algorithms.py | 13 +++----- src/optimagic/visualization/history_plots.py | 4 +-- .../optimization/test_history_collection.py | 2 +- .../optimization/test_many_algorithms.py | 2 +- .../test_with_nonlinear_constraints.py | 2 +- 6 files changed, 25 insertions(+), 31 deletions(-) diff --git a/.tools/create_algo_selection_code.py b/.tools/create_algo_selection_code.py index 29c0b4983..07ae8ae7f 100644 --- a/.tools/create_algo_selection_code.py +++ b/.tools/create_algo_selection_code.py @@ -109,7 +109,7 @@ def _get_algorithms_in_module(module: ModuleType) -> dict[str, Type[Algorithm]]: } algos = {} for candidate in candidate_dict.values(): - name = candidate.__algo_info__.name + name = candidate.algo_info.name if issubclass(candidate, Algorithm) and candidate is not Algorithm: algos[name] = candidate return algos @@ -119,7 +119,7 @@ def _get_algorithms_in_module(module: ModuleType) -> dict[str, Type[Algorithm]]: # Functions to filter algorithms by selectors # ====================================================================================== def _is_gradient_based(algo: Type[Algorithm]) -> bool: - return algo.__algo_info__.needs_jac # type: ignore + return algo.algo_info.needs_jac def _is_gradient_free(algo: Type[Algorithm]) -> bool: @@ -127,7 +127,7 @@ def _is_gradient_free(algo: Type[Algorithm]) -> bool: def _is_global(algo: Type[Algorithm]) -> bool: - return algo.__algo_info__.is_global # type: ignore + return algo.algo_info.is_global def _is_local(algo: Type[Algorithm]) -> bool: @@ -135,31 +135,31 @@ def _is_local(algo: Type[Algorithm]) -> bool: def _is_bounded(algo: Type[Algorithm]) -> bool: - return algo.__algo_info__.supports_bounds # type: ignore + return algo.algo_info.supports_bounds def _is_linear_constrained(algo: Type[Algorithm]) -> bool: - return algo.__algo_info__.supports_linear_constraints # type: ignore + return algo.algo_info.supports_linear_constraints def _is_nonlinear_constrained(algo: Type[Algorithm]) -> bool: - return algo.__algo_info__.supports_nonlinear_constraints # type: ignore + return algo.algo_info.supports_nonlinear_constraints def _is_scalar(algo: Type[Algorithm]) -> bool: - return algo.__algo_info__.solver_type == AggregationLevel.SCALAR # type: ignore + return algo.algo_info.solver_type == AggregationLevel.SCALAR def _is_least_squares(algo: Type[Algorithm]) -> bool: - return algo.__algo_info__.solver_type == AggregationLevel.LEAST_SQUARES # type: ignore + return algo.algo_info.solver_type == AggregationLevel.LEAST_SQUARES def _is_likelihood(algo: Type[Algorithm]) -> bool: - return algo.__algo_info__.solver_type == AggregationLevel.LIKELIHOOD # type: ignore + return algo.algo_info.solver_type == AggregationLevel.LIKELIHOOD def _is_parallel(algo: Type[Algorithm]) -> bool: - return algo.__algo_info__.supports_parallelism # type: ignore + return algo.algo_info.supports_parallelism def _get_filters() -> dict[str, Callable[[Type[Algorithm]], bool]]: @@ -385,7 +385,7 @@ def _all(self) -> list[Type[Algorithm]]: def _available(self) -> list[Type[Algorithm]]: _all = self._all() return [ - a for a in _all if a.__algo_info__.is_available # type: ignore + a for a in _all if a.algo_info.is_available # type: ignore ] @property @@ -398,22 +398,19 @@ def Available(self) -> list[Type[Algorithm]]: @property def AllNames(self) -> list[str]: - return [a.__algo_info__.name for a in self._all()] # type: ignore + return [str(a.name) for a in self._all()] @property def AvailableNames(self) -> list[str]: - return [a.__algo_info__.name for a in self._available()] # type: ignore + return [str(a.name) for a in self._available()] @property def _all_algorithms_dict(self) -> dict[str, Type[Algorithm]]: - return {a.__algo_info__.name: a for a in self._all()} # type: ignore + return {str(a.name): a for a in self._all()} @property def _available_algorithms_dict(self) -> dict[str, Type[Algorithm]]: - return { - a.__algo_info__.name: a # type: ignore - for a in self._available() - } + return {str(a.name): a for a in self._available()} """) return out diff --git a/src/optimagic/algorithms.py b/src/optimagic/algorithms.py index 4341744b4..a892f5a51 100644 --- a/src/optimagic/algorithms.py +++ b/src/optimagic/algorithms.py @@ -90,7 +90,7 @@ def _available(self) -> list[Type[Algorithm]]: return [ a for a in _all - if a.__algo_info__.is_available # type: ignore + if a.algo_info.is_available # type: ignore ] @property @@ -103,22 +103,19 @@ def Available(self) -> list[Type[Algorithm]]: @property def AllNames(self) -> list[str]: - return [a.__algo_info__.name for a in self._all()] # type: ignore + return [str(a.name) for a in self._all()] @property def AvailableNames(self) -> list[str]: - return [a.__algo_info__.name for a in self._available()] # type: ignore + return [str(a.name) for a in self._available()] @property def _all_algorithms_dict(self) -> dict[str, Type[Algorithm]]: - return {a.__algo_info__.name: a for a in self._all()} # type: ignore + return {str(a.name): a for a in self._all()} @property def _available_algorithms_dict(self) -> dict[str, Type[Algorithm]]: - return { - a.__algo_info__.name: a # type: ignore - for a in self._available() - } + return {str(a.name): a for a in self._available()} @dataclass(frozen=True) diff --git a/src/optimagic/visualization/history_plots.py b/src/optimagic/visualization/history_plots.py index 1d2247e71..50b3f09e9 100644 --- a/src/optimagic/visualization/history_plots.py +++ b/src/optimagic/visualization/history_plots.py @@ -199,9 +199,9 @@ def _harmonize_inputs_to_dict(results, names): def _convert_key_to_str(key: Any) -> str: if inspect.isclass(key) and issubclass(key, Algorithm): - out = key.__algo_info__.name # type: ignore + out = str(key.name) elif isinstance(key, Algorithm): - out = key.__algo_info__.name # type: ignore + out = str(key.name) else: out = str(key) return out diff --git a/tests/optimagic/optimization/test_history_collection.py b/tests/optimagic/optimization/test_history_collection.py index b94d097db..743b8cf43 100644 --- a/tests/optimagic/optimization/test_history_collection.py +++ b/tests/optimagic/optimization/test_history_collection.py @@ -17,7 +17,7 @@ OPTIMIZERS = [] BOUNDED = [] for name, algo in AVAILABLE_ALGORITHMS.items(): - info = algo.__algo_info__ + info = algo.algo_info if not info.disable_history: if info.supports_parallelism: OPTIMIZERS.append(name) diff --git a/tests/optimagic/optimization/test_many_algorithms.py b/tests/optimagic/optimization/test_many_algorithms.py index 1882aeb08..47fbe7553 100644 --- a/tests/optimagic/optimization/test_many_algorithms.py +++ b/tests/optimagic/optimization/test_many_algorithms.py @@ -28,7 +28,7 @@ BOUNDED_ALGORITHMS = [] for name, algo in LOCAL_ALGORITHMS.items(): - if algo.__algo_info__.supports_bounds: + if algo.algo_info.supports_bounds: BOUNDED_ALGORITHMS.append(name) diff --git a/tests/optimagic/optimization/test_with_nonlinear_constraints.py b/tests/optimagic/optimization/test_with_nonlinear_constraints.py index 1df8bef3a..baaef2f8f 100644 --- a/tests/optimagic/optimization/test_with_nonlinear_constraints.py +++ b/tests/optimagic/optimization/test_with_nonlinear_constraints.py @@ -121,7 +121,7 @@ def test_nonlinear_optimization(nlc_2d_example, algorithm, constr_type): warnings.simplefilter("ignore") result = maximize(algorithm=algorithm, **kwargs[constr_type]) - if NLC_ALGORITHMS[algorithm].__algo_info__.is_global: + if NLC_ALGORITHMS[algorithm].algo_info.is_global: decimal = 0 else: decimal = 4