File size: 12,407 Bytes
79b0bef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
src/db/models.py
────────────────────────────────────────────────────────────────
SQLAlchemy ORM models β€” one class per database table.

Table overview
──────────────
  clinical_notes   : processed MTSamples / MIMIC-III notes
  entities         : NER extractions (one row per entity span)
  icd10_mappings   : entity β†’ ICD-10 code matches
  model_runs       : audit trail for classifier training runs

Design decisions
────────────────
  - All primary keys are auto-incrementing integers.  This is
    simpler than UUIDs and sufficient for this scale.

  - created_at is set by the database server, not the application,
    so it is consistent regardless of timezone or clock skew.

  - Text columns use Text() not String(N) β€” clinical notes can
    be arbitrarily long and we do not want silent truncation.

  - Relationships are defined with lazy="select" (the default)
    so they do not cause N+1 query problems in the API routes.
    Callers that need related data should use joinedload() or
    subqueryload() explicitly.

  - Every table has an updated_at column maintained by an
    onupdate trigger so we can audit changes without extra code.
────────────────────────────────────────────────────────────────
"""

from __future__ import annotations

from datetime import datetime

from sqlalchemy import (
    JSON,
    Boolean,
    DateTime,
    Float,
    ForeignKey,
    Integer,
    String,
    Text,
    func,
)
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship


class Base(DeclarativeBase):
    """Shared declarative base for all ORM models.

    Every table inherits from this so SQLAlchemy can discover
    them all when create_all_tables() is called.
    """


class ClinicalNote(Base):
    """A single processed clinical note.

    Populated by the ETL pipeline from MTSamples or MIMIC-III.
    One row per unique transcription.

    Columns
    ───────
    source_id       : stable identifier from the source dataset
                      (e.g. "mtsamples_42").  Used to detect
                      duplicate loads on re-runs.
    transcription   : cleaned note text
    specialty       : medical specialty (e.g. "Cardiology")
    note_type       : document type (e.g. "Discharge Summary")
    severity        : weak-supervision label β€” routine / urgent / critical
    word_count      : pre-computed for fast filtering
    data_source     : which dataset this came from (mtsamples / mimic3)
    """

    __tablename__ = "clinical_notes"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)

    source_id: Mapped[str] = mapped_column(
        String(120), unique=True, nullable=False, index=True,
        comment="Stable ID from the source dataset β€” prevents duplicate loads",
    )
    transcription: Mapped[str] = mapped_column(
        Text, nullable=False,
        comment="Cleaned clinical note text",
    )
    specialty: Mapped[str | None] = mapped_column(
        String(120), nullable=True, index=True,
        comment="Medical specialty (e.g. Cardiology, Neurology)",
    )
    note_type: Mapped[str | None] = mapped_column(
        String(120), nullable=True,
        comment="Document type (e.g. Discharge Summary, Consult Note)",
    )
    severity: Mapped[str | None] = mapped_column(
        String(20), nullable=True, index=True,
        comment="Weak-supervision severity label: routine / urgent / critical",
    )
    word_count: Mapped[int | None] = mapped_column(
        Integer, nullable=True,
        comment="Pre-computed word count for fast length filtering",
    )
    data_source: Mapped[str] = mapped_column(
        String(30), nullable=False, default="mtsamples",
        comment="Source dataset identifier (mtsamples or mimic3)",
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), nullable=False,
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(),
        onupdate=func.now(), nullable=False,
    )

    # A note can have many extracted entities
    entities: Mapped[list[Entity]] = relationship(
        "Entity", back_populates="note", cascade="all, delete-orphan",
    )

    def __repr__(self) -> str:
        return (
            f"ClinicalNote(id={self.id}, specialty={self.specialty!r}, "
            f"severity={self.severity!r}, words={self.word_count})"
        )


class Entity(Base):
    """A single named entity extracted from a clinical note.

    One row per entity span.  A note with ten extracted entities
    produces ten rows in this table.

    Columns
    ───────
    note_id     : foreign key to clinical_notes
    text        : the exact extracted text (e.g. "hypertension")
    label       : entity type from the NER model
                  (DISEASE, MEDICATION, PROCEDURE, SYMPTOM, ANATOMY)
    start_char  : character offset in the cleaned transcription
    end_char    : character offset end
    confidence  : model confidence score (0.0 – 1.0) where available
    """

    __tablename__ = "entities"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)

    note_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("clinical_notes.id", ondelete="CASCADE"),
        nullable=False, index=True,
    )
    text: Mapped[str] = mapped_column(
        String(500), nullable=False,
        comment="Extracted entity text as it appears in the note",
    )
    label: Mapped[str] = mapped_column(
        String(50), nullable=False, index=True,
        comment="Entity type: DISEASE, MEDICATION, PROCEDURE, SYMPTOM, ANATOMY",
    )
    start_char: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0,
        comment="Start character offset in the transcription",
    )
    end_char: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0,
        comment="End character offset in the transcription",
    )
    confidence: Mapped[float | None] = mapped_column(
        Float, nullable=True,
        comment="NER model confidence score (0.0–1.0)",
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), nullable=False,
    )

    # Back-reference to the parent note
    note: Mapped[ClinicalNote] = relationship(
        "ClinicalNote", back_populates="entities",
    )

    # An entity can have multiple ICD-10 candidate mappings
    icd10_mappings: Mapped[list[ICD10Mapping]] = relationship(
        "ICD10Mapping", back_populates="entity", cascade="all, delete-orphan",
    )

    def __repr__(self) -> str:
        return (
            f"Entity(id={self.id}, text={self.text!r}, "
            f"label={self.label!r}, conf={self.confidence:.2f})"
            if self.confidence is not None
            else f"Entity(id={self.id}, text={self.text!r}, label={self.label!r})"
        )


class ICD10Mapping(Base):
    """An ICD-10 code matched to an extracted entity.

    One entity can have multiple candidate codes (top-k results).
    The best match is the one with the highest confidence.

    Columns
    ───────
    entity_id    : foreign key to entities
    icd10_code   : ICD-10-CM code (e.g. "I10", "J18.9")
    description  : human-readable description of the code
    match_method : how the match was made β€” "lookup" (fuzzy string
                   match) or "embedding" (cosine similarity fallback)
    confidence   : match confidence (0.0 – 1.0)
    rank         : position in the top-k results (1 = best match)
    """

    __tablename__ = "icd10_mappings"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)

    entity_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("entities.id", ondelete="CASCADE"),
        nullable=False, index=True,
    )
    icd10_code: Mapped[str] = mapped_column(
        String(20), nullable=False, index=True,
        comment="ICD-10-CM diagnostic code",
    )
    description: Mapped[str] = mapped_column(
        String(500), nullable=False, default="",
        comment="Human-readable ICD-10 code description",
    )
    match_method: Mapped[str] = mapped_column(
        String(20), nullable=False, default="lookup",
        comment="How the match was found: lookup or embedding",
    )
    confidence: Mapped[float] = mapped_column(
        Float, nullable=False, default=0.0,
        comment="Match confidence score (0.0–1.0)",
    )
    rank: Mapped[int] = mapped_column(
        Integer, nullable=False, default=1,
        comment="Position in top-k candidates (1 = best match)",
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), nullable=False,
    )

    # Back-reference to the parent entity
    entity: Mapped[Entity] = relationship(
        "Entity", back_populates="icd10_mappings",
    )

    def __repr__(self) -> str:
        return (
            f"ICD10Mapping(code={self.icd10_code!r}, "
            f"method={self.match_method!r}, conf={self.confidence:.2f})"
        )


class ModelRun(Base):
    """Audit record for a classifier training run.

    Tracks which model was trained, when, on how many samples,
    and what performance metrics it achieved.  Useful for
    comparing successive fine-tuning runs.

    Columns
    ───────
    model_name    : HuggingFace model ID used as base
    task          : classification task (severity / readmission)
    training_samples : number of training examples
    val_accuracy  : validation accuracy at end of training
    val_f1        : weighted F1 score on validation set
    epochs        : number of training epochs completed
    notes         : free-text notes about the run
    """

    __tablename__ = "model_runs"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)

    model_name: Mapped[str] = mapped_column(
        String(200), nullable=False,
        comment="Base model used for fine-tuning",
    )
    task: Mapped[str] = mapped_column(
        String(50), nullable=False, index=True,
        comment="Classification task: severity or readmission",
    )
    training_samples: Mapped[int | None] = mapped_column(
        Integer, nullable=True,
    )
    val_accuracy: Mapped[float | None] = mapped_column(
        Float, nullable=True,
        comment="Validation accuracy (0.0–1.0)",
    )
    val_f1: Mapped[float | None] = mapped_column(
        Float, nullable=True,
        comment="Weighted F1 score on validation set",
    )
    test_accuracy: Mapped[float | None] = mapped_column(
        Float, nullable=True,
        comment="Held-out test set accuracy (0.0-1.0)",
    )
    test_f1: Mapped[float | None] = mapped_column(
        Float, nullable=True,
        comment="Weighted F1 score on the held-out test set",
    )
    per_class: Mapped[dict | None] = mapped_column(
        JSON, nullable=True,
        comment="Per-class {label: {precision, recall, f1, support}} on the test set",
    )
    confusion_matrix: Mapped[list | None] = mapped_column(
        JSON, nullable=True,
        comment="Test-set confusion matrix, rows=actual/cols=predicted, class order matches ClassifierConfig labels",
    )
    history: Mapped[list | None] = mapped_column(
        JSON, nullable=True,
        comment="Per-epoch [{epoch, loss, accuracy, f1}] validation history",
    )
    epochs: Mapped[int | None] = mapped_column(
        Integer, nullable=True,
    )
    is_deployed: Mapped[bool] = mapped_column(
        Boolean, nullable=False, default=False,
        comment="Whether this run is the currently deployed model",
    )
    run_notes: Mapped[str | None] = mapped_column(
        Text, nullable=True,
        comment="Free-text notes about training conditions or observations",
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), nullable=False,
    )

    def __repr__(self) -> str:
        return (
            f"ModelRun(id={self.id}, task={self.task!r}, "
            f"val_f1={self.val_f1}, deployed={self.is_deployed})"
        )