def test_append_to_foreground(self):

        a = pylcaio.LCAIO(1, verbose=False)
        b = pylcaio.LCAIO(1, verbose=False)
        a.extract_background(self.matdict)
        a.extract_foreground(self.matdict)

        B = {}
        B['PRO_f'] = np.array([['foo', 10, 'kg']], dtype=object)

        B['A_bf'] = scipy.sparse.csc_matrix([[1.0], [0.0], [0.0], [0.0]])

        B['PRO_header'] = np.array([['FULL NAME', 'MATRIXID', 'UNIT']])

        B['A_ff'] = scipy.sparse.csc_matrix([[11.0]])

        # WITH THREE STRESSORS

        B['STR'] = np.array(
            [['stress01', 1614, 'kg'], ['stress02', 1615, 'kg'],
             ['stress03', 1616, 'kg']],
            dtype=object)

        B['F_f'] = scipy.sparse.csc_matrix([[0.0], [0.2], [0.0]])

        B['y_f'] = scipy.sparse.csc_matrix([[2.0]])

        b.extract_background(self.matdict)
        b.extract_foreground(B)

        a.append_to_foreground(b)

        y_f = pd.DataFrame({0: {10005: 1.0, 10002: 0.0, 10: 0.0}})
        assert_frames_equivalent(a.y_f, y_f)
    def test_append_to_foreground_w_ValueError(self):
        a = pylcaio.LCAIO(verbose=False)
        b = pylcaio.LCAIO(verbose=False)
        a.extract_foreground(self.matdict)
        b.extract_foreground(self.matdict)

        with self.assertRaises(ValueError):
            a.append_to_foreground(b)
    def test_append_to_foreground_w_ValueError_w_multiindex(self):
        index = [0, 1, -1]
        a = pylcaio.LCAIO(index, verbose=False)
        b = pylcaio.LCAIO(index, verbose=False)
        a.extract_foreground(self.matdict)
        b.extract_foreground(self.matdict)

        with self.assertRaises(ValueError):
            a.append_to_foreground(b)
    def test_append_to_foreground_multiindex(self):
        a = pylcaio.LCAIO([0, 1, -1], verbose=False)
        b = pylcaio.LCAIO([0, 1, -1], verbose=False)
        a.extract_foreground(self.matdict)

        B = {}
        B['PRO_f'] = np.array([['foo', 10, 'kg']], dtype=object)

        B['PRO_gen'] = np.array([['back01', 1, 'kg'], ['back02', 2, 'kg'],
                                 ['back03', 3, 'MJ'], ['back04', 4, 'MJ']],
                                dtype=object)

        B['A_bf'] = scipy.sparse.csc_matrix([[1.0], [0.0], [0.0], [0.0]])

        B['PRO_header'] = np.array([['FULL NAME', 'MATRIXID', 'UNIT']])

        B['A_ff'] = scipy.sparse.csc_matrix([[11.0]])

        # WITH THREE STRESSORS

        B['STR'] = np.array(
            [['stress01', 1614, 'kg'], ['stress02', 1615, 'kg'],
             ['stress03', 1616, 'kg']],
            dtype=object)

        B['F_f'] = scipy.sparse.csc_matrix([[0.0], [0.2], [0.0]])

        B['y_f'] = scipy.sparse.csc_matrix([[0]])

        b.extract_background(self.matdict)
        b.extract_foreground(B)

        a.append_to_foreground(b)

        A_bf = pd.DataFrame({
            ('foo', 10, 'kg'): {
                ('back01', 1, 'kg'): 1.0,
                ('back03', 3, 'MJ'): 0.0,
                ('back04', 4, 'MJ'): 0.0,
                ('back02', 2, 'kg'): 0.0
            },
            ('Batt Packing', 10002, 'kg'): {
                ('back01', 1, 'kg'): 1.0,
                ('back03', 3, 'MJ'): 0.0,
                ('back04', 4, 'MJ'): 0.0,
                ('back02', 2, 'kg'): 0.0
            },
            ('s+orm', 10005, 'kg'): {
                ('back01', 1, 'kg'): 0.0,
                ('back03', 3, 'MJ'): 1.0,
                ('back04', 4, 'MJ'): 0.0,
                ('back02', 2, 'kg'): 1.0
            }
        })

        assert_frames_equivalent(a.A_bf, A_bf)
    def test_match_foreground_background_trivial(self):
        a = pylcaio.LCAIO(verbose=False)
        a.extract_background(self.matdict)
        a.extract_foreground(self.matdict)

        b = pylcaio.LCAIO(verbose=False)
        b.extract_background(self.matdict)
        b.extract_foreground(self.matdict)

        a.match_foreground_to_background()
        assert (np.all(a.A_bf == b.A_bf))
        assert (np.all(a.F_f == b.F_f))
    def test_hybridization_detail(self):

        # define arda inventory object and populate
        a = pylcaio.LCAIO([0, 1], verbose=False)
        a.extract_background(self.matdict)
        a.extract_foreground(self.matdict)
        a.extract_io_background_from_pymrio(self.mrio)

        # Define two categories of IO flows
        a.io_categories['material'] = ['mining', 'food']
        a.io_categories['energy'] = ['electricity']

        # Hybridize, correcting for double counting in these two categories
        a.hybridize_process(('Batt Packing', 10002), ('reg2', 'transport'),
                            0.1,
                            doublecounted_categories=('material', 'energy'))

        # assert that it got copied properly
        self.assertEqual(
            a.A.ix[('reg2', 'manufactoring'), ('Batt Packing', 10002)],
            a.A.ix[('reg2', 'manufactoring'), ('reg2', 'transport')] / 10)

        # assert that double counting from materiol inputs got corrected properly
        self.assertEqual(a.A.ix[('reg1', 'food'), ('Batt Packing', 10002)], 0)
        # assert that double counting from energy inputs got corrected properly
        self.assertEqual(
            a.A.ix[('reg1', 'electricity'), ('Batt Packing', 10002)], 0)

        # assert that intrasector flows were removed
        self.assertEqual(
            a.A.ix[('reg1', 'transport'), ('Batt Packing', 10002)], 0)

        # assert that direct emissions did not get copied
        self.assertEqual(
            a.F.ix[('emission_type1', 'air'), ('Batt Packing', 10002)], 0)
    def test_hybridization_withOverwrite(self):

        # define arda inventory object and populate
        a = pylcaio.LCAIO([0, 1], verbose=False)
        a.extract_background(self.matdict)
        a.extract_foreground(self.matdict)
        a.extract_io_background_from_pymrio(self.mrio)

        # Define two categories of IO flows
        a.io_categories['material'] = ['mining', 'food']
        a.io_categories['energy'] = ['electricity']

        # Hybridize, correcting for double counting in these two categories
        a.hybridize_process(('Batt Packing', 10002), ('reg2', 'transport'),
                            0.1,
                            doublecounted_categories=('material', 'energy'),
                            overwrite=True,
                            verbose=True)

        A_io_f_0 = a.A_io_f.copy()

        # Hybridize second time
        a.hybridize_process(('Batt Packing', 10002), ('reg2', 'transport'),
                            1E9,
                            doublecounted_categories=('material', 'energy'),
                            overwrite=True,
                            verbose=True)

        # assert that second hybridization DID happen
        self.assertTrue(np.all(a.A_io_f >= A_io_f_0))
