def test_empty_biosphere(): resources = [ { "name": "a", "path": "a.npy", "matrix": "technosphere", "data": [ dictionary_formatter({"row": 3, "col": 5, "amount": 1.0}), dictionary_formatter({"row": 4, "col": 6, "amount": 1.0}), dictionary_formatter({"row": 3, "col": 6, "amount": 0.5, "flip": True}), ], }, { "name": "basic-characterization", "path": "b.npy", "matrix": "characterization", "data": [ dictionary_formatter({"row": 1, "amount": 10.0}), dictionary_formatter({"row": 2, "amount": 100.0}), ], }, ] fp = create_calculation_package( name="test-fixture-basic-matrices", resources=resources, path=None, compress=False ) lca = LCA({3: 1}, [fp]) with pytest.raises(NoArrays): lca.lci()
def test_basic_calculation_in_memory(): resources = [ { "name": "basic-technosphere", "path": "a.npy", "matrix": "technosphere", "data": [ dictionary_formatter({"row": 3, "col": 5, "amount": 1.0}), dictionary_formatter({"row": 4, "col": 6, "amount": 1.0}), dictionary_formatter({"row": 3, "col": 6, "amount": 0.5, "flip": True}), ], }, { "name": "basic-biosphere", "path": "b.npy", "matrix": "biosphere", "data": [ dictionary_formatter({"row": 1, "col": 5, "amount": 3.0}), dictionary_formatter({"row": 2, "col": 6, "amount": 2.0}), ], }, { "name": "basic-characterization", "path": "c.npy", "matrix": "characterization", "data": [ dictionary_formatter({"row": 1, "amount": 10.0}), dictionary_formatter({"row": 2, "amount": 100.0}), ], }, ] fp = create_calculation_package( name="test-fixture-basic-matrices", resources=resources, path=None, compress=False ) lca = LCA({3: 1}, [fp]) lca.lci() lca.lcia() assert lca.score == 30 lca.redo_lcia({4: 1}) assert lca.score == 200 + 30 / 2
def test_circular_chains(): resources = [ { "name": "a", "path": "a.npy", "matrix": "technosphere", "data": [ dictionary_formatter({"row": 3, "col": 5, "amount": 1.0}), dictionary_formatter({"row": 4, "col": 5, "amount": 0.1, "flip": True}), dictionary_formatter({"row": 4, "col": 6, "amount": 1.0}), dictionary_formatter({"row": 3, "col": 6, "amount": 0.5, "flip": True}), ], }, { "name": "basic-biosphere", "path": "b.npy", "matrix": "biosphere", "data": [ dictionary_formatter({"row": 1, "col": 5, "amount": 3.0}), dictionary_formatter({"row": 2, "col": 6, "amount": 2.0}), ], }, { "name": "basic-characterization", "path": "c.npy", "matrix": "characterization", "data": [ dictionary_formatter({"row": 1, "amount": 10.0}), dictionary_formatter({"row": 2, "amount": 100.0}), ], }, ] fp = create_calculation_package( name="test-fixture-basic-matrices", resources=resources, path=None, compress=False ) lca = LCA({4: 1}, [fp]) lca.lci() lca.lcia() assert np.allclose(lca.score, 226.31578965117728)
def test_substitution(): resources = [ { "name": "a", "path": "a.npy", "matrix": "technosphere", "data": [ dictionary_formatter({"row": 3, "col": 5, "amount": 1.0}), dictionary_formatter({"row": 4, "col": 6, "amount": 1.0}), # Substitution exchange dictionary_formatter({"row": 3, "col": 6, "amount": 0.5}), ], }, { "name": "basic-biosphere", "path": "b.npy", "matrix": "biosphere", "data": [ dictionary_formatter({"row": 1, "col": 5, "amount": 3.0}), dictionary_formatter({"row": 2, "col": 6, "amount": 2.0}), ], }, { "name": "basic-characterization", "path": "c.npy", "matrix": "characterization", "data": [ dictionary_formatter({"row": 1, "amount": 10.0}), dictionary_formatter({"row": 2, "amount": 100.0}), ], }, ] fp = create_calculation_package( name="test-fixture-basic-matrices", resources=resources, path=None, compress=False ) lca = LCA({4: 1}, [fp]) lca.lci() lca.lcia() assert lca.score == 200 - 30 / 2
def test_non_one_production_values(): resources = [ { "name": "a", "path": "a.npy", "matrix": "technosphere", "data": [ # Modified production exchange dictionary_formatter({"row": 3, "col": 5, "amount": 2.0}), dictionary_formatter({"row": 4, "col": 6, "amount": 1.0}), dictionary_formatter({"row": 3, "col": 6, "amount": 0.5, "flip": True}), ], }, { "name": "basic-biosphere", "path": "b.npy", "matrix": "biosphere", "data": [ dictionary_formatter({"row": 1, "col": 5, "amount": 3.0}), dictionary_formatter({"row": 2, "col": 6, "amount": 2.0}), ], }, { "name": "basic-characterization", "path": "c.npy", "matrix": "characterization", "data": [ dictionary_formatter({"row": 1, "amount": 10.0}), dictionary_formatter({"row": 2, "amount": 100.0}), ], }, ] fp = create_calculation_package( name="test-fixture-basic-matrices", resources=resources, path=None, compress=False ) lca = LCA({3: 1}, [fp]) lca.lci() lca.lcia() assert lca.score == 15
def import_exiobase(version="3.3.17 hybrid"): """Import Exiobase. Both migrations and this code are hard-coded against ecoinvent version 3.6.""" collection_name = f"Exiobase {version}" if Collection.select().where(Collection.name == collection_name).count(): print("EXIOBASE version already imported") return assert version in VERSIONS print("Getting row and column labels for all inputs") labels = get_labels_for_exiobase(version) data = { "exchanges": [], "characterization factors": [], "methods": [], "uncertainty types": [], "collections": [{ "id": 1, "name": collection_name }], "geocollections": [{ "id": 1, "name": "Exiobase {}".format(version[0]) }], } locations = sorted({row[0] for row in labels["HIOT"][1]}) location_mapping = {key: i for i, key in enumerate(locations)} data["locations"] = [{ "id": location_mapping[key] + 1, "name": key, "geocollection_id": 1 } for key in locations] flows = [tuple(row[:2]) for row in labels["HIOT"][0]] flow_mapping = {key: i for i, key in enumerate(flows)} data["flows"] = [{ "id": flow_mapping[tuple(row[:2])] + 1, "name": row[1], "unit": row[4], "kind": "product", "location_id": location_mapping[row[0]] + 1, "collection_id": 1, "code 1": row[2], "code 2": row[3], } for row in labels["HIOT"][0]] activities = [tuple(row[:2]) for row in labels["HIOT"][1]] activity_mapping = {key: i for i, key in enumerate(activities)} data["activities"] = [{ "id": activity_mapping[tuple(row[:2])] + 1, "name": row[1], "collection_id": 1, "location_id": location_mapping[row[0]] + 1, "reference_product_id": i + 1, "code 1": row[2], "code 2": row[3], } for i, row in enumerate(labels["HIOT"][1])] print("Writing activity and flow data") backend.create(data) location_mapping = {o["id"]: o["name"] for o in data["locations"]} for item in data["activities"]: item["location"] = location_mapping[item["location_id"]] for item in data["flows"]: item["location"] = location_mapping[item["location_id"]] # Make sure our scheme guarantees uniqueness assert len(data["flows"]) == len({(o["name"], o["location"]) for o in data["flows"]}) assert len(data["activities"]) == len({(o["name"], o["location"]) for o in data["activities"]}) # Ensure square matrix size = len(data["activities"]) assert size == len(data["flows"]) rows = {(o["name"], o["location"]): i for i, o in enumerate(data["flows"])} cols = {(o["name"], o["location"]): i for i, o in enumerate(data["activities"])} flow_row_to_index = { rows[(a.name, a.location.name)]: a.id for a in Flow.select().where(Flow.collection == Collection.get( name=f"Exiobase {version}")) } activity_col_to_index = { cols[(a.name, a.location.name)]: a.id for a in Activity.select().where(Activity.collection == Collection.get( name=collection_name)) } flows_to_ids = { (f.name, f.categories): f.id for f in Flow.select().where(Flow.collection == Collection.get( name="ecoinvent 3.6 biosphere")) } resources = [{ 'name': f"{collection_name} technosphere", 'data': fill_missing_production(version, flow_row_to_index, activity_col_to_index, size), 'path': f'{collection_name}.technosphere.npy', 'dtype': DTYPE, 'matrix': 'technosphere', }, { 'name': f"{collection_name} biosphere", 'data': itertools.chain( emissions_iterator(version, "Emiss_act", labels, flows_to_ids, activity_col_to_index), emissions_iterator(version, "resource_act", labels, flows_to_ids, activity_col_to_index), emissions_iterator(version, "Land_act", labels, flows_to_ids, activity_col_to_index), ), 'path': f'{collection_name}.biosphere.npy', 'dtype': DTYPE, 'matrix': 'biosphere', }] print("Writing matrices") fp = create_calculation_package( name=collection_name.replace(" ", "") + ".lci", resources=resources, path=config.processed_dir, metadata={ "importer": "bw_exiobase.import_exiobase", "licenses": [{ "name": "CC-BY-SA-4.0", "path": "https://creativecommons.org/licenses/by-sa/4.0/", "title": "Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) ", }] }, compress=True) CalculationPackage.create(filepath=fp, collection=Collection.get(name=collection_name))
def write_package(self, name, **kwargs): return create_calculation_package(name, self.resources, **kwargs)
def generate_basic_matrices(): directory = base_dir / "basic-calculation-package" directory.mkdir(mode=0o755, exist_ok=True) # Biosphere emissions: 1, 2 # Products: 3, 4 # Activities: 5, 6 resources = [{ 'name': 'basic-technosphere', 'matrix': 'technosphere', "path": "basic-technosphere.npy", 'data': [ dictionary_formatter({ 'row': 3, 'col': 5, 'amount': 1. }), dictionary_formatter({ 'row': 4, 'col': 6, 'amount': 1. }), dictionary_formatter({ 'row': 3, 'col': 6, 'amount': 0.5, "flip": True, 'uncertainty_type': 2, 'loc': 0, 'scale': 0.1 }), ], }, { 'name': 'basic-biosphere', 'path': 'basic-biosphere.npy', 'matrix': 'biosphere', 'data': [ dictionary_formatter({ 'row': 1, 'col': 5, 'amount': 3., 'uncertainty_type': 5, 'loc': 3., 'minimum': 1., 'maximum': 10 }), dictionary_formatter({ 'row': 2, 'col': 6, 'amount': 2., 'uncertainty_type': 3, 'loc': 2., 'scale': 0.25, 'minimum': 0 }), ], }, { 'name': 'basic-characterization', 'path': 'basic-characterization.npy', 'matrix': 'characterization', 'data': [ dictionary_formatter({ 'row': 1, 'amount': 10., 'uncertainty_type': 7, 'minimum': 5, 'maximum': 15 }), dictionary_formatter({ 'row': 2, 'amount': 100., 'uncertainty_type': 4, 'minimum': 75, 'maximum': 125 }), ], }] fp = create_calculation_package(name="test-fixture-basic-matrices", resources=deepcopy(resources), path=directory) fp.rename(directory / "basic-calculation-package.zip") directory = base_dir / "basic-cp-directory" directory.mkdir(mode=0o755, exist_ok=True) create_calculation_package(name="test-fixture-basic-matrices-directory", resources=resources, path=directory, compress=False)