def test_fileio(tmpdir): """ Round trip with saving and loading the testmrio Also tests some fileio related util functions """ mr = pymrio.load_test() save_path = str(tmpdir.mkdir('pymrio_test')) mr.save_all(save_path) # Testing getting the repo content, some random sample test fc = pymrio.get_repo_content(save_path) assert fc.iszip is False assert 'Z.txt' in [os.path.basename(f) for f in fc.filelist] assert 'FY.txt' in [os.path.basename(f) for f in fc.filelist] assert 'unit.txt' in [os.path.basename(f) for f in fc.filelist] mr2 = pymrio.load_all(save_path) npt.assert_allclose(mr.Z.values, mr2.Z.values, rtol=1e-5) # Testing the zip archive functions zip_arc = os.path.join(save_path, 'test_mrio.zip') if os.path.exists(zip_arc): os.remove(zip_arc) pymrio.archive(source=save_path, archive=zip_arc, remove_source=False, path_in_arc='test') emissions = pymrio.load(zip_arc, path_in_arc='test/emissions') npt.assert_allclose(mr.emissions.F.values, emissions.F.values, rtol=1e-5) mr3 = pymrio.load_all(zip_arc) npt.assert_allclose(mr.Z.values, mr3.Z.values, rtol=1e-5)
def test_underdefined_agg(): """ Testing correct error message for underdefined aggregation """ io = pymrio.load_test().calc_all() io.reset_all_to_coefficients() with pytest.raises(pymrio.core.mriosystem.AggregationError): io.aggregate(region_agg='global', sector_agg='total')
def test_wrong_inputs(): """ Tests if correct Exceptions are raised for wrong shaped input arguments """ io = pymrio.load_test().calc_all() with pytest.raises(ValueError) as VA_sector_number: sec_agg = range(len(io.get_sectors()) - 1) _ = io.aggregate(sector_agg=sec_agg, inplace=False) assert 'sector aggregation' in str(VA_sector_number.value).lower() with pytest.raises(ValueError) as VA_region_number: reg_agg = range(len(io.get_regions()) + 13) _ = io.aggregate(region_agg=reg_agg, inplace=False) assert 'region aggregation' in str(VA_region_number.value).lower() with pytest.raises(ValueError) as VA_sector_name: sec_agg = range(len(io.get_sectors())) _ = io.aggregate(sector_agg=sec_agg, sector_names='C', inplace=False) assert 'sector aggregation' in str(VA_sector_name.value).lower() with pytest.raises(ValueError) as VA_region_name: reg_agg = range(len(io.get_regions())) _ = io.aggregate(region_agg=reg_agg, # noqa region_names=['a', 'b'], inplace=False) assert 'region aggregation' in str(VA_region_name.value).lower()
def test_fileio(tmpdir): """ Round trip with saving and loading the testmrio Also tests some fileio related util functions """ mr = pymrio.load_test() save_path = str(tmpdir.mkdir('pymrio_test')) mr.save_all(save_path) # Testing getting the repo content, some random sample test fc = pymrio.get_repo_content(save_path) assert fc.iszip is False assert 'Z.txt' in [os.path.basename(f) for f in fc.filelist] assert 'FY.txt' in [os.path.basename(f) for f in fc.filelist] assert 'unit.txt' in [os.path.basename(f) for f in fc.filelist] mr2 = pymrio.load_all(save_path) npt.assert_allclose(mr.Z.values, mr2.Z.values, rtol=1e-5) # Testing the zip archive functions zip_arc = os.path.join(save_path, 'test_mrio.zip') if os.path.exists(zip_arc): os.remove(zip_arc) pymrio.archive( source=save_path, archive=zip_arc, remove_source=False, path_in_arc='test') emissions = pymrio.load(zip_arc, path_in_arc='test/emissions') npt.assert_allclose(mr.emissions.F.values, emissions.F.values, rtol=1e-5) mr3 = pymrio.load_all(zip_arc) npt.assert_allclose(mr.Z.values, mr3.Z.values, rtol=1e-5)
def test_total_agg(): """Testing aggregation to total values""" io = pymrio.load_test().calc_all() np.testing.assert_allclose( io.emissions.D_cba.sum(axis=1).to_frame().values, io.aggregate(region_agg="global", sector_agg="total").emissions.D_cba.values, )
def test_all(td_testmrio): mr = pymrio.load_test() mr.calc_all() npt.assert_allclose( td_testmrio.factor_inputs.D_imp_values, mr.factor_inputs.D_imp_reg.values, rtol = 1e-5 )
def test_aggreation_regions(): """ Testing aggregation of regions in various ways """ # All these representations should lead to the same results reg_agg_matrix = np.array([ [1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1] ]) reg_agg_vec1 = np.array([0, 0, 0, 1, 1, 1]) reg_agg_vec2 = [0, 0, 0, 1, 1, 1] reg_agg_vec3 = ['a', 'a', 'a', 'b', 'b', 'b'] reg_agg_df = pd.DataFrame( data=[('reg1', 'a'), ('reg5', 'b'), ('reg3', 'a'), ('reg4', 'b'), ('reg2', 'a'), ('reg6', 'b')], index=range(6), columns=['original', 'aggregated']) io = pymrio.load_test() io.calc_all() manual_agg = (io.emissions.D_cba_reg.reg1 + io.emissions.D_cba_reg.reg2 + io.emissions.D_cba_reg.reg3) io_agg_wo_names = io.aggregate(region_agg=reg_agg_matrix, inplace=False) np.testing.assert_allclose(manual_agg.values, io_agg_wo_names.emissions.D_cba_reg.reg0) assert ['reg0', 'reg1'] == io_agg_wo_names.get_regions().tolist() io_agg_with_names = io.aggregate(region_agg=reg_agg_matrix, region_names=['a', 'b'], inplace=False) np.testing.assert_allclose(manual_agg.values, io_agg_with_names.emissions.D_cba_reg.a) assert ['a', 'b'] == io_agg_with_names.get_regions().tolist() io_vec1 = io.aggregate(region_agg=reg_agg_vec1, inplace=False) io_vec2 = io.aggregate(region_agg=reg_agg_vec2, inplace=False) io_vec3 = io.aggregate(region_agg=reg_agg_vec3, inplace=False) np.testing.assert_allclose(manual_agg.values, io_vec1.emissions.D_cba_reg.reg0) np.testing.assert_allclose(manual_agg.values, io_vec2.emissions.D_cba_reg.reg0) np.testing.assert_allclose(manual_agg.values, io_vec3.emissions.D_cba_reg.a) io_df = io.aggregate(region_agg=reg_agg_df, inplace=False) np.testing.assert_allclose(manual_agg.values, io_df.emissions.D_cba_reg.a) assert ['a', 'b'] == io_df.get_regions().tolist()
def test_total_agg(): """ Testing aggregation to total values """ io = pymrio.load_test().calc_all() np.testing.assert_allclose(io.emissions.D_cba.sum(axis=1 ).to_frame().values, io.aggregate( region_agg='global', sector_agg='total').emissions.D_cba.values)
class TestMRIO: testmrio = pymrio.load_test() sectors = ['food', 'mining', 'manufactoring', 'electricity', 'construction', 'trade', 'transport', 'other'] regions = ['reg1', 'reg2', 'reg3', 'reg4', 'reg5', 'reg6']
class TestMRIO: testmrio = pymrio.load_test() sectors = [ 'food', 'mining', 'manufactoring', 'electricity', 'construction', 'trade', 'transport', 'other' ] regions = ['reg1', 'reg2', 'reg3', 'reg4', 'reg5', 'reg6'] Y_cat = [ 'Final consumption expenditure by households', 'Final consumption expenditure by non-profit ' 'organisations serving households (NPISH)', 'Final consumption expenditure by government', 'Gross fixed capital formation', 'Changes in inventories', 'Changes in valuables', 'Export' ]
def test_numerical_aggreation_sectors(): """Testing aggregation of sectors with a numeric array""" sec_agg = np.array([[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1]]) io = pymrio.load_test() io.calc_all() manual_agg = (io.emissions.D_pba.xs("trade", level="sector", axis=1) + io.emissions.D_pba.xs("transport", level="sector", axis=1) + io.emissions.D_pba.xs("other", level="sector", axis=1)) io.aggregate(sector_agg=sec_agg) np.testing.assert_allclose( manual_agg.values, io.emissions.D_pba.xs("sec2", level="sector", axis=1))
def test_all(td_testmrio): """ Full integration test Checks: 1) the cba calculation 2) concate extension """ mr = pymrio.load_test() mr.calc_all() npt.assert_allclose(td_testmrio.factor_inputs.D_imp_values, mr.factor_inputs.D_imp_reg.values, rtol=1e-5) sat_new = pymrio.concate_extension(mr.emissions, mr.factor_inputs, name='sat_new') assert len(sat_new.D_cba) == 3 assert mr.emissions.F.index[0] in sat_new.F.index assert mr.factor_inputs.F.index[0] in sat_new.F_Y.index assert all(mr.emissions.get_regions() == sat_new.get_regions()) assert all(mr.emissions.get_sectors() == sat_new.get_sectors())
def test_numerical_aggreation_sectors(): """ Testing aggregation of sectors with a numeric array """ sec_agg = np.array([ [1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1] ]) io = pymrio.load_test() io.calc_all() manual_agg = (io.emissions.D_pba.xs('trade', level='sector', axis=1) + io.emissions.D_pba.xs('transport', level='sector', axis=1) + io.emissions.D_pba.xs('other', level='sector', axis=1)) io.aggregate(sector_agg=sec_agg) np.testing.assert_allclose(manual_agg.values, io.emissions.D_pba.xs('sec2', level='sector', axis=1))
def test_all(td_testmrio): """ Full integration test Checks: 1) the cba calculation 2) concate extension """ mr = pymrio.load_test() mr.calc_all() npt.assert_allclose( td_testmrio.factor_inputs.D_imp_values, mr.factor_inputs.D_imp_reg.values, rtol=1e-5 ) sat_new = pymrio.concate_extension(mr.emissions, mr.factor_inputs, name='sat_new') assert len(sat_new.D_cba) == 3 assert mr.emissions.F.index[0] in sat_new.F.index assert mr.factor_inputs.F.index[0] in sat_new.FY.index assert all(mr.emissions.get_regions() == sat_new.get_regions()) assert all(mr.emissions.get_sectors() == sat_new.get_sectors())
class TestMRIO: testmrio = pymrio.load_test() sectors = [ "food", "mining", "manufactoring", "electricity", "construction", "trade", "transport", "other", ] regions = ["reg1", "reg2", "reg3", "reg4", "reg5", "reg6"] Y_cat = [ "Final consumption expenditure by households", "Final consumption expenditure by non-profit " "organisations serving households (NPISH)", "Final consumption expenditure by government", "Gross fixed capital formation", "Changes in inventories", "Changes in valuables", "Export", ]
class TestMRIO: testmrio = pymrio.load_test().calc_all()
import sys # Creating a string with the path to the pymrio package on your computer _pymrio_path = r"C:/Programming/pymrio" # if the pymrio path is not in "System path" - then add it if not _pymrio_path in sys.path: sys.path.append(_pymrio_path) # Cleaning up: deleting the string object del _pymrio_path # Importing all the code to a "namespace" called "mr" - now you can run all functions by writing # mr.func1("inputstring") input string in example function # mr.func2(2) - input int in example function import pymrio as mr mrio = mr.load_test() mrio.calc_all() mrio.save_all(r"C:\Programming\full_mrio_txt", table_format="txt")
# ## Example # For this example we use the test MRIO included in Pymrio. We also need # the Pandas library for loading the characterization table and pathlib for some folder manipulation. from pathlib import Path import pandas as pd import pymrio from pymrio.core.constants import PYMRIO_PATH # noqa # To load the test MRIO we use: io = pymrio.load_test() # and the characterization table with some foo factors can be loaded by charact_table = pd.read_csv( (PYMRIO_PATH["test_mrio"] / Path("concordance") / "emissions_charact.tsv"), sep="\t", ) charact_table # This table contains the columns 'stressor' and 'compartment' which correspond # to the index names of the test_mrio emission satellite accounts: io.emissions.F # Theses index-names / columns-names need to match in order to match
def setUp(self): self.matdict = {} # A 2X2 FOREGROUND self.matdict['PRO_f'] = np.array([['s+orm', 10005, 'kg'], ['Batt Packing', 10002, 'kg']], dtype=object) self.matdict['PRO_header'] = np.array([['FULL NAME', 'MATRIXID','UNIT']]) self.matdict['A_ff'] = scipy.sparse.csc_matrix([[0, 1], [10, 11]]) # WITH THREE STRESSORS self.matdict['STR_header'] = np.array([['FULL NAME', 'MATRIXID','UNIT']]) self.matdict['STR'] = np.array([['stress01', 1614, 'kg'], ['stress02', 1615, 'kg'], ['stress03', 1616, 'kg']], dtype=object) self.matdict['F_f'] = scipy.sparse.csc_matrix([[0.3, 0.0], [0.1, 0.2], [0.0, 0.0]]) self.matdict['y_f'] = scipy.sparse.csc_matrix([[1.0],[0.0]]) # FOUR BACKGROUND PROCESSES self.matdict['A_gen'] = scipy.sparse.csc_matrix([[0, 1, 0, 0], [0, 0, 2, 0], [1, 0, 1, 0], [0, 3, 0, 0]]) self.matdict['PRO_gen'] = np.array([['back01', 1, 'kg'], ['back02', 2, 'kg'], ['back03', 3, 'MJ'], ['back04', 4, 'MJ']], dtype=object) self.matdict['A_bf'] = scipy.sparse.csc_matrix([[0.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 0.0]]) self.matdict['F_gen'] = scipy.sparse.csc_matrix(np.zeros((3,4))) self.matdict['y_gen'] = scipy.sparse.csc_matrix(np.zeros((4,1))) self.matdict['C'] = scipy.sparse.csc_matrix(np.ones((1,3))) self.matdict['IMP'] = np.array([['GWP100', 1,'kgCO2-eq']], dtype=object) self.matdict['IMP_header'] = np.array([['FULL NAME', 'MATRIXID','UNIT']]) #----------------- self.bigdict = self.matdict.copy() self.bigdict['PRO_gen'] = np.array([['back01', 1, 'kg'], ['back05', 5, 'kg'], ['back03', 3, 'MJ'], ['back02', 2, 'kg'], ['back04', 4, 'MJ']], dtype=object) self.bigdict['A_gen'] = scipy.sparse.csc_matrix( [[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 1, 0, 0], [0, 0, 2, 0, 0], [0, 3, 0, 0, 0]]) self.bigdict['y_gen'] = scipy.sparse.csc_matrix(np.zeros((5, 1))) self.bigdict['F_gen'] = scipy.sparse.csc_matrix(np.zeros((3, 5))) #----------------- self.smalldict = self.matdict.copy() self.smalldict['PRO_gen'] = np.array([['back01', 1, 'kg'], ['back03', 3, 'MJ'], ['back04', 4, 'MJ']], dtype=object) self.smalldict['A_gen'] = scipy.sparse.csc_matrix( [[0, 0, 0], [1, 1, 0], [0, 0, 0]]) self.smalldict['y_gen'] = scipy.sparse.csc_matrix(np.zeros((3, 1))) self.smalldict['F_gen'] = scipy.sparse.csc_matrix(np.zeros((3, 3))) self.mrio = pymrio.load_test() self.mrio.calc_all()
def test_fileio(tmpdir): """Round trip with saving and loading the testmrio Also tests some fileio related util functions """ mr = pymrio.load_test() save_path = str(tmpdir.mkdir("pymrio_test")) with pytest.raises(ValueError): mr.save_all(save_path, table_format="nan") mr.save_all(save_path) # Testing getting the repo content, some random sample test fc = pymrio.get_repo_content(save_path) assert fc.iszip is False assert "Z.txt" in [os.path.basename(f) for f in fc.filelist] assert "F_Y.txt" in [os.path.basename(f) for f in fc.filelist] assert "unit.txt" in [os.path.basename(f) for f in fc.filelist] mr2 = pymrio.load_all(save_path) npt.assert_allclose(mr.Z.values, mr2.Z.values, rtol=1e-5) # Testing the zip archive functions zip_arc = os.path.join(save_path, "test_mrio.zip") if os.path.exists(zip_arc): os.remove(zip_arc) with pytest.raises(FileExistsError): pymrio.archive( source=fc.filelist, archive=zip_arc, remove_source=False, path_in_arc="test_duplicates", ) pymrio.archive( source=fc.filelist, archive=zip_arc, remove_source=False, path_in_arc="test_duplicates", ) if os.path.exists(zip_arc): os.remove(zip_arc) pymrio.archive(source=save_path, archive=zip_arc, remove_source=True, path_in_arc="test") fc = pymrio.get_repo_content(zip_arc) assert fc.iszip is True assert "Z.txt" in [os.path.basename(f) for f in fc.filelist] assert "F_Y.txt" in [os.path.basename(f) for f in fc.filelist] assert "unit.txt" in [os.path.basename(f) for f in fc.filelist] emissions = pymrio.load(zip_arc, path_in_arc="test/emissions") npt.assert_allclose(mr.emissions.F.values, emissions.F.values, rtol=1e-5) mr3 = pymrio.load_all(zip_arc) npt.assert_allclose(mr.Z.values, mr3.Z.values, rtol=1e-5) with pytest.raises(pymrio.ReadError): pymrio.load_all(zip_arc, path_in_arc="./foo") with pytest.raises(pymrio.ReadError): pymrio.load(path="./foo")
def test_underdefined_agg(): """Testing correct error message for underdefined aggregation""" io = pymrio.load_test().calc_all() io.reset_all_to_coefficients() with pytest.raises(pymrio.core.mriosystem.AggregationError): io.aggregate(region_agg="global", sector_agg="total")
def setUp(self): self.matdict = {} # A 2X2 FOREGROUND self.matdict['PRO_f'] = np.array( [['s+orm', 10005, 'kg'], ['Batt Packing', 10002, 'kg']], dtype=object) self.matdict['PRO_header'] = np.array( [['FULL NAME', 'MATRIXID', 'UNIT']]) self.matdict['A_ff'] = scipy.sparse.csc_matrix([[0, 1], [10, 11]]) # WITH THREE STRESSORS self.matdict['STR_header'] = np.array( [['FULL NAME', 'MATRIXID', 'UNIT']]) self.matdict['STR'] = np.array( [['stress01', 1614, 'kg'], ['stress02', 1615, 'kg'], ['stress03', 1616, 'kg']], dtype=object) self.matdict['F_f'] = scipy.sparse.csc_matrix([[0.3, 0.0], [0.1, 0.2], [0.0, 0.0]]) self.matdict['y_f'] = scipy.sparse.csc_matrix([[1.0], [0.0]]) # FOUR BACKGROUND PROCESSES self.matdict['A_gen'] = scipy.sparse.csc_matrix([[0, 1, 0, 0], [0, 0, 2, 0], [1, 0, 1, 0], [0, 3, 0, 0]]) self.matdict['PRO_gen'] = np.array( [['back01', 1, 'kg'], ['back02', 2, 'kg'], ['back03', 3, 'MJ'], ['back04', 4, 'MJ']], dtype=object) self.matdict['A_bf'] = scipy.sparse.csc_matrix([[0.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 0.0]]) self.matdict['F_gen'] = scipy.sparse.csc_matrix(np.zeros((3, 4))) self.matdict['y_gen'] = scipy.sparse.csc_matrix(np.zeros((4, 1))) self.matdict['C'] = scipy.sparse.csc_matrix(np.ones((1, 3))) self.matdict['IMP'] = np.array([['GWP100', 1, 'kgCO2-eq']], dtype=object) self.matdict['IMP_header'] = np.array( [['FULL NAME', 'MATRIXID', 'UNIT']]) #----------------- self.bigdict = self.matdict.copy() self.bigdict['PRO_gen'] = np.array( [['back01', 1, 'kg'], ['back05', 5, 'kg'], ['back03', 3, 'MJ'], ['back02', 2, 'kg'], ['back04', 4, 'MJ']], dtype=object) self.bigdict['A_gen'] = scipy.sparse.csc_matrix([[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 1, 0, 0], [0, 0, 2, 0, 0], [0, 3, 0, 0, 0]]) self.bigdict['y_gen'] = scipy.sparse.csc_matrix(np.zeros((5, 1))) self.bigdict['F_gen'] = scipy.sparse.csc_matrix(np.zeros((3, 5))) self.smalldict = self.matdict.copy() self.smalldict['PRO_gen'] = np.array( [['back01', 1, 'kg'], ['back03', 3, 'MJ'], ['back04', 4, 'MJ']], dtype=object) self.smalldict['A_gen'] = scipy.sparse.csc_matrix([[0, 0, 0], [1, 1, 0], [0, 0, 0]]) self.smalldict['y_gen'] = scipy.sparse.csc_matrix(np.zeros((3, 1))) self.smalldict['F_gen'] = scipy.sparse.csc_matrix(np.zeros((3, 3))) self.mrio = pymrio.load_test() self.mrio.calc_all()