Skip to content

Commit

Permalink
enforce that data types coming from the dataset (e.g. dataframe) to b…
Browse files Browse the repository at this point in the history
…e set as values in the parametric model in Grasshopper are of type float, int, bool and str for DataReal, DataInt, DataBool and DataCategorical, respectively.
  • Loading branch information
funkchaser committed Aug 27, 2024
1 parent 1de6231 commit 990808e
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/aixd_ara/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,37 @@ def get_dataobject_names_from_block(self, datablock_nickname):
return {"msg": "", "names": self.datamodule.output_ml_dblock.names_list}
return {"msg": f"Wrong block nickname: {datablock_nickname}.", "names": []}

def cast_to_python_type(self, dataobject_name, value):
"""
Cast the values (usually coming from a dataframe) to the correct python type.
Value can be a single value or a list.
"""
if not self.dataset:
raise ValueError("Dataset is not loaded.")

dobj = self.dataset.get_data_objects_by_name([dataobject_name])[0]

if not isinstance(value, list):
value = [value]
single = True
else:
single = False

if isinstance(dobj, DataInt):
castvalue = [int(v) for v in value]
elif isinstance(dobj, DataReal):
castvalue = [float(v) for v in value]
elif isinstance(dobj, DataBool):
castvalue = [bool(v) for v in value]
elif isinstance(dobj, DataCategorical):
castvalue = [str(v) for v in value]
else:
raise ValueError(f"Dataobject type not recognized: {dobj.type}")

if single:
castvalue = castvalue[0]
return castvalue

def get_dataobject_types(self):
"""
Returns names of the data types of the dataobjects in the dataset.
Expand Down Expand Up @@ -462,12 +493,12 @@ def get_one_sample(self, item):

dct = reformat_dataframeflat_to_dict(self.dataset.design_par.data, self.dataset.design_par.dobj_list)
for key, values in dct.items():
dct[key] = values[item]
dct[key] = self.cast_to_python_type(key, values[item])
sample["design_parameters"] = dct

dct = reformat_dataframeflat_to_dict(self.dataset.perf_attributes.data, self.dataset.perf_attributes.dobj_list)
for key, values in dct.items():
dct[key] = values[item]
dct[key] = self.cast_to_python_type(key, values[item])
sample["performance_attributes"] = dct

# for single-value entries, unpack them from a list [123] -> 123
Expand Down

0 comments on commit 990808e

Please sign in to comment.