def read_in_datafile(self, path_to_datafile: str, config: Dict) -> Amply: """Read in a datafile using the Amply parsing class Arguments --------- path_to_datafile: str config: Dict """ parameter_definitions = self._load_parameter_definitions(config) datafile_parser = Amply(parameter_definitions) with open(path_to_datafile, "r") as datafile: datafile_parser.load_file(datafile) return datafile_parser
def test_amply(self): Amply("""set REGION; # set REGION := SIMPLICITY; set TECHNOLOGY; set TECHNOLOGY := ETHPLANT GAS_EXTRACTION; set MODE_OF_OPERATION; set MODE_OF_OPERATION := 1 2; set YEAR; set YEAR := 2014; end;""")
def test_catch_error_no_parameter(self, caplog): """Fix for https://github.com/OSeMOSYS/otoole/issues/70 where parameter in datafile but not in config causes error. Instead, throw warning (and advise that user should use a custom configuration). """ read = ReadDatafile() config = read.input_config amply_datafile = amply = Amply("""set REGION; set TECHNOLOGY; set MODE_OF_OPERATION; set YEAR;""") amply.load_string("""param ResultsPath := 'test_path';""") read._convert_amply_to_dataframe(amply_datafile, config) assert ( "Parameter ResultsPath could not be found in the configuration." in caplog.text)
def test_convert_amply_to_dataframe(self): config = { "VariableCost": { "type": "param", "indices": ["REGION", "TECHNOLOGY", "MODE_OF_OPERATION", "YEAR"], "dtype": "float", "default": 0, }, "REGION": { "type": "set", "dtype": "str" }, "YEAR": { "dtype": "int", "type": "set" }, "MODE_OF_OPERATION": { "dtype": "int", "type": "set" }, "TECHNOLOGY": { "dtype": "str", "type": "set" }, } amply = Amply("""set REGION; set REGION := SIMPLICITY; set TECHNOLOGY; set TECHNOLOGY := ETHPLANT GAS_EXTRACTION; set MODE_OF_OPERATION; set MODE_OF_OPERATION := 1 2; set YEAR; set YEAR := 2014;""") amply.load_string( "param VariableCost {REGION,TECHNOLOGY,MODE_OF_OPERATION,YEAR};") # amply.load_string("""param default 0 : VariableCost := # SIMPLICITY ETHPLANT 1 2014 2.89 # SIMPLICITY ETHPLANT 2 2014 999999.0 # SIMPLICITY GAS_EXTRACTION 1 2014 7.5 # SIMPLICITY GAS_EXTRACTION 2 2014 999999.0""") amply.load_string(""" param VariableCost default 0.0001 := [SIMPLICITY,ETHPLANT,*,*]: 2014 := 1 2.89 2 999999.0 [SIMPLICITY,GAS_EXTRACTION,*,*]: 2014 := 1 7.5 2 999999.0;""") read = ReadDatafile() actual = read._convert_amply_to_dataframe(amply, config) expected = pd.DataFrame( data=[ ["SIMPLICITY", "ETHPLANT", 1, 2014, 2.89], ["SIMPLICITY", "ETHPLANT", 2, 2014, 999999.0], ["SIMPLICITY", "GAS_EXTRACTION", 1, 2014, 7.5], ["SIMPLICITY", "GAS_EXTRACTION", 2, 2014, 999999.0], ], columns=[ "REGION", "TECHNOLOGY", "MODE_OF_OPERATION", "YEAR", "VALUE" ], ) pd.testing.assert_frame_equal(actual["VariableCost"], expected)