# asterbons/cliodynamics architecture

> Architecture diagram of the GitHub repository asterbons/cliodynamics, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/asterbons/cliodynamics
- Repository: https://github.com/asterbons/cliodynamics

## Overview

1. **Repository purpose**
   - This is a Python-based data pipeline for building a **Political Stress Index (PSI)** for Germany using cliodynamics / Structural-Demographic Theory.
   - It is not an application server; the main architecture is an **ETL + analysis + visualization** workflow centered on CSV files and generated dashboard artifacts.

2. **Primary data boundary**
   - The project is organized around three filesystem zones:
     - `data/raw/` for source extracts
     - `data/processed/` for cleaned and merged datasets
     - `output/` for final visual outputs
   - This clean separation is the core architectural boundary: ingestion writes raw inputs, preprocessing creates the analytical master dataset, and analysis reads processed data to produce final artifacts.

3. **Ingestion subsystem**
   - The `src/loaders/` package is responsible for acquiring data from external sources and placing it into `data/raw/`.
   - Sources called out in the README include **Destatis / GENESIS**, **WSI strike data**, **Google Trends**, and other German socioeconomic datasets.
   - Loader scripts are domain-specific, not generic: separate scripts exist for trends, students, rent/wages, table search, and a larger `loader_v4.py` entrypoint.

4. **External data dependencies**
   - The architecture depends on government statistics and public data services rather than an internal database.
   - Destatis credentials are expected via environment variables, which means data acquisition is parameterized and credential-bound.
   - Google Trends is treated as an additional mobilization signal, and WSI strike records act as a social unrest proxy.

5. **Raw dataset shape**
   - The raw layer contains multiple topic-specific CSVs such as wages, CPI, GDP, tax revenue, civil servants, students, youth population, food prices, holders, and strike data.
   - There is also `google_trends_mobilization.csv`, indicating that the pipeline blends both official statistics and alternative signal data.
   - The presence of `data_holders_raw.csv` alongside `data_holders.csv` suggests a raw-to-cleaned step for some sources.

6. **Preprocessing subsystem**
   - `src/preprocessors/` is the transformation layer that normalizes, cleans, aligns time series, and merges indicators into a unified analytical dataset.
   - `preprocessor_v4.py` appears to be the current canonical pipeline, while `preprocessor.py` and `afterpreprocessor.py` look like earlier or supplementary versions.
   - This layer likely computes the PSI components and ensures that heterogeneous series become time-aligned and comparable.

7. **Analytical model layer**
   - `src/analysis/Wealth_Pump_Index.py` and `Seasonal_Decomposition.py` indicate focused analytical routines for component-level indicators and time-series decomposition.
   - `process_and_merge_trends.py` bridges Google Trends into the main dataset, showing that trend data is treated as an analytical augmentation rather than a raw source alone.
   - The analysis layer is where domain concepts such as wealth pump, elite pressure, and mobilization are operationalized.

8. **Visualization and reporting**
   - Visualization is handled separately from preprocessing, mainly through `src/analysis/visualizer_v1.py`, `visualizer_v2.py`, and `src/preprocessors/visualize_v4.py`.
   - The README references a generated interactive Plotly dashboard (`output/psi_v4_dashboard.html`) and a static figure output.
   - This separation suggests the pipeline is designed to produce reusable analytical datasets first, then multiple presentation formats.

9. **Pipeline flow**
   - The end-to-end flow is:
     - fetch raw indicators
     - store in `data/raw/`
     - preprocess into merged/master CSVs in `data/processed/`
     - run analysis and visualization
     - emit dashboard artifacts into `output/`
   - The processed master files (`master_cliodynamics.csv` and `master_cliodynamics_v2.csv`) are the central handoff point between data engineering and analysis.

10. **Versioning and evolution**
   - The presence of multiple `v2` and `v4` scripts indicates iterative development of the PSI methodology.
   - The architecture seems to preserve older scripts for comparison or fallback while promoting a newer “v4” pipeline as the main path.
   - This suggests the repo values reproducibility and methodological evolution rather than a single frozen implementation.

11. **Operational constraints**
   - Runtime is Python 3.9+ with a dependency-managed environment from `requirements.txt`.
   - There is a `preprocessor_debug.log`, which implies preprocessing is a nontrivial, failure-prone stage and likely contains diagnostic instrumentation.
   - Execution is file-based and script-driven, so the deployment model is batch processing rather than a long-running service.

