예제 #1
0
    def test_auto_export(self):
        cero = pd.DataFrame.from_dict({"A": [1], "B": [2], "C": [3]},
                                      orient='index',
                                      dtype=pd.np.float32)
        cero.sort_index(inplace=True)
        cero.columns = pd.DatetimeIndex(data=pd.to_datetime([2018], format="%Y"))

        fc = FromCERO(cfg.d_td + "test_procedure_autoexport.yaml")
        fc.exec_procedures(cero)

        df1 = pd.read_csv("auto_csv_export.csv", index_col=0)
        test_list = [1, 2, 3]
        df1_vals = [x[0] for x in df1.values.tolist()]
        self.assertTrue(all([np.isclose(x, y) for (x, y) in zip(test_list, df1_vals)]))
        test_list = ["A", "B", "C"]
        self.assertTrue(all([x == y for (x, y) in zip(test_list, df1.index.tolist())]))

        df1 = pd.read_excel("auto_xlsx_export.xlsx", index_col=0)
        test_list = [1, 2, 3]
        df1_vals = [x[0] for x in df1.values.tolist()]
        self.assertTrue(all([np.isclose(x, y) for (x, y) in zip(test_list, df1_vals)]))
        test_list = ["A", "B", "C"]
        self.assertTrue(all([x == y for (x, y) in zip(test_list, df1.index.tolist())]))

        os.remove("auto_csv_export.csv")
        os.remove("auto_xlsx_export.xlsx")
    def test_replace_har_header_in_file(self):

        dd = os.path.join(os.path.dirname(__file__), "data", "")
        shutil.copy2(dd + "test_Forenew7.shk", "test_Forenew7.shk")

        # CERO path
        df = DataTools.get_test_data(dd +
                                     "test_replace_har_header_in_file.pickle")
        fc = FromCERO(dd + "test_replace_har_header_in_file.yaml")
        fc.exec_procedures(df)

        test_har = DataTools.get_test_data("test_Forenew7.shk")
        tn = test_har.getHeaderArrayNames()

        gn = harpy.HarFileObj.loadFromDisk(
            filename="Forenew7.shk").getHeaderArrayNames()

        try:
            self.assertTrue(all([x == y for x, y in zip(tn, gn)]))
        except AssertionError as e:
            print("Test data headers: ", tn)
            print("Generated data headers: ", gn)
            raise e

        os.remove("test_Forenew7.shk")  # Tidy up
        os.remove("Forenew7.shk")  # Tidy up
예제 #3
0
    def test_plotoutput(self):

        try:
            import seaborn
        except ImportError:
            raise unittest.SkipTest("PyQt4 not installed, and therefore ConCERO's plotting capabilities cannot be used.")

        nf = "AssociateProfessionals.png"

        # CERO path
        png = DataTools.get_test_data(TestPlotOutput._dd + "test_plotoutput.png")

        cero = CERO.read_xlsx(TestPlotOutput._dd + "test_plotoutput.xlsx")
        fc = FromCERO(TestPlotOutput._dd + "test_plotoutput.yaml")
        fc.exec_procedures(cero)
        plt = DataTools.get_test_data(nf)

        # These lines have been commented out because figures are very hard to compare accurately - defaults seem to \
        # differ depending on operating system.
        # try:
        #     self.assertEqual(plt, png)
        # except AssertionError as e:
        #     raise e

        # Tidy up
        os.remove(os.path.relpath(nf))
