Spaces:
Running
Running
File size: 11,677 Bytes
adecc9b |
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
"""
Phase 3 Verification: Unified Workflow
========================================
Tests the complete discovery pipeline end-to-end:
1. Ingest sample data into Qdrant
2. Run discovery pipeline with query
3. Verify predictions and traceability
Usage:
python scripts/verify_phase3.py
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
import logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
logger = logging.getLogger(__name__)
def create_mock_components():
"""Create mock encoder, retriever, predictor for testing."""
from bioflow.core import (
BioEncoder, BioRetriever, BioPredictor,
Modality, EmbeddingResult, RetrievalResult, PredictionResult
)
import hashlib
class MockEncoder(BioEncoder):
def encode(self, content, modality):
h = hashlib.md5(str(content).encode()).hexdigest()
vector = [int(c, 16) / 15.0 for c in h] * 48
return EmbeddingResult(vector=vector[:768], modality=modality, dimension=768)
def encode_auto(self, content):
return self.encode(content, Modality.TEXT)
def batch_encode(self, contents, modality):
return [self.encode(c, modality) for c in contents]
@property
def dimension(self): return 768
class MockRetriever(BioRetriever):
def __init__(self, encoder):
self.encoder = encoder
self._data = {}
self._vectors = {}
self._id_counter = 0
def search(self, query, limit=10, filters=None, collection=None, modality=None, **kwargs):
if isinstance(query, str):
query_vec = self.encoder.encode(query, modality or Modality.TEXT).vector
else:
query_vec = query
# Simple cosine similarity
import math
results = []
for id_, (vec, payload) in self._vectors.items():
dot = sum(a*b for a, b in zip(query_vec, vec))
norm_q = math.sqrt(sum(a*a for a in query_vec))
norm_v = math.sqrt(sum(b*b for b in vec))
score = dot / (norm_q * norm_v) if norm_q * norm_v > 0 else 0
results.append(RetrievalResult(
id=id_,
score=score,
content=payload.get("content", ""),
modality=Modality(payload.get("modality", "text")),
payload=payload
))
results.sort(key=lambda x: x.score, reverse=True)
return results[:limit]
def ingest(self, content, modality, payload=None, collection=None, id=None):
self._id_counter += 1
id_ = id or f"item_{self._id_counter}"
vec = self.encoder.encode(content, modality).vector
full_payload = {"content": content, "modality": modality.value, **(payload or {})}
self._vectors[id_] = (vec, full_payload)
return id_
def count(self, collection=None):
return len(self._vectors)
class MockPredictor(BioPredictor):
def predict(self, drug, target):
import random
random.seed(hash(drug + target) % 2**32)
score = random.uniform(0.2, 0.9)
return PredictionResult(
score=score,
confidence=0.7,
label="binding" if score > 0.5 else "non-binding"
)
encoder = MockEncoder()
retriever = MockRetriever(encoder)
predictor = MockPredictor()
return encoder, retriever, predictor
def test_node_execution():
"""Test individual node execution."""
print("\n" + "="*60)
print("π§© TEST 1: Node Execution")
print("="*60)
from bioflow.core.nodes import (
EncodeNode, RetrieveNode, PredictNode, FilterNode, TraceabilityNode
)
from bioflow.core import Modality
encoder, retriever, predictor = create_mock_components()
# Test EncodeNode
encode_node = EncodeNode("enc", encoder, Modality.SMILES)
result = encode_node.execute("CCO")
print(f" EncodeNode: vector dim = {len(result.data.vector)}")
# Test FilterNode
filter_node = FilterNode("filter", threshold=0.5, top_k=3)
items = [{"score": 0.9}, {"score": 0.4}, {"score": 0.7}, {"score": 0.3}]
result = filter_node.execute(items)
print(f" FilterNode: {len(items)} items β {len(result.data)} after filtering")
# Test TraceabilityNode
trace_node = TraceabilityNode("trace")
items = [{"id": "PMID_12345", "content": "test", "payload": {"pmid": "12345"}}]
result = trace_node.execute(items)
print(f" TraceabilityNode: Added {result.metadata['with_evidence']} evidence links")
print("β
All nodes execute correctly")
return True
def test_discovery_pipeline():
"""Test the full discovery pipeline."""
print("\n" + "="*60)
print("π¬ TEST 2: Discovery Pipeline")
print("="*60)
from bioflow.workflows import DiscoveryPipeline, generate_sample_molecules
from bioflow.core import Modality
encoder, retriever, predictor = create_mock_components()
# Create pipeline
pipeline = DiscoveryPipeline(
encoder=encoder,
retriever=retriever,
predictor=predictor,
collection="test_molecules"
)
# Ingest sample data
print("\n1. Ingesting sample molecules...")
sample_data = generate_sample_molecules()
for mol in sample_data:
retriever.ingest(
content=mol["smiles"],
modality=Modality.SMILES,
payload={"name": mol["name"], **{k: v for k, v in mol.items() if k not in ["smiles", "modality"]}}
)
print(f" Ingested {retriever.count()} molecules")
# Run discovery
print("\n2. Running discovery pipeline...")
target_sequence = "MKTVRQERLKSIVRILERSKEPVSGAQLAEELSVSRQVIVQDIAYLRSLGYNIVATPRGYVLAGG"
result = pipeline.discover(
query="anti-inflammatory compound",
target_sequence=target_sequence,
limit=5,
threshold=0.3,
top_k=3
)
print(f"\n Discovery Result:")
print(f" β’ Query: {result.query[:40]}...")
print(f" β’ Candidates retrieved: {len(result.candidates)}")
print(f" β’ Predictions made: {len(result.predictions)}")
print(f" β’ Top hits: {len(result.top_hits)}")
print(f" β’ Execution time: {result.execution_time_ms:.0f}ms")
print("\n3. Top hits:")
for i, hit in enumerate(result.top_hits[:3]):
drug = hit.get("drug", "")[:30]
score = hit.get("score", 0)
evidence = hit.get("evidence_links", {})
print(f" {i+1}. {drug}... (score: {score:.3f})")
if evidence:
print(f" Evidence: {list(evidence.keys())}")
print("\nβ
Discovery pipeline works!")
return True
def test_simple_search():
"""Test simple similarity search."""
print("\n" + "="*60)
print("π TEST 3: Simple Search")
print("="*60)
from bioflow.workflows import DiscoveryPipeline, generate_sample_abstracts
from bioflow.core import Modality
encoder, retriever, predictor = create_mock_components()
pipeline = DiscoveryPipeline(
encoder=encoder,
retriever=retriever,
predictor=predictor
)
# Ingest abstracts
print("\n1. Ingesting sample abstracts...")
for abstract in generate_sample_abstracts():
retriever.ingest(
content=abstract["content"],
modality=Modality.TEXT,
payload={k: v for k, v in abstract.items() if k not in ["content", "modality"]}
)
print(f" Ingested {retriever.count()} items")
# Search
print("\n2. Searching for 'EGFR cancer treatment'...")
results = pipeline.search(
query="EGFR cancer treatment",
modality=Modality.TEXT,
limit=3
)
print(f"\n Found {len(results)} results:")
for r in results:
print(f" β’ Score: {r.score:.3f} | {r.content[:50]}...")
print("\nβ
Search works!")
return True
def test_ingestion_utilities():
"""Test data ingestion utilities."""
print("\n" + "="*60)
print("π₯ TEST 4: Ingestion Utilities")
print("="*60)
from bioflow.workflows.ingestion import (
generate_sample_molecules,
generate_sample_proteins,
generate_sample_abstracts
)
molecules = generate_sample_molecules()
proteins = generate_sample_proteins()
abstracts = generate_sample_abstracts()
print(f" β’ Sample molecules: {len(molecules)}")
print(f" - Example: {molecules[0]['name']} ({molecules[0]['smiles'][:20]}...)")
print(f" β’ Sample proteins: {len(proteins)}")
print(f" - Example: {proteins[0]['name']} ({proteins[0]['sequence'][:20]}...)")
print(f" β’ Sample abstracts: {len(abstracts)}")
print(f" - Example: {abstracts[0]['title']}")
print("\nβ
Ingestion utilities work!")
return True
def test_traceability():
"""Test evidence linking and traceability."""
print("\n" + "="*60)
print("π TEST 5: Traceability & Evidence Linking")
print("="*60)
from bioflow.core.nodes import TraceabilityNode
trace_node = TraceabilityNode("trace")
# Test with different ID formats
test_items = [
{"id": "PMID_12345678", "content": "Paper about EGFR", "payload": {}},
{"id": "mol_1", "content": "CCO", "payload": {"drugbank_id": "DB00316", "pubchem_id": "702"}},
{"id": "prot_1", "content": "MKTVRQ...", "payload": {"uniprot": "P00533"}},
]
result = trace_node.execute(test_items)
print(" Evidence links generated:")
for item in result.data:
print(f" β’ ID: {item['id']}")
links = item.get("evidence_links", {})
if links:
for source, url in links.items():
print(f" β {source}: {url}")
else:
print(f" β No links (payload missing IDs)")
print(f"\n Items with evidence: {result.metadata['with_evidence']}/{len(test_items)}")
print("\nβ
Traceability works!")
return True
def main():
"""Run all Phase 3 verification tests."""
print("="*60)
print("𧬠BioFlow Phase 3 Verification: Unified Workflow")
print("="*60)
results = {}
results["Nodes"] = test_node_execution()
results["Discovery"] = test_discovery_pipeline()
results["Search"] = test_simple_search()
results["Ingestion"] = test_ingestion_utilities()
results["Traceability"] = test_traceability()
# Summary
print("\n" + "="*60)
print("π VERIFICATION SUMMARY")
print("="*60)
for test, passed in results.items():
status = "β
PASS" if passed else "β FAIL"
print(f" {test}: {status}")
all_passed = all(results.values())
print("\n" + ("β
All Phase 3 tests passed!" if all_passed else "β οΈ Some tests failed"))
if all_passed:
print("\nπ The unified workflow is ready!")
print(" You can now:")
print(" β’ Ingest molecules, proteins, and literature")
print(" β’ Run cross-modal similarity search")
print(" β’ Predict drug-target interactions")
print(" β’ Trace results back to sources")
return 0 if all_passed else 1
if __name__ == "__main__":
sys.exit(main())
|