Skip to content

Commit

Permalink
#359 Add read() method to Results and add interface tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fcooper8472 committed Apr 26, 2019
1 parent d4e0e65 commit 4ee685a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/smif/data_layer/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,46 @@ def available_results(self, model_run_name):
results['sector_models'][model_name]['outputs'][output][dec] = ts

return results

def read(self,
model_run_names: list,
sec_model_names: list,
output_names: list,
timesteps: list = None,
decisions: list = None,
time_decision_tuples: list = None,
):
""" Return the results from the store.
Parameters
----------
model_run_names: list the requested model run names
sec_model_names: list the requested sector model names (exactly one required)
output_names: list the requested output names (exactly one required)
timesteps: list the requested timesteps
decisions: list the requested decision iterations
time_decision_tuples: list a list of requested (timestep, decision) tuples
Returns
-------
A dictionary of DataArrays, a single DataArray for each model run
"""

if len(sec_model_names) != 1:
raise NotImplementedError(
'Results.read() currently requires exactly one sector model'
)

if len(output_names) != 1:
raise NotImplementedError(
'Results.read() currently requires exactly one output'
)

return self._store.get_results(
model_run_names,
sec_model_names[0],
output_names[0],
timesteps,
decisions,
time_decision_tuples
)
35 changes: 35 additions & 0 deletions tests/data_layer/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,38 @@ def test_available_results(self, results_with_model_run, sample_results):
#
# assert outputs['cost'] == output_answer
# assert outputs['water_demand'] == output_answer

def test_read_exceptions(self, results_with_model_run):

# Passing anything other than one sector model or output is current not implemented
with raises(NotImplementedError) as e:
results_with_model_run.read(
model_run_names=['one', 'two'],
sec_model_names=[],
output_names=['one']
)
assert 'requires exactly one sector model' in str(e.value)

with raises(NotImplementedError) as e:
results_with_model_run.read(
model_run_names=['one', 'two'],
sec_model_names=['one', 'two'],
output_names=['one']
)
assert 'requires exactly one sector model' in str(e.value)

with raises(NotImplementedError) as e:
results_with_model_run.read(
model_run_names=['one', 'two'],
sec_model_names=['one'],
output_names=[]
)
assert 'requires exactly one output' in str(e.value)

with raises(NotImplementedError) as e:
results_with_model_run.read(
model_run_names=['one', 'two'],
sec_model_names=['one'],
output_names=['one', 'two']
)
assert 'requires exactly one output' in str(e.value)

0 comments on commit 4ee685a

Please sign in to comment.