示例#8
0
def test_identify_rows(random_LCA_system, random_IO_system, random_parameters):
    pylcaio_object = pylcaio.LCAIO(
        PRO_f=random_LCA_system['PRO_f'],
        A_ff=random_LCA_system['A_ff'],
        F_f=random_LCA_system['F_f'],
        C_f=random_LCA_system['C_f'],
        STR_f=random_LCA_system['STR_f'],
        y_f=random_LCA_system['y_f'],
        lca_database_name_and_version='ecoinvent3.5',
        A_io=random_IO_system['A_io'],
        F_io=random_IO_system['F_io'],
        C_io=random_IO_system['C_io'],
        STR_io=random_IO_system['STR_io'],
        y_io=random_IO_system['y_io'],
        io_database_name_and_version='exiobase3',
        listcountry=random_parameters['listcountry'],
        listregions=random_parameters['listregions'],
        countries_per_regions=random_parameters['countries_per_regions'],
        replacements1=random_parameters['replacements1'],
        reference_year_IO=random_parameters['reference_year_IO'],
        number_of_countries_IO=random_parameters['number_of_countries_IO'],
        number_of_products_IO=random_parameters['number_of_products_IO'],
        list_to_hyb=random_LCA_system['list_to_hyb'],
        list_not_hyb=random_LCA_system['list_not_to_hyb'],
        listmarket=random_LCA_system['listmarket'])
    pylcaio_object.identify_rows()

    sets = []
    for RoW in pylcaio_object.dictRoW.values():
        sets.append(set(RoW))
    assert set(['FR', 'CH', 'ZA']) in sets
    assert set(['CA', 'ZA']) in sets
    assert len(pylcaio_object.dictRoW.values()) == 2
    assert type(pylcaio_object.dictRoW) == dict
    assert 'RoW' not in pylcaio_object.PRO_f.io_geography.tolist()
    def test_calc_lifecycle(self):

        a = pylcaio.LCAIO([0, 1], verbose=False)
        a.extract_background(self.matdict)
        a.extract_foreground(self.matdict)
        x = a.calc_lifecycle('production')
        x0 = pd.DataFrame.from_dict({
            0: {
                ('back03', 3): -0.25,
                ('back02', 2): -0.0,
                ('back04', 4): -0.0,
                ('back01', 1): -0.5,
                ('s+orm', 10005): 0.5,
                ('Batt Packing', 10002): -0.5
            }
        })

        assert_frames_equivalent(x, x0)

        e = a.calc_lifecycle('emissions')
        e0 = pd.DataFrame.from_dict({
            0: {
                ('stress02', 1615): -0.050000000000000003,
                ('stress01', 1614): 0.14999999999999999,
                ('stress03', 1616): 0.0
            }
        })
        assert_frames_equivalent(e, e0)

        d = a.calc_lifecycle('impacts')
        d0 = pylcaio.i2s(
            pd.DataFrame.from_dict({0: {
                ('GWP100', 1): 0.099999999999999992
            }}))
        assert_frames_equivalent(d, d0)
