Ejemplo n.º 1
0
    def test_basic_lcia(self):
        test_data = {
            ("t", "1"): {
                'exchanges': [{
                    'amount': 1,
                    'input': ('t', "2"),
                    'type': 'technosphere',
                }],
            },
            ("t", "2"): {
                'exchanges': [{
                    'amount': 1,
                    'input': ('biosphere', "1"),
                    'type': 'biosphere',
                }],
            },
        }
        method_data = [(('biosphere', "1"), 42)]
        self.add_basic_biosphere()
        test_db = Database("t")
        test_db.register()
        test_db.write(test_data)

        method = Method(("a method", ))
        method.register()
        method.write(method_data)

        lca = LCA({("t", "1"): 1}, ("a method", ))
        lca.lci()
        lca.lcia()

        self.assertTrue(np.allclose(42, lca.score))
Ejemplo n.º 2
0
    def test_redo_lcia_switches_demand(self):
        test_data = {
            ("t", "1"): {
                'exchanges': [{
                    'amount': 1,
                    'input': ('t', "2"),
                    'type': 'technosphere',
                }],
            },
            ("t", "2"): {
                'exchanges': [{
                    'amount': 1,
                    'input': ('biosphere', "1"),
                    'type': 'biosphere',
                }],
            },
        }
        method_data = [(('biosphere', "1"), 42)]
        self.add_basic_biosphere()
        test_db = Database("t")
        test_db.register()
        test_db.write(test_data)

        method = Method(("a method", ))
        method.register()
        method.write(method_data)

        lca = LCA({("t", "1"): 1}, ("a method", ))
        lca.lci()
        lca.lcia()
        self.assertEqual(lca.demand, {("t", "1"): 1})

        lca.redo_lcia({("t", "2"): 2})
        self.assertEqual(lca.demand, {("t", "2"): 2})
Ejemplo n.º 3
0
    def test_redo_lcia_switches_demand(self):
        test_data = {
            ("t", "1"): {
                "exchanges": [{
                    "amount": 1,
                    "input": ("t", "2"),
                    "type": "technosphere",
                }],
            },
            ("t", "2"): {
                "exchanges": [{
                    "amount": 1,
                    "input": ("biosphere", "1"),
                    "type": "biosphere",
                }],
            },
        }
        method_data = [(("biosphere", "1"), 42)]
        self.add_basic_biosphere()
        test_db = Database("t")
        test_db.register()
        test_db.write(test_data)

        method = Method(("a method", ))
        method.register()
        method.write(method_data)

        lca = LCA({("t", "1"): 1}, ("a method", ))
        lca.lci()
        lca.lcia()
        self.assertEqual(lca.demand, {("t", "1"): 1})

        lca.redo_lcia({("t", "2"): 2})
        self.assertEqual(lca.demand, {("t", "2"): 2})
Ejemplo n.º 4
0
    def test_basic_lcia(self):
        test_data = {
            ("t", "1"): {
                "exchanges": [{
                    "amount": 1,
                    "input": ("t", "2"),
                    "type": "technosphere",
                }],
            },
            ("t", "2"): {
                "exchanges": [{
                    "amount": 1,
                    "input": ("biosphere", "1"),
                    "type": "biosphere",
                }],
            },
        }
        method_data = [(("biosphere", "1"), 42)]
        self.add_basic_biosphere()
        test_db = Database("t")
        test_db.register()
        test_db.write(test_data)

        method = Method(("a method", ))
        method.register()
        method.write(method_data)

        lca = LCA({("t", "1"): 1}, ("a method", ))
        lca.lci()
        lca.lcia()

        self.assertTrue(np.allclose(42, lca.score))
Ejemplo n.º 5
0
def test_monte_carlo_as_iterator():
    packages = [
        fixture_dir / "mc_basic.zip",
    ]
    mc = LCA({3: 1}, data_objs=packages, use_distributions=True)
    mc.lci()
    mc.lcia()
    for _, _ in zip(mc, range(10)):
        assert mc.score > 0
Ejemplo n.º 6
0
def test_plain_monte_carlo():
    packages = [
        fixture_dir / "mc_basic.zip",
    ]
    mc = LCA({3: 1}, data_objs=packages, use_distributions=True)
    mc.lci()
    mc.lcia()
    first = mc.score
    next(mc)
    assert first != mc.score
Ejemplo n.º 7
0
def test_empty_biosphere_lcia():
    fd = fixture_dir / "empty_biosphere"
    key = load_mapping(fd / "mapping.json")[("t", "1")]

    packages = [
        fd / "biosphere.zip",
        fd / "test_db.zip",
        fd / "method.zip",
    ]

    lca = LCA({key: 1}, data_objs=packages)
    lca.lci()
    with pytest.raises(EmptyBiosphere):
        lca.lcia()
Ejemplo n.º 8
0
def test_lca_has():
    mapping = dict(
        json.load(open(fixture_dir / "bw2io_example_db_mapping.json")))
    packages = [
        fixture_dir / "bw2io_example_db.zip",
        fixture_dir / "ipcc_simple.zip",
    ]

    lca = LCA(
        {mapping["Driving an electric car"]: 1},
        data_objs=packages,
    )
    lca.lci()
    lca.lcia()
    assert lca.has("technosphere")
    assert lca.has("characterization")
    assert not lca.has("foo")
Ejemplo n.º 9
0
def test_example_db_basic():
    mapping = dict(
        json.load(open(fixture_dir / "bw2io_example_db_mapping.json")))
    print(mapping)
    packages = [
        fixture_dir / "bw2io_example_db.zip",
        fixture_dir / "ipcc_simple.zip",
    ]

    lca = LCA(
        {mapping["Driving an electric car"]: 1},
        data_objs=packages,
    )
    lca.lci()
    lca.lcia()
    assert lca.supply_array.sum()
    assert lca.technosphere_matrix.sum()
    assert lca.score
