Skip to content
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

Check geometry generation function has a suitable signature (optimization) #90

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ansys/simai/core/data/optimizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# SOFTWARE.

import logging
from inspect import signature
from typing import Callable, Dict, List, Optional, Tuple

from tqdm import tqdm
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions tests/test_optimizations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 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.
# ruff: noqa: E731

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."""

my_geometry_generation_function = lambda param_a: f"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"""

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)
Loading