Download src/core/persistence/models.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 10.7 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/persistence/models.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/core/persistence/models.py
-
curl -L -o models.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/persistence/models.py
10.7 kB
| """ | |
| Core Persistence Models | |
| Database models for YAZ Care platform with gastric cancer study support | |
| """ | |
| from sqlalchemy import ( | |
| Column, | |
| Integer, | |
| String, | |
| Float, | |
| Date, | |
| DateTime, | |
| Text, | |
| Boolean, | |
| ForeignKey, | |
| JSON, | |
| ) | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy.orm import relationship | |
| from datetime import datetime, date | |
| from typing import Optional, Dict, Any | |
| Base = declarative_base() | |
| class Cohort(Base): # type: ignore[misc, operator, valid-type] | |
| """Cohort model for study groups""" | |
| __tablename__ = "cohorts" | |
| id = Column(String(50), primary_key=True) | |
| name = Column(String(200), nullable=False) | |
| description = Column(Text) | |
| study_type = Column(String(50), default="gastric_cancer") | |
| status = Column(String(20), default="active") | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| last_updated = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) | |
| created_by = Column(String(50)) | |
| filters = Column(JSON) # ElectricSQL compatible JSON field | |
| # Relationships | |
| cases = relationship("Case", back_populates="cohort", cascade="all, delete-orphan") | |
| snapshots = relationship("Snapshot", back_populates="cohort", cascade="all, delete-orphan") | |
| class Case(Base): # type: ignore[misc, operator, valid-type] | |
| """Patient case model""" | |
| __tablename__ = "cases" | |
| id = Column(String(50), primary_key=True) | |
| cohort_id = Column(String(50), ForeignKey("cohorts.id"), nullable=False) | |
| patient_key = Column(String(50), nullable=False) # De-identified patient ID | |
| age = Column(Integer) | |
| gender = Column(String(10)) | |
| surgery_date = Column(Date) | |
| diagnosis = Column(String(100), default="gastric_adenocarcinoma") | |
| stage = Column(String(20)) | |
| tumor_size = Column(Float) # cm | |
| histology_type = Column(String(50)) | |
| tumor_grade = Column(String(20)) | |
| lauren_type = Column(String(30)) # Lauren classification | |
| her2_status = Column(String(20)) | |
| msi_status = Column(String(20)) | |
| ecog_ps = Column(Integer) # Performance status | |
| comorbidity_score = Column(Integer) | |
| weight_loss_percent = Column(Float) | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) | |
| # Relationships | |
| cohort = relationship("Cohort", back_populates="cases") | |
| procedures = relationship("Procedure", back_populates="case", cascade="all, delete-orphan") | |
| chemotherapy = relationship("Chemotherapy", back_populates="case", cascade="all, delete-orphan") | |
| outcomes = relationship("Outcome", back_populates="case", cascade="all, delete-orphan") | |
| flags = relationship("CaseFlag", back_populates="case", cascade="all, delete-orphan") | |
| class Procedure(Base): # type: ignore[misc, operator, valid-type] | |
| """Surgical procedure model""" | |
| __tablename__ = "procedures" | |
| id = Column(String(50), primary_key=True) | |
| case_id = Column(String(50), ForeignKey("cases.id"), nullable=False) | |
| procedure_type = Column(String(100), nullable=False) | |
| surgical_approach = Column(String(30)) # open, laparoscopic, robotic | |
| primary_surgeon = Column(String(100)) | |
| treating_hospital = Column(String(100)) | |
| annual_case_volume = Column(Integer) # Surgeon's annual volume | |
| nodes_examined = Column(Integer) | |
| node_stations = Column(JSON) # Array of station numbers | |
| margin_status = Column(String(20)) # negative, positive, close | |
| blood_loss_ml = Column(Integer) | |
| operative_time_min = Column(Integer) | |
| length_of_stay_days = Column(Integer) | |
| icu_admission = Column(Boolean, default=False) | |
| readmission_30d = Column(Boolean, default=False) | |
| complications = Column(JSON) # Array of complication types | |
| reconstruction_type = Column(String(50)) | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) | |
| # Relationships | |
| case = relationship("Case", back_populates="procedures") | |
| class Chemotherapy(Base): # type: ignore[misc, operator, valid-type] | |
| """Chemotherapy treatment model""" | |
| __tablename__ = "chemotherapy" | |
| id = Column(String(50), primary_key=True) | |
| case_id = Column(String(50), ForeignKey("cases.id"), nullable=False) | |
| regimen = Column(String(50), nullable=False) # FLOT, etc. | |
| neoadjuvant = Column(Boolean, default=True) | |
| cycles_planned = Column(Integer) | |
| cycles_completed = Column(Integer) | |
| dose_intensity = Column(Float) # 0.0 to 1.0 | |
| start_date = Column(Date) | |
| end_date = Column(Date) | |
| adjuvant_start_date = Column(Date) | |
| adjuvant_end_date = Column(Date) | |
| treatment_delays = Column(JSON) # Array of delay records | |
| dose_modifications = Column(JSON) # Array of dose modification records | |
| toxicities = Column(JSON) # Array of toxicity records | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) | |
| # Relationships | |
| case = relationship("Case", back_populates="chemotherapy") | |
| class Outcome(Base): # type: ignore[misc, operator, valid-type] | |
| """Patient outcome model""" | |
| __tablename__ = "outcomes" | |
| id = Column(String(50), primary_key=True) | |
| case_id = Column(String(50), ForeignKey("cases.id"), nullable=False) | |
| survival_months = Column(Float) | |
| survival_status = Column(String(20)) # alive, dead, lost | |
| recurrence_date = Column(Date) | |
| recurrence_site = Column(String(50)) | |
| last_followup_date = Column(Date) | |
| followup_status = Column(String(20)) # active, lost, complete | |
| cause_of_death = Column(String(100)) | |
| quality_of_life_scores = Column(JSON) # QoL assessment scores | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) | |
| # Relationships | |
| case = relationship("Case", back_populates="outcomes") | |
| class Snapshot(Base): # type: ignore[misc, operator, valid-type] | |
| """Cohort snapshot model for versioning""" | |
| __tablename__ = "snapshots" | |
| id = Column(String(50), primary_key=True) | |
| cohort_id = Column(String(50), ForeignKey("cohorts.id"), nullable=False) | |
| snapshot_type = Column(String(30), nullable=False) # baseline, optimized, filtered | |
| dsl_hash = Column(String(64)) # Hash of the filter DSL | |
| case_count = Column(Integer, default=0) | |
| filters = Column(JSON) | |
| snapshot_metadata = Column(JSON) # Renamed from metadata to avoid SQLAlchemy conflict | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| created_by = Column(String(50)) | |
| # Relationships | |
| cohort = relationship("Cohort", back_populates="snapshots") | |
| class Job(Base): # type: ignore[misc, operator, valid-type] | |
| """Background job model""" | |
| __tablename__ = "jobs" | |
| id = Column(String(50), primary_key=True) | |
| type = Column(String(50), nullable=False) # validation, impact, optimization | |
| status = Column(String(20), default="pending") # pending, running, completed, failed | |
| progress = Column(Integer, default=0) # 0-100 | |
| payload = Column(JSON) # Job input data | |
| result = Column(JSON) # Job output data | |
| error = Column(Text) | |
| started_at = Column(DateTime) | |
| completed_at = Column(DateTime) | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| created_by = Column(String(50)) | |
| class Report(Base): # type: ignore[misc, operator, valid-type] | |
| """Generated report model""" | |
| __tablename__ = "reports" | |
| id = Column(String(50), primary_key=True) | |
| cohort_id = Column(String(50), ForeignKey("cohorts.id")) | |
| snapshot_id = Column(String(50), ForeignKey("snapshots.id")) | |
| report_type = Column(String(50), nullable=False) # pdf, csv, clinical_report | |
| title = Column(String(200)) | |
| file_path = Column(String(500)) | |
| url_key = Column(String(100)) # For signed URL access | |
| file_size = Column(Integer) | |
| mime_type = Column(String(100)) | |
| file_metadata = Column(JSON) # Renamed from metadata to avoid SQLAlchemy conflict | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| created_by = Column(String(50)) | |
| expires_at = Column(DateTime) # For temporary reports | |
| class CaseFlag(Base): # type: ignore[misc, operator, valid-type] | |
| """Case-level flags and annotations""" | |
| __tablename__ = "case_flags" | |
| id = Column(String(50), primary_key=True) | |
| case_id = Column(String(50), ForeignKey("cases.id"), nullable=False) | |
| flag_type = Column(String(50), nullable=False) # quality, protocol_deviation, etc. | |
| severity = Column(String(20)) # critical, major, minor, info | |
| message = Column(Text) | |
| flag_metadata = Column(JSON) # Renamed from metadata to avoid SQLAlchemy conflict | |
| resolved = Column(Boolean, default=False) | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| created_by = Column(String(50)) | |
| resolved_at = Column(DateTime) | |
| resolved_by = Column(String(50)) | |
| # Relationships | |
| case = relationship("Case", back_populates="flags") | |
| class ValidationResult(Base): # type: ignore[misc, operator, valid-type] | |
| """Validation result model""" | |
| __tablename__ = "validation_results" | |
| id = Column(String(50), primary_key=True) | |
| cohort_id = Column(String(50), ForeignKey("cohorts.id")) | |
| snapshot_id = Column(String(50), ForeignKey("snapshots.id")) | |
| validation_type = Column(String(50), nullable=False) # protocol, quality, completeness | |
| is_valid = Column(Boolean, default=True) | |
| errors = Column(JSON) # Array of error messages | |
| warnings = Column(JSON) # Array of warning messages | |
| validated_at = Column(DateTime, default=datetime.utcnow) | |
| validator_version = Column(String(20)) | |
| validation_metadata = Column(JSON) | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| created_by = Column(String(50)) | |
| # Indexes for performance | |
| from sqlalchemy import Index | |
| # Create indexes for common queries | |
| Index("idx_cases_cohort_id", Case.cohort_id) | |
| Index("idx_cases_surgery_date", Case.surgery_date) | |
| Index("idx_procedures_case_id", Procedure.case_id) | |
| Index("idx_chemotherapy_case_id", Chemotherapy.case_id) | |
| Index("idx_outcomes_case_id", Outcome.case_id) | |
| Index("idx_snapshots_cohort_id", Snapshot.cohort_id) | |
| Index("idx_jobs_status", Job.status) | |
| Index("idx_jobs_type", Job.type) | |
| Index("idx_reports_cohort_id", Report.cohort_id) | |
| Index("idx_case_flags_case_id", CaseFlag.case_id) | |
| # Composite indexes for complex queries | |
| Index("idx_cases_cohort_date", Case.cohort_id, Case.surgery_date) | |
| Index("idx_procedures_type_date", Procedure.procedure_type, Procedure.case_id) | |
| Index("idx_chemo_regimen_date", Chemotherapy.regimen, Chemotherapy.start_date) | |