Skip to content

Commit

Permalink
Merge pull request #479 from fast-aircraft-design/issue-472_variable_…
Browse files Browse the repository at this point in the history
…viewer

Issue 472 variable viewer with values of type string
  • Loading branch information
christophe-david committed Feb 14, 2023
2 parents 2818eda + 424964e commit ea875c5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/fastoad/openmdao/tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ def test_df_from_to_variables():
vars["a"] = {"val": 5}
vars["b"] = {"val": np.array([1.0, 2.0, 3.0]), "units": "m"}
vars["c"] = {"val": [1.0, 2.0, 3.0], "units": "kg/s", "desc": "some test"}
vars["d"] = {"val": "my value is a string"}

df = vars.to_dataframe()
assert np.all(df["name"] == ["a", "b", "c"])
assert np.all(df["val"] == [5, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]])
assert np.all(df["units"].to_list() == [None, "m", "kg/s"])
assert np.all(df["desc"].to_list() == ["", "", "some test"])
assert np.all(df["name"] == ["a", "b", "c", "d"])
assert np.all(df["val"] == [5, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0], "my value is a string"])
assert np.all(df["units"].to_list() == [None, "m", "kg/s", None])
assert np.all(df["desc"].to_list() == ["", "", "some test", ""])

new_vars = VariableList.from_dataframe(df)

Expand Down
5 changes: 4 additions & 1 deletion src/fastoad/openmdao/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ def __eq__(self, other):
return (
isinstance(other, Variable)
and self.name == other.name
and np.all(np.isclose(my_value, other_value, equal_nan=True))
and (
np.all(my_value == other_value) # This condition is for val of type string
or np.all(np.isclose(my_value, other_value, equal_nan=True))
)
and my_metadata == other_metadata
)

Expand Down
10 changes: 5 additions & 5 deletions src/fastoad/openmdao/variables/variable_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ def to_dataframe(self) -> pd.DataFrame:
var_dict.update({metadata_name: [] for metadata_name in self.metadata_keys()})

for variable in self:
value = self._as_list_or_float(variable.value)
value = self._as_list_or_item(variable.value)
var_dict["name"].append(variable.name)
for metadata_name in self.metadata_keys():
if metadata_name == "val":
var_dict["val"].append(value)
else:
# TODO: make this more generic
if metadata_name in ["val", "initial_value", "lower", "upper"]:
metadata = self._as_list_or_float(variable.metadata[metadata_name])
metadata = self._as_list_or_item(variable.metadata[metadata_name])
else:
metadata = variable.metadata[metadata_name]
var_dict[metadata_name].append(metadata)
Expand Down Expand Up @@ -205,14 +205,14 @@ def from_ivc(cls, ivc: om.IndepVarComp) -> "VariableList":
).items():
metadata = metadata.copy()
value = metadata.pop("val")
value = cls._as_list_or_float(value)
value = cls._as_list_or_item(value)
metadata.update({"val": value})
variables[name] = metadata

return variables

@classmethod
def _as_list_or_float(cls, value):
def _as_list_or_item(cls, value):
value = np.asarray(value)
if np.size(value) == 1:
value = value.item()
Expand Down Expand Up @@ -242,7 +242,7 @@ def _get_variable(row):
# TODO: make this more generic
for key, val in var_as_dict.items():
if key in ["val", "initial_value", "lower", "upper"]:
var_as_dict[key] = cls._as_list_or_float(val)
var_as_dict[key] = cls._as_list_or_item(val)
else:
pass
return Variable(**var_as_dict)
Expand Down

0 comments on commit ea875c5

Please sign in to comment.