Skip to content

Commit

Permalink
simapro by default, brightway as a fallback for constructed ing
Browse files Browse the repository at this point in the history
  • Loading branch information
ccomb committed Mar 5, 2024
1 parent caeb959 commit 2031eab
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions data/common/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def progress_bar(index, total):

def with_subimpacts(process):
"""compute subimpacts in the process"""
if not process["impacts"]:
return process
# etf-o = etf-o1 + etf-o2
process["impacts"]["etf-o"] = (
process["impacts"]["etf-o1"] + process["impacts"]["etf-o2"]
Expand Down
6 changes: 4 additions & 2 deletions data/common/impacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@

def bytrigram(definitions, bynames):
"""takes the definitions above and some impacts by name, return the impacts by trigram"""
if type(bynames) is not dict:
print(bynames)
names2trigrams = {method[1]: trigram for trigram, method in definitions.items()}
try:
return {
names2trigrams.get(name): amount["amount"]
for name, amount in bynames.items()
if names2trigrams.get(name)
}
except:
return None
except Exception as e:
return str(e)
7 changes: 5 additions & 2 deletions data/food/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ def compute_impacts(processes):
).content
),
)
if results is not None:
# simapro failed (unexisting Ecobalyse project or some other reason)
if type(results) is dict and results:
# simapro succeeded
process["impacts"] = results
print(f"got impacts from simapro for: {process['name']}")
else:
# simapro failed (unexisting Ecobalyse project or some other reason)
# brightway
lca = bw2calc.LCA(
{
Expand All @@ -223,6 +225,7 @@ def compute_impacts(processes):
process.setdefault("impacts", {})[key] = float(
"{:.10g}".format(lca.score)
)
print(f"got impacts from brightway for: {process['name']}")

# compute subimpacts
process = with_subimpacts(process)
Expand Down
6 changes: 5 additions & 1 deletion data/spapi/simapro.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ async def impact(_: Request, project: str, process: str, method: str):
"""
print(f"{project}/{process}/{method}")
global lock
global server
global current_project
while lock:
print("waiting for lock release...")
sleep(1)

lock = True

impacts = {}
if os.path.exists("impacts.json"):
impacts = json.load(open("impacts.json"))
try:
if not impacts.get(f"{project}/{process}", {}).get(method, {}):
if project != current_project:
if not server.ProjectOpen or project != current_project:
print("Opening project...")
server.OpenProject(project, "")
current_project = project
Expand All @@ -57,6 +59,8 @@ async def impact(_: Request, project: str, process: str, method: str):
results = impacts.get(f"{project}/{process}", {}).get(method, {})
except Exception as e:
results = repr(e)
current_project = None

lock = False
print(results)
return results

0 comments on commit 2031eab

Please sign in to comment.