Skip to content

Commit

Permalink
Data frame validator method
Browse files Browse the repository at this point in the history
  • Loading branch information
davidorme committed Jan 7, 2025
1 parent 27e7ff6 commit 8ba6cb5
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions virtual_ecosystem/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,39 @@ def output_current_state(

return out_path

def confirm_variables_form_data_frame(
self, var_names: list[str]
) -> tuple[bool, str]:
"""Check a list of named variables form a 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.
Args:
var_names: A list of the variable names that form a data frame.
"""

# All vars present
missing_var = [v for v in var_names if v not in self]

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]

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]}"

return True, "Variables form a data frame"


def merge_continuous_data_files(
data_options: dict[str, Any], continuous_data_files: list[Path]
Expand Down

0 comments on commit 8ba6cb5

Please sign in to comment.