Skip to content

Commit

Permalink
Test dataframe validation method on Data
Browse files Browse the repository at this point in the history
  • Loading branch information
davidorme committed Jan 7, 2025
1 parent 8ba6cb5 commit c8cd3ae
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions virtual_ecosystem/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def confirm_variables_form_data_frame(
This is a utility method to check if a set of named variables are present in the
Data object and that together they form a data frame: a set of equal length, one
dimensional arrays.
dimensional arrays, providing consistent tuples of values across the variables.
Args:
var_names: A list of the variable names that form a data frame.
Expand All @@ -498,17 +498,15 @@ def confirm_variables_form_data_frame(
if missing_var:
return False, f"Missing variables: {', '.join(missing_var)}"

# All vars identically sized and 1D
data_shapes = [self[var].shape for var in var_names]
# All vars one dimensional
data_not_one_d = [var for var in var_names if self[var].ndim > 1]
if data_not_one_d:
return False, f"Variables not one dimensional: {','.join(data_not_one_d)}"

if len(set(data_shapes)) != 1:
return False, (
f"Unequal variable dimensions:"
f" {','.join([str(v) for v in set(data_shapes)])}"
)

if len(data_shapes[0]) != 1:
return False, f"Variables not one dimensional: {data_shapes[0]}"
# All vars equal sized
shapes = sorted(set(str(self[var].shape[0]) for var in var_names))
if len(shapes) != 1:
return False, f"Variables of unequal length: {','.join(shapes)}"

return True, "Variables form a data frame"

Expand Down

0 comments on commit c8cd3ae

Please sign in to comment.