Skip to content

feat(malaria): implement robust malaria image classification model#9

Merged
5uru merged 1 commit intomainfrom
js-branch-1
Nov 4, 2025
Merged

feat(malaria): implement robust malaria image classification model#9
5uru merged 1 commit intomainfrom
js-branch-1

Conversation

@5uru
Copy link
Copy Markdown
Owner

@5uru 5uru commented Nov 4, 2025

Summary by CodeRabbit

  • New Features

    • Unified blood analysis pipeline combining detection and classification in a single operation.
    • Added malaria classification capability for parasitized cell detection.
    • Model settings panel with configurable confidence threshold and per-task enablement toggles.
  • Refactor

    • Consolidated detection and classification workflows into centralized registry system.
    • Reorganized application UI for improved analysis workflow.

@5uru 5uru merged commit bbd66f9 into main Nov 4, 2025
1 check passed
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 4, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

The pull request refactors the blood smear analysis architecture from a monolithic detection approach to a modular, registry-based pipeline. It introduces a centralized analysis function, creates dedicated detection and classification modules (MalariaClassifier, CellDetectionModel), establishes a model registry for centralized management, and updates the Streamlit UI to consume the unified API. Legacy code is removed and utilities are adapted to the new structure.

Changes

Cohort / File(s) Summary
Core Pipeline & Registry
healthbox/blood_stain/blood_analysis.py, healthbox/blood_stain/registry.py
New run_blood_analysis() orchestrates detection and classification stages with confidence thresholding and enabled task filtering. New registries map task names to model instances and store per-classifier metadata (name, info, input size, padding, target class).
Detection Module
healthbox/blood_stain/detection/cell_detection.py
New CellDetectionModel wraps Ultralytics YOLO for cell detection. Supports configurable confidence threshold and model path. predict() returns structured results with detections (class, confidence, coordinates).
Classification Modules
healthbox/blood_stain/classification/malaria_classifier.py
New MalariaClassifier implements complete inference pipeline with model creation (timm backbone + custom head), transform construction (TIMM config), weight loading with error handling, and predict() returning class, confidence, and probabilities. Singleton classify_image() wrapper for simple usage.
Utilities
utils.py
crop_cells() signature updated: accepts class_name: str instead of cell_class_id: int. Internally maps class name to ID via new CLASS_IDS import.
Legacy Removal
healthbox/cell_detection.py
Module deleted; functionality migrated to CellDetectionModel.
Application Layer
streamlit_app.py
Major refactor: replaced local imports with centralized run_blood_analysis and OPTIONAL_TASKS. Reworked session state (added uploaded_image, results, confidence_threshold). Added Model Settings dialog with per-task toggles. Unified inference call replaces separate detection/classification pipelines. Updated results display to iterate over new nested structure.
Notebooks
notebooks/blood_stain/cell_detection.ipynb
Cleared ROBOFLOW_API_KEY environment variable (empty string).

Sequence Diagram

sequenceDiagram
    actor User
    participant UI as Streamlit UI
    participant Pipeline as run_blood_analysis
    participant Detector as CellDetectionModel
    participant Classifier as Classifier<br/>(WBC/Malaria)
    participant Utils as crop_cells

    User->>UI: Upload image + Configure settings
    UI->>Pipeline: run_blood_analysis(image, conf_threshold, enabled_tasks)
    
    activate Pipeline
    Pipeline->>Detector: Set confidence threshold
    Pipeline->>Detector: predict(image)
    Detector-->>Pipeline: {detections: [...]}
    
    Note over Pipeline: Iterate enabled classifiers
    
    Pipeline->>Pipeline: Check if classifier enabled
    alt Classifier enabled & detections match input_class
        Pipeline->>Utils: crop_cells(image, results, class_name, padding, output_size)
        Utils-->>Pipeline: [cropped_cells]
        
        loop For each cropped cell
            Pipeline->>Classifier: predict(cell_image)
            Classifier-->>Pipeline: {class, confidence, probabilities}
        end
        
        Pipeline->>Pipeline: Aggregate into results[classifier_key]
    end
    
    Pipeline-->>UI: {detection: {...}, classification: {...}}
    deactivate Pipeline
    
    UI->>UI: Render results & visualizations
    UI-->>User: Display analysis
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~45–60 minutes

  • MalariaClassifier model initialization: Dense logic with PyTorch model creation, timm backbone configuration, custom classification head, weight loading with error handling, and transform pipeline. Requires verification of initialization correctness and error recovery paths.
  • run_blood_analysis orchestration: New logic coordinating detection and multi-classifier pipelines; enabled task filtering and result aggregation. Needs validation of control flow and result structure consistency.
  • Streamlit UI state management and Model Settings dialog: Complex session state refactoring with new keys (uploaded_image, results, confidence_threshold), dialog-based settings flow, per-task toggles, and conditional reruns. Requires tracing state transitions and ensuring idempotency.
  • crop_cells signature change: Parameter migration from cell_class_id: int to class_name: str with internal CLASS_IDS mapping. Verify all call sites updated and behavior preserved.
  • Integration points: Registry instantiation, model path resolution, and unified inference flow—ensure no stale imports or missing dependencies.

Poem

🐰 Hop along, dear cells, through pipelines so bright!
Detection and classifiers now work in unison's light.
Models in registries, organized true,
From YOLO to timm, fresh and new!
One function to rule them all—
Malaria found, WBCs called!

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch js-branch-1

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9928370 and 0fa88fc.

📒 Files selected for processing (8)
  • healthbox/blood_stain/blood_analysis.py (1 hunks)
  • healthbox/blood_stain/classification/malaria_classifier.py (1 hunks)
  • healthbox/blood_stain/detection/cell_detection.py (1 hunks)
  • healthbox/blood_stain/registry.py (1 hunks)
  • healthbox/cell_detection.py (0 hunks)
  • notebooks/blood_stain/cell_detection.ipynb (1 hunks)
  • streamlit_app.py (1 hunks)
  • utils.py (1 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant