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
Example #2
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))
    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
Example #4
0
    def test_vurm2cero(self):
        '''Tests VURM2CERO conversion process.'''

        v2c = ToCERO(conf=(TestVURM2CERO._dd + r'test_vurm_to_cero.yaml'))
        cero = v2c.create_cero()
        df = DataTools.get_test_data(TestVURM2CERO._dd +
                                     r'test_vurm_to_cero_finaldata.pickle')

        self.assertTrue(cero.equals(df))
    def test_sceninputs_to_cero2(self):
        '''Test xlsx to CERO conversion.'''
        s2c = ToCERO(conf=(TestScenIn2CERO._dd + 'test_xlsx_to_cero.yaml'))
        cero = s2c.create_cero()
        df = DataTools.get_test_data(
            os.path.normpath('data/test_xlsx_to_cero.pickle'))

        self.assertTrue(
            cero.loc[df.index].equals(df))  # Order-independent test
Example #6
0
    def test_gtape2cero(self):

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

        g2c = ToCERO(dd + r'test_gtape_to_cero.yaml')
        cero = g2c.create_cero()
        df = DataTools.get_test_data(dd +
                                     r'test_gtape_to_cero_finaldata.pickle')

        self.assertTrue(cero.equals(df))
Example #7
0
    def test_complex_xlsx(self):

        to_cero = ToCERO(conf=(TestToCERO._dd +
                               r'test_complex_xlsx_import.yaml'))
        cero = to_cero.create_cero()

        df = DataTools.get_test_data(TestToCERO._dd +
                                     "test_complex_xlsx_result.pickle")

        self.assertTrue(CERO.is_cero(cero))
        self.assertTrue(cero.equals(df))
Example #8
0
    def test_time_dim(self):

        shutil.copy2(TestHAR2CERO._dd + "test_timedim.har", "test_timedim.har")

        h2c = ToCERO(conf=(TestHAR2CERO._dd +
                           r'test_har_to_cero_timedim.yaml'))
        cero = h2c.create_cero()
        df = DataTools.get_test_data(TestHAR2CERO._dd +
                                     r'test_har_to_cero_timedim.pickle')

        self.assertTrue(
            cero.loc[df.index].equals(df))  # Order-independent test

        os.remove("test_timedim.har")
Example #9
0
    def test_sceninputs_to_cero(self):
        '''Tests the conversion of hars to ceros.'''

        shutil.copy2(TestHAR2CERO._dd + "Mdatnew7.har", "Mdatnew7.har")

        s2c = ToCERO(conf=(TestHAR2CERO._dd + r'test_har_to_cero.yaml'))
        cero = s2c.create_cero()
        df = DataTools.get_test_data(TestHAR2CERO._dd +
                                     r'test_har_to_cero.pickle')

        self.assertTrue(
            cero.loc[df.index].equals(df))  # Order-independent test

        os.remove("Mdatnew7.har")
Example #10
0
    def test_har_repetitive_sets(self):

        fo = ToCERO._FileObj({
            "file":
            "test.har",
            "search_paths":
            TestToCERO_FileObj._dd,
            "head_arrs": [{
                "name": "ARR7",
                "default_year": 2018
            }]
        })
        cero = fo.import_file_as_cero()

        df = DataTools.get_test_data(TestToCERO_FileObj._dd +
                                     "test_har_repetitive_sets.pickle")

        self.assertTrue(cero.equals(df))
Example #11
0
    def test_cero2austimes(self):
        '''Tests CERO2AusTIMES conversion process.
        '''
        dd = os.path.dirname(__file__) + os.sep + "data" + os.sep

        df = DataTools.get_test_data(dd + r'test_cero_to_luto_initialdata.pickle') # Uses same data as LUTO
        c2a = FromCERO(df, conf_file=(dd + r'test_cero_to_austimes.yaml'))

        for series in c2a.series:
            if isinstance(series, dict):
                gen_output_file = series.get('output_file', series.get('name'))
                output_file = series['name']
            elif isinstance(series, str):
                gen_output_file = series
                output_file = series
            gen = np.load(gen_output_file + '.npy')
            old = np.load('test_' + output_file + '.npy')
            self.assertTrue(all(np.isclose(gen, old)))
Example #12
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)
Example #13
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')
Example #14
0
    def test_idxconflict(self):
        cero = DataTools.get_test_data(TestCERO._dd + "test_cero.pickle")

        with self.assertRaises(CERO.CEROIndexConflict):
            CERO.combine_ceros([cero, cero], overwrite=False, verify_cero=True)