Enhanced Repeat Relationship Extraction - Implementation Summary¶
โ Current System Capabilities¶
Your Graffold now has comprehensive repeat relationship extraction capabilities that are natively integrated into the main pipeline. Here's what has been implemented:
๐ Native Integration with Main Pipeline¶
The enhanced relationship extraction is now automatically activated when running main.py:
# Enhanced relationship tracking runs automatically
python main.py --search-term "insulin diabetes" --max-results 20
What happens automatically:
- โ
Abstracts are fetched from PubMed
- โ
Enhanced KG processing captures full relationship contexts immediately
- โ
Confidence scores calculated for each relationship as it's extracted
- โ
Sentence-level context stored with every relationship occurrence
- โ
Comprehensive quality report generated after processing
- โ
High-confidence relationships logged with evidence details
1. Advanced Relationship Tracking (relationship_counter.py)¶
The RelationshipCounter class now includes:
Original Features:¶
- โ Frequency counting for duplicate relationships across documents
- โ Source document aggregation and tracking
- โ High-confidence relationship filtering based on frequency
Enhanced Features (NEW):¶
- โ
Full Context Storage:
store_relationship_with_context()stores complete abstract text, sentence context, and metadata - โ Confidence Scoring: Each relationship gets a confidence score based on contextual factors
- โ
Rich Evidence Retrieval:
get_relationship_evidence()returns all supporting evidence with full context - โ
Pattern Analysis:
analyze_relationship_patterns()identifies extraction method performance and conflicts - โ
High-Confidence Filtering: Enhanced
get_high_confidence_relationships()with confidence thresholds
2. Enhanced KG Pipeline (kg_pipeline.py)¶
The KGPipeline class now includes:
Enhanced Processing Methods:¶
- โ
Sentence Context Extraction:
_extract_sentence_context()finds specific sentences mentioning both entities - โ
Dynamic Confidence Calculation:
_calculate_relationship_confidence()scores relationships based on: - Entity proximity in text
- Presence of relationship keywords
- Sentence-level co-occurrence
- โ
Enhanced Document Processing:
process_documents_enhanced()combines standard extraction with rich context storage - โ
Quality Reporting:
get_relationship_quality_report()provides comprehensive extraction analytics
3. Relationship Occurrence Storage¶
The system now creates RelationshipOccurrence nodes that store:
- ๐ Full Abstract Text: Complete source document content
- ๐ Sentence Context: Specific sentence mentioning the relationship
- ๐ Confidence Score: Algorithmic assessment of relationship strength
- ๐ Extraction Method: Track which method found the relationship
- ๐ Rich Metadata: PMID, study type, chunk IDs, etc.
- โฐ Temporal Tracking: When relationships were first/last seen
๐ How to Use the Enhanced System¶
Automatic Usage (RECOMMENDED)¶
The enhanced relationship extraction now runs automatically with the main pipeline:
# All enhanced features activate automatically
python main.py --search-term "protein disease association" --max-results 50
# Force re-processing of existing abstracts with enhanced tracking
python main.py --search-term "insulin diabetes" --max-results 20 --force
# Monitor the logs for relationship quality reports
Output you'll see:
INFO - Relationship Quality Summary:
INFO - Total relationships: 245
INFO - Total occurrences: 580
INFO - Unique source documents: 45
INFO - Overall avg confidence: 0.823
INFO - Found 67 high-confidence relationships:
INFO - Insulin (Protein) -[ASSOCIATED_WITH]-> Diabetes Mellitus (Disease): confidence=0.945, occurrences=12
INFO - TNF-alpha (Protein) -[REGULATES]-> Inflammation (Process): confidence=0.891, occurrences=8
Manual/Programmatic Usage¶
Manual/Programmatic Usage¶
For advanced use cases, you can still access all enhanced features programmatically:
from src.pipeline.kg_pipeline import KGPipeline
from src.processors.relationship_counter import RelationshipCounter
# Initialize enhanced pipeline
pipeline = KGPipeline(service="local", database="olink")
# Process documents with enhanced relationship tracking (happens automatically in main.py)
docs = await pipeline.process_documents_enhanced(documents, metadata={"source": "pubmed"})
# Get comprehensive quality report (happens automatically in main.py)
quality_report = pipeline.get_relationship_quality_report()
# Access rich relationship evidence
counter = RelationshipCounter(database="olink")
evidence = counter.get_relationship_evidence("P01308", "D003920", "ASSOCIATED_WITH")
# Analyze extraction patterns
patterns = counter.analyze_relationship_patterns()
# Get high-confidence relationships with confidence thresholds
high_conf = counter.get_high_confidence_relationships(
min_occurrences=3,
min_avg_confidence=0.8
)
Enhanced Usage (LEGACY - now integrated)¶
from src.pipeline.kg_pipeline import KGPipeline
from src.processors.relationship_counter import RelationshipCounter
# Initialize enhanced pipeline
pipeline = KGPipeline(service="local", database="olink")
# Process documents with enhanced relationship tracking
docs = await pipeline.process_documents_enhanced(documents, metadata={"source": "pubmed"})
# Get comprehensive quality report
quality_report = pipeline.get_relationship_quality_report()
# Access rich relationship evidence
counter = RelationshipCounter(database="olink")
evidence = counter.get_relationship_evidence("P01308", "D003920", "ASSOCIATED_WITH")
# Analyze extraction patterns
patterns = counter.analyze_relationship_patterns()
# Get high-confidence relationships with confidence thresholds
high_conf = counter.get_high_confidence_relationships(
min_occurrences=3,
min_avg_confidence=0.8
)
๐ Example Output¶
Relationship Evidence¶
evidence = counter.get_relationship_evidence("P01308", "D003920", "ASSOCIATED_WITH")
# Returns:
[
{
"source_doc": "pubmed_12345",
"abstract_text": "Insulin plays a crucial role in diabetes mellitus...",
"sentence_context": "insulin resistance is strongly associated with type 2 diabetes",
"confidence_score": 0.87,
"extraction_method": "llm_enhanced",
"metadata": {"pmid": "12345", "study_type": "clinical"},
"extracted_at": "2024-01-01T10:00:00",
"total_count": 5
}
]
Quality Report¶
report = pipeline.get_relationship_quality_report()
# Returns:
{
"summary": {
"total_relationships": 150,
"total_occurrences": 450,
"unique_source_documents": 75,
"overall_avg_confidence": 0.78
},
"high_confidence_relationships": [...],
"pattern_analysis": {
"extraction_methods": [
{"method": "llm_enhanced", "count": 400},
{"method": "rule_based", "count": 50}
],
"confidence_distribution": [...],
"conflicting_evidence": [...]
},
"quality_metrics": {
"total_high_confidence_relationships": 45,
"average_confidence_score": 0.823,
"average_occurrences_per_relationship": 3.2
}
}
๐งช Testing¶
The system includes comprehensive test coverage:
- โ
test_relationship_counter_enhanced.py - Tests all enhanced relationship tracking features
- โ
Integration with existing test suite
- โ
Mocked database operations for fast testing
- โ
Error handling and edge case coverage
๐ฏ Key Benefits¶
- Native Integration: Enhanced relationship extraction runs automatically with main pipeline - no code changes needed
- Immediate Processing: Relationship contexts are captured as each abstract is processed (no batch delays)
- Real-time Quality Reporting: Get comprehensive relationship statistics immediately after processing
- No Duplicate Files: All enhancements are integrated into existing files - no path updates needed
- Backward Compatibility: Existing functionality remains unchanged
- Rich Context: Full abstract text and sentence-level context for every relationship
- Quality Assurance: Confidence scoring and pattern analysis for relationship validation
- Comprehensive Evidence: Track all supporting documents with full metadata
- Performance Analytics: Understand which extraction methods work best
๐ Use Cases¶
- Automated Scientific Literature Analysis: Simply run main.py to track protein-disease relationships across studies
- Real-time Evidence Strength Assessment: Identify relationships with strongest supporting evidence as they're processed
- Method Comparison: Compare performance of different extraction algorithms automatically
- Quality Control: Identify relationships with conflicting or low-confidence evidence in real-time
- Knowledge Validation: Cross-reference relationship claims across multiple papers immediately
๐ Migration Path¶
No migration needed! Your existing code will continue to work unchanged. The enhanced relationship extraction is now:
- โ
Automatically activated when running
main.py - โ Immediate processing - relationships are analyzed as abstracts are processed
- โ Quality reporting - comprehensive statistics logged after each run
- โ Backward compatible - existing RelationshipCounter methods still work
Just run your normal pipeline commands and you'll get enhanced relationship tracking automatically!