Ejemplo n.º 10
0
def test_example_db_basic():
    fd = fixture_dir / "example_db"
    mapping = load_mapping(fd / "mapping.json")

    packages = [
        fd / "example_db.zip",
        fd / "ipcc.zip",
    ]

    lca = LCA(
        {mapping[("Mobility example", "Driving an electric car")]: 1},
        data_objs=packages,
    )
    lca.lci()
    lca.lcia()
    assert lca.supply_array.sum()
    assert lca.technosphere_matrix.sum()
    assert lca.score
Ejemplo n.º 11
0
def test_monte_carlo_all_matrices_change():
    packages = [
        fixture_dir / "mc_basic.zip",
    ]
    mc = LCA({3: 1}, data_objs=packages, use_distributions=True)
    mc.lci()
    mc.lcia()
    a = [
        mc.technosphere_matrix.sum(),
        mc.biosphere_matrix.sum(),
        mc.characterization_matrix.sum()
    ]
    next(mc)
    b = [
        mc.technosphere_matrix.sum(),
        mc.biosphere_matrix.sum(),
        mc.characterization_matrix.sum()
    ]
    print(a, b)
    for x, y in zip(a, b):
        assert x != y
Ejemplo n.º 12
0
    def test_lcia_regionalized_ignored(self):
        test_data = {
            ("t", "1"): {
                'exchanges': [{
                    'amount': 1,
                    'input': ('t', "2"),
                    'type': 'technosphere',
                }],
            },
            ("t", "2"): {
                'exchanges': [{
                    'amount': 1,
                    'input': ('biosphere', "1"),
                    'type': 'biosphere',
                }],
            },
        }
        method_data = [
            (('biosphere', "1"), 21),
            (('biosphere', "1"), 21, config.global_location),
            (('biosphere', "1"), 100, "somewhere else"),
        ]
        self.add_basic_biosphere()
        test_db = Database("t")
        test_db.register()
        test_db.write(test_data)

        method = Method(("a method", ))
        method.register()
        method.write(method_data)

        lca = LCA({("t", "1"): 1}, ("a method", ))
        lca.lci()
        lca.lcia()

        print(lca.score)
        self.assertTrue(np.allclose(42, lca.score))
Ejemplo n.º 13
0
def test_empty_biosphere_lcia():
    biosphere = Database("biosphere")
    biosphere.register()
    biosphere.write({
        ("biosphere", "1"): {
            'categories': ['things'],
            'exchanges': [],
            'name': 'an emission',
            'type': 'emission',
            'unit': 'kg'
        }
    })

    test_data = {
        ("t", "1"): {
            'exchanges': [{
                'amount': 1,
                'input': ('t', "2"),
                'type': 'technosphere',
            }],
        },
        ("t", "2"): {
            'exchanges': []
        },
    }
    test_db = Database("t")
    test_db.register()
    test_db.write(test_data)

    method = Method(("a method", ))
    method.register()
    method.write([(('biosphere', "1"), 42)])

    lca = LCA({("t", "1"): 1}, ("a method", ))
    lca.lci()
    with pytest.raises(EmptyBiosphere):
        lca.lcia()
Ejemplo n.º 14
0
def test_empty_biosphere_lcia():
    biosphere = Database("biosphere")
    biosphere.register()
    biosphere.write({
        ("biosphere", "1"): {
            "categories": ["things"],
            "exchanges": [],
            "name": "an emission",
            "type": "emission",
            "unit": "kg",
        }
    })

    test_data = {
        ("t", "1"): {
            "exchanges": [{
                "amount": 1,
                "input": ("t", "2"),
                "type": "technosphere",
            }],
        },
        ("t", "2"): {
            "exchanges": []
        },
    }
    test_db = Database("t")
    test_db.register()
    test_db.write(test_data)

    method = Method(("a method", ))
    method.register()
    method.write([(("biosphere", "1"), 42)])

    lca = LCA({("t", "1"): 1}, ("a method", ))
    lca.lci()
    with pytest.raises(EmptyBiosphere):
        lca.lcia()
Ejemplo n.º 15
0
    def test_lcia_regionalized_ignored(self):
        test_data = {
            ("t", "1"): {
                "exchanges": [{
                    "amount": 1,
                    "input": ("t", "2"),
                    "type": "technosphere",
                }],
            },
            ("t", "2"): {
                "exchanges": [{
                    "amount": 1,
                    "input": ("biosphere", "1"),
                    "type": "biosphere",
                }],
            },
        }
        method_data = [
            (("biosphere", "1"), 21),
            (("biosphere", "1"), 21, config.global_location),
            (("biosphere", "1"), 100, "somewhere else"),
        ]
        self.add_basic_biosphere()
        test_db = Database("t")
        test_db.register()
        test_db.write(test_data)

        method = Method(("a method", ))
        method.register()
        method.write(method_data)

        lca = LCA({("t", "1"): 1}, ("a method", ))
        lca.lci()
        lca.lcia()

        self.assertTrue(np.allclose(42, lca.score))
Ejemplo n.º 16
0
def test_empty_biosphere_lcia():
    lca = LCA({1: 1}, data_objs=[fixture_dir / "empty_biosphere.zip"])
    lca.lci()
    assert lca.biosphere_matrix.shape[0] == 0
    with pytest.raises(EmptyBiosphere):
        lca.lcia()