Skip to content

Main

View or edit on GitHub

This page is synchronized from trase/models/paraguay/corn/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 Corn

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

import pandas as pd

from trase.tools.aws import get_pandas_df, upload_pandas_df_to_s3

# from trase.tools.jupyter import observable

from trase.tools.sps import SupplyChain

from trase.models.paraguay.corn.model_pandas import *


for country in ["Paraguay"]:
    for commodity in ["Corn"]:
        for year in range(2014, 2019 + 1):
            display("-" * 40)
            display("Running {} {} {}...".format(country, commodity, year))
            sc = SupplyChain("paraguay/corn", year=year, bucket="trase-storage")
            sc.preparation()
            sc.load()

            results = run(sc, verbose=True)
            sc.export_results(df=results)
            sc.upload_results(create_ingest_metadata=False)

Write results locally to deforestation free

write_to_df = True

if write_to_df:
    print(
        f"Writing {sc.context.year} preliminary results to deforestation free at 'results/pr_corn_prelim_results_'{sc.context.year}'.csv'..."
    )
    results.to_csv(f"results/pr_corn_prelim_results_{sc.context.year}.csv", index=False)

A Short Section that kinda helps with QA

import pandas as pd

before = pd.read_csv("2016/IN/flows.csv", sep=";", dtype=str, keep_default_na=False)
before
after = after[after["destination_id"].str.startswith("PY-PORT")].rename(
    columns={"destination_id"}
)

Check some key breakdowns, make sure that trade volume matches results export volume

results.groupby("is_domestic").sum()["volume_tn"]
import numpy as np

# Does original trade volume match final trade volume?
np.isclose(
    sc.get("flows")["volume_raw"].sum(),
    results.groupby("is_domestic").sum()["volume_tn"][0],
)
sc.get("flows")["volume_adjusted"].sum()

Look at the same things with observable maps

def observable_map_from_model_outputs(df, include_domestic=True, width=600):

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

    df["prod_id"] = df["prod"].str.slice(3)

    results_map = df.groupby("prod_id").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": 2,
            "width": width,
            "tooltipKey": "Results (TONS)",
        },
    )

Flows without domestic demand

observable_map_from_model_outputs(results, include_domestic=False)

Flows with domestic demand

observable_map_from_model_outputs(results, include_domestic=True)

Production

prod = sc.get("production")
prod["geocode"] = prod["geocode"].str.slice(3)
prod_map = prod.groupby("geocode").agg("sum").loc[:, ["tons"]].reset_index()
prod_map.columns = ["id", "value"]
observable.notebook(
    "d/1415b9010825b4d7",
    #         "d/436d61d79acea690", #zoom
    ["chart"],
    {
        "country": "paraguay",
        "data": prod_map.to_dict(orient="records"),
        "level": 2,
        "width": 600,
        "tooltipKey": "Production (TONS)",
    },
)