def test_convert_backend(self):
     self.maxDiff = None
     database = DatabaseChooser("a database")
     database.write({
         ("a database", "foo"): {
             'exchanges': [{
                 'input': ("a database", "foo"),
                 'amount': 1,
                 'type': 'production',
             }],
             'location': 'bar',
             'name': 'baz'
         },
     })
     database = convert_backend('a database', "singlefile")
     self.assertEqual(databases['a database']['backend'], 'singlefile')
     self.assertEqual(databases['a database']['version'], 1)
     expected = {
         ("a database", "foo"): {
             'code': 'foo',
             'database': 'a database',
             'exchanges': [{
                 'input': ("a database", "foo"),
                 'output': ("a database", "foo"),
                 'amount': 1,
                 'type': 'production',
             }],
             'location': 'bar',
             'name': 'baz'
         },
     }
     self.assertEqual(database.load(), expected)
Пример #2
0
def test_processed_array():
    database = DatabaseChooser("a database")
    database.write(
        {
            ("a database", "2"): {
                "type": "process",
                "exchanges": [
                    {
                        "input": ("a database", "2"),
                        "amount": 42,
                        "uncertainty_type": 7,
                        "type": "production",
                    }
                ],
            }
        }
    )
    package = load_datapackage(ZipFS(database.filepath_processed()))
    print(package.resources)
    array = package.get_resource("a_database_technosphere_matrix.data")[0]

    assert array.shape == (1,)
    assert array[0] == 42

    array = package.get_resource("a_database_technosphere_matrix.distributions")[0]
    assert array.shape == (1,)
    assert array[0]["uncertainty_type"] == 7
Пример #3
0
def test_uncertainty():
    database = DatabaseChooser("db")
    database.write({
        ("db", "a"): {
            'exchanges': [{
                'input': ("db", "a"),
                'amount': 2,
                'type': 'production',
                'uncertainty type': 1,
                'loc': 2,
                'scale': 3,
                'shape': 4,
                'maximum': 5,
                'negative': True,
            'name': 'a'
        }]}
    })
    expected = {
        'uncertainty type': 1,
        'loc': 2,
        'scale': 3,
        'shape': 4,
        'maximum': 5,
        'negative': True,
    }
    exchange = list(database.get("a").exchanges())[0]
    assert exchange.uncertainty == expected
Пример #4
0
def test_uncertainty():
    database = DatabaseChooser("db")
    database.write({
        ("db", "a"): {
            "exchanges": [{
                "input": ("db", "a"),
                "amount": 2,
                "type": "production",
                "uncertainty type": 1,
                "loc": 2,
                "scale": 3,
                "shape": 4,
                "maximum": 5,
                "negative": True,
                "name": "a",
            }]
        }
    })
    expected = {
        "uncertainty type": 1,
        "loc": 2,
        "scale": 3,
        "shape": 4,
        "maximum": 5,
        "negative": True,
    }
    exchange = list(database.get("a").exchanges())[0]
    assert exchange.uncertainty == expected
Пример #5
0
def test_untyped_exchange_error():
    database = DatabaseChooser("testy")
    database_data = {
        ("testy", "A"): {"exchanges": [{"amount": 1, "input": ("testy", "A")}]},
    }
    with pytest.raises(UntypedExchange):
        database.write(database_data, process=False)
Пример #6
0
def test_no_input_raises_invalid_exchange():
    database = DatabaseChooser("testy")
    database_data = {
        ("testy", "A"): {"exchanges": [{"amount": 1}]},
    }
    with pytest.raises(InvalidExchange):
        database.write(database_data, process=False)
Пример #7
0
def test_exchange_save():
    database = DatabaseChooser("testy")
    data = {
        ("testy", "A"): {},
        ("testy", "C"): {"type": "biosphere"},
        ("testy", "B"): {
            "exchanges": [
                {"input": ("testy", "A"), "amount": 1, "type": "technosphere"},
                {"input": ("testy", "B"), "amount": 1, "type": "production"},
                {"input": ("testy", "C"), "amount": 1, "type": "biosphere"},
            ]
        },
    }
    then = datetime.datetime.now().isoformat()
    database.write(data)
    act = database.get("B")
    exc = [x for x in act.production()][0]
    exc["amount"] = 2
    exc.save()
    assert databases[database.name].get("dirty")
    assert database.metadata.get("dirty")
    assert database.metadata["modified"] > then

    exc = [x for x in act.production()][0]
    assert exc["amount"] == 2
