Skip to content

Commit

Permalink
write the aggregated impacts in python during export
Browse files Browse the repository at this point in the history
  • Loading branch information
ccomb committed Oct 9, 2024
1 parent 227ab7e commit 5b017b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ delete_method:
@$(call DOCKER,python3 common/delete_methods.py)

export_food:
@$(call DOCKER,bash -c "python3 food/export.py && npm run processes:build")
@$(call DOCKER,bash -c "python3 food/export.py")

compare_food:
@$(call DOCKER,python3 export.py compare)
Expand Down
13 changes: 13 additions & 0 deletions data/common/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import functools
import json
import logging
from collections import OrderedDict
from copy import deepcopy

import bw2data
from bw2io.utils import activity_hash
Expand All @@ -22,6 +24,17 @@ def spproject(activity):
return "AGB3.1.1 2023-03-06"


def remove_detailed_impacts(processes):
result = list()
for process in processes:
new_process = OrderedDict(deepcopy(process))
for k in new_process["impacts"].keys():
if k not in ("pef", "ecs"):
new_process["impacts"][k] = 0
result.append(new_process)
return result


def export_json(data, filename):
"""
Export data to a JSON file, with added newline at the end.
Expand Down
6 changes: 6 additions & 0 deletions data/food/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
export_json,
load_json,
progress_bar,
remove_detailed_impacts,
spproject,
with_corrected_impacts,
with_subimpacts,
Expand Down Expand Up @@ -58,6 +59,7 @@
"UGB_FILE": f"{PROJECT_ROOT_DIR}/data/food/ecosystemic_services/ugb.csv",
"INGREDIENTS_FILE": f"{PROJECT_ROOT_DIR}/public/data/food/ingredients.json",
"PROCESSES_FILE": f"{ECOBALYSE_DATA_DIR}/data/food/processes_impacts.json",
"AGGREGATED": f"{PROJECT_ROOT_DIR}/public/data/food/processes.json",
"LAND_OCCUPATION_METHOD": ("selected LCI results", "resource", "land occupation"),
"GRAPH_FOLDER": f"{PROJECT_ROOT_DIR}/data/food/impact_comparison",
}
Expand Down Expand Up @@ -428,3 +430,7 @@ def csv_export_impact_comparison(compared_impacts):
export_json(ingredients_animal_es, CONFIG["INGREDIENTS_FILE"])
display_changes("id", oldprocesses, processes_corrected_impacts)
export_json(list(processes_corrected_impacts.values()), CONFIG["PROCESSES_FILE"])
export_json(
remove_detailed_impacts(list(processes_corrected_impacts.values())),
CONFIG["AGGREGATED"],
)

0 comments on commit 5b017b1

Please sign in to comment.