Exemplo n.º 1
0
    def test_convert_amply_data_to_list_of_lists(self):

        data = {
            "SIMPLICITY": {
                "ETHPLANT": {
                    1.0: {
                        2014.0: 2.89
                    },
                    2.0: {
                        2014.0: 999999.0
                    }
                },
                "GAS_EXTRACTION": {
                    1.0: {
                        2014.0: 7.5
                    },
                    2.0: {
                        2014.0: 999999.0
                    }
                },
            }
        }
        expected = [
            ["SIMPLICITY", "ETHPLANT", 1.0, 2014.0, 2.89],
            ["SIMPLICITY", "ETHPLANT", 2.0, 2014.0, 999999.0],
            ["SIMPLICITY", "GAS_EXTRACTION", 1.0, 2014.0, 7.5],
            ["SIMPLICITY", "GAS_EXTRACTION", 2.0, 2014.0, 999999.0],
        ]
        read = ReadDatafile()
        actual = read._convert_amply_data_to_list(data)
        assert actual == expected
Exemplo n.º 2
0
    def test_load_sets(self):

        config = {"TestSet": {"type": "set"}}

        read = ReadDatafile()
        actual = read._load_parameter_definitions(config)
        expected = "set TestSet;\n"
        assert actual == expected
Exemplo n.º 3
0
    def test_load_parameters(self):

        config = {
            "TestParameter": {
                "type": "param",
                "indices": ["index1", "index2"]
            }
        }
        read = ReadDatafile()
        actual = read._load_parameter_definitions(config)
        expected = "param TestParameter {index1,index2};\n"
        assert actual == expected
Exemplo n.º 4
0
    def test_read_config(self):

        read = ReadDatafile()

        actual = read._read_config()
        expected = {
            "default": 0,
            "dtype": "float",
            "indices": ["REGION", "FUEL", "YEAR"],
            "type": "param",
        }
        assert actual["AccumulatedAnnualDemand"] == expected
Exemplo n.º 5
0
def result_matrix(args):
    """Post-process results from CBC solution file into CSV format

    """
    msg = "Conversion from {} to {} is not yet implemented".format(
        args.from_format, args.to_format)

    read_strategy = None
    write_strategy = None

    if args.from_format == "cbc":
        read_strategy = ReadCbc()
    elif args.from_format == "cplex":
        read_strategy = ReadCplex()
    elif args.from_format == "gurobi":
        read_strategy = ReadGurobi()

    if args.to_format == "csv":
        write_strategy = WriteCsv()

    if args.input_datapackage:
        input_data, _ = ReadDatapackage().read(args.input_datapackage)
    elif args.input_datafile:
        input_data, _ = ReadDatafile().read(args.input_datafile)
    else:
        input_data = {}

    if read_strategy and write_strategy:
        context = Context(read_strategy, write_strategy)
        context.convert(args.from_path, args.to_path, input_data=input_data)
    else:
        raise NotImplementedError(msg)
Exemplo n.º 6
0
 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)
Exemplo n.º 7
0
def conversion_matrix(args):
    """Convert from one format to another

    Implemented conversion functions::

        from\to     ex cs dp df
        -----------------------
        excel       -- yy -- --
        csv         nn -- yy nn
        datapackage yy -- -- yy
        datafile    nn -- yy --

    """

    msg = "Conversion from {} to {} is not yet implemented".format(
        args.from_format, args.to_format)

    read_strategy = None
    write_strategy = None

    config = None
    if args.config:
        for filename in args.config:
            _, ending = os.path.splitext(filename)
            with open(filename, "r") as config_file:
                config = _read_file(config_file, ending)
            logger.info("Reading config from {}".format(filename))

    if args.from_format == "datafile":
        read_strategy = ReadDatafile(user_config=config)
    elif args.from_format == "datapackage":
        read_strategy = ReadDatapackage(user_config=config)
    elif args.from_format == "csv":
        read_strategy = ReadCsv(user_config=config)
    elif args.from_format == "excel":
        read_strategy = ReadExcel(user_config=config)

    if args.to_format == "datapackage":
        write_strategy = WriteDatapackage()
    elif args.to_format == "excel":
        write_strategy = WriteExcel()
    elif args.to_format == "datafile":
        write_strategy = WriteDatafile()
    elif args.to_format == "csv":
        write_strategy = WriteCsv()

    if read_strategy and write_strategy:
        context = Context(read_strategy, write_strategy)
        context.convert(args.from_path, args.to_path)
    else:
        raise NotImplementedError(msg)
Exemplo n.º 8
0
def conversion_matrix(args):
    """Convert from one format to another

    Implemented conversion functions::

        from\to     ex cs dp df
        -----------------------
        excel       -- yy -- --
        csv         nn -- yy nn
        datapackage yy -- -- yy
        datafile    nn -- yy --

    """

    msg = "Conversion from {} to {} is not yet implemented".format(
        args.from_format, args.to_format)

    read_strategy = None
    write_strategy = None

    if args.from_format == "datafile":
        read_strategy = ReadDatafile()
    elif args.from_format == "datapackage":
        read_strategy = ReadDatapackage()
    elif args.from_format == "csv":
        read_strategy = ReadCsv()
    elif args.from_format == "excel":
        read_strategy = ReadExcel()

    if args.to_format == "datapackage":
        write_strategy = WriteDatapackage()
    elif args.to_format == "excel":
        write_strategy = WriteExcel()
    elif args.to_format == "datafile":
        write_strategy = WriteDatafile()
    elif args.to_format == "csv":
        write_strategy = WriteCsv()

    if read_strategy and write_strategy:
        context = Context(read_strategy, write_strategy)
        context.convert(args.from_path, args.to_path)
    else:
        raise NotImplementedError(msg)
Exemplo n.º 9
0
    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)