This is a Python conversion of an R-based Multi-Regional Input-Output (MRIO) model merged with FoodLife for the purpose of analyzing agricultural trade and its impact on cropland use and species habitats.
Important
This code is built upon code from the following publications:
Schwarzmueller, F. & Kastner, T (2022), Agricultural trade and its impact on cropland use and the global loss of species' habitats. Sustainability Science, doi: 10.1007/s11625-022-01138-7
Ball, T.S., Dales, M., Eyres, A. et al. Food impacts on species extinction risks can vary by three orders of magnitude. Nat Food 6, 848–856 (2025). doi: 10.1038/s43016-025-01224-w
Please cite appropriately when using this code.
main.py- Main execution script (equivalent to_Execute.R)
processing/unzip_data.py- Utility for unzipping FAOSTAT data (equivalent toUnzip data.R)processing/calculate_trade_matrix.py- Calculates apparent consumption and trade links (equivalent toCalculating_Trade_Matrix.R)processing/animal_products_to_feed.py- Converts animal products into embedded feed items (equivalent toanimal_products_to_feed.R)provenance/_provenance.py,provenance/_get_impacts_bd.pyandprovenance/_process_dat.py- Modified version of LIFE impact code
requirements.txt- Python package dependenciesREADME.md- This documentation filePipelineChangesLDN.md- List of significant changes made from original code
- Python 3.8 or higher
- pip (Python package manager)
- Clone or download this repository
- Navigate to the python directory
- Install required packages:
pip install -r requirements.txt
-
If downloading from Github LFS takes too long, download the FAOSTAT data files and place in the input_data directory
- Production_Crops_Livestock_E_All_Data_(Normalized).zip
- FoodBalanceSheetsHistoric_E_All_Data_(Normalized).zip
- FoodBalanceSheets_E_All_Data_(Normalized).zip
- Trade_DetailedTradeMatrix_E_All_Data_(Normalized).zip
- CommodityBalances_(non-food)_(-2013_old_methodology)E_All_Data(Normalized).zip
- SUA_Crops_Livestock_E_All_Data_(Normalized).zip
- Inputs_LandUse_E_All_Data.zip
-
Configure settings in main.py
-
Run the main script:
python main.py
You can modify the following options in main.py:
YEARS = list(range(1986, 2022)) # Limits: 1986-2022, default: 2013-2022Choose from the following nutritional conversion methods:
conversion_opt = "dry_matter"Available options:
"dry_matter"(default)"Energy""Protein""Fiber_TD""Zinc""Iron""Calcium""Folate_Tot""Riboflavin""Choline_Tot""Potassium""Vit_E""Vit_B12""Vit_K""Vit_A"
Choose whether to prefer import or export data:
prefer_import = "import" # or "export"Choose whether to use 2020 mapspam data:
USE_2020_DATA = TrueControl which parts of the pipeline to run:
PIPELINE_COMPONENTS:list = [0]Component options:
0= Full pipeline (all components)1= Unzipping data only2= Error matrix calculation3= Trade matrix calculation4= Animal products to feed calculation5= Country-level impact calculations (as in LIFE)
Which countries to analyse in detail:
COUNTRIES = ["GBR"]The pipeline expects the following data files in the input_data directory:
CommodityBalances_(non-food)_(-2013_old_methodology)_E_All_Data_(Normalized).zipFoodBalanceSheets_E_All_Data_(Normalized).zipFoodBalanceSheetsHistoric_E_All_Data_(Normalized).zipProduction_Crops_Livestock_E_All_Data_(Normalized).zipSUA_Crops_Livestock_E_All_Data_(Normalized).zipTrade_DetailedTradeMatrix_E_All_Data_(Normalized).zipInputs_LandUse_E_All_Data.zip
CB_code_FAO_code_for_conversion_factors.csvCB_items_split.csvCB_to_primary_items_map.csvcommodity_crosswalk.csvcomposition_old_vs_new.csvcontent_factors_per_100g.xlsxcountry_opp_cost_v6.csvnocsDataExport_20251021-164754.xlsxPlanet-Based Diets - Data and Viewer.xlsxprimary_item_map_feed.csvReporting_Dates.xlsschwarzmueller_wwf.csvSUA_Crops_Livestock_E_ItemCodes.csv(also produced during unzip)tb_pasture_factors_2.csvweighing_factors.csv
For each processed year, the pipeline generates:
results/{year}/.mrio/TradeMatrix_{conversion}_{year}.csv- Main trade links for apparent consumptionresults/{year}/.mrio/TradeMatrixFeed_{conversion}_{year}.csv- as above, broken down for feedresults/{year}/.mrio/Pasture_calc.csv- Pasture efficiencies calculated for the relevant year- and all additional files as in LIFE
kdf.csvhas been renamedimpacts_aggregate.csvxdf.csvhave been renamedimpacts_full.csvdf_uk.csvis nowdf_{country_iso}.csvrather thanukfor all countriesfood_commodity_impacts.csvhas been added with calculated per kg impacts
Note
Processing time: ~2 hrs for all years (1986-2013) on a machine with 32GB RAM Recommended minimum 32GB RAM
Caution
In theory this code should run cleanly for 1986 - 2022, however commodity crosswalks are missing for the 2000 MAPSPAM commodities and FAO data mapping is unreliable prior to 2010. We therefore cannot guarantee the accuracy of results prior to 2010.
