Skip to content

Commit ebaf2b1

Browse files
authored
Improved error handling for existing_capacity file (#767)
1 parent f98cd0f commit ebaf2b1

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/muse/readers/csv.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
__all__ = [
3333
"read_agent_parameters",
3434
"read_attribute_table",
35+
"read_csv",
3536
"read_existing_trade",
3637
"read_global_commodities",
3738
"read_initial_capacity",
@@ -276,6 +277,9 @@ def standardize_dataframe(
276277
Returns:
277278
DataFrame containing the standardized data
278279
"""
280+
if required_columns is None:
281+
required_columns = []
282+
279283
# Standardize column names
280284
data = standardize_columns(data)
281285

@@ -294,7 +298,7 @@ def standardize_dataframe(
294298
data = convert_column_types(data)
295299

296300
# Validate required columns if provided
297-
if required_columns is not None:
301+
if required_columns:
298302
missing_columns = [col for col in required_columns if col not in data.columns]
299303
if missing_columns:
300304
raise ValueError(f"Missing required columns: {missing_columns}")

src/muse/sectors/subsector.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,16 @@ def factory(
118118
from muse import investments as iv
119119
from muse.agents import InvestingAgent, agents_factory
120120
from muse.commodities import is_enduse
121-
from muse.readers import read_existing_trade, read_initial_capacity
121+
from muse.readers import read_csv, read_existing_trade, read_initial_capacity
122122

123123
# Read existing capacity or existing trade file
124-
try:
124+
# Have to peek at the file to determine what format the data is in
125+
# TODO: ideally would be more explicit about this. Consider changing
126+
# the parameter name in the settings file
127+
df = read_csv(settings.existing_capacity)
128+
if "year" not in df.columns:
125129
existing_capacity = read_initial_capacity(settings.existing_capacity)
126-
except ValueError:
127-
# TODO: ideally would be more explicit about this. Consider changing
128-
# the parameter name in the settings file
130+
else:
129131
existing_capacity = read_existing_trade(settings.existing_capacity)
130132

131133
# Create agents

0 commit comments

Comments
 (0)