Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(data): AGB 3.2 + EI 3.10 #727

Merged
merged 9 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions data/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ env | grep ECOBALYSE_DATA_DIR || exit
docker exec -u jovyan -it -e ECOBALYSE_DATA_DIR=/home/jovyan/ecobalyse-private/ -w /home/jovyan/ecobalyse/data $(NAME) $(1);\
else \
echo "(Creating a new container)" &&\
docker run --rm -it -v $(NAME):/home/jovyan -v $$PWD/../:/home/jovyan/ecobalyse -v $(ECOBALYSE_DATA_DIR):/home/jovyan/ecobalyse-private -e ECOBALYSE_DATA_DIR=/home/jovyan/ecobalyse-private/ -w /home/jovyan/ecobalyse/data $(NAME) $(1); fi
docker run --rm -it -v $(NAME):/home/jovyan -v $$PWD/../:/home/jovyan/ecobalyse -v $$PWD/../../dbfiles/:/home/jovyan/dbfiles -v $(ECOBALYSE_DATA_DIR):/home/jovyan/ecobalyse-private -e ECOBALYSE_DATA_DIR=/home/jovyan/ecobalyse-private/ -w /home/jovyan/ecobalyse/data $(NAME) $(1); fi
endef

all: import export
import : image import_agribalyse import_ecoinvent import_method sync_datapackages
import : image import_food import_ecoinvent import_method sync_datapackages
export: export_food format

image:
docker build -t $(NAME) docker

import_agribalyse:
@$(call DOCKER,python3 import_agribalyse.py --recreate-activities)
import_food:
@$(call DOCKER,python3 import_food.py --recreate-activities)

import_method:
@$(call DOCKER,python3 import_method.py)
Expand Down Expand Up @@ -64,12 +64,12 @@ jupyter_password:
start_notebook:
@docker run --rm -it -d \
-v $(NAME):/home/jovyan \
-v $$PWD/../../dbfiles:/home/jovyan/dbfiles \
-v $$PWD/../:/home/jovyan/ecobalyse \
-v $(ECOBALYSE_DATA_DIR):/home/jovyan/ecobalyse-private \
-e ECOBALYSE_DATA_DIR=/home/jovyan/ecobalyse-private/ \
-e JUPYTER_PORT=$(JUPYTER_PORT) \
-e JUPYTER_ENABLE_LAB=yes \
-w /home/jovyan/ecobalyse/data \
-p $(JUPYTER_PORT):$(JUPYTER_PORT) \
--name $(NAME) \
$(NAME) start-notebook.sh --collaborative
Expand Down
14 changes: 7 additions & 7 deletions data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Comment générer les données json utilisées par le frontal elm :
- Si vous êtes sur Mac avec architecture ARM, affectez 6Go de RAM à Docker dans Docker Desktop :
Settings → Ressources → Advanced → Memory = 6G
- Préparez les bases de données à importer, elle ne font pas partie du dépôt :
- Agribalyse : compressé dans un fichier `AGB3.1.1.20230306.CSV.zip` dans ce dossier data/
- Autres bases alimentaire : consultez les noms de fichier dans `import_agribalyse.py`
- Agribalyse : compressé dans un fichier `AGB3.1.1.20230306.CSV.zip` dans un dossier `dbfiles/` au dessus du dépôt
- Autres bases alimentaire : consultez les noms de fichier dans `import_food.py`
- Ecoinvent : décompressé dans un dossier `ECOINVENT3.9.1` dans ce même dossier
- Lancez **`make`** ce qui va successivement :
- construire l'image docker ;
Expand All @@ -20,12 +20,12 @@ d'abord un `make clean_data` (qui supprime le volume docker).
## Autres commandes :

