Skip to content

Commit e38616e

Browse files
committed
Improve error message when loading data fails
1 parent a469608 commit e38616e

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

switch_model/utilities/load_data.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ def __str__(self):
9999
return repr(self.value)
100100

101101

102-
def load_data(switch_data, optional, auto_select, optional_params, **kwds):
103-
path = kwds["filename"]
102+
def load_data(switch_data, optional, auto_select, optional_params, **kwargs):
103+
path = kwargs["filename"]
104104
# Skip if the file is missing
105105
if optional and not os.path.isfile(path):
106106
return
107107
# If this is a .dat file, then skip the rest of this fancy business; we'll
108108
# only check if the file is missing and optional for .csv files.
109109
filename, extension = os.path.splitext(path)
110110
if extension == ".dat":
111-
switch_data.load(**kwds)
111+
switch_data.load(**kwargs)
112112
return
113113

114114
# copy the optional_params to avoid side-effects when the list is altered below
@@ -134,14 +134,14 @@ def load_data(switch_data, optional, auto_select, optional_params, **kwds):
134134
# Try to get a list of parameters. If param was given as a
135135
# singleton or a tuple, make it into a list that can be edited.
136136
params = []
137-
if "param" in kwds:
137+
if "param" in kwargs:
138138
# Tuple -> list
139-
if isinstance(kwds["param"], tuple):
140-
kwds["param"] = list(kwds["param"])
139+
if isinstance(kwargs["param"], tuple):
140+
kwargs["param"] = list(kwargs["param"])
141141
# Singleton -> list
142-
elif not isinstance(kwds["param"], list):
143-
kwds["param"] = [kwds["param"]]
144-
params = kwds["param"]
142+
elif not isinstance(kwargs["param"], list):
143+
kwargs["param"] = [kwargs["param"]]
144+
params = kwargs["param"]
145145
# optional_params may include Param objects instead of names. In
146146
# those cases, convert objects to names.
147147
for (i, p) in enumerate(optional_params):
@@ -157,11 +157,11 @@ def load_data(switch_data, optional, auto_select, optional_params, **kwds):
157157
optional_params.append(p.name)
158158
# How many index columns do we expect?
159159
# Grab the dimensionality of the index param if it was provided.
160-
if "index" in kwds:
161-
num_indexes = kwds["index"].dimen
160+
if "index" in kwargs:
161+
num_indexes = kwargs["index"].dimen
162162
if num_indexes == UnknownSetDimen:
163163
raise Exception(
164-
f"Index {kwds['index'].name} has unknown dimension. Specify dimen= during its creation."
164+
f"Index {kwargs['index'].name} has unknown dimension. Specify dimen= during its creation."
165165
)
166166
# Next try the first parameter's index.
167167
elif len(params) > 0:
@@ -185,7 +185,7 @@ def load_data(switch_data, optional, auto_select, optional_params, **kwds):
185185
# within the file (e.g., "cost" and "limit"). We could also require the data file
186186
# to be called "rfm_supply_tier.csv" for greater consistency/predictability.
187187
if auto_select:
188-
if "select" in kwds:
188+
if "select" in kwargs:
189189
raise InputError(
190190
"You may not specify a select parameter if "
191191
+ "auto_select is set to True."
@@ -197,15 +197,15 @@ def get_column_name(p):
197197
else:
198198
return p.name
199199

200-
kwds["select"] = headers[0:num_indexes] + [get_column_name(p) for p in params]
200+
kwargs["select"] = headers[0:num_indexes] + [get_column_name(p) for p in params]
201201
# Check to see if expected column names are in the file. If a column
202202
# name is missing and its parameter is optional, then drop it from
203203
# the select & param lists.
204-
if "select" in kwds:
205-
if isinstance(kwds["select"], tuple):
206-
kwds["select"] = list(kwds["select"])
204+
if "select" in kwargs:
205+
if isinstance(kwargs["select"], tuple):
206+
kwargs["select"] = list(kwargs["select"])
207207
del_items = []
208-
for (i, col) in enumerate(kwds["select"]):
208+
for (i, col) in enumerate(kwargs["select"]):
209209
p_i = i - num_indexes
210210
if col not in headers:
211211
if len(params) > p_i >= 0 and params[p_i].name in optional_params:
@@ -218,17 +218,20 @@ def get_column_name(p):
218218
# to first so that the indexes won't get messed up as we go.
219219
del_items.sort(reverse=True)
220220
for (i, p_i) in del_items:
221-
del kwds["select"][i]
222-
del kwds["param"][p_i]
221+
del kwargs["select"][i]
222+
del kwargs["param"][p_i]
223223

224224
if optional and file_has_no_data_rows:
225225
# Skip the file. Note that we are only doing this after having
226226
# validated the file's column headings.
227227
return
228228

229229
# Use our custom DataManager to allow 'inf' in csvs.
230-
if kwds["filename"][-4:] == ".csv":
231-
kwds["using"] = "switch_csv"
230+
if kwargs["filename"][-4:] == ".csv":
231+
kwargs["using"] = "switch_csv"
232232
# All done with cleaning optional bits. Pass the updated arguments
233233
# into the DataPortal.load() function.
234-
switch_data.load(**kwds)
234+
try:
235+
switch_data.load(**kwargs)
236+
except:
237+
raise Exception(f"Failed to load data from file {path}.")

0 commit comments

Comments
 (0)