Skip to content

Commit

Permalink
fix result processing with defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorb1 committed Apr 1, 2024
1 parent 1257fba commit d9635dd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 37 deletions.
8 changes: 5 additions & 3 deletions src/otoole/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def read_results(
from_path: str,
input_format: str,
input_path: str,
write_defaults: bool = False,
glpk_model: Optional[str] = None,
) -> Tuple[Dict[str, pd.DataFrame], Dict[str, float]]:
"""Read OSeMOSYS results from CBC, GLPK, Gurobi, or CPLEX results files
Expand Down Expand Up @@ -58,7 +59,9 @@ def read_results(
"""
user_config = _get_user_config(config)
input_strategy = _get_read_strategy(user_config, input_format)
result_strategy = _get_read_result_strategy(user_config, from_format, glpk_model)
result_strategy = _get_read_result_strategy(
user_config, from_format, glpk_model, write_defaults
)

if input_strategy:
input_data, _ = input_strategy.read(input_path)
Expand Down Expand Up @@ -427,11 +430,10 @@ def write(
"""
user_config = _get_user_config(config)
write_strategy = _get_write_strategy(user_config, to_format)
if default_values is None:
write_strategy = _get_write_strategy(user_config, to_format)
write_strategy.write(inputs, to_path, {})
else:
write_strategy = _get_write_strategy(user_config, to_format)
write_strategy.write(inputs, to_path, default_values)

return True
17 changes: 11 additions & 6 deletions src/otoole/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def convert(self, input_filepath: str, output_filepath: str, **kwargs: Dict):
input_filepath: str
output_filepath: str
"""

inputs, default_values = self._read(input_filepath, **kwargs)
self._write(inputs, output_filepath, default_values, **kwargs)

Expand Down Expand Up @@ -541,8 +542,9 @@ def _expand_dataframe(
----------
name: str
Name of parameter/result to expand
df: pd.DataFrame,
input parameter/result data to be expanded
input_data: Dict[str, pd.DataFrame],
internal datastore
default_values: Dict[str, pd.DataFrame],
Returns
-------
Expand Down Expand Up @@ -612,20 +614,23 @@ def write_default_params(

def write_default_results(
self,
result_data: Dict[str, pd.DataFrame],
input_data: Dict[str, pd.DataFrame],
default_values: Dict[str, Union[str, int, float]],
) -> Dict[str, pd.DataFrame]:
"""Returns result dataframes with default values expanded"""

all_data = {**result_data, **input_data}
names = [x for x in self.user_config if self.user_config[x]["type"] == "result"]
for name in names:
try:
logger.debug(f"Serching for {name} data to expand")
input_data[name] = self._expand_dataframe(
name, input_data, default_values
result_data[name] = self._expand_dataframe(
name, all_data, default_values
)
except KeyError:
logger.warning(f"Can not expand {name} data")
return input_data
logger.debug(f"Can not expand {name} data")
return result_data

@abstractmethod
def read(
Expand Down
3 changes: 1 addition & 2 deletions src/otoole/results/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def read(
) # type: Dict[str, pd.DataFrame]

if self.write_defaults:
all_data = {**input_data, **results} if input_data else results.copy()
results = self.write_default_results(all_data, default_values)
results = self.write_default_results(results, input_data, default_values)

return results, default_values

Expand Down
28 changes: 2 additions & 26 deletions tests/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,20 +331,17 @@ def test_write_default_params(

assert_frame_equal(actual, expected)

def test_write_default_results_correct(
def test_write_default_results(
self,
simple_user_config,
simple_input_data,
simple_available_results,
simple_default_values,
):
# data merged by result package
input_data = simple_input_data.copy()
input_data.update(simple_available_results)

read_strategy = DummyReadStrategy(user_config=simple_user_config)
actual_expanded = read_strategy.write_default_results(
input_data, simple_default_values
simple_available_results, simple_input_data, simple_default_values
)

actual = actual_expanded["NewCapacity"]
Expand All @@ -363,27 +360,6 @@ def test_write_default_results_correct(

assert_frame_equal(actual, expected)

def test_write_default_results_incorrect(
self,
simple_user_config,
simple_input_data,
simple_available_results,
simple_default_values,
):
# data merged by result package
input_data = simple_input_data.copy()
input_data.update(simple_available_results)

read_strategy = DummyReadStrategy(user_config=simple_user_config)
actual_expanded = read_strategy.write_default_results(
input_data, simple_default_values
)

actual = actual_expanded["CapitalCost"]
expected = simple_input_data["CapitalCost"]

assert_frame_equal(actual, expected)


class TestReadStrategy:

Expand Down

0 comments on commit d9635dd

Please sign in to comment.