- `make image` : pour construire l'image docker choisie
- `make import_agribalyse` : pour importer les bases de données alimentaire dans Brightway.
Assurez-vous d'avoir les bon fichiers de données dans `data/`
- `make import_ecoinvent` : pour importer Ecoinvent 3.9.1. dans Brightway. Assurez-vous
d'avoir le bon dossier de données dans `data/`
- `make import_food` : pour importer les bases de données alimentaire dans Brightway.
Assurez-vous d'avoir les bon fichiers de données dans `dbfiles/` au dessus du dépôt
- `make import_ecoinvent` : pour importer Ecoinvent 3.9.1. dans Brightway.
Assurez-vous d'avoir le bon dossier de données dans `dbfiles/` au dessus du dépôt
- `make import_method` : pour importer EF 3.1 adapted dans Brightway.
Assurez-vous d'avoir le bon fichier de données dans `data/`
Assurez-vous d'avoir le bon fichier de données dans `dbfiles/` au dessus du dépôt
- `make export_food` : pour exporter les json pour le builder alimentaire
- `make delete_database DB=<dbname>` : pour supprimer une base de données (Ex avec espace: make delete_database DB="Ecoinvent\ 3.9.1")
- `make delete_method` : pour supprimer la méthode EF3.1
Expand Down
20 changes: 13 additions & 7 deletions data/common/import_.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def import_simapro_csv(
dbname,
biosphere=BIOSPHERE,
migrations=[],
first_strategies=[],
excluded_strategies=[],
other_strategies=[],
source=None,
Expand All @@ -186,10 +187,10 @@ def import_simapro_csv(
# unzip
with ZipFile(datapath) as zf:
print("### Extracting the zip file...")
zf.extractall()
zf.extractall(path=os.path.dirname(datapath))
unzipped = datapath[0:-4]

if "AGB3.1.1" in datapath:
if "AGB" in datapath:
print("### Patching Agribalyse...")
# `yield` is used as a variable in some Simapro parameters. bw2parameters cannot handle it:
# (sed is faster than Python)
Expand Down Expand Up @@ -221,11 +222,16 @@ def import_simapro_csv(

print("### Applying strategies...")
# exclude strategies/migrations
database.strategies = [
s
for s in database.strategies
if not any([e in repr(s) for e in excluded_strategies])
] + other_strategies
database.strategies = (
first_strategies
+ [
s
for s in database.strategies
if not any([e in repr(s) for e in excluded_strategies])
]
+ other_strategies
)

database.apply_strategies()
database.statistics()
# try to link remaining unlinked technosphere activities
Expand Down
6 changes: 3 additions & 3 deletions data/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM jupyter/minimal-notebook:notebook-7.0.6

ENV BRIGHTWAY2_DIR /home/$NB_USER/data
ENV BRIGHTWAY2_DOCKER 1
ENV BRIGHTWAY2_OUTPUT_DIR /home/$NB_USER/output
ENV BRIGHTWAY2_DIR=/home/$NB_USER/data
ENV BRIGHTWAY2_DOCKER=1
ENV BRIGHTWAY2_OUTPUT_DIR=/home/$NB_USER/output
ENV XDG_CACHE_HOME="/home/${NB_USER}/.cache/"

USER $NB_USER
Expand Down
3 changes: 0 additions & 3 deletions data/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ fi
mkdir -p /home/jovyan/.npm
chown -R jovyan:100 "/home/jovyan/.npm"

# Clear npm cache
su jovyan -c "npm cache clean --force"

exec gosu jovyan "$@"
13 changes: 11 additions & 2 deletions data/import_ecoinvent.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/usr/bin/env python3


from os.path import join

import bw2data
import bw2io
from bw2data.project import projects
from common.import_ import add_missing_substances, import_simapro_csv

# Ecoinvent
DATAPATH = "./Ecoinvent3.9.1.CSV.zip"
EI391 = "./Ecoinvent3.9.1.CSV.zip"
EI310 = "./Ecoinvent3.10.CSV.zip"
BIOSPHERE = "biosphere3"
PROJECT = "default"

Expand All @@ -20,7 +23,13 @@ def main():
add_missing_substances(PROJECT, BIOSPHERE)

if (db := "Ecoinvent 3.9.1") not in bw2data.databases:
import_simapro_csv(DATAPATH, db)
import_simapro_csv(join("..", "..", "dbfiles", EI391), db)
else:
print(f"{db} already imported")

if (db := "Ecoinvent 3.10") not in bw2data.databases:
import_simapro_csv(join("..", "..", "dbfiles", EI310), db)

else:
print(f"{db} already imported")

Expand Down
48 changes: 41 additions & 7 deletions data/import_agribalyse.py → data/import_food.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import copy
import functools
from os.path import join

import bw2data
import bw2io
Expand All @@ -15,7 +16,8 @@
)

PROJECT = "default"
AGRIBALYSE = "AGB3.1.1.20230306.CSV.zip" # Agribalyse
AGRIBALYSE31 = "AGB3.1.1.20230306.CSV.zip" # Agribalyse 3.1
AGRIBALYSE32 = "AGB32beta_08082024.CSV.zip" # Agribalyse 3.2
GINKO = "CSV_369p_et_298chapeaux_final.csv.zip" # additional organic processes
PASTOECO = [
"CONVEN~1.CSV.zip",
Expand Down Expand Up @@ -178,6 +180,19 @@ def remove_negative_land_use_on_tomato(db):
return new_db


def remove_some_processes(db):
"""Some processes make the whole import fail
due to inability to parse the Input and Calculated parameters"""
new_db = []
for ds in db:
new_ds = copy.deepcopy(ds)
if ds.get("simapro metadata", {}).get("Process identifier") not in (
"EI3CQUNI000025017103662",
):
new_db.append(new_ds)
return new_db


GINKO_STRATEGIES = [
remove_negative_land_use_on_tomato,
remove_azadirachtine,
Expand Down Expand Up @@ -205,12 +220,25 @@ def remove_negative_land_use_on_tomato(db):
bw2io.bw2setup()
add_missing_substances(PROJECT, BIOSPHERE)

# AGRIBALYSE
# AGRIBALYSE 3.1.1
if (db := "Agribalyse 3.1.1") not in bw2data.databases:
import_simapro_csv(
AGRIBALYSE,
join("..", "..", "dbfiles", AGRIBALYSE31),
db,
migrations=AGRIBALYSE_MIGRATIONS,
excluded_strategies=EXCLUDED,
other_strategies=AGB_STRATEGIES,
)
else:
print(f"{db} already imported")

# AGRIBALYSE 3.2
if (db := "Agribalyse 3.2 beta 08/08/2024") not in bw2data.databases:
import_simapro_csv(
join("..", "..", "dbfiles", AGRIBALYSE32),
db,
migrations=AGRIBALYSE_MIGRATIONS,
first_strategies=[remove_some_processes],
excluded_strategies=EXCLUDED,
other_strategies=AGB_STRATEGIES,
)
Expand All @@ -220,14 +248,16 @@ def remove_negative_land_use_on_tomato(db):
# PASTO ECO
if (db := "PastoEco") not in bw2data.databases:
for p in PASTOECO:
import_simapro_csv(p, db, excluded_strategies=EXCLUDED)
import_simapro_csv(
join("..", "..", "dbfiles", p), db, excluded_strategies=EXCLUDED
)
else:
print(f"{db} already imported")

# GINKO
if (db := "Ginko") not in bw2data.databases:
import_simapro_csv(
GINKO,
join("..", "..", "dbfiles", GINKO),
db,
excluded_strategies=EXCLUDED,
other_strategies=GINKO_STRATEGIES,
Expand All @@ -238,13 +268,17 @@ def remove_negative_land_use_on_tomato(db):

# CTCPA
if (db := "CTCPA") not in bw2data.databases:
import_simapro_csv(CTCPA, db, excluded_strategies=EXCLUDED)
import_simapro_csv(
join("..", "..", "dbfiles", CTCPA), db, excluded_strategies=EXCLUDED
)
else:
print(f"{db} already imported")

# WFLDB
if (db := "WFLDB") not in bw2data.databases:
import_simapro_csv(WFLDB, db, excluded_strategies=EXCLUDED)
import_simapro_csv(
join("..", "..", "dbfiles", WFLDB), db, excluded_strategies=EXCLUDED
)
else:
print(f"{db} already imported")

Expand Down
2 changes: 1 addition & 1 deletion data/import_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Agribalyse
BIOSPHERE = "biosphere3"
METHODNAME = "Environmental Footprint 3.1 (adapted) patch wtu" # defined inside the csv
METHODPATH = METHODNAME + ".CSV.zip"
METHODPATH = os.path.join("..", "..", "dbfiles", METHODNAME + ".CSV.zip")

# excluded strategies and migrations
EXCLUDED_FOOD = [
Expand Down