OSeMOSYS / otoole

OSeMOSYS Tools for Energy
https://otoole.readthedocs.io
MIT License
23 stars 17 forks source link

[Bug]: CPLEX results processing does not return zero values #214

Open willu47 opened 7 months ago

willu47 commented 7 months ago

The Issue

Some users need to extract zero values from a CPLEX solution file.

Expected Behavior

Given a CPLEX solution file, the otoole results cplex csv... command should write out a folder of CSV files, and those CSV files should contain zero-valued rows.

Steps To Reproduce

No response

Log output

No response

Operating System

MacOS

What version of otoole are you running?

v1.1.2

Possible Solution

Replace the ReadCPLEX class with the following

class ReadCplex(ReadWideResults):
    """Read a CPLEX solution file into memory"""

    def _convert_to_dataframe(self, file_path: Union[str, TextIO]) -> pd.DataFrame:
        """Reads a Cplex solution file into a pandas DataFrame

        Arguments
        ---------
        user_config : Dict[str, Dict]
        file_path : Union[str, TextIO]
        """
        df = pd.read_xml(file_path, xpath=".//variable", parser="etree")
        df[["Variable", "Index"]] = df["name"].str.split("(", expand=True)
        df["Index"] = df["Index"].str.replace(")", "", regex=False)
        LOGGER.debug(df)
        df = df.reset_index().rename(columns={"value": "Value"})
        return df[["Variable", "Index", "Value"]].astype({"Value": float})

Anything else?

No response