示例#10
0
def test_calc_proc(random_LCA_system, random_IO_system, random_parameters):
    pylcaio_object = pylcaio.LCAIO(
        PRO_f=random_LCA_system['PRO_f'],
        A_ff=random_LCA_system['A_ff'],
        F_f=random_LCA_system['F_f'],
        C_f=random_LCA_system['C_f'],
        STR_f=random_LCA_system['STR_f'],
        y_f=random_LCA_system['y_f'],
        lca_database_name_and_version='ecoinvent3.5',
        A_io=random_IO_system['A_io'],
        F_io=random_IO_system['F_io'],
        C_io=random_IO_system['C_io'],
        STR_io=random_IO_system['STR_io'],
        y_io=random_IO_system['y_io'],
        io_database_name_and_version='exiobase3',
        listcountry=random_parameters['listcountry'],
        listregions=random_parameters['listregions'],
        countries_per_regions=random_parameters['countries_per_regions'],
        replacements1=random_parameters['replacements1'],
        reference_year_IO=random_parameters['reference_year_IO'],
        number_of_countries_IO=random_parameters['number_of_countries_IO'],
        number_of_products_IO=random_parameters['number_of_products_IO'],
        list_to_hyb=random_LCA_system['list_to_hyb'],
        list_not_hyb=random_LCA_system['list_not_to_hyb'],
        listmarket=random_LCA_system['listmarket'])
    pylcaio_object.identify_rows()
    pylcaio_object.calc_productions()

    # check prod not empty
    assert len(pylcaio_object.total_prod_country) != 0
    assert len(pylcaio_object.total_prod_region) != 0
    assert len(pylcaio_object.total_prod_RoW) != 0
    # check no string in prod
    assert type(pylcaio_object.total_prod_country.sum().sum()) == np.float64
    assert type(pylcaio_object.total_prod_region.sum().sum()) == np.float64
    assert type(pylcaio_object.total_prod_RoW.sum().sum()) == np.float64
    # check non NaN in prod
    assert not pylcaio_object.total_prod_country.isnull().any()[0]
    assert not pylcaio_object.total_prod_region.isnull().any()[0]
    assert not pylcaio_object.total_prod_RoW.isnull().any()[0]
    # check if prods are in a dataframe format
    assert type(pylcaio_object.total_prod_country) == pd.core.frame.DataFrame
    assert type(pylcaio_object.total_prod_region) == pd.core.frame.DataFrame
    assert type(pylcaio_object.total_prod_RoW) == pd.core.frame.DataFrame
    # check no prod is negative
    assert pylcaio_object.total_prod_country[
        pylcaio_object.total_prod_country < 0].sum()[0] == 0
    assert pylcaio_object.total_prod_region[
        pylcaio_object.total_prod_region < 0].sum().sum() == 0
    assert pylcaio_object.total_prod_RoW[
        pylcaio_object.total_prod_RoW < 0].sum().sum() == 0
    # check that indexes are the same
    assert (pylcaio_object.A_io.index ==
            pylcaio_object.total_prod_country.index).all()
    assert (pylcaio_object.A_io.index.levels[1] ==
            pylcaio_object.total_prod_region.index).all()
    assert (pylcaio_object.A_io.index.levels[1] ==
            pylcaio_object.total_prod_RoW.index).all()
