How im-kozhandha/groundwater-risk-assessment works
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: risk_predictor.py Predictor · groundwater_data_cleaned.csv Cleaned data · spatial_index_model.joblib Spatial outputs · 06_system_integration_DSS.ipynb System integration
Components 16
Offline pipeline
Data cleaning, labeling, training, and artifact generation.
- Raw data · Incoming groundwater datasets before cleanup.
- Preprocess · Cleans groundwater data and normalizes domain-specific issues.
preprocessing/preprocess_groundwater_data.py - Cleaned data · Canonical cleaned groundwater datasets used for analysis and training.
data/cleaned/groundwater_data_cleaned.csv - EDA · Exploratory analysis and data understanding.
notebooks/01_eda.ipynb - Risk labels · Derives BIS IS 10500:2012 risk labels from chemistry data.
notebooks/03_risk_label_generation.ipynb - Model training · Trains the risk classifier and validates performance.
notebooks/05_model_training.ipynb - Spatial confidence · Builds spatial estimation confidence and interpolation logic.
notebooks/04_spatial_estimation_confidence.ipynb - System 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 · Loads artifacts and arbitrates between ML and spatial predictions.
deploy/risk_predictor.py - FastAPI app · Exposes health, standards, and prediction endpoints.
deploy/app.py - Streamlit UI · User-facing interface that calls the backend API.
frontend.py
Artifacts
Serialized models, spatial references, and generated outputs.
- ML artifacts · Classifier, scaler, and label encoder used at inference time.
models/ml/best_risk_classifier.joblib - Spatial reference · Reference points used by the spatial estimator.
models/spatial/spatial_reference_data.csv - Spatial outputs · Spatial prediction artifacts and configuration generated offline.
outputs/spatial_predictions/spatial_index_model.joblib - Reports · Metrics, summaries, feature importance, and integration reports.
outputs/ml/model_evaluation_metrics.json
Other components
- Spatial model · Spatial inference utilities for location-based risk estimation.
spatial/spatial_models.py
Connections 20
- 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.