# im-kozhandha/groundwater-risk-assessment architecture

> Architecture diagram of the GitHub repository im-kozhandha/groundwater-risk-assessment, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/im-kozhandha/groundwater-risk-assessment
- Repository: https://github.com/im-kozhandha/groundwater-risk-assessment

## Overview

1. **What this system is**
   - This repo is a groundwater risk decision support system, centered on classifying drinking-water risk under BIS IS 10500:2012.
   - It is not just a model repo: it has an offline data/model-building pipeline and an online inference stack with a FastAPI backend and a Streamlit UI.

2. **Two major subsystems: training/offline vs serving/online**
   - The offline side cleans source groundwater data, derives labels, trains the ML classifier, and builds spatial estimation artifacts.
   - The online side loads serialized artifacts and exposes prediction services through API endpoints consumed by the frontend.

3. **Data ingestion and preprocessing boundary**
   - Raw groundwater data is cleaned in `preprocessing/preprocess_groundwater_data.py`.
   - The cleaned datasets in `data/cleaned/` are the canonical inputs for analysis and training.
   - The preprocessing step appears to handle domain-specific cleanup such as repeated headers and coordinate corrections, which is important because it determines the quality of both ML and spatial inference.

4. **Exploration, labeling, and model development**
   - The `notebooks/` folder contains the research-to-model pipeline: EDA, standardization, risk label generation, spatial confidence estimation, model training, and system integration.
   - This is where the domain logic is turned into trainable features and risk labels before artifacts are serialized for deployment.

5. **Machine learning inference path**
   - The ML model lives in `models/ml/` as serialized Joblib artifacts: classifier, feature scaler, and label encoder.
   - At runtime, the backend loads these artifacts to classify risk from chemistry inputs only.
   - This path is the main “lab results available” route and likely uses the same feature schema produced during preprocessing/training.

6. **Spatial estimation path**
   - Spatial logic is separated into `spatial/spatial_models.py` and deployment artifacts under `outputs/spatial_predictions/`.
   - The repo uses a nearest-neighbor / IDW-style spatial interpolation with confidence scoring for location-only queries.
   - The presence of `spatial_reference_data.csv` indicates the spatial model depends on reference points, not just live request data.

7. **Hybrid decision logic**
   - The system’s key architectural feature is hybrid inference: it can use chemistry alone, location alone, or both.
   - The backend chooses the most confident source-aware prediction, rather than always forcing one model path.
   - This means the API is not a thin wrapper around a single classifier; it is a decision layer combining ML risk, spatial risk, and confidence metadata.

8. **Serving layer and API contract**
   - `deploy/app.py` is the FastAPI application exposing `/health`, `/standards`, and `/predict`.
   - `deploy/risk_predictor.py` likely encapsulates model loading and inference orchestration, keeping API routing separate from prediction logic.
   - The API is the boundary between serialized model artifacts and client applications, and it returns both prediction and explanation-oriented data.

9. **Frontend boundary and user interaction**
   - `frontend.py` is a Streamlit application that calls the backend API rather than running model inference directly.
   - It supports the three input modes described in the README: ML-only, spatial-only, and hybrid.
   - This separation means UI rendering, user input handling, and prediction logic are decoupled.

10. **Artifacts, outputs, and reproducibility**
   - `outputs/` stores generated evaluation reports, feature importance, spatial prediction results, confidence configuration, and system integration metadata.
   - These files act as traceable evidence of the offline pipeline and likely support debugging, reporting, and demonstration.
   - `models/` and `outputs/spatial_predictions/` are the deployment-critical artifact boundaries that must be present wherever the API runs.

11. **External/runtime characteristics that affect architecture**
   - The stack is Python-based, with FastAPI for the service layer, Streamlit for the UI, XGBoost/scikit-learn for ML, and Joblib for serialization.
   - The backend is started with Uvicorn; the frontend is a separate Streamlit process, so deployment is naturally split into two runtimes even if they run locally together.
   - The README notes a hardcoded API URL in the frontend, which means environment/config management is currently part of the integration boundary.

12. **How to draw the diagram**
   - A good architecture diagram should show: raw CGWB data → preprocessing → cleaned data → notebook-driven training → serialized ML/spatial artifacts → FastAPI inference service → Streamlit UI.
   - Show the spatial reference dataset as a side input to the spatial estimator, and show hybrid logic inside the backend that arbitrates between ML and spatial predictions.
   - Keep outputs/reports as a downstream artifact store off the training path, not part of the live request path.