示例#11
0
    def test_properties_multiindex(self):
        a = pylcaio.LCAIO([0, 1], verbose=False)
        a.extract_background(self.matdict)
        a.extract_foreground(self.matdict)
        a.extract_io_background_from_pymrio(self.mrio)

        a.A
        a.F
        a.PRO
        a.STR_all
        a.C_all
示例#12
0
 def test_match_foreground_background(self):
     a = pylcaio.LCAIO(verbose=False)
     a.extract_foreground(self.matdict)
     a.extract_background(self.bigdict)
     a.match_foreground_to_background()
     assert (np.all(a.A_bf.values == np.array([
         [0, 1],
         [0, 0],  # <--row5 insert here
         [1, 0],
         [1, 0],  # <--row2 moved here
         [0, 0]
     ])))
示例#13
0
    def test_extract_io_background_from_pymrio(self):

        a = pylcaio.LCAIO(verbose=False)
        a.extract_background(self.matdict)
        a.extract_io_background_from_pymrio(self.mrio)

        # assertion1
        assert (np.all(a.A_io.values == self.mrio.A.values))

        # assertion2: stest extensions preserved
        self.assertAlmostEqual(
            a.F_io.values.sum(),
            self.mrio.emissions.S.values.sum() +
            self.mrio.factor_inputs.S.values.sum())
示例#14
0
    def test_delete_processes_foreground(self):
        a = pylcaio.LCAIO(1, verbose=False)
        a.extract_foreground(self.matdict)
        a.delete_processes_foreground([10005])

        # A 2X2 FOREGROUND

        B = {}
        B['PRO_f'] = np.array([['Batt Packing', 10002, 'kg']], dtype=object)

        B['A_bf'] = scipy.sparse.csc_matrix([[1], [0], [0], [0]])

        B['PRO_header'] = np.array([['FULL NAME', 'MATRIXID', 'UNIT']])

        B['A_ff'] = scipy.sparse.csc_matrix([[11]])

        # WITH THREE STRESSORS

        B['STR'] = np.array(
            [['stress01', 1614, 'kg'], ['stress02', 1615, 'kg'],
             ['stress03', 1616, 'kg']],
            dtype=object)

        B['F_f'] = scipy.sparse.csc_matrix([[0.0], [0.2], [0.0]])

        B['y_f'] = scipy.sparse.csc_matrix([[0.0]])

        b = pylcaio.LCAIO(1, verbose=False)
        b.extract_background(self.matdict)
        b.extract_foreground(B)
        pdt.assert_frame_equal(a.A_ff, b.A_ff)
        pdt.assert_frame_equal(a.F_f, b.F_f)
        pdt.assert_frame_equal(a.y_f, b.y_f)

        assert (np.all(a.A_bf.values == b.A_bf.values))
        assert (np.all(a.PRO_f == b.PRO_f))
示例#15
0
    def test_import_and_export_matdict_keys_roundtrip(self):

        if os.name == 'nt':
            temporary = '/temp/'
        else:
            temporary = '/tmp/'

        a = pylcaio.LCAIO(verbose=False)
        a.extract_background(self.matdict)
        a.to_matfile(temporary + 'test_background.mat',
                     foreground=False,
                     background=True)
        a.extract_foreground(self.matdict)
        a.to_matfile(temporary + 'test_foreground.mat',
                     foreground=True,
                     background=False)

        fore = sio.loadmat(temporary + 'test_foreground.mat')
        back = sio.loadmat(temporary + 'test_background.mat')

        assert (len(self.matdict.keys() - fore.keys() - back.keys()) == 0)
