Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions data_prep/ibtracs_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from matplotlib.patches import Rectangle

import extremeweatherbench.data
from extremeweatherbench import inputs, regions, utils
from extremeweatherbench import cases, inputs, regions, utils

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -537,9 +537,8 @@ def update_cases_with_storm_bounds(storm_bounds, all_storms_df):
"""
logger.info("Updating cases with storm bounds...")

cases_all = utils.load_events_yaml()
cases_old = cases_all["cases"]
cases_new = cases_old.copy()
cases_all = cases.load_ewb_events_yaml_into_case_collection()
cases_new = cases_all.copy()

# Update the yaml cases with storm bounds from IBTrACS data
for single_case in cases_new:
Expand Down
18 changes: 4 additions & 14 deletions scripts/validate_events_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,14 @@ def validate_events_yaml(file_path: Path) -> List[str]:
except yaml.YAMLError as e:
return [f"Invalid YAML syntax: {e}"]

if not isinstance(data, dict):
return ["Root element must be a dictionary"]

if "cases" not in data:
return ["Missing 'cases' key in root"]

cases = data["cases"]
if not isinstance(cases, list):
return ["'cases' must be a list"]

if len(cases) == 0:
return ["'cases' list is empty"]
if not isinstance(data, dict) and not isinstance(data, list):
return ["Root element must be a dictionary or a list"]

# Validate each case
previous_case_id = None
for i, case in enumerate(cases):
for i, case in enumerate(data):
if not isinstance(case, dict):
errors.append(f"Case {i + 1}: must be a dictionary")
errors.append(f"Case {i + 1}: must be a dictionary: {data} {file_path}")
continue

# Check required fields
Expand Down
13 changes: 11 additions & 2 deletions src/extremeweatherbench/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def build_case_operators(
return case_operators


def load_individual_cases(cases: dict[str, list]) -> IndividualCaseCollection:
def load_individual_cases(
cases: Union[dict[str, list], list],
) -> IndividualCaseCollection:
"""Load IndividualCase metadata from a dictionary.

Args:
Expand All @@ -204,6 +206,13 @@ def load_individual_cases(cases: dict[str, list]) -> IndividualCaseCollection:
Returns:
A collection of IndividualCase objects.
"""
assert isinstance(cases, dict) or isinstance(cases, list), (
"cases must be a dictionary or a list."
)

# if cases is a list, convert to a dictionary for dacite.from_dict
if isinstance(cases, list):
cases = {"cases": cases}
case_metadata_collection = dacite.from_dict(
data_class=IndividualCaseCollection,
data=cases,
Expand Down Expand Up @@ -267,7 +276,7 @@ def load_ewb_events_yaml_into_case_collection() -> IndividualCaseCollection:
return load_individual_cases(yaml_event_case)


def read_incoming_yaml(input_pth: Union[str, pathlib.Path]) -> dict:
def read_incoming_yaml(input_pth: Union[str, pathlib.Path]):
"""Read events yaml from data into a dictionary.

This function is a wrapper around yaml.safe_load that reads the yaml file directly.
Expand Down
Loading
Loading