Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

World Trade Network v2 — Conflict-Aware Trade Risk Explorer

An improved, modular rebuild of a "war impact on trade" visualizer. It models global trade flows and geopolitical conflicts as two linked network graphs, computes real graph metrics (centrality, chokepoints, disruption impact), and renders everything as an interactive Streamlit + Plotly dashboard.

This project was built as a direct improvement on a single-file prototype, addressing its main weaknesses:

Prototype problem Fix in this version
One 1700-line app.py Split into data/, graph/, viz/, app.py
Real dataset (Comtrade CSV) present but never loaded Data loader reads and blends it with sourced conflict data
Conflict list had no sources/dates Every conflict edge has source + last_updated fields
Trade edges were binary (route exists / doesn't) Edges carry volume_usd_millions so impact is weighted, not binary
No real graph analysis NetworkX centrality, shortest-path rerouting, chokepoint detection
No tests tests/ with pytest covering the graph-building logic
Committed __pycache__, logs .gitignore added
No README This file

Folder Structure

world-trade-network/
├── app.py                     # Streamlit entry point (UI + layout only)
├── requirements.txt
├── .gitignore
├── README.md
├── data/
│   ├── conflicts.py            # Sourced tension/conflict edges (dataclasses)
│   ├── trade_edges.csv         # Sample trade-flow dataset (commodity, mode, $ volume)
│   ├── coords_fallback.py      # Hardcoded lat/lon fallback if API is unreachable
│   └── loader.py                # Loads + validates + merges all data sources
├── graph/
│   ├── build.py                 # Builds NetworkX graphs + adjacency/impact tensors
│   └── metrics.py               # Centrality, chokepoints, rerouting logic
├── viz/
│   └── map.py                   # Plotly Scattergeo figure builders
└── tests/
    └── test_graph.py            # pytest unit tests for graph/build.py & metrics.py

How to Run It

# 1. Clone / unzip the project, then enter the folder
cd world-trade-network

# 2. Create and activate a virtual environment
python -m venv venv
source venv/bin/activate        # Windows: venv\Scripts\activate

# 3. Install dependencies
pip install -r requirements.txt

# 4. Run the app
streamlit run app.py

Then open the URL Streamlit prints (usually http://localhost:8501).

No API key is required. The app tries a free restcountries.com lookup for live coordinates and falls back to data/coords_fallback.py automatically if the network call fails, so it also works fully offline.

To run the test suite:

pip install pytest
pytest tests/ -v

What's Improved Over the Original Prototype

  1. Weighted impact, not binary flags. Every trade edge carries an actual volume_usd_millions estimate, so the dashboard can show how much trade value sits behind a conflict-exposed route, not just whether one exists.
  2. Sourced conflict data. Each conflict edge cites a source string and last_updated date so the data is auditable instead of asserted.
  3. Real graph metrics via NetworkX:
    • Betweenness centrality — which countries are the most critical trade hubs (removing them fragments the most routes).
    • Chokepoint detection — edges whose removal disconnects otherwise linked trade clusters.
    • Alternate-route suggestion — if a route is war-exposed, the app computes the next-shortest available path avoiding conflict nodes.
  4. Modular code — each concern (data, graph math, visualization, UI) is its own file, independently testable and independently readable.
  5. Tests — graph/build.py and graph/metrics.py are pure functions with pytest coverage, so future changes can't silently break the math.

Suggested Next Improvements

  • Swap data/trade_edges.csv for a live pull of the full UN Comtrade bulk API (requires free registration) to replace the illustrative sample values with real bilateral trade statistics.
  • Add a time slider once you have multiple years of trade + conflict data, to animate how the network shifts year over year.
  • Add st.download_button exports (filtered CSV, PNG snapshot) for reporting.
  • Deploy to Streamlit Community Cloud for a shareable public demo link.

Data Sources & Caveats

  • data/trade_edges.csv is an illustrative sample shaped like real UN Comtrade categories (commodity, mode, reporter, partner, trade value) — swap it for a genuine Comtrade export before drawing real-world conclusions.
  • data/conflicts.py entries are sourced from public reporting as of the last_updated date on each entry; conflict status is fluid and should be re-verified before use in any serious analysis.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages