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 functionality for input based model options #1270

Merged
merged 7 commits into from
Jan 26, 2024
38 changes: 33 additions & 5 deletions watertap/ui/fsapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@
display_name: str = None
description: str = None
display_values: List[Any] = []
values_allowed: List[Any] = []
values_allowed: Union[str, List[Any]]
value: Any = None
min_val: Union[None, int, float] = None
max_val: Union[None, int, float] = None

@validator("display_name", always=True)
@classmethod
Expand All @@ -218,11 +220,29 @@
@classmethod
def validate_value(cls, v, values):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read this right, we can allow users select an option from a list, or enter a string, int or float?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true according to @MichaelPesce

allowed = values.get("values_allowed", None)
# allowed = list(allowed.keys())
if v in allowed:
return v
# check if values allowed is int or float and ensure valid value
if allowed == "int":
if isinstance(v, int):
return v
else:
raise ValueError(f"'value' ({v}) not a valid integer")

Check warning on line 228 in watertap/ui/fsapi.py

View check run for this annotation

Codecov / codecov/patch

watertap/ui/fsapi.py#L228

Added line #L228 was not covered by tests
elif allowed == "float":
if isinstance(v, int) or isinstance(v, float):
return v

Check warning on line 231 in watertap/ui/fsapi.py

View check run for this annotation

Codecov / codecov/patch

watertap/ui/fsapi.py#L230-L231

Added lines #L230 - L231 were not covered by tests
else:
raise ValueError(f"'value' ({v}) not a valid float")

Check warning on line 233 in watertap/ui/fsapi.py

View check run for this annotation

Codecov / codecov/patch

watertap/ui/fsapi.py#L233

Added line #L233 was not covered by tests
# check if values allowed is string
elif allowed == "string":
if isinstance(v, str):
return v

Check warning on line 237 in watertap/ui/fsapi.py

View check run for this annotation

Codecov / codecov/patch

watertap/ui/fsapi.py#L236-L237

Added lines #L236 - L237 were not covered by tests
else:
raise ValueError(f"'value' ({v}) not a valid string")

Check warning on line 239 in watertap/ui/fsapi.py

View check run for this annotation

Codecov / codecov/patch

watertap/ui/fsapi.py#L239

Added line #L239 was not covered by tests
# values_allowed is a list. make sure v is in the list of values allowed
else:
raise ValueError(f"'value' ({v}) not in allowed values: {allowed}")
if v in allowed:
return v
else:
raise ValueError(f"'value' ({v}) not in allowed values: {allowed}")

Check warning on line 245 in watertap/ui/fsapi.py

View check run for this annotation

Codecov / codecov/patch

watertap/ui/fsapi.py#L245

Added line #L245 was not covered by tests


class FlowsheetExport(BaseModel):
Expand Down Expand Up @@ -611,6 +631,14 @@
self.add_action("diagram", None)

self._actions["custom_do_param_sweep_kwargs"] = custom_do_param_sweep_kwargs
self.fs_exp.add_option(
name="NumParallelWorkers",
display_name="Number of multi-processing workers",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be more clear to say "Number of multi-processing workers for sensitivity analysis"

right now when there are build options, we don't have a way to do any groupings. Might be nice if we had a groups for build options like we do for flowsheet inputs (e.g. flowsheet model build options, analysis options etc..)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @MichaelPesce mentioned that he would exclude this bit from the PR

values_allowed="int",
value=1,
max_val=16,
min_val=1,
)

def build(self, **kwargs):
"""Build flowsheet
Expand Down
Loading