From 92c641d61103db04d4e24da52559200cd4957dba Mon Sep 17 00:00:00 2001 From: romainsacchi Date: Thu, 14 Sep 2023 16:58:38 +0200 Subject: [PATCH] Fix issue with removing unused datasets + version bump --- setup.py | 2 +- unfold/__init__.py | 2 +- unfold/unfold.py | 26 +++++++++++++++++++++----- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/setup.py b/setup.py index 64182ca..e45a1e6 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ def package_files(directory): setup( name="unfold", - version="1.0.8", + version="1.0.9", python_requires=">=3.9", packages=packages, author="Romain Sacchi ", diff --git a/unfold/__init__.py b/unfold/__init__.py index 352e570..164dcd0 100644 --- a/unfold/__init__.py +++ b/unfold/__init__.py @@ -1,5 +1,5 @@ __all__ = ("Unfold", "Fold") -__version__ = (1, 0, 8) +__version__ = (1, 0, 9) from .fold import Fold from .unfold import Unfold diff --git a/unfold/unfold.py b/unfold/unfold.py index 2874b5c..d3fbc63 100644 --- a/unfold/unfold.py +++ b/unfold/unfold.py @@ -433,8 +433,8 @@ def find_correct_id(self, key: tuple) -> [tuple, None]: return key[0] + ", in ground", key[1], key[2], key[3] else: - print("Could not find key", key) - return key + raise f"Could not find key: {key}" + #return key def fix_key(self, key: tuple) -> tuple: if key in self.dependency_mapping: @@ -761,19 +761,36 @@ def filter_out_datasets( """ # Get the list of datasets not used in the current scenario. - df_gr = self.scenario_df.groupby("to activity name").sum(numeric_only=True) + df_gr = self.scenario_df.groupby(["to activity name", "to reference product", "to location"]).sum(numeric_only=True) datasets_not_in_scenario = df_gr.loc[ (df_gr[scenario_name] == 0) & (df_gr.sum(1) != 0), : ].index.tolist() + # remove items from datasets_not_in_scenario that are + # used in exchanges of database + for act in database: + for exc in act["exchanges"]: + if exc["type"] == "technosphere": + key = (exc["name"], exc["product"], exc["location"]) + if key in datasets_not_in_scenario: + datasets_not_in_scenario.remove(key) + # Remove datasets that are not in the current scenario. database = [ - act for act in database if act["name"] not in datasets_not_in_scenario + act for act in database + if (act["name"], act["reference product"], act["location"]) not in datasets_not_in_scenario ] return database + def check_usage(self, name: str, product: str, location: str, database: list) -> bool: + # check that name, product, location are not used in any exchange of database + + pass + + + def generate_superstructure_database(self) -> List[dict]: """ Generates the superstructure database. @@ -1097,7 +1114,6 @@ def unfold( self.check_dependencies(dependencies) self.extract_source_database() self.extract_additional_inventories() - self.format_dataframe(scenarios=scenarios, superstructure=superstructure) self.generate_factors()