Spaces:
Sleeping
Sleeping
File size: 10,616 Bytes
b69e9e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | """
CMPO Mapping Examples and Demonstrations
This file demonstrates the key concepts and usage patterns of the CMPO integration module.
Run with:
python -m anton.cmpo.examples (from project root)
OR
python examples.py (from anton/cmpo/ directory)
"""
import sys
import logging
from pathlib import Path
# Handle both direct execution and module execution
if __name__ == "__main__" and __package__ is None:
# Add parent directories to path for direct execution
current_dir = Path(__file__).parent
project_root = current_dir.parent.parent
sys.path.insert(0, str(project_root))
from anton.cmpo.ontology import CMPOOntology
from anton.cmpo.mapping import map_to_cmpo
else:
# Normal relative imports for module execution
from .ontology import CMPOOntology
from .mapping import map_to_cmpo
logging.basicConfig(level=logging.INFO)
def demonstrate_basic_mapping():
"""Demonstrate basic CMPO mapping functionality."""
print("=" * 60)
print("BASIC CMPO MAPPING DEMONSTRATION")
print("=" * 60)
# Initialize CMPO ontology
cmpo = CMPOOntology()
print(f"Loaded CMPO ontology with {len(cmpo.ontology)} terms\n")
# Example descriptions from VLM analysis
test_descriptions = [
"cells arrested in metaphase with condensed chromosomes",
"fragmented nuclei with membrane blebbing indicating apoptosis",
"abnormal spindle formation during cell division",
"enlarged cell bodies with irregular morphology",
"normal healthy fibroblast cells with typical morphology"
]
for desc in test_descriptions:
print(f"Description: '{desc}'")
results = map_to_cmpo(desc, cmpo)
if results:
print(f"Found {len(results)} CMPO mappings:")
for i, result in enumerate(results[:3], 1):
print(f" {i}. {result['CMPO_ID']}: {result['term_name']}")
print(f" Confidence: {result['confidence']:.3f}")
print(f" Evidence: {result['supporting_evidence']}")
if result.get('hierarchy_path'):
print(f" Hierarchy: {' β '.join(result['hierarchy_path'])}")
print()
else:
print(" No CMPO mappings found")
print("-" * 50)
def demonstrate_context_aware_mapping():
"""Demonstrate context-aware mapping with research focus."""
print("\n" + "=" * 60)
print("CONTEXT-AWARE MAPPING DEMONSTRATION")
print("=" * 60)
cmpo = CMPOOntology()
# Same description, different research contexts
description = "abnormal cell division with chromosome segregation defects"
contexts = [
("cell_cycle", "Cell cycle research focus"),
("morphology", "Morphology research focus"),
(None, "No specific context")
]
for context, context_desc in contexts:
print(f"\n{context_desc}:")
print(f"Description: '{description}'")
results = map_to_cmpo(description, cmpo, context=context)
if results:
for i, result in enumerate(results[:2], 1):
print(f" {i}. {result['term_name']} (confidence: {result['confidence']:.3f})")
print(f" Context boost from: {result['supporting_evidence']}")
else:
print(" No mappings found")
def demonstrate_hierarchical_navigation():
"""Show how CMPO terms relate hierarchically."""
print("\n" + "=" * 60)
print("HIERARCHICAL NAVIGATION DEMONSTRATION")
print("=" * 60)
cmpo = CMPOOntology()
# Find a term with rich hierarchy
for term_id, term_data in cmpo.ontology.items():
if term_data.get('parent_terms') and len(term_data['parent_terms']) > 0:
print(f"Term: {term_data['name']} ({term_id})")
print(f"Description: {term_data.get('description', 'No description')}")
if term_data.get('synonyms'):
print(f"Synonyms: {', '.join(term_data['synonyms'])}")
print(f"Parent terms:")
for parent_id in term_data['parent_terms']:
parent_term = cmpo.get_term(parent_id)
if parent_term:
print(f" β {parent_term['name']} ({parent_id})")
if term_data.get('equivalent_to'):
print(f"Equivalent to: {term_data['equivalent_to']}")
break
def demonstrate_semantic_analysis():
"""Show semantic component analysis."""
print("\n" + "=" * 60)
print("SEMANTIC ANALYSIS DEMONSTRATION")
print("=" * 60)
# Import internal functions for demonstration
if __name__ == "__main__" and __package__ is None:
from anton.cmpo.mapping import _extract_biological_tokens, _find_direct_matches
else:
from .mapping import _extract_biological_tokens, _find_direct_matches
cmpo = CMPOOntology()
description = "apoptotic cells with fragmented nuclei and chromatin condensation"
print(f"Analyzing: '{description}'")
# Show token extraction
tokens = _extract_biological_tokens(description)
print(f"Biological tokens: {sorted(tokens)}")
# Show direct matches
direct_matches = _find_direct_matches(description.lower(), cmpo)
if direct_matches:
print("\nDirect matches found:")
for term_id, confidence, evidence in direct_matches[:3]:
term = cmpo.get_term(term_id)
if term:
print(f" {term['name']}: {confidence:.3f} (matched: {evidence})")
def demonstrate_integration_patterns():
"""Show how CMPO integrates with Anton pipeline."""
print("\n" + "=" * 60)
print("INTEGRATION PATTERNS DEMONSTRATION")
print("=" * 60)
# Simulate VLM output from different pipeline stages
vlm_outputs = {
"stage_1_global": "Dense population of adherent cells with fibroblast morphology",
"stage_3_features": "Individual cells show elongated spindle shape with prominent stress fibers",
"stage_4_population": "Population exhibits normal growth patterns with typical cell-cell contacts"
}
cmpo = CMPOOntology()
print("Simulating Anton pipeline integration:")
for stage, output in vlm_outputs.items():
print(f"\n{stage.replace('_', ' ').title()}:")
print(f"VLM Output: {output}")
# Map to CMPO
mappings = map_to_cmpo(output, cmpo)
if mappings:
best_match = mappings[0]
print(f"Best CMPO Match: {best_match['term_name']}")
print(f"Confidence: {best_match['confidence']:.3f}")
else:
print("No CMPO mappings found")
def demonstrate_multi_stage_cmpo():
"""Demonstrate multi-stage CMPO integration across pipeline stages."""
print("\n" + "=" * 60)
print("MULTI-STAGE CMPO INTEGRATION DEMONSTRATION")
print("=" * 60)
cmpo = CMPOOntology()
# Simulate different types of biological observations at each stage
stage_data = {
"Stage 1 - Global Context": {
"description": "Dense cell population with mitotic figures visible throughout",
"context": "cell_population"
},
"Stage 3 - Individual Cells": {
"description": "Cell arrested in metaphase with condensed chromosomes",
"context": "cellular_phenotype"
},
"Stage 4 - Population Insights": {
"description": "20% of population shows apoptotic markers with fragmented nuclei",
"context": "cell_population"
}
}
all_mappings = {}
print("π¬ Multi-Stage CMPO Analysis:")
for stage_name, data in stage_data.items():
print(f"\n{stage_name}:")
print(f"Description: '{data['description']}'")
print(f"Context: {data['context']}")
# Map with stage-appropriate context
mappings = map_to_cmpo(data['description'], cmpo, context=data['context'])
if mappings:
print(f"Found {len(mappings)} CMPO mappings:")
for i, mapping in enumerate(mappings[:2], 1):
print(f" {i}. {mapping['term_name']} (confidence: {mapping['confidence']:.3f})")
# Track for cross-stage analysis
cmpo_id = mapping['CMPO_ID']
if cmpo_id not in all_mappings:
all_mappings[cmpo_id] = {
'term': mapping['term_name'],
'stages': [],
'max_confidence': 0
}
all_mappings[cmpo_id]['stages'].append(stage_name.split(' - ')[0])
all_mappings[cmpo_id]['max_confidence'] = max(
all_mappings[cmpo_id]['max_confidence'],
mapping['confidence']
)
else:
print(" No CMPO mappings found")
# Cross-stage analysis
print("\nπ Cross-Stage CMPO Analysis:")
multi_stage_terms = {k: v for k, v in all_mappings.items() if len(v['stages']) > 1}
if multi_stage_terms:
print("Terms detected across multiple stages:")
for cmpo_id, data in multi_stage_terms.items():
print(f" β’ {data['term']} - detected in: {', '.join(data['stages'])}")
print(f" Max confidence: {data['max_confidence']:.3f}")
else:
print("No terms detected across multiple stages (expected - different biological levels)")
print(f"\nTotal unique CMPO terms identified: {len(all_mappings)}")
print("β
Multi-stage integration provides comprehensive phenotype classification!")
def main():
"""Run all demonstrations."""
print("CMPO Module Demonstration Suite")
print("This script demonstrates the key capabilities of Anton's CMPO integration")
try:
demonstrate_basic_mapping()
demonstrate_context_aware_mapping()
demonstrate_hierarchical_navigation()
demonstrate_semantic_analysis()
demonstrate_integration_patterns()
demonstrate_multi_stage_cmpo() # New multi-stage demo
print("\n" + "=" * 60)
print("DEMONSTRATION COMPLETE")
print("=" * 60)
print("For more information, see anton/cmpo/README.md")
print("β¨ NEW: Multi-stage CMPO integration across all pipeline stages!")
except Exception as e:
print(f"Error during demonstration: {e}")
print("Ensure CMPO ontology is properly loaded")
if __name__ == "__main__":
main() |