Skip to content

Commit

Permalink
Merge pull request #63 from OSeMOSYS/fix_index
Browse files Browse the repository at this point in the history
Lack of indexes on dataframe break results
  • Loading branch information
willu47 committed Sep 14, 2020
2 parents 31e5dc4 + 1e18378 commit 040f703
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/otoole/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ class ReadStrategy(Strategy):
Strategies.
"""

def _check_index(self, input_data: Dict):
"""Checks and applied that an index is applied to the parameter DataFrame
"""
for name, df in input_data.items():
details = self.config[name]
try:
df.set_index(details["indices"], inplace=True)
except KeyError:
logger.debug("Parameter %s is indexed", name)

@abstractmethod
def read(self, filepath: str) -> Tuple[Dict[str, pd.DataFrame], Dict[str, Any]]:
raise NotImplementedError()
11 changes: 8 additions & 3 deletions src/otoole/read_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def read(

config = self.config
default_values = self._read_default_values(config)

self._check_index(self._parameters)
return self._parameters, default_values


Expand Down Expand Up @@ -133,6 +133,8 @@ def read(self, filepath) -> Tuple[Dict[str, pd.DataFrame], Dict[str, Any]]:

input_data[mod_name] = narrow_checked

self._check_index(input_data)

return input_data, default_values


Expand Down Expand Up @@ -176,6 +178,8 @@ def read(self, filepath) -> Tuple[Dict[str, pd.DataFrame], Dict[str, Any]]:

input_data[parameter] = narrow_checked

self._check_index(input_data)

return input_data, default_values


Expand All @@ -184,6 +188,7 @@ def read(self, filepath) -> Tuple[Dict[str, pd.DataFrame], Dict[str, Any]]:
inputs = read_datapackage(filepath)
default_resource = inputs.pop("default_values").set_index("name").to_dict()
default_values = default_resource["default_value"]
self._check_index(inputs)
return inputs, default_values


Expand All @@ -194,7 +199,7 @@ def read(self, filepath) -> Tuple[Dict[str, pd.DataFrame], Dict[str, Any]]:
default_values = self._read_default_values(config)
amply_datafile = self.read_in_datafile(filepath, config)
inputs = self._convert_amply_to_dataframe(amply_datafile, config)

self._check_index(inputs)
return inputs, default_values

def read_in_datafile(self, path_to_datafile: str, config: Dict) -> Amply:
Expand Down Expand Up @@ -258,7 +263,7 @@ def _convert_amply_to_dataframe(
for name in datafile_parser.symbols.keys():
logger.debug("Extracting data for %s", name)
if config[name]["type"] == "param":
indices = config[name]["indices"]
indices = config[name]["indices"].copy()
indices_dtypes = [config[index]["dtype"] for index in indices]
indices.append("VALUE")
indices_dtypes.append("float")
Expand Down
5 changes: 4 additions & 1 deletion src/otoole/results/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ def convert_dataframe_to_csv(

df = df.rename(columns={"Value": "VALUE"})

results[name] = df[indices + ["VALUE"]].set_index(indices)
columns = indices + ["VALUE"]

df = df[columns]
results[name] = df.set_index(details["indices"])

else:
not_found.append(name)
Expand Down
2 changes: 1 addition & 1 deletion src/otoole/results/result_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def annual_emissions(self) -> pd.DataFrame:
except KeyError as ex:
raise KeyError(self._msg("AnnualEmissions", str(ex)))

mid = emission_activity_ratio.mul(yearsplit, fill_value=0.0)
mid = emission_activity_ratio.mul(yearsplit)
data = mid.mul(rate_of_activity, fill_value=0.0)

if not data.empty:
Expand Down
1 change: 0 additions & 1 deletion tests/results/test_results_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ def test_missing_tech(self, two_tech):
data=[["SIMPLICITY", "CO2", 2014, 1.0]],
columns=["REGION", "EMISSION", "YEAR", "VALUE"],
).set_index(["REGION", "EMISSION", "YEAR"])

assert_frame_equal(actual, expected)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_read_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_read_memory(self):
expected = {
"AccumulatedAnnualDemand": pd.DataFrame(
data=data, columns=["REGION", "FUEL", "YEAR", "VALUE"]
)
).set_index(["REGION", "FUEL", "YEAR"])
}

assert "AccumulatedAnnualDemand" in actual.keys()
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_convert_amply_to_dataframe(self):
],
columns=["REGION", "TECHNOLOGY", "MODE_OF_OPERATION", "YEAR", "VALUE"],
)

print(actual, expected)
pd.testing.assert_frame_equal(actual["VariableCost"], expected)

def test_convert_amply_data_to_list_of_lists(self):
Expand Down

0 comments on commit 040f703

Please sign in to comment.