Skip to content

Commit

Permalink
Tests for new refinements regarding (in)ability to change a GdxSymbol…
Browse files Browse the repository at this point in the history
…'s number of dimensions once that has been declared.
  • Loading branch information
elainethale committed Oct 7, 2018
1 parent a922e2c commit bce8a69
Showing 1 changed file with 153 additions and 21 deletions.
174 changes: 153 additions & 21 deletions gdxpds/test/test_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,56 +277,166 @@ def test_setting_dataframes(manage_rundir):
gdx.append(gdxpds.gdx.GdxSymbol('sym_5',gdxpds.gdx.GamsDataType.Parameter))
gdx[-1].dataframe = pds.DataFrame([['u1','CC',8727.2],
['u2','CC',7500.2],
['u3','CT',9258.0]])
['u3','CT',9258.0]],
columns=['u','q','val'])
assert gdx[-1].num_dims == 2
assert gdx[-1].num_records == 3
# full list of lists

gdx.append(gdxpds.gdx.GdxSymbol('sym_6',gdxpds.gdx.GamsDataType.Parameter))
gdx[-1].dataframe = [['u1','CC',8727.2],
['u2','CC',7500.2],
['u3','CT',9258.0],
['u4','Coal',10100.0]]
assert gdx[-1].num_dims == 2
assert gdx[-1].num_records == 4
# reset with empty list
gdx.append(gdxpds.gdx.GdxSymbol('sym_7',gdxpds.gdx.GamsDataType.Parameter))
gdx[-1].dataframe = gdx[-2].dataframe.copy()
gdx[-1].dataframe = []
assert gdx[-1].num_dims == 2
assert gdx[-1].num_records == 0

# > 0 dims - GdxSymbol initialized with dims=n

# dataframe of dims

gdx.append(gdxpds.gdx.GdxSymbol('sym_8',gdxpds.gdx.GamsDataType.Variable,
dims=3,variable_type=gdxpds.gdx.GamsVariableType.Positive))
gdx[-1].dataframe = pds.DataFrame([['u0','BES','c2'],
['u0','BES','c1'],
['u1','BES','c2']])
assert gdx[-1].num_dims == 3;
assert gdx[-1].dims == ['*','*','*']
assert len(gdx[-1].dataframe.columns) > 3
gdx[-1].dataframe[gdxpds.gdx.GamsValueType.Level.name] = 1.0
gdx[-1].dataframe[gdxpds.gdx.GamsValueType.Upper.name] = 10.0
# full dataframe

gdx.append(gdxpds.gdx.GdxSymbol('sym_9',gdxpds.gdx.GamsDataType.Parameter,
dims=3))
gdx[-1].dataframe = pds.DataFrame([['u0','BES','c2',2.0],
['u0','BES','c1',1.0],
['u1','BES','c2',2.0]],
columns=['u','q','c','storage_duration_h'])
assert list(gdx[-1].dataframe.columns) == ['u','q','c','Value']
# list of lists containing dims only

gdx.append(gdxpds.gdx.GdxSymbol('sym_10',gdxpds.gdx.GamsDataType.Equation,
dims=4,equation_type=gdxpds.gdx.GamsEquationType.LessThan))
gdx[-1].dataframe = [['u0','PHES','c0','1'],
['u0','PHES','c0','2'],
['u0','PHES','c0','3'],
['u0','PHES','c0','4'],
['u0','PHES','c0','5']]
gdx[-1].dataframe['Level'] = -15.0
assert list(gdx[-1].dataframe.columns[:gdx[-1].num_dims]) == ['*'] * 4
# full list of lists

gdx.append(gdxpds.gdx.GdxSymbol('sym_11',gdxpds.gdx.GamsDataType.Set,
dims=2))
gdx[-1].dataframe = [['PV','c0',True],
['CSP','c0',False],
['CSP','c1',False],
['Wind','c0',True]]
assert gdx[-1].num_dims == 2
# reset with empty list
gdx.append(gdxpds.gdx.GdxSymbol('sym_12',gdxpds.gdx.GamsDataType.Set,
dims=2))
gdx[-1].dataframe = gdx[-1].dataframe.copy()
gdx[-1].dataframe = []
assert gdx[-1].num_dims == 2
assert gdx[-1].dims == ['*'] * 2
assert gdx[-1].num_records == 0

# > 0 dims - GdxSymbol initialized with dims=[list of actual dims]

# dataframe of dims

gdx.append(gdxpds.gdx.GdxSymbol('sym_13',gdxpds.gdx.GamsDataType.Variable,
dims=['u','q','c'],variable_type=gdxpds.gdx.GamsVariableType.Positive))
gdx[-1].dataframe = pds.DataFrame([['u0','BES','c2'],
['u0','BES','c1'],
['u1','BES','c2']])
assert gdx[-1].num_dims == 3;
assert gdx[-1].dims == ['u','q','c']
assert len(gdx[-1].dataframe.columns) > 3
gdx[-1].dataframe[gdxpds.gdx.GamsValueType.Level.name] = 1.0
gdx[-1].dataframe[gdxpds.gdx.GamsValueType.Upper.name] = 10.0
# full dataframe

gdx.append(gdxpds.gdx.GdxSymbol('sym_14',gdxpds.gdx.GamsDataType.Parameter,
dims=['u','q','c']))
gdx[-1].dataframe = pds.DataFrame([['u0','BES','c2',2.0],
['u0','BES','c1',1.0],
['u1','BES','c2',2.0]],
columns=['u','q','c','storage_duration_h'])
assert list(gdx[-1].dataframe.columns) == ['u','q','c','Value']
# list of lists containing dims only

