# bowang-lab/medrax architecture

> Architecture diagram of the GitHub repository bowang-lab/medrax, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bowang-lab/medrax
- Repository: https://github.com/bowang-lab/medrax

## Overview

##### Project Type and Purpose
- MedRAX is a hybrid application that integrates a large language model (GPT-4o with vision) with various medical imaging analysis tools (e.g., classification, segmentation, report generation) to process and interpret chest X-rays.
- Its purpose is to serve as a unified medical reasoning platform, combining state-of-the-art visual processing (e.g., CheXagent, LLaVA-Med) with a language model to answer complex clinical queries.
- Notice the integration of LangChain and LangGraph frameworks, which also suggests a modular design pattern where individual components (tools) can be added or removed.

##### Analysis of File Structure
- Top-level files (e.g., main.py, interface.py, quickstart.py) and configuration files (pyproject.toml, .gitignore, LICENSE) indicate entry points for running the service.
- The “medrax” directory is the core of the project and contains several key submodules:
- medrax/agent: Contains the agent logic that initializes the system with a selection of tools.
- medrax/llava: Houses code related to multimodal (text and image) reasoning, including evaluation scripts and model building for both language and vision components.
- medrax/tools: Contains individual tool implementations (e.g., classification, segmentation, ground truth processing) each handling specific functionalities for chest X-ray analysis.
- medrax/utils and medrax/docs: Provide utilities and documentation that support the core functionality.
- Supporting directories like “experiments” and “benchmark” contain scripts for evaluation (e.g., running benchmarks such as ChestAgentBench and analyzing logs), which support performance evaluation and research reproducibility.
- The “demo” and “assets” directories supply sample data and media for demonstrating the system's capabilities.

a. Main Components to Represent:
- User Interface Layer: The Gradio-based interface which is invoked by main.py and interface.py.
- Agent Core: Located in medrax/agent, it is responsible for initializing the system, selecting appropriate tools, and delegating tasks.
- Language Model Integration: The integration of GPT-4o (or another LLM) as the backbone that processes medical queries using LangChain and LangGraph.
- Tools/Microservices Layer: Each specialized tool from medrax/tools (e.g., ChestXRayClassifierTool, ChestXRaySegmentationTool, Visual QA, etc.) should be depicted as distinct components.
- Model Download/Management: Highlight the mechanism for automatic model weight download (cache_dir and model_dir configurations) and configuration of quantization (8-bit/4-bit support).
- Data and Benchmarking: Incorporate modules for evaluation such as ChestAgentBench that are organized under experiments/ and benchmark/.
- Data Handling Utilities: Represent DICOM processing utilities and image processing (found in medrax/tools/dicom.py and medrax/utils).

b. Relationships and Data Flow:
- The Agent Core coordinates with both the LLM and specific tools (each represented as an independent processing unit) to generate responses.
- Data flows from external datasets (located in “data” and “demo”) into the tools for analysis.
- Model weight management should be noted as a background process that feeds the tools.

c. Patterns and Design Principles:
- Modular Design: The system is structured around independent tools that can be selectively initialized, highlighting a plug-and-play architecture.
- Explicit Separation: Clearly delineate front-end (UI), business logic (agent and LLM coordination), and service layers (individual tools performing specialized tasks).
- Scalability & Extensibility: Note that the design allows for the integration of more tools and models, which is key when updating the system.

d. Key Technologies & Frameworks:
- LangChain and LangGraph for chaining queries and managing dialogue flows.
- GPT-4o (or similar advanced LLM) for understanding and generating medical reports.
- Gradio for the production-ready user interface.
- Deep learning models (e.g., DenseNet-121, SwinV2, MedSAM, etc.) integrated through specific modules in medrax/tools.
- Python as the core language, with dependencies installed via pip and managed by pyproject.toml.