12. **What to diagram**
   - A good architecture diagram should show:
     - external data sources on the left
     - loader scripts writing to `data/raw/`
     - preprocessors producing `data/processed/master_cliodynamics*.csv`
     - analysis scripts consuming processed data
     - visualization outputs in `output/`
   - The key conceptual modules to emphasize are **data ingestion**, **indicator engineering**, **composite PSI calculation**, and **dashboard/report generation**.

Main entry points: [`data/processed/master_cliodynamics_v2.csv`](https://github.com/asterbons/cliodynamics/blob/master/data/processed/master_cliodynamics_v2.csv) (Master v2), [`data/raw`](https://github.com/asterbons/cliodynamics/tree/master/data/raw) (Raw data), [`src/loaders/loader_v4.py`](https://github.com/asterbons/cliodynamics/blob/master/src/loaders/loader_v4.py) (Loader v4), [`src/preprocessors/preprocessor_v4.py`](https://github.com/asterbons/cliodynamics/blob/master/src/preprocessors/preprocessor_v4.py) (Preprocessor v4)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_external["External data"]
  node_destatis(("Destatis<br/>source"))
  node_wsi(("WSI strikes<br/>source"))
  node_trends(("Google Trends<br/>source"))
end

subgraph group_ingestion["Ingestion"]
  node_loader_v4["Loader v4<br/>ingest pipeline<br/>[loader_v4.py]"]
  node_trends_loader["Trends loader<br/>ingest script<br/>[fetch_trends.py]"]
  node_students_loader["Students loader<br/>ingest script<br/>[load_students.py]"]
  node_wages_loader["Wages loader<br/>ingest script"]
  node_table_search["Table search<br/>ingest helper<br/>[table_search.py]"]
  node_raw_zone[("Raw data<br/>filesystem zone")]
end

subgraph group_prep["Preprocessing"]
  node_pre_v4["Preprocessor v4<br/>transform pipeline<br/>[preprocessor_v4.py]"]
  node_pre_legacy["Legacy prep<br/>transform scripts<br/>[preprocessor.py]"]
  node_afterprep["After prep<br/>postprocess"]
  node_processed_zone[("Processed data<br/>filesystem zone")]
  node_master_v2["Master v2<br/>master dataset"]
end

subgraph group_analysis["Analysis"]
  node_merge_trends["Merge trends<br/>analysis bridge"]
  node_wealth_pump["Wealth pump<br/>indicator model"]
  node_seasonal["Seasonal decomp<br/>timeseries analysis"]
  node_viz_v2["Visualizer v2<br/>reporting<br/>[visualizer_v2.py]"]
  node_viz_v4["Viz v4<br/>reporting<br/>[visualize_v4.py]"]
end

subgraph group_deliver["Delivery"]
  node_dashboard["PSI dashboard<br/>artifact"]
  node_output_zone[("Output<br/>filesystem zone")]
end

node_destatis -->|"feeds"| node_loader_v4
node_wsi -->|"feeds"| node_loader_v4
node_trends -->|"feeds"| node_trends_loader
node_loader_v4 -->|"writes"| node_raw_zone
node_trends_loader -->|"writes"| node_raw_zone
node_students_loader -->|"writes"| node_raw_zone
node_wages_loader -->|"writes"| node_raw_zone
node_table_search -.->|"supports"| node_loader_v4
node_raw_zone -->|"reads"| node_pre_v4
node_pre_legacy -.->|"evolves"| node_pre_v4
node_afterprep -.->|"extends"| node_pre_v4
node_pre_v4 -->|"writes"| node_processed_zone
node_processed_zone -->|"contains"| node_master_v2
node_master_v2 -->|"reads"| node_merge_trends
node_master_v2 -->|"reads"| node_wealth_pump
node_master_v2 -->|"reads"| node_seasonal
node_master_v2 -->|"reads"| node_viz_v2
node_master_v2 -->|"reads"| node_viz_v4
node_merge_trends -->|"contributes"| node_dashboard
node_viz_v2 -->|"renders"| node_dashboard
node_viz_v4 -->|"renders"| node_dashboard
node_dashboard -->|"stored in"| node_output_zone

click node_loader_v4 "https://github.com/asterbons/cliodynamics/blob/master/src/loaders/loader_v4.py"
click node_trends_loader "https://github.com/asterbons/cliodynamics/blob/master/src/loaders/fetch_trends.py"
click node_students_loader "https://github.com/asterbons/cliodynamics/blob/master/src/loaders/load_students.py"
click node_wages_loader "https://github.com/asterbons/cliodynamics/blob/master/src/loaders/load_rent_and_wages.py"
click node_table_search "https://github.com/asterbons/cliodynamics/blob/master/src/loaders/table_search.py"
click node_raw_zone "https://github.com/asterbons/cliodynamics/tree/master/data/raw"
click node_pre_v4 "https://github.com/asterbons/cliodynamics/blob/master/src/preprocessors/preprocessor_v4.py"
click node_pre_legacy "https://github.com/asterbons/cliodynamics/blob/master/src/preprocessors/preprocessor.py"
click node_afterprep "https://github.com/asterbons/cliodynamics/blob/master/src/preprocessors/afterpreprocessor.py"
click node_processed_zone "https://github.com/asterbons/cliodynamics/tree/master/data/processed"
click node_master_v2 "https://github.com/asterbons/cliodynamics/blob/master/data/processed/master_cliodynamics_v2.csv"
click node_merge_trends "https://github.com/asterbons/cliodynamics/blob/master/src/analysis/process_and_merge_trends.py"
click node_wealth_pump "https://github.com/asterbons/cliodynamics/blob/master/src/analysis/Wealth_Pump_Index.py"
click node_seasonal "https://github.com/asterbons/cliodynamics/blob/master/src/analysis/Seasonal_Decomposition.py"
click node_viz_v2 "https://github.com/asterbons/cliodynamics/blob/master/src/analysis/visualizer_v2.py"
click node_viz_v4 "https://github.com/asterbons/cliodynamics/blob/master/src/preprocessors/visualize_v4.py"
click node_dashboard "https://github.com/asterbons/cliodynamics/blob/master/output/psi_v4_dashboard.html"
click node_output_zone "https://github.com/asterbons/cliodynamics/tree/master/output"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_destatis,node_wsi,node_trends toneBlue
class node_loader_v4,node_trends_loader,node_students_loader,node_wages_loader,node_table_search,node_raw_zone toneAmber
class node_pre_v4,node_pre_legacy,node_afterprep,node_processed_zone,node_master_v2 toneMint
class node_merge_trends,node_wealth_pump,node_seasonal,node_viz_v2,node_viz_v4 toneRose
class node_dashboard,node_output_zone toneIndigo
```

## Components

### External data

Public statistics and alternative signals used as inputs

- **Destatis**: source · German official statistics / GENESIS data source
- **WSI strikes**: source · Strike and unrest proxy data source
- **Google Trends**: source · Mobilization signal from search trends

### Ingestion

Loaders that fetch and stage source data

- [Loader v4](https://github.com/asterbons/cliodynamics/blob/master/src/loaders/loader_v4.py): ingest pipeline · Main batch entrypoint for acquiring raw indicators · `src/loaders/loader_v4.py`
- [Trends loader](https://github.com/asterbons/cliodynamics/blob/master/src/loaders/fetch_trends.py): ingest script · Fetches and stages Google Trends data · `src/loaders/fetch_trends.py`
- [Students loader](https://github.com/asterbons/cliodynamics/blob/master/src/loaders/load_students.py): ingest script · Loads education-related source series · `src/loaders/load_students.py`
- [Wages loader](https://github.com/asterbons/cliodynamics/blob/master/src/loaders/load_rent_and_wages.py): ingest script · Loads rent and wage indicators · `src/loaders/load_rent_and_wages.py`
- [Table search](https://github.com/asterbons/cliodynamics/blob/master/src/loaders/table_search.py): ingest helper · Source lookup / retrieval helper for tabular data · `src/loaders/table_search.py`
- [Raw data](https://github.com/asterbons/cliodynamics/tree/master/data/raw): filesystem zone · Staging area for source CSV extracts · `data/raw`

### Preprocessing

Cleaning, alignment, and master dataset assembly

- [Preprocessor v4](https://github.com/asterbons/cliodynamics/blob/master/src/preprocessors/preprocessor_v4.py): transform pipeline · Canonical cleaning, alignment, and merge pipeline · `src/preprocessors/preprocessor_v4.py`
- [Legacy prep](https://github.com/asterbons/cliodynamics/blob/master/src/preprocessors/preprocessor.py): transform scripts · Earlier or supplementary preprocessing variants · `src/preprocessors/preprocessor.py`
- [After prep](https://github.com/asterbons/cliodynamics/blob/master/src/preprocessors/afterpreprocessor.py): postprocess · Follow-up transformation stage for prepared data · `src/preprocessors/afterpreprocessor.py`
- [Processed data](https://github.com/asterbons/cliodynamics/tree/master/data/processed): filesystem zone · Merged analytical datasets and master CSVs · `data/processed`
- [Master v2](https://github.com/asterbons/cliodynamics/blob/master/data/processed/master_cliodynamics_v2.csv): master dataset · Primary processed handoff for analysis · `data/processed/master_cliodynamics_v2.csv`

### Analysis

Indicator engineering and time-series routines

- [Merge trends](https://github.com/asterbons/cliodynamics/blob/master/src/analysis/process_and_merge_trends.py): analysis bridge · Integrates Google Trends into the main dataset · `src/analysis/process_and_merge_trends.py`
- [Wealth pump](https://github.com/asterbons/cliodynamics/blob/master/src/analysis/Wealth_Pump_Index.py): indicator model · Computes a core PSI component / derived indicator · `src/analysis/Wealth_Pump_Index.py`
- [Seasonal decomp](https://github.com/asterbons/cliodynamics/blob/master/src/analysis/Seasonal_Decomposition.py): timeseries analysis · Decomposes seasonality in indicators · `src/analysis/Seasonal_Decomposition.py`
- [Visualizer v2](https://github.com/asterbons/cliodynamics/blob/master/src/analysis/visualizer_v2.py): reporting · Creates analytical plots and presentation views · `src/analysis/visualizer_v2.py`
- [Viz v4](https://github.com/asterbons/cliodynamics/blob/master/src/preprocessors/visualize_v4.py): reporting · Preprocessor-side visualization for the current pipeline · `src/preprocessors/visualize_v4.py`

### Delivery

Generated artifacts and reports

- [PSI dashboard](https://github.com/asterbons/cliodynamics/blob/master/output/psi_v4_dashboard.html): artifact · Interactive Plotly dashboard output · `output/psi_v4_dashboard.html`
- [Output](https://github.com/asterbons/cliodynamics/tree/master/output): filesystem zone · Final generated artifacts and figures · `output`

## Connections

- Destatis → Loader v4: feeds · Official statistics are fetched through the main loader
- WSI strikes → Loader v4: feeds · Strike data is acquired as a source input
- Google Trends → Trends loader: feeds · Search trend signal is fetched separately
- Loader v4 → Raw data: writes · Loader stages source CSVs into the raw data zone
- Trends loader → Raw data: writes · Trend extracts are stored with other raw inputs
- Students loader → Raw data: writes · Education series are staged as raw extracts
- Wages loader → Raw data: writes · Wage and rent series are staged as raw extracts
- Table search → Loader v4: supports · Helper supports source retrieval and table lookup
- Raw data → Preprocessor v4: reads · Preprocessing consumes raw source CSVs
- Legacy prep → Preprocessor v4: evolves · Older preprocessing variants inform the canonical pipeline
- After prep → Preprocessor v4: extends · Follow-up transformation stage complements preprocessing
- Preprocessor v4 → Processed data: writes · Canonical pipeline emits processed datasets
- Processed data → Master v2: contains · Processed zone contains the master cliodynamics dataset
- Master v2 → Merge trends: reads · Analysis bridge augments the master dataset with trends
- Master v2 → Wealth pump: reads · Indicator model operates on the processed master data
- Master v2 → Seasonal decomp: reads · Time-series decomposition runs on processed indicators
- Master v2 → Visualizer v2: reads · Visualizer consumes the processed analytical dataset
- Master v2 → Viz v4: reads · Current visualization path is driven from the processed master dataset
- Merge trends → PSI dashboard: contributes · Trend-augmented analysis feeds the final dashboard
- Visualizer v2 → PSI dashboard: renders · Visualization scripts generate presentation artifacts
- Viz v4 → PSI dashboard: renders · Current visualization pipeline renders the dashboard output
- PSI dashboard → Output: stored in · Interactive dashboard artifact is written to the output zone
