Esempio n. 1
0
    def test_check_index(self):

        data = [
            ["IW0016", 2017, 0.238356164],
            ["IW0016", 2018, 0.238356164],
            ["IW0016", 2019, 0.238356164],
            ["IW1624", 2017, 0.119178082],
            ["IW1624", 2018, 0.119178082],
            ["IW1624", 2019, 0.119178082],
            ["IH0012", 2017, 0.071232876],
            ["IH0012", 2018, 0.071232876],
            ["IH0012", 2019, 0.071232876],
        ]
        fixture = {
            "YearSplit":
            pd.DataFrame(data, columns=["TIMESLICE", "YEAR", "VALUE"]).astype({
                "YEAR":
                object
            }).set_index(["TIMESLICE", "YEAR"])
        }
        reader = ReadExcel()
        actual = reader._check_index(fixture)
        expected = {
            "YearSplit":
            pd.DataFrame(data,
                         columns=["TIMESLICE", "YEAR",
                                  "VALUE"]).set_index(["TIMESLICE", "YEAR"])
        }
        pd.testing.assert_frame_equal(actual["YearSplit"],
                                      expected["YearSplit"])
Esempio n. 2
0
    def test_narrow_parameters(self):
        data = [
            ["IW0016", 0.238356164, 0.238356164, 0.238356164],
            ["IW1624", 0.119178082, 0.119178082, 0.119178082],
            ["IH0012", 0.071232876, 0.071232876, 0.071232876],
        ]
        df = pd.DataFrame(data, columns=["TIMESLICE", 2017, 2018, 2019])
        config_details = ["TIMESLICE", "YEAR"]
        name = "YearSplit"

        reader = ReadExcel()
        actual = reader._check_parameter(df, config_details, name)
        data = [
            ["IW0016", 2017, 0.238356164],
            ["IW1624", 2017, 0.119178082],
            ["IH0012", 2017, 0.071232876],
            ["IW0016", 2018, 0.238356164],
            ["IW1624", 2018, 0.119178082],
            ["IH0012", 2018, 0.071232876],
            ["IW0016", 2019, 0.238356164],
            ["IW1624", 2019, 0.119178082],
            ["IH0012", 2019, 0.071232876],
        ]
        expected = (pd.DataFrame(data,
                                 columns=["TIMESLICE", "YEAR",
                                          "VALUE"]).astype({
                                              "YEAR": "object"
                                          }).set_index(["TIMESLICE", "YEAR"]))
        pd.testing.assert_frame_equal(actual, expected)
Esempio n. 3
0
    def test_read_excel_yearsplit(self):
        """
        """
        spreadsheet = os.path.join("tests", "fixtures", "combined_inputs.xlsx")
        reader = ReadExcel()
        actual, _ = reader.read(spreadsheet)
        data = [
            ["IW0016", 2017, 0.238356164],
            ["IW0016", 2018, 0.238356164],
            ["IW0016", 2019, 0.238356164],
            ["IW1624", 2017, 0.119178082],
            ["IW1624", 2018, 0.119178082],
            ["IW1624", 2019, 0.119178082],
            ["IH0012", 2017, 0.071232876],
            ["IH0012", 2018, 0.071232876],
            ["IH0012", 2019, 0.071232876],
        ]
        expected = pd.DataFrame(data,
                                columns=["TIMESLICE", "YEAR", "VALUE"
                                         ]).set_index(["TIMESLICE", "YEAR"])

        assert "YearSplit" in actual

        index = [
            ("IW0016", 2017),
            ("IW0016", 2018),
            ("IW0016", 2019),
            ("IW1624", 2017),
            ("IW1624", 2018),
            ("IW1624", 2019),
            ("IH0012", 2017),
            ("IH0012", 2018),
            ("IH0012", 2019),
        ]

        assert actual["YearSplit"].index.names == ["TIMESLICE", "YEAR"]
        actual_data = actual["YearSplit"].loc[index, "VALUE"]

        expected = [
            0.238356164,
            0.238356164,
            0.238356164,
            0.119178082,
            0.119178082,
            0.119178082,
            0.071232876,
            0.071232876,
            0.071232876,
        ]

        assert (actual_data == expected).all()
Esempio n. 4
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)
Esempio n. 5
0
    def test_convert_excel_to_csv(self):

        read_strategy = ReadExcel()
        write_strategy = WriteCsv()
        context = Context(read_strategy, write_strategy)

        tmpfile = TemporaryDirectory()
        from_path = os.path.join("tests", "fixtures", "combined_inputs.xlsx")

        context.convert(from_path, tmpfile.name)

        with open(os.path.join(tmpfile.name, "EMISSION.csv")) as csv_file:
            csv_file.seek(0)
            actual = csv_file.readlines()

        assert actual[-1] == "NOX\n"
        assert actual[0] == "VALUE\n"
        assert actual[1] == "CO2\n"
Esempio n. 6
0
    def test_convert_excel_to_datafile(self):

        read_strategy = ReadExcel()
        write_strategy = WriteDatafile()
        context = Context(read_strategy, write_strategy)

        tmpfile = NamedTemporaryFile()
        from_path = os.path.join("tests", "fixtures", "combined_inputs.xlsx")

        context.convert(from_path, tmpfile.name)

        tmpfile.seek(0)
        actual = tmpfile.readlines()
        tmpfile.close()

        assert actual[-1] == b"end;\n"
        assert actual[0] == b"# Model file written by *otoole*\n"
        assert actual[2] == b"09_ROK d_bld_2_coal_products 2017 20.8921\n"
        assert actual[8996] == b"param default 1 : DepreciationMethod :=\n"
Esempio 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

    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)