Skip to content

Commit

Permalink
Code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Aug 4, 2023
1 parent 6b58c2d commit 57a9e86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 56 deletions.
Binary file modified dev/profile.prof
Binary file not shown.
79 changes: 23 additions & 56 deletions premise/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit 57a9e86

Please sign in to comment.