Пример #8
0
def activity():
    database = DatabaseChooser("db")
    database.write({
        ("db", "a"): {
            'exchanges': [{
                'input': ("db", "a"),
                'amount': 2,
                'type': 'production',
            }, {
                'input': ("db", "b"),
                'amount': 3,
                'type': 'technosphere',
            }, {
                'input': ("db", "c"),
                'amount': 4,
                'type': 'biosphere',
            }],
            'name': 'a'
        },
        ("db", "b"): {'name': 'b'},
        ("db", "c"): {'name': 'c', 'type': 'biosphere'},
        ("db", "d"): {
            'name': 'd',
            'exchanges': [{
                'input': ("db", "a"),
                'amount': 5,
                'type': 'technosphere'
            }, {
                'input': ("db", "b"),
                'amount': -0.1,
                'type': 'substitution'
            }]
        },
    })
    return database.get("a")
Пример #9
0
def test_method_write_adds_num_cfs_to_metadata(reset):
    assert not len(databases)
    assert not len(methods)
    assert not AD.select().count()

    database = DatabaseChooser("testy")
    data = {
        ("testy", "A"): {},
        ("testy", "B"): {
            "exchanges": [
                {"input": ("testy", "A"), "amount": 1, "type": "technosphere"},
            ]
        },
    }
    database.write(data)

    method_data = [
        [("testy", "A"), 1],
        [("testy", "B"), 1],
    ]
    name = ("a", "method")
    method = Method(name)
    method.register()
    method.write(method_data)
    assert methods[name]["num_cfs"] == 2
Пример #10
0
def activity_and_method():
    database = DatabaseChooser("db")
    database.write({
        ("db", "a"): {
            'exchanges': [{
                'input': ("db", "a"),
                'amount': 2,
                'type': 'production',
            }, {
                'input': ("db", "b"),
                'amount': 3,
                'type': 'technosphere',
            }, {
                'input': ("db", "c"),
                'amount': 4,
                'type': 'biosphere',
            }],
            'name': 'a'
        },
        ("db", "b"): {'name': 'b'},
        ("db", "c"): {'name': 'c', 'type': 'biosphere'},
        ("db", "d"): {
            'name': 'd',
            'exchanges': [{
                'input': ("db", "a"),
                'amount': 5,
                'type': 'technosphere'
            }]
        },
    })
    cfs = [(("db", "c"), 42)]
    method = Method(("a method",))
    method.register()
    method.write(cfs)
    return database.get("a"), method
Пример #11
0
 def test_dirty_activities(self):
     database = DatabaseChooser("testy")
     data = {
         ("testy", "A"): {},
         ("testy", "C"): {'type': 'biosphere'},
         ("testy", "B"): {'exchanges': [
             {'input': ("testy", "A"),
              'amount': 1,
              'type': 'technosphere'},
             {'input': ("testy", "B"),
              'amount': 1,
              'type': 'production'},
             {'input': ("testy", "C"),
              'amount': 1,
              'type': 'biosphere'},
         ]},
     }
     database.write(data)
     act = database.get("B")
     exc = [x for x in act.production()][0]
     exc['amount'] = 2
     exc.save()
     self.assertTrue(databases['testy']['dirty'])
     lca = act.lca()
     self.assertFalse(databases['testy'].get('dirty'))
     self.assertEqual(
         lca.supply_array[lca.activity_dict[("testy", "A")]],
         0.5
     )
Пример #12
0
    def test_exchange_save(self):
        database = DatabaseChooser("testy")
        data = {
            ("testy", "A"): {},
            ("testy", "C"): {'type': 'biosphere'},
            ("testy", "B"): {'exchanges': [
                {'input': ("testy", "A"),
                 'amount': 1,
                 'type': 'technosphere'},
                {'input': ("testy", "B"),
                 'amount': 1,
                 'type': 'production'},
                {'input': ("testy", "C"),
                 'amount': 1,
                 'type': 'biosphere'},
            ]},
        }
        then = datetime.datetime.now().isoformat()
        database.write(data)
        act = database.get("B")
        exc = [x for x in act.production()][0]
        exc['amount'] = 2
        exc.save()
        self.assertTrue(databases[database.name].get("dirty"))
        self.assertTrue(database.metadata.get("dirty"))
        self.assertTrue(database.metadata['modified'] > then)

        exc = [x for x in act.production()][0]
        self.assertEqual(exc['amount'], 2)
Пример #13
0
def test_change_code_not_unique_raises_error():
    database = DatabaseChooser("a database")
    database.write(
        {
            ("a database", "foo"): {
                "exchanges": [
                    {"input": ("a database", "foo"), "amount": 1, "type": "production",}
                ],
                "location": "bar",
                "name": "baz",
            },
            ("a database", "already there"): {
                "exchanges": [
                    {
                        "input": ("a database", "already there"),
                        "amount": 1,
                        "type": "production",
                    }
                ],
                "location": "bar",
                "name": "baz",
            },
        }
    )
    act = database.get("foo")
    with pytest.raises(ValueError):
        act["code"] = "already there"
