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

build preset to take only supported values #84

Merged
merged 3 commits into from
Sep 16, 2024
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
40 changes: 29 additions & 11 deletions src/ansys/simai/core/data/model_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
if TYPE_CHECKING:
from ansys.simai.core.data.projects import Project

SupportedBuildPresets = {
"debug": "debug",
"1_day": "short",
"2_days": "default",
"7_days": "long",
}


@dataclass
class DomainAxisDefinition:
Expand Down Expand Up @@ -210,11 +217,11 @@ class ModelConfiguration:

| *debug*: < 30 min, only 4 dat

| *short*: < 24 hours
| *1_day*: < 24 hours

| *default*: < 2 days, default value.
| *2_days*: < 2 days, default value.

| *long*: < 1 week
| *7_days*: < 1 week
continuous: indicates if continuous learning is enabled. Default is False.
input: the inputs of the model.
output: the outputs of the model.
Expand Down Expand Up @@ -280,12 +287,6 @@ class ModelConfiguration:
"""

project: "Optional[Project]" = None
build_preset: Literal[
"debug",
"short",
"default",
"long",
] = "default"
continuous: bool = False
input: ModelInput = field(default_factory=lambda: ModelInput())
output: ModelOutput = field(default_factory=lambda: ModelOutput())
Expand Down Expand Up @@ -313,11 +314,26 @@ def __get_gc(self):

global_coefficients: list[GlobalCoefficientDefinition] = property(__get_gc, __set_gc)

def __validate_build_preset(self, val: str):
if val not in SupportedBuildPresets:
raise InvalidArguments(
f"Invalid value for build_preset, build_preset should be one of {list(SupportedBuildPresets.keys())}"
)

def __set_build_preset(self, val: str):
self.__validate_build_preset(val)
self.__dict__["build_preset"] = val

def __get_build_preset(self):
return self.__dict__.get("build_preset")

build_preset = property(__get_build_preset, __set_build_preset)
yias marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
project: "Project",
boundary_conditions: Optional[dict[str, Any]] = None,
build_preset: Optional[str] = None,
build_preset: Optional[str] = "debug",
continuous: bool = False,
fields: Optional[dict[str, Any]] = None,
global_coefficients: Optional[list[GlobalCoefficientDefinition]] = None,
Expand Down Expand Up @@ -441,7 +457,7 @@ def _to_payload(self):

return {
"boundary_conditions": bcs,
"build_preset": self.build_preset,
"build_preset": SupportedBuildPresets[self.build_preset],
"continuous": self.continuous,
"fields": flds,
"global_coefficients": gcs,
Expand All @@ -462,3 +478,5 @@ def compute_global_coefficient(self):
)
for gc in self.global_coefficients
]

del __set_build_preset, __get_build_preset
47 changes: 47 additions & 0 deletions tests/test_model_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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.model_configuration import ModelConfiguration, SupportedBuildPresets
from ansys.simai.core.errors import InvalidArguments


def test_build_preset_error(simai_client):
"""WHEN build_preset gets a non-supported value
THEN an InvalidArgument is raised."""

raw_project = {
"id": "xX007Xx",
"name": "fifi",
}

project = simai_client._project_directory._model_from(raw_project)

with pytest.raises(InvalidArguments) as excinfo:
ModelConfiguration(
project=project,
build_preset="not_valid_value",
)

assert f"{list(SupportedBuildPresets)}" in excinfo.value
Loading