예제 #4
0
    def test_cero2gallme(self):
        '''Tests CERO2GALLME conversion process. Tests procedure but not output to file.
        '''

        dd = os.path.dirname(__file__) + os.sep + "data" + os.sep

        df = DataTools.get_test_data(dd +
                                     r'test_cero_to_gallme_initialdata.pickle')

        c2g = FromCERO(dd + r'test_cero_to_gallme.yaml')
        c2g.exec_procedures(df)

        ser_a = df.loc[(("L_OUTPUT", "Electricity", "CAF"),),].iloc[0] + \
                df.loc[(("L_OUTPUT", "Electricity", "NAF"),),].iloc[0] + \
                df.loc[(("L_OUTPUT", "Electricity", "OSA"),),].iloc[0] + \
                df.loc[(("L_OUTPUT", "Electricity", "ZAF"),),].iloc[0]
        ser_b = df.loc[(("qo", "Electricity", "CAF"),),].iloc[0] + \
                df.loc[(("qo", "Electricity", "NAF"),),].iloc[0] + \
                df.loc[(("qo", "Electricity", "OSA"),),].iloc[0] + \
                df.loc[(("qo", "Electricity", "ZAF"),),].iloc[0]

        ser_b = ser_a * ser_b
        self.assertTrue(
            np.allclose(ser_b.values,
                        c2g.output_procedures['DemandYearly']["Value"].values))

        os.remove("gallme_input_data.gdx")  # Tidy up
예제 #5
0
    def test_load2(self):

        cero = pd.DataFrame.from_dict({"A": [1], "B": [2], "C": [3], "D": [4], "E": [5], "F": [6], }, orient='index',
                                      dtype=pd.np.float32)
        cero.sort_index(inplace=True)
        cero.columns = pd.DatetimeIndex(data=pd.to_datetime([2018], format="%Y"))

        fc = FromCERO(cfg.d_td + "test_fromcero_procedureload2.yaml")
        self.assertEqual(fc["procedures"][0]["name"], "Unnamed_proc_0")
        fc.exec_procedures(cero)

        df1 = pd.read_csv("procedureload2.csv", index_col=0)
        test_list = [1, 2, 3]
        df1_vals = [x[0] for x in df1.values.tolist()]
        self.assertTrue(all([np.isclose(x, y) for (x, y) in zip(test_list, df1_vals)]))
        test_list = ["A", "B", "C"]
        self.assertTrue(all([x == y for (x, y) in zip(test_list, df1.index.tolist())]))

        os.remove("procedureload2.csv")
예제 #6
0
    def test_sets_and_mapping(self):

        cero = pd.DataFrame.from_dict(
            {
                "A": [1],
                "B": [2],
                "C": [3],
                "D": [4],
                "E": [5],
                "F": [6],
            },
            orient='index',
            dtype=pd.np.float32)
        cero.sort_index(inplace=True)
        cero.columns = pd.DatetimeIndex(
            data=pd.to_datetime([2018], format="%Y"))
        self.assertTrue(CERO.is_cero(cero))

        fc = FromCERO(TestFromCERO._dd + "test_fromcero_mapping.yaml")
        fc.exec_procedures(cero)

        df1 = pd.read_csv("test_fromcero_mapping1.csv", index_col=0)
        test_list = [1, 2, 3]
        df1_vals = [x[0] for x in df1.values.tolist()]
        self.assertTrue(
            all([np.isclose(x, y) for (x, y) in zip(test_list, df1_vals)]))
        test_list = ["A", "B", "C"]
        self.assertTrue(
            all([x == y for (x, y) in zip(test_list, df1.index.tolist())]))

        df2 = pd.read_csv("test_fromcero_mapping2.csv", index_col=0)
        test_list = [4, 5, 6]
        df2_vals = [x[0] for x in df2.values.tolist()]
        self.assertTrue(all([x == y for (x, y) in zip(test_list, df2_vals)]))
        test_list = ["G", "H", "I"]
        self.assertTrue(
            all([x == y for (x, y) in zip(test_list, df2.index.tolist())]))

        os.remove("test_fromcero_mapping1.csv")
        os.remove("test_fromcero_mapping2.csv")