示例#16
0
    def test_change_process_ids(self):
        a = pylcaio.LCAIO([1], verbose=False)
        a.extract_foreground(self.matdict)

        a.increase_foreground_process_ids(70000)

        A_ff = pd.DataFrame({
            80005: {
                80005: 0,
                80002: 10
            },
            80002: {
                80005: 1,
                80002: 11
            }
        })
        assert_frames_equivalent(a.A_ff, A_ff)

        PRO_f = np.array(
            [['s+orm', 80005, 'kg'], ['Batt Packing', 80002, 'kg']],
            dtype=object)
        assert (np.all(a.PRO_f == PRO_f))
示例#17
0
def test_hybridize(random_LCA_system, random_IO_system, random_parameters):
    pylcaio_object = pylcaio.LCAIO(
        PRO_f=random_LCA_system['PRO_f'],
        A_ff=random_LCA_system['A_ff'],
        F_f=random_LCA_system['F_f'],
        C_f=random_LCA_system['C_f'],
        STR_f=random_LCA_system['STR_f'],
        y_f=random_LCA_system['y_f'],
        lca_database_name_and_version='ecoinvent3.5',
        A_io=random_IO_system['A_io'],
        F_io=random_IO_system['F_io'],
        C_io=random_IO_system['C_io'],
        STR_io=random_IO_system['STR_io'],
        y_io=random_IO_system['y_io'],
        io_database_name_and_version='exiobase3',
        listcountry=random_parameters['listcountry'],
        listregions=random_parameters['listregions'],
        countries_per_regions=random_parameters['countries_per_regions'],
        replacements1=random_parameters['replacements1'],
        reference_year_IO=random_parameters['reference_year_IO'],
        number_of_countries_IO=random_parameters['number_of_countries_IO'],
        number_of_products_IO=random_parameters['number_of_products_IO'],
        list_to_hyb=random_LCA_system['list_to_hyb'],
        list_not_hyb=random_LCA_system['list_not_to_hyb'],
        listmarket=random_LCA_system['listmarket'],
        number_of_RoW_IO=random_parameters['number_of_RoW_IO'],
        io_categories=random_parameters['io_categories'],
        STAM_table=random_parameters['STAM_table'],
        categories_same_functionality=random_parameters[
            'categories_same_functionality'])
    pylcaio_object.hybridize('STAM')

    assert pylcaio_object.A_io_f.sum().sum() != 0
    assert not pylcaio_object.A_io_f.empty
    assert (pylcaio_object.A_io_f.index == pylcaio_object.A_io.index).all()
    assert (pylcaio_object.A_io_f.columns == pylcaio_object.A_ff.columns).all()

    # all the sector2, 3 and 5 rows should be zero
    assert pylcaio_object.A_io_f.loc[[
        i for i in pylcaio_object.A_io_f.index if i[1] == 'sector2'
    ]].sum().sum() == 0
    assert pylcaio_object.A_io_f.loc[[
        i for i in pylcaio_object.A_io_f.index if i[1] == 'sector3'
    ]].sum().sum() == 0
    assert pylcaio_object.A_io_f.loc[[
        i for i in pylcaio_object.A_io_f.index if i[1] == 'sector5'
    ]].sum().sum() == 0

    # the sector4 row should not have ANY zero value
    assert pylcaio_object.A_io_f.loc[[
        i for i in pylcaio_object.A_io_f.index if i[1] == 'sector4'
    ]][pylcaio_object.A_io_f.loc[
        [i for i in pylcaio_object.A_io_f.index
         if i[1] == 'sector4']] == 0].isnull().all().all()

    # no zero in row sector1 columns 0, 2 and 4
    assert pylcaio_object.A_io_f.loc[
        [i for i in pylcaio_object.A_io_f.index if i[1] == 'sector1'], [
            pylcaio_object.A_io_f.columns[0], pylcaio_object.A_io_f.
            columns[2], pylcaio_object.A_io_f.columns[4]
        ]][pylcaio_object.A_io_f.loc[
            [i for i in pylcaio_object.A_io_f.index if i[1] == 'sector1'], [
                pylcaio_object.A_io_f.columns[0], pylcaio_object.A_io_f.
                columns[2], pylcaio_object.A_io_f.columns[4]
            ]] == 0].isnull().all().all()
    # only zeros in row sector1 columns 1 and 3
    assert pylcaio_object.A_io_f.loc[
        [i for i in pylcaio_object.A_io_f.index if i[1] == 'sector1'],
        [pylcaio_object.A_io_f.columns[1], pylcaio_object.A_io_f.
         columns[3]]].sum().sum() == 0

    # check that if uncorrected is different from corrected, then it is equal to zero (with STAM)
    assert pylcaio_object.A_io_f.loc[[
        i for i in pylcaio_object.A_io_f_uncorrected.index
        if pylcaio_object.A_io_f_uncorrected.loc[
            i, pylcaio_object.A_io_f_uncorrected.columns[0]] != pylcaio_object.
        A_io_f.loc[i, pylcaio_object.A_io_f_uncorrected.columns[0]]
    ]].sum().sum() == 0