Пример #14
0
def test_change_code_not_unique_raises_error():
    database = DatabaseChooser("a database")
    database.write({
        ("a database", "foo"): {
            'exchanges': [{
                'input': ("a database", "foo"),
                'amount': 1,
                'type': 'production',
            }],
            'location':
            'bar',
            'name':
            'baz'
        },
        ("a database", "already there"): {
            'exchanges': [{
                'input': ("a database", "already there"),
                'amount': 1,
                'type': 'production',
            }],
            'location':
            'bar',
            'name':
            'baz'
        },
    })
    act = database.get('foo')
    with pytest.raises(ValueError):
        act['code'] = "already there"
Пример #15
0
def test_raise_wrong_database():
    data = {
        ("foo", '1'): {}
    }
    d = DatabaseChooser("bar")
    with pytest.raises(WrongDatabase):
        d.write(data)
Пример #16
0
def test_process_without_exchanges_still_in_processed_array():
    database = DatabaseChooser("a database")
    database.write({("a database", "foo"): {}})

    package = load_datapackage(ZipFS(database.filepath_processed()))
    array = package.get_resource("a_database_technosphere_matrix.data")[0]
    assert array[0] == 1
    assert array.shape == (1,)
Пример #17
0
def test_no_amount_raises_invalid_exchange():
    database = DatabaseChooser("testy")
    database_data = {
        ("testy", "A"): {
            "exchanges": [{"input": ("testy", "A"), "type": "technosphere"}]
        },
    }
    with pytest.raises(InvalidExchange):
        database.write(database_data, process=False)
Пример #18
0
def test_process_adds_to_mappings():
    database = DatabaseChooser("testy")
    database_data = {
        ("testy", "A"): {'location': 'CH'},
        ("testy", "B"): {'location': 'DE'},
    }
    database.write(database_data)
    assert ("testy", "A") in mapping and ("testy", "B") in mapping
    assert "CH" in geomapping and "DE" in geomapping
Пример #19
0
def test_zero_amount_is_valid_exchange():
    database = DatabaseChooser("testy")
    database_data = {
        ("testy", "A"): {
            "exchanges": [
                {"input": ("testy", "A"), "type": "technosphere", "amount": 0.0}
            ]
        },
    }
    database.write(database_data, process=False)
Пример #20
0
 def test_process_without_exchanges_still_in_processed_array(self):
     database = DatabaseChooser("a database")
     database.write({("a database", "foo"): {}})
     fp = os.path.join(
         projects.dir,
         "processed",
         database.filename + ".npy"
     )
     array = np.load(fp)
     self.assertEqual(array['amount'][0], 1)
     self.assertEqual(array.shape, (1,))
Пример #21
0
def test_process_checks_process_type():
    database = DatabaseChooser("a database")
    database.write(
        {
            ("a database", "foo"): {"exchanges": [], "type": "process"},
            ("a database", "bar"): {"type": "definitely not a process"},
        },
        process=True,
    )
    # This shouldn't raise an error
    assert database.process() is None
Пример #22
0
def test_deletes_from_database():
    d = DatabaseChooser("biosphere")
    d.write(biosphere)
    assert "biosphere" in databases
    del databases['biosphere']
    assert next(sqlite3_lci_db.execute_sql(
        "select count(*) from activitydataset where database = 'biosphere'"
    )) == (0,)
    assert next(sqlite3_lci_db.execute_sql(
        "select count(*) from exchangedataset where output_database = 'biosphere'"
    )) == (0,)
Пример #23
0
def test_method_processed_array(reset):
    database = DatabaseChooser("foo")
    database.write({("foo", "bar"): {}})

    method = Method(("a", "method"))
    method.write([[("foo", "bar"), 42]])
    package = load_datapackage(ZipFS(method.filepath_processed()))
    data = package.get_resource("a_method_matrix_data.data")[0]
    assert np.allclose(data, [42])

    indices = package.get_resource("a_method_matrix_data.indices")[0]
    assert np.allclose(indices["row"], get_id(("foo", "bar")))
    assert np.allclose(indices["col"], geomapping[config.global_location])
Пример #24
0
def test_process_unknown_object():
    database = DatabaseChooser("testy")
    data = {
        ("testy", "A"): {},
        ("testy", "B"): {
            "exchanges": [
                {"input": ("testy", "A"), "amount": 1, "type": "technosphere"},
                {"input": ("testy", "C"), "amount": 1, "type": "technosphere"},
            ]
        },
    }
    with pytest.raises(UnknownObject):
        database.write(data)