gdx.append(gdxpds.gdx.GdxSymbol('sym_15',gdxpds.gdx.GamsDataType.Equation,
dims=['u','q','c','t'],equation_type=gdxpds.gdx.GamsEquationType.LessThan))
gdx[-1].dataframe = [['u0','PHES','c0','1'],
['u0','PHES','c0','2'],
['u0','PHES','c0','3'],
['u0','PHES','c0','4'],
['u0','PHES','c0','5']]
gdx[-1].dataframe['Level'] = -15.0
assert list(gdx[-1].dataframe.columns[:gdx[-1].num_dims]) == ['u','q','c','t']
# full list of lists

gdx.append(gdxpds.gdx.GdxSymbol('sym_16',gdxpds.gdx.GamsDataType.Set,
dims=['q','c']))
gdx[-1].dataframe = [['PV','c0',True],
['CSP','c0',False],
['CSP','c1',False],
['Wind','c0',True]]
assert gdx[-1].num_dims == 2
# reset with empty list

gdx.append(gdxpds.gdx.GdxSymbol('sym_17',gdxpds.gdx.GamsDataType.Set,
dims=['q','c']))
gdx[-1].dataframe = gdx['sym_11'].dataframe.copy()
gdx[-1].dataframe = []
assert gdx[-1].num_dims == 2
assert list(gdx[-1].dataframe.columns[:gdx[-1].num_dims]) == ['*'] * 2
assert gdx[-1].num_records == 0

# And then document that some ways DO NOT WORK:
# dims=0

# set value, then try to unset value

# dims=0, set value, then try to set different number of dimensions

# set value, then try to set different number of dimensions
gdx.append(gdxpds.gdx.GdxSymbol('sym_18',gdxpds.gdx.GamsDataType.Parameter,
dims=0))
gdx[-1].dataframe = [[3]]
with pytest.raises(Exception) as e_info:
gdx[-1].dims = 3
# dims > 0
# explicitly set dims to something else
gdx.append(gdxpds.gdx.GdxSymbol('sym_x',gdxpds.gdx.GamsDataType.Parameter,
gdx.append(gdxpds.gdx.GdxSymbol('sym_19',gdxpds.gdx.GamsDataType.Parameter,
dims=['g','t']))
with pytest.raises(Exception) as e_info:
gdx[-1].dims = ['g','t','d']
# dataframe of different number of dims

gdx.append(gdxpds.gdx.GdxSymbol('sym_20',gdxpds.gdx.GamsDataType.Variable,
dims=['d','t']))
gdx[-1].dataframe = [['d1','1'],
['d1','2'],
['d1','3']]
tmp = gdx[-1].dataframe.copy()
cols = list(tmp.columns)
tmp['q'] = 'PV'
tmp = tmp[['q'] + cols]
with pytest.raises(Exception) as e_info:
gdx[-1].dataframe = tmp
# full dataframe of different number of dims

gdx.append(gdxpds.gdx.GdxSymbol('sym_21',gdxpds.gdx.GamsDataType.Parameter,
dims=6))
assert gdx[-1].dims == ['*'] * 6
with pytest.raises(Exception) as e_info:
gdx[-1].dataframe = pds.DataFrame([['1',6.0],
['2',7.0],
['3',-12.0]])
# list of lists of varying widths
gdx.append(gdxpds.gdx.GdxSymbol('sym_22',gdxpds.gdx.GamsDataType.Parameter,
dims=3))
with pytest.raises(Exception) as e_info:
gdx[-1].dataframe = [[1]]
with pytest.raises(Exception) as e_info:
gdx[-1].dataframe = [['1',2.5],
['2',-30.0]]
# TODO: Write test where parameter value ends up as set dimension--does
# an exception get thrown upon writing to GDX?
with pytest.raises(Exception) as e_info:
gdx[-1].dataframe = [['u1','PV','c0','1',2.5],
['u1','PV','c0','2',-30.0]]

gdx.write(os.path.join(outdir,'dataframe_set_tests.gdx'))
with gdxpds.gdx.GdxFile(lazy_load=False) as gdx:
Expand All @@ -336,5 +446,27 @@ def test_setting_dataframes(manage_rundir):
assert gdx['sym_3'].num_records == 1
assert gdx['sym_4'].num_records == 1 # GAMS defaults empty 0-dim parameter to 0
assert gdx['sym_4'].dataframe['Value'].values[0] == 0.0
assert gdx['sym_5'].dims == ['u','q']
assert gdx['sym_5'].num_records == 3
assert gdx['sym_5'].dataframe['Value'].values[1] == 7500.2
assert gdx['sym_6'].num_records == 4
assert gdx['sym_7'].num_records == 0
assert gdx['sym_8'].dims == ['*'] * 3
assert gdx['sym_8'].dataframe[gdxpds.gdx.GamsValueType.Upper.name].values[0] == 10.0
assert gdx['sym_9'].num_records == 3
assert gdx['sym_10'].num_dims == 4
assert gdx['sym_11'].num_dims == 2
assert gdx['sym_11'].num_records == 4
# ETH@20181007 - Tried to test for some values being c_bool(False) in sym_11, but
# c_bool(True) != c_bool(True), so that makes it hard to test such things.
# Also, c_bool(False) appears to be interpreted as True in GDX. Ick and yikes.
assert gdx['sym_12'].num_dims == 2
assert gdx['sym_12'].num_records == 0
assert gdx['sym_13'].dims == ['u','q','c']
assert gdx['sym_13'].dataframe[gdxpds.gdx.GamsValueType.Upper.name].values[0] == 10.0
assert gdx['sym_14'].num_records == 3
assert gdx['sym_15'].num_dims == 4
assert gdx['sym_16'].num_dims == 2
assert gdx['sym_16'].num_records == 4
assert gdx['sym_17'].num_dims == 2
assert gdx['sym_17'].num_records == 0

0 comments on commit bce8a69

Please sign in to comment.