예제 #7
0
    def test_cero2har(self):

        output_har = r"test_har_out.har"
        shutil.copy2(TestCERO2HAR._dd + "Mdatnew7.har", output_har)

        # CERO path
        pickled_cero = TestCERO2HAR._dd + r'test_cero_to_har_initdata.pickle'

        df = DataTools.get_test_data(pickled_cero)
        h2c = FromCERO(TestCERO2HAR._dd + r'test_cero_to_har.yaml')
        h2c.exec_procedures(df)

        hf = harpy.HarFileObj.loadFromDisk(output_har)
        header = hf.getHeaderArrayObj(ha_name="MAR1")
        self.assertTrue(np.isclose(header["array"][0, 0, 0, 0, 0, 0], 2.44571))
        self.assertTrue(np.isclose(header["array"][0, 0, 0, 1, 0, 0],
                                   0.637938))
        self.assertTrue(np.isclose(header["array"][0, 0, 2, 0, 0, 0],
                                   0.381556))

        # Tidy up
        os.remove(output_har)
예제 #8
0
    def test_cero2luto(self):
        '''Tests CERO2LUTO conversion process.
        '''

        dd = os.path.join(os.path.dirname(__file__), "data", "")

        df = DataTools.get_test_data(dd +
                                     "test_cero_to_luto_initialdata.pickle")
        c2l = FromCERO(dd + r'test_cero_to_luto.yaml')
        c2l.exec_procedures(df)

        for procedure in c2l["procedures"]:
            if isinstance(procedure, dict):
                gen_output_file = procedure.get('output_file',
                                                procedure.get('name'))
                output_file = procedure['name']
            elif isinstance(procedure, str):
                gen_output_file = procedure
                output_file = procedure
            gen = np.load(gen_output_file + '.npy')[0]
            old = np.load(dd + 'test_' + output_file + '.npy')
            self.assertTrue(all(np.isclose(gen, old)))
            os.remove(gen_output_file + '.npy')
예제 #9
0
    def test_sets_and_mapping2(self):

        cero = pd.DataFrame.from_dict(
            {
                ("A", "1"): [1],
                ("A", "2"): [2],
                ("A", "3"): [3],
                ("B", "1"): [4],
                ("B", "2"): [5],
                ("B", "3"): [6],
                ("C", "1"): [7],
                ("C", "2"): [8],
                ("C", "3"): [9],
            },
            orient='index',
            dtype=pd.np.float32)
        cero.sort_index(inplace=True)
        cero.columns = pd.DatetimeIndex(
            data=pd.to_datetime([2018], format="%Y"))
        self.assertTrue(CERO.is_cero(cero))

        fc = FromCERO(TestFromCERO._dd + "test_fromcero_mapping2.yaml")
        fc.exec_procedures(cero)

        tc = ToCERO({
            "files": [{
                "file": "test_fromcero_complexmapping1.xlsx",
                "sheet": "CERO",
                "index_col": [0, 1]
            }]
        })
        df1 = tc.create_cero()
        test_list = list(range(1, 10))
        df1_vals = [x[0] for x in df1.values.tolist()]
        self.assertTrue(
            all([np.isclose(x, y) for (x, y) in zip(test_list, df1_vals)]))
        test_list = [("G", "1"), ("G", "2"), ("G", "3"), ("H", "1"),
                     ("H", "2"), ("H", "3"), ("I", "1"), ("I", "2"),
                     ("I", "3")]
        self.assertTrue(
            all([x == y for (x, y) in zip(test_list, df1.index.tolist())]))

        tc = ToCERO({
            "files": [{
                "file": "test_fromcero_complexmapping2.xlsx",
                "sheet": "CERO",
                "index_col": [0, 1]
            }]
        })
        df1 = tc.create_cero()
        test_list = list(range(1, 10))
        df1_vals = [x[0] for x in df1.values.tolist()]
        self.assertTrue(
            all([np.isclose(x, y) for (x, y) in zip(test_list, df1_vals)]))
        test_list = [("A", "G"), ("A", "H"), ("A", "I"), ("B", "G"),
                     ("B", "H"), ("B", "I"), ("C", "G"), ("C", "H"),
                     ("C", "I")]
        self.assertTrue(
            all([x == y for (x, y) in zip(test_list, df1.index.tolist())]))

        os.remove("test_fromcero_complexmapping1.xlsx")
        os.remove("test_fromcero_complexmapping2.xlsx")