Skip to content

Commit

Permalink
Merge c171359 into ed57b30
Browse files Browse the repository at this point in the history
  • Loading branch information
willu47 committed Nov 5, 2020
2 parents ed57b30 + c171359 commit 8eab37f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/otoole/results/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,25 @@ def _convert_cbc_to_dataframe(self, file_path: Union[str, TextIO]) -> pd.DataFra
df = pd.read_csv(
file_path,
header=None,
names=["temp", "VALUE"],
delim_whitespace=True,
sep="(",
names=["Variable", "indexvalue"],
skiprows=1,
usecols=[1, 2],
) # type: pd.DataFrame
df.columns = ["temp", "Value"]
df[["Variable", "Index"]] = df["temp"].str.split("(", expand=True)
df = df.drop("temp", axis=1)
if df["Variable"].astype(str).str.contains(r"^\*\*").any():
LOGGER.warning(
"CBC Solution File contains decision variables out of bounds. "
+ "You have an infeasible solution"
)
df["Variable"] = (
df["Variable"]
.astype(str)
.str.replace(r"^\*\*", "")
.str.split(expand=True)[1]
)
df[["Index", "Value"]] = df["indexvalue"].str.split(expand=True).loc[:, 0:1]
df["Index"] = df["Index"].str.replace(")", "")
return df[["Variable", "Index", "Value"]]
df = df.drop(columns=["indexvalue"])
return df[["Variable", "Index", "Value"]].astype({"Value": float})

def _convert_dataframe_to_csv(
self, data: pd.DataFrame, input_data: Optional[Dict[str, pd.DataFrame]] = None
Expand Down
35 changes: 35 additions & 0 deletions tests/test_read_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,41 @@ def test_solution_to_dataframe(self):
actual[0]["TotalDiscountedCost"], expected["TotalDiscountedCost"]
)

cbc_infeasible = dedent(
"""header
381191 RateOfActivity(GLOBAL,S4D24,INRNGIM00,1,2041) 0 0
381191 RateOfActivity(GLOBAL,S4D24,INRNGIM00,1,2042) -7.7011981e-07 0.024001857
381192 RateOfActivity(GLOBAL,S1D1,INRNGIM00,1,2043) -3.6128354e-06 0.022858911
381199 RateOfActivity(GLOBAL,S1D8,INRNGIM00,1,2043) -3.1111316e-06 0.022858911
381200 RateOfActivity(GLOBAL,S1D9,INRNGIM00,1,2043) -8.2325306e-07 0.022858911
381201 RateOfActivity(GLOBAL,S1D10,INRNGIM00,1,2043) -3.1112991e-06 0.022858911
** 381218 RateOfActivity(GLOBAL,S2D3,INRNGIM00,1,2043) -1.6357402e-06 0.022858911
** 381221 RateOfActivity(GLOBAL,S2D6,INRNGIM00,1,2043) -3.1111969e-06 0.022858911
** 381229 RateOfActivity(GLOBAL,S2D14,INRNGIM00,1,2043) -1.3925924e-07 0.010964295
"""
)

def test_manage_infeasible_variables(self):
input_file = self.cbc_infeasible
reader = ReadCbc()
with StringIO(input_file) as file_buffer:
actual = reader._convert_cbc_to_dataframe(file_buffer)
expected = pd.DataFrame(
[
["RateOfActivity", "GLOBAL,S4D24,INRNGIM00,1,2041", 0],
["RateOfActivity", "GLOBAL,S4D24,INRNGIM00,1,2042", -7.7011981e-07],
["RateOfActivity", "GLOBAL,S1D1,INRNGIM00,1,2043", -3.6128354e-06],
["RateOfActivity", "GLOBAL,S1D8,INRNGIM00,1,2043", -3.1111316e-06],
["RateOfActivity", "GLOBAL,S1D9,INRNGIM00,1,2043", -8.2325306e-07],
["RateOfActivity", "GLOBAL,S1D10,INRNGIM00,1,2043", -3.1112991e-06],
["RateOfActivity", "GLOBAL,S2D3,INRNGIM00,1,2043", -1.6357402e-06],
["RateOfActivity", "GLOBAL,S2D6,INRNGIM00,1,2043", -3.1111969e-06],
["RateOfActivity", "GLOBAL,S2D14,INRNGIM00,1,2043", -1.3925924e-07],
],
columns=["Variable", "Index", "Value"],
)
pd.testing.assert_frame_equal(actual, expected)


class TestCleanOnRead:
"""Tests that a datapackage is cleaned and indexed upon reading
Expand Down

0 comments on commit 8eab37f

Please sign in to comment.