diff --git a/dev/profile.prof b/dev/profile.prof index e2462423..1a41ffd0 100644 Binary files a/dev/profile.prof and b/dev/profile.prof differ diff --git a/premise/transformation.py b/premise/transformation.py index 02f72d35..10663e88 100644 --- a/premise/transformation.py +++ b/premise/transformation.py @@ -210,6 +210,9 @@ def allocate_inputs(exc, lst): total = len(lst) pvs = [1 for _ in range(total)] + if lst[0]["name"] != exc["name"]: + exc["name"] = lst[0]["name"] + return [ new_exchange(exc, obj["location"], factor / total) for obj, factor in zip(lst, pvs) @@ -323,7 +326,9 @@ def select_multiple_suppliers( if exclude_region: extra_filters.append( - ws.exclude(ws.either(*[ws.contains("location", x) for x in exclude_region])) + ws.exclude( + ws.either(*[ws.contains("location", x) for x in exclude_region]) + ) ) try: @@ -1169,7 +1174,14 @@ def add_new_entry_to_cache( ) for e, s in zip(allocated, shares) ] - self.cache.setdefault(location, {}).setdefault(self.model, {})[exc_key] = entry + + if location not in self.cache: + self.cache[location] = {} + + if self.model not in self.cache[location]: + self.cache[location][self.model] = {} + + self.cache[location][self.model][exc_key] = entry def relink_technosphere_exchanges( self, @@ -1257,66 +1269,21 @@ def relink_technosphere_exchanges( possible_datasets[0]["reference product"] == exc["product"] ), f"candidate: {_(possible_datasets[0])}, exc: {_(exc)}" - self.cache.update( - { - dataset["location"]: { - self.model: { - ( - exc["name"], - exc["product"], - exc["location"], - exc["unit"], - ): [ - ( - e["name"], - e["product"], - e["location"], - e["unit"], - s, - ) - for e, s in zip( - [new_exchange(exc, exc["location"], 1.0)], - [1.0], - ) - ] - } - } - } + # update cache + self.add_new_entry_to_cache( + dataset["location"], exc, [new_exchange(exc, exc["location"], 1.0)], [1.0] ) new_exchanges.append(exc) continue if dataset["location"] in possible_locations: - self.cache.update( - { - dataset["location"]: { - self.model: { - ( - exc["name"], - exc["product"], - exc["location"], - exc["unit"], - ): [ - ( - e["name"], - e["product"], - e["location"], - e["unit"], - s, - ) - for e, s in zip( - [ - new_exchange( - exc, dataset["location"], 1.0 - ) - ], - [1.0], - ) - ] - } - } - } + + self.add_new_entry_to_cache( + dataset["location"], + exc, + [new_exchange(exc, dataset["location"], 1.0)], + [1.0] ) exc["location"] = dataset["location"]