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
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,)
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])
def test_geomapping_array_includes_only_processes(): database = DatabaseChooser("a database") database.write( { ("a database", "foo"): { "exchanges": [], "type": "process", "location": "bar", }, ("a database", "baz"): {"exchanges": [], "type": "emission"}, } ) package = load_datapackage(ZipFS(database.filepath_processed())) array = package.get_resource("a_database_inventory_geomapping_matrix.indices")[0] assert array.shape == (1,) assert array[0]["col"] == geomapping["bar"]
def test_singlefile_processed_array_order(): database = DatabaseChooser("testy", "singlefile") data = { ("testy", "C"): {}, ("testy", "A"): {'type': 'biosphere'}, ("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: mapping[("testy", k)] for k in "ABC"} expected = sorted([ (lookup['A'], lookup['B'], 1), (lookup['A'], lookup['B'], 2), (lookup['B'], lookup['B'], 1), (lookup['B'], lookup['B'], 4), (lookup['C'], lookup['C'], 1), (lookup['C'], lookup['B'], 2), (lookup['C'], lookup['B'], 3), ]) array = np.load(database.filepath_processed()) assert array.shape == (7,) result = [(array['input'][x], array['output'][x], array['amount'][x]) for x in range(7)] assert expected == result
def test_no_distributions_if_no_uncertainty(): database = DatabaseChooser("a database") database.write( { ("a database", "2"): { "type": "process", "exchanges": [ { "input": ("a database", "2"), "amount": 42.0, "type": "technosphere", } ], } } ) package = load_datapackage(ZipFS(database.filepath_processed())) print(package.resources) with pytest.raises(KeyError): package.get_resource("a_database_technosphere_matrix.distributions")
def test_can_split_processes_products(): database = DatabaseChooser("a database") database.write( { ("a database", "product"): {"type": "product"}, ("a database", "foo"): { "exchanges": [ { "input": ("a database", "product"), "output": ("a database", "product"), "type": "production", "amount": 1, } ], "type": "process", }, } ) package = load_datapackage(ZipFS(database.filepath_processed())) array = package.get_resource("a_database_technosphere_matrix.indices")[0] assert array.shape == (1,) assert array["col"][0] == get_id(("a database", "foo")) assert array["row"][0] == get_id(("a database", "product"))