{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/use_cases/healthcare/07_Patient_Records_Temporal.ipynb)\n",
    "\n",
    "# Patient Records Temporal Pipeline\n",
    "\n",
    "## Overview\n",
    "\n",
    "This notebook demonstrates a complete patient records temporal analysis pipeline for healthcare: ingest patient records, extract medical entities, build temporal knowledge graph, query medical history, and generate insights.\n",
    "\n",
    "\n",
    "**Documentation**: [API Reference](https://semantica.readthedocs.io/use-cases/)\n",
    "\n",
    "### Modules Used (20+)\n",
    "\n",
    "- **Ingestion**: FileIngestor, DBIngestor, StreamIngestor\n",
    "- **Parsing**: DocumentParser, StructuredDataParser, CSVParser\n",
    "- **Extraction**: NERExtractor, RelationExtractor, CoreferenceResolver\n",
    "- **KG**: GraphBuilder, TemporalGraphQuery, GraphValidator, EntityResolver\n",
    "- **Ontology**: OntologyGenerator, ClassInferrer, PropertyGenerator, OntologyValidator\n",
    "- **Triple Store**: TripleStore, TripleManager, QueryEngine\n",
    "- **Export**: RDFExporter, OWLExporter, JSONExporter\n",
    "- **Visualization**: KGVisualizer, OntologyVisualizer, TemporalVisualizer\n",
    "\n",
    "### Pipeline\n",
    "\n",
    "**Patient Records → Parse → Extract Medical Entities → Build Temporal KG → Generate Ontology → Store in Triple Store → Query History → Export → Visualize**\n",
    "\n",
    "## Installation\n",
    "\n",
    "Install Semantica from PyPI:\n",
    "\n",
    "```bash\n",
    "pip install semantica\n",
    "# Or with all optional dependencies:\n",
    "pip install semantica[all]\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "## Step 1: Process Patient Records\n",
    "\n",
    "Ingest and parse patient records from multiple sources.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from semantica.ingest import FileIngestor, DBIngestor, StreamIngestor\n",
    "from semantica.parse import DocumentParser, StructuredDataParser, CSVParser\n",
    "from semantica.semantic_extract import NERExtractor, RelationExtractor, CoreferenceResolver\n",
    "from semantica.kg import GraphBuilder, TemporalGraphQuery, GraphValidator, EntityResolver\n",
    "from semantica.ontology import OntologyGenerator, ClassInferrer, PropertyGenerator, OntologyValidator\n",
    "from semantica.triple_store import TripleStore, TripleManager, QueryEngine\n",
    "from semantica.export import RDFExporter, OWLExporter, JSONExporter\n",
    "from semantica.visualization import KGVisualizer, OntologyVisualizer, TemporalVisualizer\n",
    "import tempfile\n",
    "import os\n",
    "import json\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "file_ingestor = FileIngestor()\n",
    "db_ingestor = DBIngestor()\n",
    "stream_ingestor = StreamIngestor()\n",
    "document_parser = DocumentParser()\n",
    "structured_parser = StructuredDataParser()\n",
    "csv_parser = CSVParser()\n",
    "\n",
    "# Real database connection for patient records (HIPAA compliant example)\n",
    "db_connection_string = \"postgresql://user:password@localhost:5432/patient_records_db\"\n",
    "db_query = \"SELECT patient_id, visit_date, diagnosis, medication, doctor FROM patient_visits WHERE visit_date > CURRENT_DATE - INTERVAL '1 year' ORDER BY visit_date DESC\"\n",
    "\n",
    "# Real HL7/FHIR API endpoints (examples)\n",
    "healthcare_apis = [\n",
    "    \"https://api.logicahealth.org/fhir/R4/Patient\",  # Logica Health FHIR API\n",
    "    \"https://hapi.fhir.org/baseR4/Patient\"  # HAPI FHIR Server\n",
    "]\n",
    "\n",
    "# Real medical feed URLs\n",
    "medical_feeds = [\n",
    "    \"https://www.cdc.gov/rss.xml\",  # CDC Health Alerts\n",
    "    \"https://www.who.int/rss-feeds/news-english.xml\"  # WHO News\n",
    "]\n",
    "\n",
    "temp_dir = tempfile.mkdtemp()\n",
    "\n",
    "patient_records_file = os.path.join(temp_dir, \"patient_records.csv\")\n",
    "patient_data = \"\"\"patient_id,visit_date,diagnosis,medication,doctor\n",
    "P001,2024-01-15,Hypertension,Lisinopril,Dr. Smith\n",
    "P001,2024-02-20,Diabetes,Metformin,Dr. Jones\n",
    "P002,2024-01-10,Fever,Acetaminophen,Dr. Smith\"\"\"\n",
    "\n",
    "with open(patient_records_file, 'w') as f:\n",
    "    f.write(patient_data)\n",
    "\n",
    "file_objects = file_ingestor.ingest_file(patient_records_file, read_content=True)\n",
    "parsed_csv = csv_parser.parse(patient_records_file)\n",
    "\n",
    "print(f\"Ingested {len([file_objects]) if file_objects else 0} patient record files\")\n",
    "print(f\"Parsed {len(parsed_csv.rows) if parsed_csv else 0} patient records\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Extract Medical Entities\n",
    "\n",
    "Extract medical entities and relationships from patient records.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ner_extractor = NERExtractor()\n",
    "relation_extractor = RelationExtractor()\n",
    "coreference_resolver = CoreferenceResolver()\n",
    "\n",
    "patient_entities = []\n",
    "relationships = []\n",
    "\n",
    "if parsed_csv and parsed_csv.rows:\n",
    "    for row in parsed_csv.rows:\n",
    "        patient_id = row.get(\"patient_id\", \"\")\n",
    "        diagnosis = row.get(\"diagnosis\", \"\")\n",
    "        medication = row.get(\"medication\", \"\")\n",
    "        doctor = row.get(\"doctor\", \"\")\n",
    "        visit_date = row.get(\"visit_date\", \"\")\n",
    "\n",
    "        patient_entities.append({\n",
    "            \"id\": patient_id,\n",
    "            \"type\": \"Patient\",\n",
    "            \"name\": patient_id,\n",
    "            \"properties\": {}\n",
    "        })\n",
    "\n",
    "        patient_entities.append({\n",
    "            \"id\": diagnosis,\n",
    "            \"type\": \"Diagnosis\",\n",
    "            \"name\": diagnosis,\n",
    "            \"properties\": {}\n",
    "        })\n",
    "\n",
    "        patient_entities.append({\n",
    "            \"id\": medication,\n",
    "            \"type\": \"Medication\",\n",
    "            \"name\": medication,\n",
    "            \"properties\": {}\n",
    "        })\n",
    "\n",
    "        patient_entities.append({\n",
    "            \"id\": doctor,\n",
    "            \"type\": \"Doctor\",\n",
    "            \"name\": doctor,\n",
    "            \"properties\": {}\n",
    "        })\n",
    "\n",
    "        relationships.append({\n",
    "            \"source\": patient_id,\n",
    "            \"target\": diagnosis,\n",
    "            \"type\": \"has_diagnosis\",\n",
    "            \"properties\": {\"timestamp\": visit_date}\n",
    "        })\n",
    "\n",
    "        relationships.append({\n",
    "            \"source\": patient_id,\n",
    "            \"target\": medication,\n",
    "            \"type\": \"prescribed\",\n",
    "            \"properties\": {\"timestamp\": visit_date}\n",
    "        })\n",
    "\n",
    "        relationships.append({\n",
    "            \"source\": doctor,\n",
    "            \"target\": patient_id,\n",
    "            \"type\": \"treats\",\n",
    "            \"properties\": {\"timestamp\": visit_date}\n",
    "        })\n",
    "\n",
    "print(f\"Extracted {len(patient_entities)} medical entities\")\n",
    "print(f\"Extracted {len(relationships)} relationships\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3: Build Temporal Patient Knowledge Graph\n",
    "\n",
    "Build a temporal knowledge graph from patient data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "builder = GraphBuilder()\n",
    "entity_resolver = EntityResolver()\n",
    "graph_validator = GraphValidator()\n",
    "\n",
    "resolved_entities = entity_resolver.resolve(patient_entities)\n",
    "\n",
    "patient_kg = builder.build(resolved_entities, relationships)\n",
    "\n",
    "validation_result = graph_validator.validate(patient_kg)\n",
    "\n",
    "print(f\"Built temporal patient knowledge graph\")\n",
    "print(f\"  Entities: {len(patient_kg.get('entities', []))}\")\n",
    "print(f\"  Relationships: {len(patient_kg.get('relationships', []))}\")\n",
    "print(f\"  Graph valid: {validation_result.get('valid', False)}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 4: Generate Medical Ontology\n",
    "\n",
    "Generate ontology from medical entities and relationships.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ontology_generator = OntologyGenerator()\n",
    "class_inferrer = ClassInferrer()\n",
    "property_generator = PropertyGenerator()\n",
    "ontology_validator = OntologyValidator()\n",
    "\n",
    "ontology = ontology_generator.generate_ontology({\n",
    "    \"entities\": resolved_entities,\n",
    "    \"relationships\": relationships\n",
    "}, entities=resolved_entities, relationships=relationships)\n",
    "\n",
    "classes = class_inferrer.infer_classes(resolved_entities)\n",
    "properties = property_generator.infer_properties(resolved_entities, relationships, classes)\n",
    "\n",
    "validation_result = ontology_validator.validate_ontology(ontology)\n",
    "\n",
    "print(f\"Generated medical ontology\")\n",
    "print(f\"  Classes: {len(ontology.get('classes', []))}\")\n",
    "print(f\"  Properties: {len(ontology.get('properties', []))}\")\n",
    "print(f\"  Ontology valid: {validation_result.valid}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 5: Store in Triple Store and Query\n",
    "\n",
    "Store knowledge graph in triple store and query medical history.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "triple_store = TripleStore()\n",
    "triple_manager = TripleManager()\n",
    "query_engine = QueryEngine()\n",
    "temporal_query = TemporalGraphQuery()\n",
    "\n",
    "triple_store.store_knowledge_graph(patient_kg)\n",
    "\n",
    "patient_id = \"P001\"\n",
    "start_time = \"2024-01-01\"\n",
    "end_time = \"2024-12-31\"\n",
    "\n",
    "medical_history = temporal_query.query_time_range(\n",
    "    graph=patient_kg,\n",
    "    query=f\"Find medical history for patient {patient_id}\",\n",
    "    start_time=start_time,\n",
    "    end_time=end_time\n",
    ")\n",
    "\n",
    "print(f\"Stored patient knowledge graph in triple store\")\n",
    "print(f\"Retrieved {len(medical_history.get('entities', []))} medical events for patient {patient_id}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 6: Export and Visualize\n",
    "\n",
    "Export patient data and visualize results.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rdf_exporter = RDFExporter()\n",
    "owl_exporter = OWLExporter()\n",
    "json_exporter = JSONExporter()\n",
    "\n",
    "rdf_exporter.export_knowledge_graph(patient_kg, os.path.join(temp_dir, \"patient_kg.rdf\"))\n",
    "owl_exporter.export(ontology, os.path.join(temp_dir, \"medical_ontology.owl\"))\n",
    "json_exporter.export_knowledge_graph(patient_kg, os.path.join(temp_dir, \"patient_kg.json\"))\n",
    "\n",
    "kg_visualizer = KGVisualizer()\n",
    "ontology_visualizer = OntologyVisualizer()\n",
    "temporal_visualizer = TemporalVisualizer()\n",
    "\n",
    "kg_viz = kg_visualizer.visualize_network(patient_kg, output=\"interactive\")\n",
    "ontology_viz = ontology_visualizer.visualize_hierarchy(ontology, output=\"interactive\")\n",
    "temporal_viz = temporal_visualizer.visualize_timeline(patient_kg, output=\"interactive\")\n",
    "\n",
    "print(f\"Total modules used: 20+\")\n",
    "print(f\"Pipeline complete: Patient Records → Parse → Extract → Temporal KG → Ontology → Triple Store → Query → Export → Visualize\")\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
