Skip to content

Commit

Permalink
#359 Return a dataframe with cols for model run, timestep and decision
Browse files Browse the repository at this point in the history
  • Loading branch information
fcooper8472 committed Apr 29, 2019
1 parent 4b886ed commit ac88b36
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/smif/data_layer/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os

import pandas as pd
from smif.data_layer.file import (CSVDataStore, FileMetadataStore,
ParquetDataStore, YamlConfigStore)
from smif.data_layer.store import Store
Expand All @@ -29,9 +30,10 @@ class Results:
directory
store: Store optional pre-created Store object
"""

def __init__(self, details_dict: dict = None, store: Store = None):

assert bool(details_dict) != bool(store),\
assert bool(details_dict) != bool(store), \
'Results() accepts either a details dict or a store'

self._store = store
Expand Down Expand Up @@ -170,11 +172,24 @@ def read(self,
'Results.read() currently requires exactly one output'
)

return self._store.get_results(
results_dict = self._store.get_results(
model_run_names,
sec_model_names[0],
output_names[0],
timesteps,
decisions,
time_decision_tuples
)

# Get each DataArray as a pandas data frame and concatenate, resetting the index to
# give back a flat data array
list_of_df = [x.as_df() for x in results_dict.values()]
names_of_df = [x for x in results_dict.keys()]

results = pd.concat(list_of_df, keys=names_of_df, names=['model_run']).reset_index()

# Unpack the timestep_decision tuples into individual columns and return
results[['timestep', 'decision']] = pd.DataFrame(results['timestep_decision'].tolist(),
index=results.index)

return results.drop(columns=['timestep_decision'])

0 comments on commit ac88b36

Please sign in to comment.