# yaswinkumar02/neuroscan-ai architecture

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

- Interactive diagram: https://gitdiagram.com/yaswinkumar02/neuroscan-ai
- Repository: https://github.com/yaswinkumar02/neuroscan-ai

## Overview

#### Purpose and Runtime Entry Point
NeuroScan AI is a local Streamlit clinical workstation for brain-MRI intake, tumor classification/segmentation, volumetric analysis, report-oriented clinical workflows, and role-specific access. Launch via `python -m streamlit run app.py`; the application entry point is `app.py`. Python/PyTorch provide model execution, with optional CUDA acceleration.

#### UI Orchestration and Role Boundaries
`app.py` is the primary controller for authentication, clinician diagnostic workflows, patient read-only history, and administrator management views. It coordinates inference, persistence, RAG, object storage, and visualization rather than exposing a separate web/API service. Streamlit presentation configuration resides in `.streamlit/config.toml`.

#### Authentication, RBAC, and Audit Persistence
`database.py` owns the relational persistence boundary: user credentials, roles, patient/scan records, and audit-oriented operations. The deployed local artifact is `neuroscan.db`; the README indicates SQLite and MySQL support. Password handling uses bcrypt with legacy-verification/migration behavior, while role isolation separates clinician, patient, and administrator actions.

#### MRI Intake Guardrail and Classification
Uploaded images first pass a medical-image validity gate implemented/trained through `guardrail_train.py`, with inference weights in `models/best_guardrail.pth`. Accepted scans proceed to the EfficientNet-B0 classifier checkpoint at `models/best_efficientnet_b0.pth`, producing one of Glioma, Meningioma, Pituitary, or No Tumor. `convert_to_grayscale.py` supports image normalization/preparation consistent with MRI-oriented processing.

#### Lesion Segmentation and Explainability
The segmentation stage produces pixel-level tumor masks after classification-oriented intake. Its U-Net/EfficientNet-B0 model artifact is present both at `models/best_unet_effb0.pth` and `seg_models/best_unet_effb0.pth`; `seg_train.py` is the training pipeline. The UI is expected to consume mask outputs for lesion area, overlays, and downstream volumetry; classifier training is defined in `train.py`.

#### Volumetric Reconstruction
`volume_engine.py` is the 2D-mask-to-3D analysis boundary. It applies Marching Cubes to multi-slice MRI/mask data to create brain/tumor surface meshes, compute tumor volume, and derive penetration/depth-style metrics for interactive visualization. This stage depends on ordered/co-registered scan slices and should distinguish inference masks from final clinical claims.

#### Clinical Knowledge and External AI
`rag_engine.py` provides local retrieval over markdown clinical material in `knowledge_base/`, including `knowledge_base/glioma.md`, `knowledge_base/meningioma.md`, and `knowledge_base/general_patient_faq.md`. Retrieved context grounds assistant responses; the README identifies Google Gemini as the multimodal generation provider, creating an external-service/privacy boundary for scan or question content.

#### Storage, Model Assets, and Operations
`s3_storage.py` encapsulates optional S3-backed object storage for scan-related artifacts, separating binary uploads/exports from relational metadata in `database.py`. Versioned model checkpoints and metrics live under `models/` and `seg_models/`. `verify.py` validates runtime environment/CUDA readiness before operating or training the workstation.

