diff --git a/src/seismometer/configuration/config_helpers.py b/src/seismometer/configuration/config_helpers.py index d885f68..889a260 100644 --- a/src/seismometer/configuration/config_helpers.py +++ b/src/seismometer/configuration/config_helpers.py @@ -26,6 +26,8 @@ def generate_dictionary_from_parquet( for events section, by default "Type". """ df = pd.read_parquet(inpath) + if df.empty: + raise ValueError("No data loaded; check the input file") datadict = None match section: @@ -36,9 +38,6 @@ def generate_dictionary_from_parquet( case _: raise ValueError(f"Section {section} not recognized; only supports predictions and events") - if not datadict: - raise ValueError("No items generated; check the input file") - write_yaml(datadict.model_dump(), outpath) @@ -53,8 +52,12 @@ def _generate_prediction_dictionary(df: pd.DataFrame) -> PredictionDictionary: def _generate_event_dictionary(df, column="Type") -> EventDictionary: """Generates entry for each unique value in the specified column""" - items = [ - DictionaryItem(name=c, dtype="string", definition=f"Placeholder description for {c}") - for c in df[column].unique() - ] + items = ( + [ + DictionaryItem(name=c, dtype="string", definition=f"Placeholder description for {c}") + for c in df[column].unique() + ] + if column in df + else [] + ) return EventDictionary(events=items) diff --git a/tests/configuration/test_helpers.py b/tests/configuration/test_helpers.py index 291dbbf..5845e5b 100644 --- a/tests/configuration/test_helpers.py +++ b/tests/configuration/test_helpers.py @@ -80,3 +80,11 @@ def test_generate_dict_invalid_section(self, mock_read): undertest.generate_dictionary_from_parquet("TESTIN", "out.yml", section=section_type) assert section_type in str(err.value) + + +# This is intentionally outside the class to NOT use the shared pd patch +@pytest.mark.parametrize("section_type", ["events", "predictions"]) +@patch.object(undertest.pd, "read_parquet", return_value=pd.DataFrame()) +def test_generate_dict_no_data_raises_error(mock_read, section_type, tmp_as_current): + with pytest.raises(ValueError, match="No data loaded"): + undertest.generate_dictionary_from_parquet("TESTIN", "out.yml", section=section_type)