Skip to content

Commit d0849d1

Browse files
committed
Improve error message when loading data fails
1 parent a543594 commit d0849d1

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
@@ -83,16 +83,16 @@ def __init__(self, value):
8383
def __str__(self):
8484
return repr(self.value)
8585

86-
def load_data(switch_data, optional, auto_select, optional_params, **kwds):
87-
path = kwds['filename']
86+
def load_data(switch_data, optional, auto_select, optional_params, **kwargs):
87+
path = kwargs['filename']
8888
# Skip if the file is missing
8989
if optional and not os.path.isfile(path):
9090
return
9191
# If this is a .dat file, then skip the rest of this fancy business; we'll
9292
# only check if the file is missing and optional for .csv files.
9393
filename, extension = os.path.splitext(path)
9494
if extension == '.dat':
95-
switch_data.load(**kwds)
95+
switch_data.load(**kwargs)
9696
return
9797

9898
# copy the optional_params to avoid side-effects when the list is altered below
@@ -118,14 +118,14 @@ def load_data(switch_data, optional, auto_select, optional_params, **kwds):
118118
# Try to get a list of parameters. If param was given as a
119119
# singleton or a tuple, make it into a list that can be edited.
120120
params = []
121-
if 'param' in kwds:
121+
if 'param' in kwargs:
122122
# Tuple -> list
123-
if isinstance(kwds['param'], tuple):
124-
kwds['param'] = list(kwds['param'])
123+
if isinstance(kwargs['param'], tuple):
124+
kwargs['param'] = list(kwargs['param'])
125125
# Singleton -> list
126-
elif not isinstance(kwds['param'], list):
127-
kwds['param'] = [kwds['param']]
128-
params = kwds['param']
126+
elif not isinstance(kwargs['param'], list):
127+
kwargs['param'] = [kwargs['param']]
128+
params = kwargs['param']
129129
# optional_params may include Param objects instead of names. In
130130
# those cases, convert objects to names.
131131
for (i, p) in enumerate(optional_params):
@@ -141,10 +141,10 @@ def load_data(switch_data, optional, auto_select, optional_params, **kwds):
141141
optional_params.append(p.name)
142142
# How many index columns do we expect?
143143
# Grab the dimensionality of the index param if it was provided.
144-
if 'index' in kwds:
145-
num_indexes = kwds['index'].dimen
144+
if 'index' in kwargs:
145+
num_indexes = kwargs['index'].dimen
146146
if num_indexes == UnknownSetDimen:
147-
raise Exception(f"Index {kwds['index'].name} has unknown dimension. Specify dimen= during its creation.")
147+
raise Exception(f"Index {kwargs['index'].name} has unknown dimension. Specify dimen= during its creation.")
148148
# Next try the first parameter's index.
149149
elif len(params) > 0:
150150
try:
@@ -165,7 +165,7 @@ def load_data(switch_data, optional, auto_select, optional_params, **kwds):
165165
# within the file (e.g., "cost" and "limit"). We could also require the data file
166166
# to be called "rfm_supply_tier.csv" for greater consistency/predictability.
167167
if auto_select:
168-
if 'select' in kwds:
168+
if 'select' in kwargs:
169169
raise InputError('You may not specify a select parameter if ' +
170170
'auto_select is set to True.')
171171

@@ -175,15 +175,15 @@ def get_column_name(p):
175175
else:
176176
return p.name
177177

178-
kwds['select'] = headers[0:num_indexes] + [get_column_name(p) for p in params]
178+
kwargs['select'] = headers[0:num_indexes] + [get_column_name(p) for p in params]
179179
# Check to see if expected column names are in the file. If a column
180180
# name is missing and its parameter is optional, then drop it from
181181
# the select & param lists.
182-
if 'select' in kwds:
183-
if isinstance(kwds['select'], tuple):
184-
kwds['select'] = list(kwds['select'])
182+
if 'select' in kwargs:
183+
if isinstance(kwargs['select'], tuple):
184+
kwargs['select'] = list(kwargs['select'])
185185
del_items = []
186-
for (i, col) in enumerate(kwds['select']):
186+
for (i, col) in enumerate(kwargs['select']):
187187
p_i = i - num_indexes
188188
if col not in headers:
189189
if(len(params) > p_i >= 0 and
@@ -197,17 +197,20 @@ def get_column_name(p):
197197
# to first so that the indexes won't get messed up as we go.
198198
del_items.sort(reverse=True)
199199
for (i, p_i) in del_items:
200-
del kwds['select'][i]
201-
del kwds['param'][p_i]
200+
del kwargs['select'][i]
201+
del kwargs['param'][p_i]
202202

203203
if optional and file_has_no_data_rows:
204204
# Skip the file. Note that we are only doing this after having
205205
# validated the file's column headings.
206206
return
207207

208208
# Use our custom DataManager to allow 'inf' in csvs.
209-
if kwds["filename"][-4:] == ".csv":
210-
kwds['using'] = "switch_csv"
209+
if kwargs["filename"][-4:] == ".csv":
210+
kwargs['using'] = "switch_csv"
211211
# All done with cleaning optional bits. Pass the updated arguments
212212
# into the DataPortal.load() function.
213-
switch_data.load(**kwds)
213+
try:
214+
switch_data.load(**kwargs)
215+
except:
216+
raise Exception(f"Failed to load data from file {path}.")

0 commit comments

Comments
 (0)