Main entry points: [`app.py`](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/app.py) (Streamlit controller), [`guardrail_train.py`](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/guardrail_train.py) (MRI validity gate), [`models/best_efficientnet_b0.pth`](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/models/best_efficientnet_b0.pth) (Tumor classifier), [`models/best_unet_effb0.pth`](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/models/best_unet_effb0.pth) (Lesion segmentation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Clinical Workstation"]
  node_app["Streamlit controller<br/>Python entry point<br/>[app.py]"]
  node_streamlit_config["Streamlit configuration<br/>presentation config<br/>[config.toml]"]
end

subgraph group_ml["MRI Analysis"]
  node_guardrail["MRI validity gate<br/>image guardrail<br/>[guardrail_train.py]"]
  node_guardrail_weights["Guardrail weights<br/>PyTorch checkpoint<br/>[best_guardrail.pth]"]
  node_classifier["Tumor classifier<br/>EfficientNet-B0 inference"]
  node_classifier_training["Classifier training<br/>training pipeline<br/>[train.py]"]
  node_normalization["MRI normalization<br/>image preparation"]
  node_segmentation["Lesion segmentation<br/>U-Net inference"]
  node_segmentation_training["Segmentation training<br/>training pipeline<br/>[seg_train.py]"]
  node_volume_engine["Volumetric reconstruction<br/>3D analysis engine<br/>[volume_engine.py]"]
  node_runtime_verify["Runtime verification<br/>environment validation<br/>[verify.py]"]
end

subgraph group_data["Data &amp; Knowledge"]
  node_database[("Clinical persistence<br/>database boundary<br/>[database.py]")]
  node_sqlite[("Local clinical database<br/>SQLite artifact<br/>[neuroscan.db]")]
  node_rag["Clinical retrieval<br/>local RAG engine<br/>[rag_engine.py]"]
  node_knowledge_base["Clinical knowledge base<br/>markdown corpus"]
end

subgraph group_external["Optional External Services"]
  node_object_storage["Artifact storage adapter<br/>S3 storage boundary<br/>[s3_storage.py]"]
  node_gemini{{"Google Gemini<br/>multimodal AI provider"}}
end

node_streamlit_config -->|"configures"| node_app
node_app -->|"auth, RBAC, records"| node_database
node_database -->|"persists to"| node_sqlite
node_app -->|"prepares uploads"| node_normalization
node_normalization -->|"normalized image"| node_guardrail
node_guardrail_weights -->|"loads"| node_guardrail
node_guardrail -->|"accepted scan"| node_classifier
node_classifier_training -.->|"produces checkpoint"| node_classifier
node_classifier -->|"diagnostic intake"| node_segmentation
node_segmentation_training -.->|"produces checkpoint"| node_segmentation
node_segmentation -->|"ordered masks"| node_volume_engine
node_volume_engine -->|"meshes and metrics"| node_app
node_app -->|"clinical question"| node_rag
node_knowledge_base -->|"retrieval corpus"| node_rag
node_rag -->|"grounded context"| node_gemini
node_app -->|"scan artifacts and exports"| node_object_storage
node_runtime_verify -.->|"validates runtime"| node_app

click node_app "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/app.py"
click node_streamlit_config "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/.streamlit/config.toml"
click node_database "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/database.py"
click node_sqlite "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/neuroscan.db"
click node_guardrail "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/guardrail_train.py"
click node_guardrail_weights "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/models/best_guardrail.pth"
click node_classifier "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/models/best_efficientnet_b0.pth"
click node_classifier_training "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/train.py"
click node_normalization "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/convert_to_grayscale.py"
click node_segmentation "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/models/best_unet_effb0.pth"
click node_segmentation_training "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/seg_train.py"
click node_volume_engine "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/volume_engine.py"
click node_rag "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/rag_engine.py"
click node_object_storage "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/s3_storage.py"
click node_runtime_verify "https://github.com/yaswinkumar02/neuroscan-ai/blob/main/verify.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_app,node_streamlit_config toneBlue
class node_guardrail,node_guardrail_weights,node_classifier,node_classifier_training,node_normalization,node_segmentation,node_segmentation_training,node_volume_engine,node_runtime_verify toneAmber
class node_database,node_sqlite,node_rag,node_knowledge_base toneMint
class node_object_storage,node_gemini toneRose
```

## Components

### Clinical Workstation

- [Streamlit controller](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/app.py): Python entry point · Coordinates role-specific clinical workflows. · `app.py`
- [Streamlit configuration](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/.streamlit/config.toml): presentation config · `.streamlit/config.toml`

### MRI Analysis

- [MRI validity gate](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/guardrail_train.py): image guardrail · Rejects invalid intake images before diagnostic analysis. · `guardrail_train.py`
- [Guardrail weights](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/models/best_guardrail.pth): PyTorch checkpoint · `models/best_guardrail.pth`
- [Tumor classifier](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/models/best_efficientnet_b0.pth): EfficientNet-B0 inference · Classifies Glioma, Meningioma, Pituitary, or No Tumor. · `models/best_efficientnet_b0.pth`
- [Classifier training](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/train.py): training pipeline · `train.py`
- [MRI normalization](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/convert_to_grayscale.py): image preparation · `convert_to_grayscale.py`
- [Lesion segmentation](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/models/best_unet_effb0.pth): U-Net inference · Produces pixel-level tumor masks and overlays. · `models/best_unet_effb0.pth`
- [Segmentation training](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/seg_train.py): training pipeline · `seg_train.py`
- [Volumetric reconstruction](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/volume_engine.py): 3D analysis engine · Builds meshes and calculates tumor volume from ordered masks. · `volume_engine.py`
- [Runtime verification](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/verify.py): environment validation · Checks runtime and CUDA readiness. · `verify.py`

### Data & Knowledge

- [Clinical persistence](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/database.py): database boundary · Manages credentials, roles, scan records, and audit operations. · `database.py`
- [Local clinical database](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/neuroscan.db): SQLite artifact · `neuroscan.db`
- [Clinical retrieval](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/rag_engine.py): local RAG engine · Retrieves grounded clinical context for assistant responses. · `rag_engine.py`
- **Clinical knowledge base**: markdown corpus

### Optional External Services

- [Artifact storage adapter](https://github.com/yaswinkumar02/neuroscan-ai/blob/main/s3_storage.py): S3 storage boundary · Separates scan binaries and exports from relational metadata. · `s3_storage.py`
- **Google Gemini**: multimodal AI provider · External privacy boundary for scan or question content.

## Connections

- Streamlit configuration → Streamlit controller: configures
- Streamlit controller → Clinical persistence: auth, RBAC, records
- Clinical persistence → Local clinical database: persists to
- Streamlit controller → MRI normalization: prepares uploads
- MRI normalization → MRI validity gate: normalized image
- Guardrail weights → MRI validity gate: loads
- MRI validity gate → Tumor classifier: accepted scan
- Classifier training → Tumor classifier: produces checkpoint
- Tumor classifier → Lesion segmentation: diagnostic intake
- Segmentation training → Lesion segmentation: produces checkpoint
- Lesion segmentation → Volumetric reconstruction: ordered masks
- Volumetric reconstruction → Streamlit controller: meshes and metrics
- Streamlit controller → Clinical retrieval: clinical question
- Clinical knowledge base → Clinical retrieval: retrieval corpus
- Clinical retrieval → Google Gemini: grounded context
- Streamlit controller → Artifact storage adapter: scan artifacts and exports
- Runtime verification → Streamlit controller: validates runtime
