Main
View or edit on GitHub
This page is synchronized from trase/models/argentina/soy/main.ipynb. Last modified on 2026-08-05 15:56 CEST by Harry Biddle.
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).
%configure_logging
from trase.tools.sps import SupplyChain
for year in [2015, 2016, 2017, 2018, 2019]:
supplychain = SupplyChain("argentina/soy", year)
supplychain.preparation()
supplychain.load()
supplychain.run()
supplychain.flow_report_by_attribute(
"vol_bean", ["status", "branch"], significant_digits=10
)
supplychain.export_results()
supplychain.upload_results()
from trase.tools.sps import SupplyChain
supplychain = SupplyChain("argentina/soy", year=2019)
supplychain.preparation()
Extracting data from source ...[0mDownloading s3://trase-storage/argentina/national/sei_pcs/v0.2.2/CD_ARGENTINA_2019_SOY.csv[0m
[97m took 0.1 seconds[0m
[1mRunning pre-processing for Flows[0m
Report: Before processing[0m
| Row Count: 936[0m
| Sum of vol_bean: 53,580,604[0m
| Sum of fob: 18,572,442,802[0m
Report: Drop vol_bean=0[0m
| Row Count: 936[0m
| Sum of vol_bean: 53,580,604[0m
| Sum of fob: 18,572,442,802[0m
/home/sagemaker-user/.admin/repos/TRASE/trase/tools/etl/pandas_wrapper.py:397: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df[column_name] = _cast_series_to_type(df[column_name], python_type)
/home/sagemaker-user/.admin/repos/TRASE/trase/tools/etl/pandas_wrapper.py:397: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df[column_name] = _cast_series_to_type(df[column_name], python_type)
/home/sagemaker-user/.admin/repos/TRASE/trase/tools/etl/pandas_wrapper.py:397: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df[column_name] = _cast_series_to_type(df[column_name], python_type)
Report: After processing[0m
| Row Count: 936[0m
| Sum of vol_bean: 53,580,604[0m
| Sum of fob: 18,572,442,802[0m
[32mWritten /home/sagemaker-user/.admin/repos/TRASE/trase/models/argentina/soy/2019/prepared/flows.csv[0m
Extracting data from source ...[0mDownloading s3://trase-storage/argentina/spatial/BOUNDARIES/provinces/out/AR_PROVINCES.csv[0m
[97m took 0.1 seconds[0m
[1mRunning pre-processing for Provinces[0m
Report: Before processing[0m
| Row Count: 24[0m
/home/sagemaker-user/.admin/repos/TRASE/trase/tools/etl/pandas_wrapper.py:397: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df[column_name] = _cast_series_to_type(df[column_name], python_type)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[2], line 3
1 from trase.tools.sps import SupplyChain
2 supplychain = SupplyChain("argentina/soy", year=2019)
----> 3 supplychain.preparation()
File ~/.admin/repos/TRASE/trase/tools/sei_pcs/utilities.py:104, in trace_memory_allocation.<locals>.wrapper(*args, **kwargs)
102 return return_value
103 else:
--> 104 return func(*args, **kwargs)
File ~/.admin/repos/TRASE/trase/tools/sei_pcs/supply_chain.py:199, in SupplyChain.preparation(self)
197 OUTPUT_FILE_OVERWRITE_TRACKER.enable()
198 with warnings_if_pd_merge_called_without_validate():
--> 199 self._call_scripts_with_function(
200 self.preparation_scripts, "preparation", self.context
201 )
202 finally:
203 OUTPUT_FILE_OVERWRITE_TRACKER.disable()
File ~/.admin/repos/TRASE/trase/tools/sei_pcs/supply_chain.py:266, in SupplyChain._call_scripts_with_function(self, scripts, function_name, *function_arguments)
262 continue
264 LOGGER.info(f"Running {function_name} from {script_file}")
--> 266 return_value = function(*function_arguments)
267 results.append(return_value)
269 return results
File ~/.admin/repos/TRASE/trase/models/argentina/soy/preparation.py:39, in preparation(context)
36 Flows(context).run()
38 # Boundaries
---> 39 Provinces(context).run()
40 df_departments = Departments(context).run()
41 Zone(context).run(df_departments)
File ~/.admin/repos/TRASE/trase/tools/etl_internal/processors.py:109, in Preprocessor.run(self, returns, *args)
106 self._extract_from_source()
108 if self.should_rerun(args):
--> 109 return super(DataframePreprocessor, self).run(
110 *args, returns=returns, force=True
111 )
112 else:
113 return super(DataframePreprocessor, self).run(
114 *args, returns=returns, prevent=True
115 )
File ~/.admin/repos/TRASE/trase/tools/etl/processors.py:177, in PreprocessorBase.run(self, returns, force, prevent, *args)
174 data = self.preprocess(data)
176 with timing("Processing data", indent=1):
--> 177 data = self.process(data, *args)
178 assert data is not None, "process() must return a value"
180 with timing("Post-processing data", indent=1):
File ~/.admin/repos/TRASE/trase/models/argentina/soy/preparation.py:139, in Provinces.process(self, df)
138 def process(self, df):
--> 139 return append(
140 df,
141 [
142 {"geocode": "XX", "trase_id": "AR-XX", "name": "UNKNOWN"},
143 {"geocode": "XX-STOCK", "trase_id": "AR-XX-STOCK", "name": "STOCK"},
144 ],
145 )
File ~/.admin/repos/TRASE/trase/tools/sei_pcs/pandas_utilities.py:705, in append(df, items)
704 def append(df: pd.DataFrame, items) -> pd.DataFrame:
--> 705 return pd.concat([df, items], ignore_index=True, verify_integrity=True)
File ~/.conda/envs/trase-env/lib/python3.10/site-packages/pandas/core/reshape/concat.py:382, in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy)
379 elif copy and using_copy_on_write():
380 copy = False
--> 382 op = _Concatenator(
383 objs,
384 axis=axis,
385 ignore_index=ignore_index,
386 join=join,
387 keys=keys,
388 levels=levels,
389 names=names,
390 verify_integrity=verify_integrity,
391 copy=copy,
392 sort=sort,
393 )
395 return op.get_result()
File ~/.conda/envs/trase-env/lib/python3.10/site-packages/pandas/core/reshape/concat.py:448, in _Concatenator.__init__(self, objs, axis, join, keys, levels, names, ignore_index, verify_integrity, copy, sort)
445 objs, keys = self._clean_keys_and_objs(objs, keys)
447 # figure out what our result ndim is going to be
--> 448 ndims = self._get_ndims(objs)
449 sample, objs = self._get_sample_object(objs, ndims, keys, names, levels)
451 # Standardize axis parameter to int
File ~/.conda/envs/trase-env/lib/python3.10/site-packages/pandas/core/reshape/concat.py:489, in _Concatenator._get_ndims(self, objs)
484 if not isinstance(obj, (ABCSeries, ABCDataFrame)):
485 msg = (
486 f"cannot concatenate object of type '{type(obj)}'; "
487 "only Series and DataFrame objs are valid"
488 )
--> 489 raise TypeError(msg)
491 ndims.add(obj.ndim)
492 return ndims
TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid
import pandas as pd
from trase.tools.sps import SupplyChain
df = supplychain.get("flows")
# df.head()
# df_assets.head()
# df = df.groupby(["exporter_name"]).agg(sum)
# df = df.sort_values("vol_bean", ascending=False)
df = df[df["branch"] == "2.2.1"]
df = df[
[
"exporter_name",
"customs_office.customs_office",
"country_of_destination",
"product_type",
"vol_bean",
"province.name",
]
]
df
import pandas as pd
import os
os.getcwd()
df = pd.read_csv(
"/usr/share/TRASE/trase/models/argentina/soy/2018/results/results.csv", sep=";"
)
# df = df[df["LVL3_TRASE_ID_PROD"]== "AR-STOCK"]
df.head()
df = df.groupby(["BRANCH"]).agg(sum)
df = df.sort_values("VOLUME_TRADED", ascending=False)
df
# destined_for_quequen = df['ZONE_DESTINATION'] == 'QUEQUEN'
# origin_not_linked_to_quequen = ~df['ZONE_ORIGIN'].isin(['QUEQUEN', 'ZONA 3', 'ZONA 4'])
# should_exclude_quequen = destined_for_quequen & origin_not_linked_to_quequen
# df = df[~should_exclude_quequen]
# df[df['destination'] == "B BLANCA"]
Sankey
from trase.tools.sps import sankey
df = supplychain.get("flows")
sankey(
df,
"vol_bean",
[
"department_of_production.province.name",
"exporter_name",
"country_of_destination",
],
)
Chloropleth
from trase.tools.sps import consolidate, rename
from trase.tools.jupyter.observable import notebook
df = supplychain.get("flows")
df = df[df["exporter.cuit"] == "3350673744"].copy() # NIDERA ARGENTINA
df = consolidate(df, ["vol_bean"], ["department_of_production.geocode"])
df = rename(df, {"department_of_production.geocode": "id", "vol_bean": "value"})
notebook(
"@trase/choropleth-canvas",
["chart"],
{"country": "argentina", "width": 1500, "data": df.to_dict("records")},
)