Main entry points: [`deploy/risk_predictor.py`](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/deploy/risk_predictor.py) (Predictor), [`data/cleaned/groundwater_data_cleaned.csv`](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/data/cleaned/groundwater_data_cleaned.csv) (Cleaned data), [`outputs/spatial_predictions/spatial_index_model.joblib`](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/outputs/spatial_predictions/spatial_index_model.joblib) (Spatial outputs), [`notebooks/06_system_integration_DSS.ipynb`](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/06_system_integration_DSS.ipynb) (System integration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_offline["Offline pipeline"]
  node_raw_data[("Raw data<br/>source groundwater data")]
  node_preprocess["Preprocess<br/>data cleaning"]
  node_clean_data[("Cleaned data<br/>training input")]
  node_eda["EDA<br/>notebook analysis<br/>[01_eda.ipynb]"]
  node_labeling["Risk labels<br/>label generation"]
  node_training["Model training<br/>ML training"]
  node_spatial_conf["Spatial confidence<br/>spatial calibration"]
  node_integration["System integration<br/>pipeline integration"]
end

subgraph group_serving["Serving stack"]
  node_risk_predictor["Predictor<br/>inference orchestration<br/>[risk_predictor.py]"]
  node_api["FastAPI app<br/>HTTP service<br/>[app.py]"]
  node_frontend["Streamlit UI<br/>client app<br/>[frontend.py]"]
end

subgraph group_artifacts["Artifacts"]
  node_ml_artifacts[("ML artifacts<br/>serialized model bundle")]
  node_spatial_ref[("Spatial reference<br/>reference dataset")]
  node_spatial_artifacts[("Spatial outputs<br/>spatial model outputs")]
  node_reports["Reports<br/>evaluation outputs"]
end

node_spatial_model["Spatial model<br/>IDW / nearest-neighbor<br/>[spatial_models.py]"]

node_raw_data -->|"clean"| node_preprocess
node_preprocess -->|"publish"| node_clean_data
node_clean_data -->|"analyze"| node_eda
node_clean_data -->|"label"| node_labeling
node_labeling -->|"train on"| node_training
node_training -->|"serialize"| node_ml_artifacts
node_clean_data -->|"calibrate"| node_spatial_conf
node_spatial_conf -->|"generate"| node_spatial_artifacts
node_spatial_ref -->|"reference"| node_spatial_model
node_spatial_model -->|"emit"| node_spatial_artifacts
node_training -->|"report"| node_reports
node_integration -->|"document"| node_reports
node_integration -->|"bundle"| node_ml_artifacts
node_integration -->|"bundle"| node_spatial_artifacts
node_ml_artifacts -->|"load"| node_risk_predictor
node_spatial_artifacts -->|"load"| node_risk_predictor
node_spatial_model -->|"assist"| node_risk_predictor
node_risk_predictor -->|"serve"| node_api
node_api -->|"consume"| node_frontend
node_api -->|"infer"| node_risk_predictor

click node_preprocess "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/preprocessing/preprocess_groundwater_data.py"
click node_clean_data "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/data/cleaned/groundwater_data_cleaned.csv"
click node_eda "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/01_eda.ipynb"
click node_labeling "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/03_risk_label_generation.ipynb"
click node_training "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/05_model_training.ipynb"
click node_spatial_conf "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/04_spatial_estimation_confidence.ipynb"
click node_integration "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/06_system_integration_DSS.ipynb"
click node_ml_artifacts "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/models/ml/best_risk_classifier.joblib"
click node_spatial_ref "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/models/spatial/spatial_reference_data.csv"
click node_spatial_artifacts "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/outputs/spatial_predictions/spatial_index_model.joblib"
click node_reports "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/outputs/ml/model_evaluation_metrics.json"
click node_spatial_model "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/spatial/spatial_models.py"
click node_risk_predictor "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/deploy/risk_predictor.py"
click node_api "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/deploy/app.py"
click node_frontend "https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/frontend.py"

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_raw_data,node_preprocess,node_clean_data,node_eda,node_labeling,node_training,node_spatial_conf,node_integration toneBlue
class node_risk_predictor,node_api,node_frontend toneAmber
class node_ml_artifacts,node_spatial_ref,node_spatial_artifacts,node_reports toneMint
class node_spatial_model toneNeutral
```

## Components

### Offline pipeline

Data cleaning, labeling, training, and artifact generation.

- **Raw data**: source groundwater data · Incoming groundwater datasets before cleanup.
- [Preprocess](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/preprocessing/preprocess_groundwater_data.py): data cleaning · Cleans groundwater data and normalizes domain-specific issues. · `preprocessing/preprocess_groundwater_data.py`
- [Cleaned data](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/data/cleaned/groundwater_data_cleaned.csv): training input · Canonical cleaned groundwater datasets used for analysis and training. · `data/cleaned/groundwater_data_cleaned.csv`
- [EDA](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/01_eda.ipynb): notebook analysis · Exploratory analysis and data understanding. · `notebooks/01_eda.ipynb`
- [Risk labels](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/03_risk_label_generation.ipynb): label generation · Derives BIS IS 10500:2012 risk labels from chemistry data. · `notebooks/03_risk_label_generation.ipynb`
- [Model training](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/05_model_training.ipynb): ML training · Trains the risk classifier and validates performance. · `notebooks/05_model_training.ipynb`
- [Spatial confidence](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/04_spatial_estimation_confidence.ipynb): spatial calibration · Builds spatial estimation confidence and interpolation logic. · `notebooks/04_spatial_estimation_confidence.ipynb`
- [System integration](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/notebooks/06_system_integration_DSS.ipynb): pipeline integration · Connects ML, spatial, and decision logic into a unified system. · `notebooks/06_system_integration_DSS.ipynb`

### Serving stack

API and UI that load serialized artifacts for inference.

- [Predictor](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/deploy/risk_predictor.py): inference orchestration · Loads artifacts and arbitrates between ML and spatial predictions. · `deploy/risk_predictor.py`
- [FastAPI app](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/deploy/app.py): HTTP service · Exposes health, standards, and prediction endpoints. · `deploy/app.py`
- [Streamlit UI](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/frontend.py): client app · User-facing interface that calls the backend API. · `frontend.py`

### Artifacts

Serialized models, spatial references, and generated outputs.

- [ML artifacts](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/models/ml/best_risk_classifier.joblib): serialized model bundle · Classifier, scaler, and label encoder used at inference time. · `models/ml/best_risk_classifier.joblib`
- [Spatial reference](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/models/spatial/spatial_reference_data.csv): reference dataset · Reference points used by the spatial estimator. · `models/spatial/spatial_reference_data.csv`
- [Spatial outputs](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/outputs/spatial_predictions/spatial_index_model.joblib): spatial model outputs · Spatial prediction artifacts and configuration generated offline. · `outputs/spatial_predictions/spatial_index_model.joblib`
- [Reports](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/outputs/ml/model_evaluation_metrics.json): evaluation outputs · Metrics, summaries, feature importance, and integration reports. · `outputs/ml/model_evaluation_metrics.json`

### Other components

- [Spatial model](https://github.com/im-kozhandha/groundwater-risk-assessment/blob/main/spatial/spatial_models.py): IDW / nearest-neighbor · Spatial inference utilities for location-based risk estimation. · `spatial/spatial_models.py`

## Connections

- Raw data → Preprocess: clean · Raw groundwater data is normalized and corrected.
- Preprocess → Cleaned data: publish · Preprocessing produces canonical cleaned datasets.
- Cleaned data → EDA: analyze · Cleaned data feeds exploration and domain inspection.
- Cleaned data → Risk labels: label · Risk labels are derived from standardized cleaned data.
- Risk labels → Model training: train on · Labels and features feed the classifier training pipeline.
- Model training → ML artifacts: serialize · Trained classifier components are saved for deployment.
- Cleaned data → Spatial confidence: calibrate · Cleaned data supports spatial confidence estimation.
- Spatial confidence → Spatial outputs: generate · Spatial estimation artifacts and configuration are produced.
- Spatial reference → Spatial model: reference · Spatial inference depends on stored reference points.
- Spatial model → Spatial outputs: emit · Spatial model logic produces prediction artifacts.
- Model training → Reports: report · Model evaluation outputs are stored for traceability.
- System integration → Reports: document · System integration produces summary statistics and decisions.
- System integration → ML artifacts: bundle · Integration uses the trained ML bundle as a deployable input.
- System integration → Spatial outputs: bundle · Integration uses spatial artifacts as a deployable input.
- ML artifacts → Predictor: load · The predictor loads the serialized classifier stack.
- Spatial outputs → Predictor: load · The predictor loads spatial prediction artifacts.
- Spatial model → Predictor: assist · Spatial estimation logic is used inside inference orchestration.
- Predictor → FastAPI app: serve · The predictor powers the FastAPI endpoints.
- FastAPI app → Streamlit UI: consume · The Streamlit UI calls the backend prediction API.
- FastAPI app → Predictor: infer · Requests are routed through hybrid prediction logic.
