A sophisticated medical document processing system that extracts and verifies structured information from pathology reports using large language models and medical guidelines.
Pathology Analyzer is an advanced tool designed to analyze pathology reports, especially in oncology. It extracts critical medical information from PDF documents and verifies it against the National Comprehensive Cancer Network (NCCN) guidelines.
The system employs a multi-stage approach:
- Text extraction from PDF documents using multiple fallback methods
- Semantic search to find relevant NCCN guidelines for each report
- Information extraction using Large Language Models (LLMs)
- Verification and correction of extracted information through a separate verification agent
- Statistical analysis of processed reports for insights and quality assessment
This comprehensive approach ensures high accuracy and reliability while providing both professional medical summaries and patient-friendly explanations.
- Robust PDF text extraction with fallback mechanisms (PyMuPDF, pdfplumber, PyPDF2, OCR)
- Semantic search using embeddings to find relevant medical guidelines
- Comprehensive information extraction including:
- Cancer organ type and subtype
- FIGO staging
- Pathologic staging (including TNM)
- Treatment recommendations based on NCCN guidelines
- Professional medical description
- Patient-friendly notes
- Two-stage verification system with automatic correction
- Field-specific confidence scores for verification results
- Statistical analysis tools for aggregating and summarizing results across multiple reports
- Parallel processing capabilities for efficient batch processing
- Efficient processing with caching for embeddings and semantic search
- Batch processing of multiple reports
- Detailed logging for tracking and debugging
- Python 3.9+
- Tesseract OCR (for OCR fallback)
-
Clone the repository:
git clone https://github.com/yourusername/pathology-analyzer.git cd pathology-analyzer -
Install dependencies:
pip install -r requirements.txt
-
Set up your OpenAI API key:
export OPENAI_API_KEY="your-api-key"
-
Place the NCCN guidelines PDF in the project root (default:
uterine_core.pdf)
Run the analyzer on a directory of case folders:
python main.pyEach case folder should contain a PDF pathology report. The analyzer will create an analysis.json file in each folder with the extracted information.
python main.py --helpOptions include:
--rebuild-cache: Force regeneration of embeddings cache--no-verify: Disable verification step--detailed-verify: Enable detailed field-by-field verification--root-dir PATH: Specify root directory for case folders--nccn-pdf PATH: Specify path to NCCN guidelines PDF--api-key KEY: Provide OpenAI API key--model MODEL: Specify OpenAI model to use--parallel: Enable parallel processing for batch operations
The analyzer produces a JSON file with the following structure:
{
"cancer_organ_type": "Uterine cancer",
"cancer_subtype": "Endometrioid adenocarcinoma",
"figo_stage": "FIGO Stage IA",
"pathologic_stage": "pT1a pN0 M0",
"recommended_treatment": "Simple hysterectomy with bilateral salpingo-oophorectomy...",
"description": "Pathology report indicates well-differentiated endometrioid adenocarcinoma...",
"patient_notes": "Your pathology report shows an early-stage uterine cancer...",
"verification": {
"passed": true,
"confidence": 0.95,
"assessment": "Extraction verified successfully",
"field_issues": { ... },
"incorrect_fields": [ ... ],
"timestamp": "2025-05-11T15:23:42.123456"
}
}After processing multiple reports, you can generate statistical summaries:
python -c "from analysis.utils import collect_results_parallel; from pathlib import Path; import json; print(json.dumps(collect_results_parallel(Path('./cases')), indent=2))"This will produce a comprehensive summary with metrics such as:
- Total cases processed
- Success rates
- Distribution of cancer types
- Verification statistics
- Field presence metrics (FIGO staging, pathologic staging, etc.)
The system architecture consists of several key components:
-
PDF Processing Module: Extracts text from pathology reports using multiple methods with automatic fallback.
-
Embedding and Retrieval System: Creates vector representations of text and performs semantic search to find relevant guidelines.
-
OpenAI API Interaction: Handles communication with OpenAI models for text processing and analysis.
-
Verification Agent: Provides independent verification of extracted information and suggests corrections.
-
Analysis Pipeline: Coordinates the entire workflow from PDF processing to result storage.
-
Statistical Analysis Tools: Processes and summarizes results across multiple reports.
- PDF Processing: PyMuPDF (fitz), pdfplumber, PyPDF2
- OCR: Tesseract (via pytesseract)
- Machine Learning: OpenAI API, Vector Embeddings
- Data Processing: NumPy, tiktoken
- Image Processing: PIL (Python Imaging Library)
- Concurrent Processing: ThreadPoolExecutor for parallel operations
- Utilities: tqdm (progress bars), tenacity (retry logic)
pathology-analyzer/
├── main.py # Main application entry point
├── config.py # Configuration settings
├── requirements.txt # Dependencies
├── analysis/ # Analysis tools and utilities
│ ├── __init__.py
│ ├── process.py # Core processing logic
│ └── utils.py # Statistical analysis utilities
├── embedding/ # Embedding and retrieval functionality
│ ├── __init__.py
│ ├── embed.py # Embedding generation
│ └── retrieval.py # Semantic search
├── llm/ # LLM interaction
│ ├── __init__.py
│ ├── client.py # OpenAI API client
│ ├── prompts.py # System prompts
│ └── verification.py # Verification logic
├── text_extraction/ # PDF text extraction
│ ├── __init__.py
│ ├── extract.py # Text extraction with fallbacks
│ └── compare.py # Extraction method comparison
├── utils/ # Utility functions
│ ├── __init__.py
│ ├── json.py # JSON handling
│ ├── logger.py # Logging setup
│ └── text.py # Text processing
├── logs/ # Log files
├── cases/ # Case folders containing PDFs
├── archived/ # Previous versions
├── uterine_core.pdf # NCCN guidelines
└── nccn_embeddings_cache.pkl # Cached embeddings
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see below for details:
MIT License
Copyright (c) 2025 Reubin George
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
- Memory Usage: Processing large PDF files and embedding models requires substantial RAM.
- API Costs: The system makes multiple calls to OpenAI API, which has usage costs.
- Cache Management: The embedding cache grows with the number of processed guidelines.
- OCR Performance: The OCR fallback is significantly slower but provides reliability for scanned documents.
- Parallel Processing: The parallel processing capability significantly improves throughput for batch operations but requires more system resources.
For questions or support, please open an issue on this repository or contact reubingeorge@usf.edu.
