From 7928964a828c4afd9211902227bb74e972a9e994 Mon Sep 17 00:00:00 2001 From: kliment-slice Date: Fri, 27 Sep 2024 00:19:36 -0500 Subject: [PATCH 1/2] check_geometry_generation_fn_signature and tests --- src/ansys/simai/core/data/optimizations.py | 10 ++++ tests/test_optimizations.py | 62 ++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 tests/test_optimizations.py diff --git a/src/ansys/simai/core/data/optimizations.py b/src/ansys/simai/core/data/optimizations.py index 934578cc..179cd291 100644 --- a/src/ansys/simai/core/data/optimizations.py +++ b/src/ansys/simai/core/data/optimizations.py @@ -21,6 +21,7 @@ # SOFTWARE. import logging +from inspect import signature from typing import Callable, Dict, List, Optional, Tuple from tqdm import tqdm @@ -161,6 +162,7 @@ def my_geometry_generation_function(param_a, param_b): print(results) """ workspace_id = get_id_from_identifiable(workspace, True, self._client._current_workspace) + _check_geometry_generation_fn_signature(geometry_generation_fn, geometry_parameters) if not minimize and not maximize: raise InvalidArguments("No global coefficient to optimize.") objective = {} @@ -221,6 +223,14 @@ def my_geometry_generation_function(param_a, param_b): return iterations_results +def _check_geometry_generation_fn_signature(geometry_generation_fn, geometry_parameters): + geometry_generation_fn_args = signature(geometry_generation_fn).parameters + if geometry_generation_fn_args.keys() != geometry_parameters.keys(): + raise InvalidArguments( + f"geometry_generation_fn requires the following signature: {list(geometry_parameters.keys())}, but got: {list(geometry_generation_fn_args.keys())}" + ) + + # Undocumented for now, users don't really need to interact with it class OptimizationTrialRunDirectory(Directory[OptimizationTrialRun]): _data_model = OptimizationTrialRun diff --git a/tests/test_optimizations.py b/tests/test_optimizations.py new file mode 100644 index 00000000..647f8d15 --- /dev/null +++ b/tests/test_optimizations.py @@ -0,0 +1,62 @@ +# Copyright (C) 2024 ANSYS, Inc. and/or its affiliates. +# SPDX-License-Identifier: MIT +# +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +import pytest + +from ansys.simai.core.data.optimizations import _check_geometry_generation_fn_signature +from ansys.simai.core.errors import InvalidArguments + + +def test_geometry_generation_fn_invalid_signature(simai_client): + """WHEN a geometry_generation_fn signature does not match geometry_parameters keys + THEN an InvalidArgument error is raised.""" + + def my_geometry_generation_function(param_a): + return "test_geometry/param_{param_a}.vtp" + + geometry_parameters = { + "param_a": {"bounds": (-12.5, 12.5)}, + "param_ski": {"choices": (0.1, 1.0)}, + } + + with pytest.raises(InvalidArguments) as exc: + simai_client.optimizations.run( + geometry_generation_fn=my_geometry_generation_function, + geometry_parameters=geometry_parameters, + boundary_conditions={"abc": 3.0}, + n_iters=5, + ) + + assert "geometry_generation_fn requires the following signature" in str(exc.value) + + +def test_check_geometry_generation_fn_valid_signature(): + """WHEN geometry_generation_fn signature matches geometry_parameters keys + THEN check passes""" + + def my_geometry_generation_function(param_c, param_d): + return "test_geometry/param_{param_c}_{param_d}.vtp" + + geometry_parameters = {"param_c": {"bounds": (-12.5, 12.5)}, "param_d": {"choices": (0.1, 1.0)}} + + _check_geometry_generation_fn_signature(my_geometry_generation_function, geometry_parameters) From 5d43ad4949a3fe3dec6cb55b046195d3b48e29f3 Mon Sep 17 00:00:00 2001 From: kliment-slice Date: Wed, 2 Oct 2024 11:09:28 -0500 Subject: [PATCH 2/2] lambdas and noqa --- tests/test_optimizations.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/test_optimizations.py b/tests/test_optimizations.py index 647f8d15..6762c297 100644 --- a/tests/test_optimizations.py +++ b/tests/test_optimizations.py @@ -19,7 +19,7 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. - +# ruff: noqa: E731 import pytest @@ -31,8 +31,7 @@ def test_geometry_generation_fn_invalid_signature(simai_client): """WHEN a geometry_generation_fn signature does not match geometry_parameters keys THEN an InvalidArgument error is raised.""" - def my_geometry_generation_function(param_a): - return "test_geometry/param_{param_a}.vtp" + my_geometry_generation_function = lambda param_a: f"test_geometry/param_{param_a}.vtp" geometry_parameters = { "param_a": {"bounds": (-12.5, 12.5)}, @@ -54,9 +53,9 @@ def test_check_geometry_generation_fn_valid_signature(): """WHEN geometry_generation_fn signature matches geometry_parameters keys THEN check passes""" - def my_geometry_generation_function(param_c, param_d): - return "test_geometry/param_{param_c}_{param_d}.vtp" - + my_geometry_generation_function = ( + lambda param_c, param_d: f"test_geometry/param_{param_c}_{param_d}.vtp" + ) geometry_parameters = {"param_c": {"bounds": (-12.5, 12.5)}, "param_d": {"choices": (0.1, 1.0)}} _check_geometry_generation_fn_signature(my_geometry_generation_function, geometry_parameters)