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

Add LinkItem type; Update model_params field to support new LinkItem type #75

Merged
merged 3 commits into from
Nov 14, 2023
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
2 changes: 1 addition & 1 deletion python/ngen_conf/src/ngen/config/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.10'
__version__ = '0.2.0'
16 changes: 15 additions & 1 deletion python/ngen_conf/src/ngen/config/bmi_formulation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pydantic import BaseModel, FilePath, DirectoryPath, PyObject, Field, root_validator, validator
from typing import Mapping, Optional, Union, Sequence, Any
from typing_extensions import TypeAlias
from pathlib import Path
from sys import platform

Expand All @@ -8,6 +9,15 @@
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)

Numeric: TypeAlias = Union[int, float]

class LinkItem(BaseModel, allow_population_by_field_name = True):
"""BMIParams model_params associative type that represents a variable or
feature and its source (e.g. source: hydrofarbic, from_var: areasqkm).
"""
source: str
from_var: Optional[str] = Field(None, alias="from")

class BMIParams(BaseModel, smart_union=True, allow_population_by_field_name = True):
"""The base of all BMI paramterized ngen model configurations.

Expand Down Expand Up @@ -42,7 +52,11 @@ class BMIParams(BaseModel, smart_union=True, allow_population_by_field_name = Tr
#strictly optional fields (null/none) by default
output_vars: Optional[Sequence[str]] = Field(None, alias="output_variables")
output_headers: Optional[Sequence[str]] = Field(None)
model_params: Optional[Mapping[str, str]]
# <= 0.1.10 supported Optional[Mapping[str, str]]. This is incorrect, see
# #72 for explanation
model_params: Optional[
Mapping[str, Union[Numeric, Sequence[Numeric], LinkItem]]
]

#non exposed fields, derived from fields and used to build up and validate certain components
#such as configuration path/file
Expand Down
30 changes: 30 additions & 0 deletions python/ngen_conf/tests/test_bmi_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from ngen.config.bmi_formulation import BMIParams, LinkItem


def test_valid_bmi_params_model_params_type_variants():
model_params = {
"natural_key": 42,
"real_key": 42.0,
"natural_seq": [1, 1, 2, 3, 5],
"real_seq": [1.0, 10.0, 100.0],
"link_item": {"source": "somewhere", "from": "something"},
"link_item_no_from": {"source": "somewhere"},
}

bmi_params = BMIParams(
name="test",
model_type_name="test_model",
main_output_variable="nothing",
config="fake_{{id}}.toml",
model_params=model_params,
)

model_params_serialized_form = {
"natural_key": 42,
"real_key": 42.0,
"natural_seq": [1, 1, 2, 3, 5],
"real_seq": [1.0, 10.0, 100.0],
"link_item": LinkItem(**{"source": "somewhere", "from": "something"}),
"link_item_no_from": LinkItem(**{"source": "somewhere"}),
}
assert bmi_params.model_params == model_params_serialized_form
Loading