示例#18
0
 def test_match_foreground_background_flowlosses(self):
     a = pylcaio.LCAIO(verbose=False)
     a.extract_foreground(self.matdict)
     a.extract_background(self.smalldict)
     with self.assertRaises(ValueError):
         a.match_foreground_to_background()
示例#19
0
    def test_append_to_foreground_with_final_demand(self):
        a = pylcaio.LCAIO(1, verbose=False)
        b = pylcaio.LCAIO(1, verbose=False)
        a.extract_background(self.matdict)
        b.extract_background(self.matdict)

        B = {}
        B['PRO_f'] = np.array([['foo', 10, 'kg']], dtype=object)

        B['A_bf'] = scipy.sparse.csc_matrix([[1.0], [0.0], [0.0], [0.0]])

        B['PRO_header'] = np.array([['FULL NAME', 'MATRIXID', 'UNIT']])

        B['A_ff'] = scipy.sparse.csc_matrix([[11]])

        # WITH THREE STRESSORS

        B['STR'] = np.array(
            [['stress01', 1614, 'kg'], ['stress02', 1615, 'kg'],
             ['stress03', 1616, 'kg']],
            dtype=object)

        B['F_f'] = scipy.sparse.csc_matrix([[0.0], [0.2], [0.0]])

        B['y_f'] = scipy.sparse.csc_matrix([[2.0]])

        a.extract_foreground(self.matdict)
        b.extract_foreground(B)

        a.append_to_foreground(b, final_demand=True)

        A_ff = pd.DataFrame({
            10005: {
                10005: 0.0,
                10002: 10.0,
                10: 0.0
            },
            10: {
                10005: 0.0,
                10002: 0.0,
                10: 11.0
            },
            10002: {
                10005: 1.0,
                10002: 11.0,
                10: 0.0
            }
        })
        assert_frames_equivalent(a.A_ff, A_ff)

        A_bf = pd.DataFrame({
            10005: {
                1: 0.0,
                2: 1.0,
                3: 1.0,
                4: 0.0
            },
            10002: {
                1: 1.0,
                2: 0.0,
                3: 0.0,
                4: 0.0
            },
            10: {
                1: 1.0,
                2: 0.0,
                3: 0.0,
                4: 0.0
            }
        })

        assert_frames_equivalent(a.A_bf, A_bf)

        F_f = pd.DataFrame({
            10005: {
                1616: 0.0,
                1614: 0.29999999999999999,
                1615: 0.10000000000000001
            },
            10002: {
                1616: 0.0,
                1614: 0.0,
                1615: 0.20000000000000001
            },
            10: {
                1616: 0.0,
                1614: 0.0,
                1615: 0.20000000000000001
            }
        })

        assert_frames_equivalent(a.F_f, F_f)

        y_f = pd.DataFrame({0: {10005: 1.0, 10002: 0.0, 10: 2.0}})

        assert_frames_equivalent(a.y_f, y_f)

        # check that order is right
        assert (np.all(
            a.PRO_f.ix[:, a._arda_default_labels] == a.A_ff.index.values))