Skip to content

Main

View or edit on GitHub

This page is synchronized from trase/models/paraguay/soy/main.ipynb. Last modified on 2025-12-14 23:19 CET by Trase Admin. Please view or edit the original file there; changes should be reflected here after a midnight build (CET time), or manually triggering it with a GitHub action (link).

Paraguay Soy

This notebook will be used to re-build the Paraguay Soy SEI-PCS model on deforestation free.

import sys
import os
from pathlib import Path
import logging

import pandas as pd

# from trase.tools.jupyter import observable
from trase.tools.sps import SupplyChain, upload_pandas_df_to_s3

# from model import *
from definition import version as MODEL_VERSION

# %configure_logging

# logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.CRITICAL)
# logging.getLogger("trase.pcs.nodes.helpers").setLevel(logging.WARNING)

pd.set_option("display.float_format", "{:,.2f}".format)

What time period do we want results from?

year_range = range(2014, 2020)
for country in ["Paraguay"]:
    for commodity in ["Soy"]:
        for year in year_range:
            psc = SupplyChain("paraguay/soy", year=year)
            psc.preparation()
            psc.load()
            results = run(psc, verbose=True)
            upload_pandas_df_to_s3(
                results,
                f"paraguay/soy/sei_pcs/v{MODEL_VERSION.replace('.', '_')}/SEIPCS_PAR_SOY_{year}_V{MODEL_VERSION}.csv",
                bucket_name="trase-storage",
            )

Observable

def observable_map_from_model_outputs(
    df, jurisdiction="district", include_domestic=True, width=600
):
    df["district"] = [x[3:] for x in df["district_of_production"]]
    df["department"] = [x[3:5] for x in df["district_of_production"]]

    if not include_domestic:
        df = df[df["country_of_import"] != "PARAGUAY"]

    results_map = (
        df.groupby(jurisdiction.lower()).agg("sum").loc[:, ["volume_tn"]].reset_index()
    )
    results_map.columns = ["id", "value"]

    return observable.notebook(
        "d/1415b9010825b4d7",
        #         "d/436d61d79acea690", #zoom
        ["chart"],
        {
            "country": "paraguay",
            "data": results_map.to_dict(orient="records"),
            "level": [3 if jurisdiction.lower() == "district" else 2],
            "width": width,
            "tooltipKey": "Results (TONS)",
        },
    )
observable_map_from_model_outputs(
    results, jurisdiction="district", include_domestic=False
)
observable_map_from_model_outputs(
    results, jurisdiction="department", include_domestic=False
)
observable_map_from_model_outputs(
    results, jurisdiction="district", include_domestic=True
)
results