def test_basic(self):
     test_data = {
         ("t", "1"): {
             'exchanges': [{
                 'amount': 0.5,
                 'input': ('t', "2"),
                 'type': 'technosphere',
                 'uncertainty type': 0
             }, {
                 'amount': 1,
                 'input': ('biosphere', "1"),
                 'type': 'biosphere',
                 'uncertainty type': 0
             }],
             'type':
             'process',
             'unit':
             'kg'
         },
         ("t", "2"): {
             'exchanges': [],
             'type': 'process',
             'unit': 'kg'
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     lca = LCA({("t", "1"): 1})
     lca.lci()
     answer = np.zeros((2, ))
     answer[lca.activity_dict[("t", "1")]] = 1
     answer[lca.activity_dict[("t", "2")]] = 0.5
     self.assertTrue(np.allclose(answer, lca.supply_array))
Beispiel #2
0
def test_warning_empty_biosphere():
    test_data = {
        ("t", "1"): {
            "exchanges": [{
                "amount": 0.5,
                "input": ("t", "2"),
                "type": "technosphere",
                "uncertainty type": 0,
            }],
            "type":
            "process",
            "unit":
            "kg",
        },
        ("t", "2"): {
            "exchanges": [],
            "type": "process",
            "unit": "kg"
        },
    }
    test_db = Database("t")
    test_db.write(test_data)
    lca = LCA({("t", "1"): 1})
    with pytest.warns(UserWarning):
        lca.lci()
Beispiel #3
0
 def test_nonsquare_technosphere_error(self):
     test_data = {
         ("t", "p1"): {
             "type": "product"
         },
         ("t", "a1"): {
             "exchanges": [
                 {
                     "amount": 1,
                     "input": ("t", "p1"),
                     "type": "production",
                 },
                 {
                     "amount": 1,
                     "input": ("t", "a1"),
                     "type": "production",
                 },
             ]
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.write(test_data)
     lca = LCA({("t", "a1"): 1})
     with self.assertRaises(NonsquareTechnosphere):
         lca.lci()
 def test_process_product_split(self):
     test_data = {
         ("t", "p1"): {
             'type': 'product'
         },
         ("t", "a1"): {
             'exchanges': [{
                 'amount': 1,
                 'input': ('t', "p1"),
                 'type': 'production',
             }, {
                 'amount': 1,
                 'input': ('t', "a1"),
                 'type': 'production',
             }]
         },
         ("t", "a2"): {
             'exchanges': [{
                 'amount': 1,
                 'input': ('t', "p1"),
                 'type': 'production',
             }]
         }
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     lca = LCA({("t", "a1"): 1})
     lca.lci()
     answer = np.zeros((2, ))
     answer[lca.activity_dict[("t", "a1")]] = 1
     answer[lca.activity_dict[("t", "a2")]] = -1
     self.assertTrue(np.allclose(answer, lca.supply_array))
Beispiel #5
0
 def test_activity_as_fu_raises_error(self):
     test_data = {
         ("t", "p1"): {
             "type": "product"
         },
         ("t", "a1"): {
             "exchanges": [
                 {
                     "amount": 1,
                     "input": ("t", "p1"),
                     "type": "production",
                 },
                 {
                     "amount": 1,
                     "input": ("t", "a1"),
                     "type": "production",
                 },
             ]
         },
         ("t", "a2"): {
             "exchanges": [{
                 "amount": 1,
                 "input": ("t", "p1"),
                 "type": "production",
             }]
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.write(test_data)
     with self.assertRaises(ValueError):
         lca = LCA({("t", "a2"): 1})
         lca.lci()
 def test_activity_as_fu_raises_error(self):
     test_data = {
         ("t", "p1"): {
             'type': 'product'
         },
         ("t", "a1"): {
             'exchanges': [{
                 'amount': 1,
                 'input': ('t', "p1"),
                 'type': 'production',
             }, {
                 'amount': 1,
                 'input': ('t', "a1"),
                 'type': 'production',
             }]
         },
         ("t", "a2"): {
             'exchanges': [{
                 'amount': 1,
                 'input': ('t', "p1"),
                 'type': 'production',
             }]
         }
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     with self.assertRaises(ValueError):
         lca = LCA({("t", "a2"): 1})
         lca.lci()
def test_warning_empty_biosphere():
    test_data = {
        ("t", "1"): {
            'exchanges': [{
                'amount': 0.5,
                'input': ('t', "2"),
                'type': 'technosphere',
                'uncertainty type': 0
            }],
            'type':
            'process',
            'unit':
            'kg'
        },
        ("t", "2"): {
            'exchanges': [],
            'type': 'process',
            'unit': 'kg'
        },
    }
    test_db = Database("t")
    test_db.write(test_data)
    lca = LCA({("t", "1"): 1})
    with pytest.warns(UserWarning):
        lca.lci()
 def test_demand_type(self):
     with self.assertRaises(ValueError):
         LCA(("foo", "1"))
     with self.assertRaises(ValueError):
         LCA("foo")
     with self.assertRaises(ValueError):
         LCA([{"foo": "1"}])
Beispiel #9
0
def test_warning_empty_biosphere():
    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)
    with pytest.warns(UserWarning):
        lca.lci()
Beispiel #10
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})
    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})
Beispiel #12
0
 def test_filepaths_empty(self):
     test_data = {
         ("t", "1"): {
             "exchanges": [{
                 "amount": 1,
                 "input": ("biosphere", "1"),
                 "type": "biosphere",
                 "uncertainty type": 0,
             }],
             "type":
             "process",
             "unit":
             "kg",
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.write(test_data)
     lca = LCA({("t", "1"): 1})
     self.assertEqual(
         sorted(
             Database(x).filepath_processed() for x in ("biosphere", "t")),
         sorted(lca.database_filepath),
     )
     self.assertTrue(lca.method_filepath is None)
     self.assertTrue(lca.normalization_filepath is None)
     self.assertTrue(lca.weighting_filepath is None)
Beispiel #13
0
def test_lca_has():
    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,
    )
    assert lca.has("technosphere")
    assert lca.has("characterization")
    assert not lca.has("foo")
 def test_filepaths_empty(self):
     test_data = {
         ("t", "1"): {
             'exchanges': [{
                 'amount': 1,
                 'input': ('biosphere', "1"),
                 'type': 'biosphere',
                 'uncertainty type': 0
             }],
             'type':
             'process',
             'unit':
             'kg'
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.write(test_data)
     lca = LCA({("t", "1"): 1})
     self.assertEqual(
         sorted(
             Database(x).filepath_processed() for x in ('biosphere', 't')),
         sorted(lca.database_filepath))
     self.assertTrue(lca.method_filepath is None)
     self.assertTrue(lca.normalization_filepath is None)
     self.assertTrue(lca.weighting_filepath is None)
Beispiel #15
0
 def test_circular_chains(self):
     test_data = {
         ("t", "1"): {
             "exchanges": [
                 {
                     "amount": 0.5,
                     "input": ("t", "2"),
                     "type": "technosphere",
                     "uncertainty type": 0,
                 },
                 {
                     "amount": 1,
                     "input": ("biosphere", "1"),
                     "type": "biosphere",
                     "uncertainty type": 0,
                 },
             ],
             "type":
             "process",
             "unit":
             "kg",
         },
         ("t", "2"): {
             "exchanges": [{
                 "amount": 0.1,
                 "input": ("t", "1"),
                 "type": "technosphere",
                 "uncertainty type": 0,
             }],
             "type":
             "process",
             "unit":
             "kg",
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     lca = LCA({("t", "1"): 1})
     lca.lci()
     answer = np.zeros((2, ))
     answer[lca.dicts.activity[("t", "1")]] = 20 / 19.0
     answer[lca.dicts.activity[("t", "2")]] = 10 / 19.0
     self.assertTrue(np.allclose(answer, lca.supply_array))
Beispiel #16
0
    def test_activity_product_dict(self):
        test_data = {
            ("t", "activity 1"): {
                "type": "process"
            },
            ("t", "activity 2"): {
                "exchanges": [{
                    "amount": 1,
                    "input": ("t", "activity 2"),
                    "type": "production",
                }]
            },
            ("t", "activity 3"): {
                "exchanges": [{
                    "amount": 1,
                    "input": ("t", "product 4"),
                    "type": "production",
                }]
            },
            ("t", "product 4"): {
                "type": "product"
            },
        }
        self.add_basic_biosphere()
        test_db = Database("t")
        test_db.write(test_data)
        lca = LCA({("t", "activity 1"): 1})
        lca.lci()
        self.assertEqual(
            [("t", "activity 1"), ("t", "activity 2"), ("t", "activity 3")],
            sorted(lca.dicts.activity),
        )
        self.assertEqual(
            [("t", "activity 1"), ("t", "activity 2"), ("t", "product 4")],
            sorted(lca.dicts.product),
        )

        self.assertEqual(
            [("t", "activity 1"), ("t", "activity 2"), ("t", "activity 3")],
            sorted(lca.dicts.activity.reversed.values()),
        )
        self.assertEqual(
            [("t", "activity 1"), ("t", "activity 2"), ("t", "product 4")],
            sorted(lca.dicts.product.reversed.values()),
        )
    def test_pass_object_as_demand(self):
        self.add_basic_biosphere()

        class Foo:
            pass

        obj = Foo()
        with self.assertRaises(ValueError):
            LCA(obj)
Beispiel #18
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")
Beispiel #19
0
 def test_substitution(self):
     # bw2data version 1.0 compatibility
     if "substitution" not in TYPE_DICTIONARY:
         return
     test_data = {
         ("t", "1"): {
             "exchanges": [
                 {
                     "amount": 1,
                     "input": ("t", "2"),
                     "type": "substitution",
                     "uncertainty type": 0,
                 },
                 {
                     "amount": 1,
                     "input": ("biosphere", "1"),
                     "type": "biosphere",
                     "uncertainty type": 0,
                 },
             ],
             "type":
             "process",
             "unit":
             "kg",
         },
         ("t", "2"): {
             "exchanges": [],
             "type": "process",
             "unit": "kg"
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     lca = LCA({("t", "1"): 1})
     lca.lci()
     answer = np.zeros((2, ))
     answer[lca.dicts.activity[("t", "1")]] = 1
     answer[lca.dicts.activity[("t", "2")]] = -1
     self.assertTrue(np.allclose(answer, lca.supply_array))
 def test_multiple_lci_calculations(self):
     test_data = {
         ("t", "1"): {
             'exchanges': [{
                 'amount': 1,
                 'input': ('biosphere', "1"),
                 'type': 'biosphere',
                 'uncertainty type': 0
             }],
             'type':
             'process',
             'unit':
             'kg'
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.write(test_data)
     lca = LCA({test_db.random(): 1})
     lca.lci()
     lca.lci()
Beispiel #21
0
 def test_multiple_lci_calculations(self):
     test_data = {
         ("t", "1"): {
             "exchanges": [{
                 "amount": 1,
                 "input": ("biosphere", "1"),
                 "type": "biosphere",
                 "uncertainty type": 0,
             }],
             "type":
             "process",
             "unit":
             "kg",
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.write(test_data)
     lca = LCA({test_db.random(): 1})
     lca.lci()
     lca.lci()
    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))
    def test_redo_lci_switches_demand(self):
        test_data = {
            ("t", "1"): {
                'exchanges': [{
                    'amount': 0.5,
                    'input': ('t', "2"),
                    'type': 'technosphere',
                    'uncertainty type': 0
                }, {
                    'amount': 1,
                    'input': ('biosphere', "1"),
                    'type': 'biosphere',
                    'uncertainty type': 0
                }],
                'type':
                'process',
                'unit':
                'kg'
            },
            ("t", "2"): {
                'exchanges': [],
                'type': 'process',
                'unit': 'kg'
            },
        }
        self.add_basic_biosphere()
        test_db = Database("t")
        test_db.write(test_data)

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

        lca.redo_lci({("t", "1"): 2})
        self.assertEqual(lca.demand, {("t", "1"): 2})
 def test_decomposed_uses_solver(self):
     test_data = {
         ("t", "1"): {
             'exchanges': [{
                 'amount': 0.5,
                 'input': ('t', "2"),
                 'type': 'technosphere',
                 'uncertainty type': 0
             }, {
                 'amount': 1,
                 'input': ('biosphere', "1"),
                 'type': 'biosphere',
                 'uncertainty type': 0
             }],
             'type':
             'process',
             'unit':
             'kg'
         },
         ("t", "2"): {
             'exchanges': [],
             'type': 'process',
             'unit': 'kg'
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     lca = LCA({("t", "1"): 1})
     lca.lci(factorize=True)
     # Indirect test because no easy way to test a function is called
     lca.technosphere_matrix = None
     self.assertEqual(float(lca.solve_linear_system().sum()), 1.5)
Beispiel #25
0
 def test_only_products(self):
     test_data = {
         ("t", "p1"): {
             "type": "product"
         },
         ("t", "p2"): {
             "type": "product"
         },
         ("t", "a1"): {
             "exchanges": [
                 {
                     "amount": 1,
                     "input": ("t", "p1"),
                     "type": "production",
                 },
                 {
                     "amount": 1,
                     "input": ("t", "p2"),
                     "type": "production",
                 },
             ]
         },
         ("t", "a2"): {
             "exchanges": [{
                 "amount": 1,
                 "input": ("t", "p2"),
                 "type": "production",
             }]
         },
     }
     self.add_basic_biosphere()
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     lca = LCA({("t", "p1"): 1})
     lca.lci()
     answer = np.zeros((2, ))
     answer[lca.dicts.activity[("t", "a1")]] = 1
     answer[lca.dicts.activity[("t", "a2")]] = -1
     self.assertTrue(np.allclose(answer, lca.supply_array))
 def test_redo_lci_with_no_new_demand_no_error(self):
     self.add_basic_biosphere()
     test_data = {
         ("t", "1"): {
             'exchanges': [{
                 'amount': 1,
                 'input': ('biosphere', "1"),
                 'type': 'biosphere'
             }]
         }
     }
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     more_test_data = {
         ("z", "1"): {
             'exchanges': [{
                 'amount': 1,
                 'input': ('t', "1"),
                 'type': 'technosphere'
             }]
         }
     }
     more_test_db = Database("z")
     more_test_db.register()
     more_test_db.write(more_test_data)
     lca = LCA({("t", "1"): 1})
     lca.lci()
     lca.redo_lci()
Beispiel #27
0
 def test_redo_lci_with_no_new_demand_no_error(self):
     self.add_basic_biosphere()
     test_data = {
         ("t", "1"): {
             "exchanges": [{
                 "amount": 1,
                 "input": ("biosphere", "1"),
                 "type": "biosphere"
             }]
         }
     }
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     more_test_data = {
         ("z", "1"): {
             "exchanges": [{
                 "amount": 1,
                 "input": ("t", "1"),
                 "type": "technosphere"
             }]
         }
     }
     more_test_db = Database("z")
     more_test_db.register()
     more_test_db.write(more_test_data)
     lca = LCA({("t", "1"): 1})
     lca.lci()
     lca.redo_lci()
Beispiel #28
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))
 def test_redo_lci_fails_if_activity_outside_technosphere(self):
     self.add_basic_biosphere()
     test_data = {
         ("t", "1"): {
             'exchanges': [{
                 'amount': 1,
                 'input': ('biosphere', "1"),
                 'type': 'biosphere'
             }]
         }
     }
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     more_test_data = {
         ("z", "1"): {
             'exchanges': [{
                 'amount': 1,
                 'input': ('t', "1"),
                 'type': 'technosphere'
             }]
         }
     }
     more_test_db = Database("z")
     more_test_db.register()
     more_test_db.write(more_test_data)
     lca = LCA({("t", "1"): 1})
     lca.lci()
     with self.assertRaises(OutsideTechnosphere):
         lca.redo_lci({("z", "1"): 1})
Beispiel #30
0
 def test_redo_lci_fails_if_activity_outside_technosphere(self):
     self.add_basic_biosphere()
     test_data = {
         ("t", "1"): {
             "exchanges": [{
                 "amount": 1,
                 "input": ("biosphere", "1"),
                 "type": "biosphere"
             }]
         }
     }
     test_db = Database("t")
     test_db.register()
     test_db.write(test_data)
     more_test_data = {
         ("z", "1"): {
             "exchanges": [{
                 "amount": 1,
                 "input": ("t", "1"),
                 "type": "technosphere"
             }]
         }
     }
     more_test_db = Database("z")
     more_test_db.register()
     more_test_db.write(more_test_data)
     lca = LCA({("t", "1"): 1})
     lca.lci()
     with self.assertRaises(OutsideTechnosphere):
         lca.redo_lci({("z", "1"): 1})