Пример #25
0
def test_process_invalid_exchange_value():
    database = DatabaseChooser("testy")
    data = {
        ("testy", "A"): {},
        ("testy", "B"): {
            "exchanges": [
                {"input": ("testy", "A"), "amount": np.nan, "type": "technosphere"},
                {"input": ("testy", "C"), "amount": 1, "type": "technosphere"},
            ]
        },
    }
    with pytest.raises(ValueError):
        database.write(data)
Пример #26
0
def test_sqlite_processed_array_order():
    database = DatabaseChooser("testy")
    data = {
        ("testy", "C"): {},
        ("testy", "A"): {},
        ("testy", "B"): {
            "exchanges": [
                {"input": ("testy", "A"), "amount": 1, "type": "technosphere"},
                {"input": ("testy", "A"), "amount": 2, "type": "technosphere"},
                {"input": ("testy", "C"), "amount": 2, "type": "biosphere"},
                {"input": ("testy", "C"), "amount": 3, "type": "biosphere"},
                {"input": ("testy", "B"), "amount": 4, "type": "production"},
                {"input": ("testy", "B"), "amount": 1, "type": "production"},
            ]
        },
    }
    database.write(data)
    lookup = {k: get_id(("testy", k)) for k in "ABC"}
    t = sorted(
        [
            (lookup["A"], lookup["B"], 1),
            (lookup["A"], lookup["B"], 2),
            # Implicit production
            (lookup["C"], lookup["C"], 1),
            (lookup["A"], lookup["A"], 1),
            # Explicit production
            (lookup["B"], lookup["B"], 4),
            (lookup["B"], lookup["B"], 1),
        ]
    )
    b = sorted([(lookup["C"], lookup["B"], 2), (lookup["C"], lookup["B"], 3),])

    package = load_datapackage(ZipFS(database.filepath_processed()))

    array = package.get_resource("testy_technosphere_matrix.data")[0]
    assert array.shape == (6,)
    assert np.allclose(array, [x[2] for x in t])

    array = package.get_resource("testy_technosphere_matrix.indices")[0]
    assert array.shape == (6,)
    assert np.allclose(array["row"], [x[0] for x in t])
    assert np.allclose(array["col"], [x[1] for x in t])

    array = package.get_resource("testy_biosphere_matrix.data")[0]
    assert array.shape == (2,)
    assert np.allclose(array, [x[2] for x in b])

    array = package.get_resource("testy_biosphere_matrix.indices")[0]
    assert array.shape == (2,)
    assert np.allclose(array["row"], [x[0] for x in b])
    assert np.allclose(array["col"], [x[1] for x in b])
Пример #27
0
def activity():
    database = DatabaseChooser("a database")
    database.write(
        {
            ("a database", "foo"): {
                "exchanges": [
                    {"input": ("a database", "foo"), "amount": 1, "type": "production",}
                ],
                "location": "bar",
                "name": "baz",
            },
        }
    )
    return database.get("foo")
Пример #28
0
def test_normalization_process_row(reset):
    database = DatabaseChooser("foo")
    database.write({("foo", "bar"): {}})

    norm = Normalization(("foo",))
    norm.write([[("foo", "bar"), 42]])
    package = load_datapackage(ZipFS(norm.filepath_processed()))

    data = package.get_resource("foo_matrix_data.data")[0]
    assert np.allclose(data, [42])

    indices = package.get_resource("foo_matrix_data.indices")[0]
    assert np.allclose(indices["row"], get_id(("foo", "bar")))
    assert np.allclose(indices["col"], get_id(("foo", "bar")))
Пример #29
0
def test_uncertainty_type_missing():
    database = DatabaseChooser("db")
    database.write({
        ("db", "a"): {
            'exchanges': [{
                'input': ("db", "a"),
                'amount': 2,
                'type': 'production',
            'name': 'a'
        }]}
    })
    exchange = list(database.get("a").exchanges())[0]
    assert exchange.uncertainty_type.id == 0
    assert exchange.uncertainty_type == sa.UndefinedUncertainty
 def test_convert_same_backend(self):
     database = DatabaseChooser("a database")
     database.write({
         ("a database", "foo"): {
             'exchanges': [{
                 'input': ("a database", "foo"),
                 'amount': 1,
                 'type': 'production',
             }],
             'location': 'bar',
             'name': 'baz'
         },
     })
     self.assertFalse(convert_backend('a database', "sqlite"))