##### Final Recommendation
- Branch out from the Agent to each tool, clearly indicating which modules handle classification, segmentation, grounding, visual QA, and report generation.
- Data flow from DICOM/image inputs (in “demo”, “data”, and “benchmark”) into the tools.
- Modularity to allow future expansion—such as the planned MedSAM integration—with a “Future Tools” note.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Clinician["User/Clinician"]:::external

    %% Data source for demonstration and evaluation
    DataSource["Data Source (Demo/Data)"]:::data

    %% User Interface Layer
    UI["User Interface (Gradio)"]:::frontend

    %% Core Engine Subgraph
    subgraph "Core Engine"
        Agent["Agent Core"]:::core
        LLM["LLM Integration"]:::llm
    end

    %% Medical Analysis Tools Subgraph
    subgraph "Medical Analysis Tools"
        Classify["Chest X-Ray Classification"]:::tool
        Segment["Chest X-Ray Segmentation"]:::tool
        Report["Report Generation Tool"]:::tool
        Grounding["Grounding Tool"]:::tool
        VQA["Visual QA Tool"]:::tool
        LlavaMed["Multimodal Med Tool"]:::tool
        Generation["Generation Tool"]:::tool
    end

    %% Supporting Services Subgraph
    subgraph "Supporting Services"
        ModelMgmt["Model Download/Management"]:::management
        Benchmark["Benchmark & Evaluation"]:::data
        Utilities["Data Handling Utilities"]:::utilities
    end

    %% Relationships
    Clinician -->|"sends_request"| UI
    UI -->|"forwards_input"| Agent
    Agent -->|"queries"| LLM

    %% Agent coordination with Tools
    Agent -->|"dispatches_to"| Classify
    Agent -->|"dispatches_to"| Segment
    Agent -->|"dispatches_to"| Report
    Agent -->|"dispatches_to"| Grounding
    Agent -->|"dispatches_to"| VQA
    Agent -->|"dispatches_to"| LlavaMed
    Agent -->|"dispatches_to"| Generation

    %% Data flow from external Data Source into Tools
    DataSource -->|"provides_data"| Classify
    DataSource -->|"provides_data"| Segment
    DataSource -->|"provides_data"| Report
    DataSource -->|"provides_data"| Grounding
    DataSource -->|"provides_data"| VQA
    DataSource -->|"provides_data"| LlavaMed
    DataSource -->|"provides_data"| Generation

    %% Model download and management supports Tools
    ModelMgmt -->|"supplies_models"| Classify
    ModelMgmt -->|"supplies_models"| Segment
    ModelMgmt -->|"supplies_models"| Report
    ModelMgmt -->|"supplies_models"| Grounding
    ModelMgmt -->|"supplies_models"| VQA
    ModelMgmt -->|"supplies_models"| LlavaMed
    ModelMgmt -->|"supplies_models"| Generation

    %% Evaluation framework linked to Agent
    Agent -->|"evaluation_data"| Benchmark

    %% Tools use Data Handling Utilities for pre-/post-processing
    Classify -->|"processes_images"| Utilities
    Segment -->|"processes_images"| Utilities
    Report -->|"processes_images"| Utilities
    Grounding -->|"processes_images"| Utilities
    VQA -->|"processes_images"| Utilities
    LlavaMed -->|"processes_images"| Utilities
    Generation -->|"processes_images"| Utilities

    %% Styles
    classDef frontend fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef core fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef llm fill:#d9ead3,stroke:#333,stroke-width:2px;
    classDef tool fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef management fill:#ead1dc,stroke:#333,stroke-width:2px;
    classDef data fill:#f9cb9c,stroke:#333,stroke-width:2px;
    classDef utilities fill:#d0e0e3,stroke:#333,stroke-width:2px;
    classDef external fill:#ccccff,stroke:#333,stroke-width:2px;

    %% Click Events for User Interface Layer
    click UI "https://github.com/bowang-lab/medrax/blob/main/main.py"
    click UI "https://github.com/bowang-lab/medrax/blob/main/interface.py"
    click UI "https://github.com/bowang-lab/medrax/blob/main/medrax/llava/serve/gradio_web_server.py"

    %% Click Event for Agent Core
    click Agent "https://github.com/bowang-lab/medrax/blob/main/medrax/agent/agent.py"

    %% Click Events for LLM Integration
    click LLM "https://github.com/bowang-lab/medrax/blob/main/medrax/llava/model/language_model/llava_mistral.py"
    click LLM "https://github.com/bowang-lab/medrax/blob/main/benchmark/llm.py"

    %% Click Events for Tools/Microservices Layer
    click Classify "https://github.com/bowang-lab/medrax/blob/main/medrax/tools/classification.py"
    click Segment "https://github.com/bowang-lab/medrax/blob/main/medrax/tools/segmentation.py"
    click Report "https://github.com/bowang-lab/medrax/blob/main/medrax/tools/report_generation.py"
    click Grounding "https://github.com/bowang-lab/medrax/blob/main/medrax/tools/grounding.py"
    click VQA "https://github.com/bowang-lab/medrax/blob/main/medrax/tools/xray_vqa.py"
    click LlavaMed "https://github.com/bowang-lab/medrax/blob/main/medrax/tools/llava_med.py"
    click Generation "https://github.com/bowang-lab/medrax/blob/main/medrax/tools/generation.py"

    %% Click Events for Model Download/Management
    click ModelMgmt "https://github.com/bowang-lab/medrax/blob/main/medrax/llava/model/builder.py"
    click ModelMgmt "https://github.com/bowang-lab/medrax/blob/main/medrax/llava/model/multimodal_encoder/builder.py"
    click ModelMgmt "https://github.com/bowang-lab/medrax/blob/main/medrax/llava/model/multimodal_projector/builder.py"

    %% Click Events for Data and Benchmarking
    click Benchmark "https://github.com/bowang-lab/medrax/tree/main/experiments/"
    click Benchmark "https://github.com/bowang-lab/medrax/tree/main/benchmark/"

    %% Click Events for Data Handling Utilities
    click Utilities "https://github.com/bowang-lab/medrax/blob/main/medrax/tools/dicom.py"
    click Utilities "https://github.com/bowang-lab/medrax/blob/main/medrax/utils/utils.py"
```
