Skip to content

Main 2023 test

View or edit on GitHub

This page is synchronized from trase/models/diet_trase/coffee_fullmodel/main_2023_test.ipynb. Last modified on 2025-12-13 00:30 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).

Documentation

Testing stuff to see if it works

import sys
from datetime import datetime

import numpy as np
import pandas as pd

# import math
from math import ceil
from tqdm import tqdm

from trase.tools.sps import *
from trase.tools.aws import upload_pandas_df_to_s3
from trase.tools.matching.fair_allocation import proportional_sourcing, direct_sourcing
from trase.tools.sps import split_flows_using_proportions
from trase.tools.sps import SupplyChain

from definition import years as model_years, version
from constants import HS6_TO_PRODUCT

# from trase.tools.sps import TraseGlpkCommand

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

HS6_TO_PRODUCT
# Run supply chain model
# for year in range(2019, 2021 + 1):
for year in [2019]:
    print("-" * 40)
    print("Running {} {} {}...".format("Peru", "Coffee", year))
    sc = SupplyChain("cocoa_coffee_global/coffee", year=year, bucket="trase-storage")
    # Set up Dataset
    sc.preparation()
    sc.load()

Production

# Municipality production
production = sc.get("production").set_index("province.geocode")["value"]
production.index = production.index.rename("geocode")

# convert to series
production = production.squeeze()

# # Get total annual production
total_production = sum(production)

# # print("production (sample)")
# # print(production.sample(3))
# # print("total production")
print(total_production)

# production.sample(10)
production

Flows

# Call in flows
flows_df = sc.get("flows")

# THIS NEEDS TO BE DONE ON RAW EQUIVALENT VALUES EVENTUALLY
total_exports = sum(sc.get("flows")["mass_tonnes"])

demand_export = flows_df
demand_export = demand_export.set_index(["port_of_export_label.port_trase_id"])[
    "mass_tonnes"
]
demand_export.index = demand_export.index.rename("port_trase_id")
demand_export = demand_export.groupby(level=["port_trase_id"]).sum()

# print("total exports")
print(total_exports)

# # print("flows_df (sample)")
# flows_df.sample(10)

print(demand_export)

Domestic demand

total_domestic_demand = total_production - total_exports

population = sc.get("populations")
population_prop = population.set_index("geocode")["proportions"]

domestic_demand = total_domestic_demand * population_prop

print(total_domestic_demand)
print(domestic_demand)

Costs

costs_jur2port = sc.get("distance_matrix_jur_port")
# costs_jur2port = costs_jur2port.set_index(["origin", "destination"])["distance"]

costs_jur2jur = sc.get("distance_matrix_jur_jur")
# costs_jur2jur = costs_jur2jur.set_index(["origin", "destination"])["distance"]

costs = costs_jur2port.append(costs_jur2jur)
costs = costs.set_index(["origin", "destination"])["distance"]

# costs_jur2port.sample(10)
# costs_jur2jur.sample(10)
costs.sample(10)

LP

supply = production
demand = demand_export.append(domestic_demand)
costs = costs

print(supply)
print(demand)
print(costs)
solve_transportation_problem(
    supply=supply,
    demand=demand,
    costs=costs,
    on_missing="warn",
)