[96a5a0]: / src / models / logical_criteria.py

Download this file

264 lines (218 with data), 8.6 kB

  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
# models/logical_criteria.py
"""
Data Models for Logical Structured Clinical Trial Criteria
This module defines the Pydantic models used to represent the logical relationships
between atomic criteria extracted from clinical trial eligibility criteria.
Classes prefixed with 'LLM' are used as output formats for LLM processing, while
other classes are used for internal data storage.
Classes:
LLMLogicalAnd: Logical AND relationship between criteria.
LLMLogicalOr: Logical OR relationship between criteria.
LLMLogicalNot: Logical NOT operation on criteria.
LLMLogicalXor: Logical XOR relationship between criteria.
LLMLogicalConditional: Conditional (if-then-else) relationship.
LLMLogicalWrapperResponse: Container for LLM logical structure response.
LogicalLine: Line with identified criteria and logical structure.
LogicalTrial: Complete trial with logically structured criteria.
Important:
The docstrings of Pydantic models are used as prompts for LLM processing.
Changing these docstrings will alter how the LLM interprets the output format.
Use code comments rather than docstring modifications if documentation changes
are needed without affecting LLM behavior.
"""
from typing import List, Union
from pydantic import BaseModel, Field
from src.models.identified_criteria import (
RawTrialData,
Requirement,
)
class SingleRequirementCriterion(BaseModel):
"""
Represents an atomic criterion with a single requirement extracted from the eligibility criteria.
example:
input:
"Tissue from tumor must be available and > 2 cm in diameter.",
output:
"exact_snippets": "Tissue from tumor must be available ... > 2 cm in diameter.",
"criterion": "tumor tissue",
"requirement": {
"requirement_type": "availability",
"expected_value": true
}
"""
exact_snippets: str = Field(
...,
description="Exact text snippets from the eligibility criteria that were used to extract this criterion, using ellipses (...) for non-consecutive text.",
)
criterion: str = Field(
...,
description="The specific property, attribute, or condition that is being tested (e.g., 'age', 'lung cancer', 'BMI').",
)
requirement: Requirement = Field(
..., description="The requirement and its expected value for the criterion."
)
# needed to make this hashable for validating logical structures
def __eq__(self, other):
if isinstance(other, SingleRequirementCriterion):
return str(self) == str(other)
return False
def __hash__(self):
return hash((tuple(self.exact_snippets), self.criterion, self.requirement))
class IdentifiedUnrolledLine(BaseModel):
"""
Represents a structured line of eligibility criteria.
"""
line: str = Field(..., description="The original line of eligibility criteria.")
criterions: List[SingleRequirementCriterion] = Field(
..., description="List of structured criteria."
)
class LLMLogicalAnd(BaseModel):
"""
Represents a logical AND relationship between criteria.
"""
and_criteria: List[
Union[
SingleRequirementCriterion,
"LLMLogicalAnd",
"LLMLogicalOr",
"LLMLogicalNot",
"LLMLogicalXor",
"LLMLogicalConditional",
]
] = Field(..., description="The criteria involved in the relationship.")
class LLMLogicalOr(BaseModel):
"""
Represents a logical OR relationship between criteria.
"""
or_criteria: List[
Union[
SingleRequirementCriterion,
"LLMLogicalAnd",
"LLMLogicalOr",
"LLMLogicalNot",
"LLMLogicalXor",
"LLMLogicalConditional",
]
] = Field(..., description="The criteria involved in the relationship.")
class LLMLogicalNot(BaseModel):
"""
Represents a logical NOT operation on a criterion or logical expression. Ensure that negation isn't already implicit in the criterion's value (e.g., 'must not be older than 18' is already represented by 'age ≤ 18 and no need for additional LLMLogicalNot').
"""
not_criteria: Union[
SingleRequirementCriterion,
"LLMLogicalAnd",
"LLMLogicalOr",
"LLMLogicalNot",
"LLMLogicalXor",
"LLMLogicalConditional",
] = Field(..., description="The criteria involved in the relationship.")
class LLMLogicalXor(BaseModel):
"""
Represents a logical XOR relationship between criteria.
"""
xor_criteria: List[
Union[
SingleRequirementCriterion,
"LLMLogicalAnd",
"LLMLogicalOr",
"LLMLogicalNot",
"LLMLogicalXor",
"LLMLogicalConditional",
]
] = Field(..., description="The criteria involved in the relationship.")
class LLMLogicalConditional(BaseModel):
"""
Represents a conditional relationship between criteria.
"""
condition: Union[
SingleRequirementCriterion,
"LLMLogicalAnd",
"LLMLogicalOr",
"LLMLogicalNot",
"LLMLogicalXor",
"LLMLogicalConditional",
] = Field(..., description="The condition criterion (antecedent)")
then_criteria: Union[
SingleRequirementCriterion,
"LLMLogicalAnd",
"LLMLogicalOr",
"LLMLogicalNot",
"LLMLogicalXor",
"LLMLogicalConditional",
None,
] = Field(
...,
description="The criteria that apply if the condition (antecedent) is met. (consequent)",
)
else_criteria: Union[
SingleRequirementCriterion,
"LLMLogicalAnd",
"LLMLogicalOr",
"LLMLogicalNot",
"LLMLogicalXor",
"LLMLogicalConditional",
None,
] = Field(
...,
description="The criteria that apply if the condition (antecedent) is not met (optional consequent).",
)
# Rebuild model schemas to resolve forward references in the Union types
LLMLogicalAnd.model_rebuild()
LLMLogicalOr.model_rebuild()
LLMLogicalNot.model_rebuild()
LLMLogicalXor.model_rebuild()
LLMLogicalConditional.model_rebuild()
# This wrapper is necessary because the LLM needs a single type to generate,
# not a Union of possible logical relation types
class LLMLogicalWrapperResponse(BaseModel):
"""
Represents the response from the LLM.
"""
logicalRepresentation: Union[
LLMLogicalAnd, LLMLogicalOr, LLMLogicalNot, LLMLogicalXor, LLMLogicalConditional
] = Field(..., description="The logical representation of the criteria.")
class LogicalLine(BaseModel):
"""
Represents a line of eligibility criteria that has been logically structured.
"""
identified_line: IdentifiedUnrolledLine = Field(
..., description="The identified line this was made from."
)
logical_structure: Union[
SingleRequirementCriterion,
LLMLogicalAnd,
LLMLogicalOr,
LLMLogicalNot,
LLMLogicalXor,
LLMLogicalConditional,
] = Field(..., description="The logically structured Criteria.")
# Note: We don't use a Pydantic validator here to check if the logical_structure
# includes all of the identified_line's criteria because we want to be able to
# store failed lines. This validation happens in the "logify" procedure instead.
class LogicalTrial(BaseModel):
"""
Represents a complete trial with logically structured eligibility criteria.
"""
info: RawTrialData = Field(..., description="Raw data of the clinical trial.")
inclusion_lines: List[LogicalLine] = Field(
..., description="List of inclusion lines successfully logically structurized."
)
exclusion_lines: List[LogicalLine] = Field(
..., description="List of exclusion lines successfully logically structurized."
)
miscellaneous_lines: List[LogicalLine] = Field(
...,
description="List of miscellaneous lines successfully logically structurized.",
)
failed_inclusion: List[LogicalLine] = Field(
...,
description="List of inclusion lines that failed to be logically structurized.",
)
failed_exclusion: List[LogicalLine] = Field(
...,
description="List of exclusion lines that failed to be logically structurized.",
)
failed_miscellaneous: List[LogicalLine] = Field(
...,
description="List of miscellaneous lines that failed to be logically structurized.",
)