glirel¶
knowledge_graph.clinical.glirel
Clinical relation extraction with GLiREL: inference and scoring for any checkpoint (predict), and adaptation (finetune).
python -m knowledge_graph.clinical.glirel predict --entities gold --out-dir DIR # zero-shot, 100 notes
python -m knowledge_graph.clinical.glirel predict --entities predicted --notes test ... # fine-tuned GLiNER mentions
python -m knowledge_graph.clinical.glirel finetune --output-dir DIR # train on 80 notes, select on 10
Every run covers the six input conditions; relations predicted in different inputs are pooled per note.
predict_relations(model, tokens, entities)
¶
GLiREL relations between token-level entity spans (inclusive ends).
Source code in src/knowledge_graph/clinical/glirel.py
29 30 31 | |
Mention
¶
Bases: NamedTuple
A candidate entity mention at note offsets.
Source code in src/knowledge_graph/clinical/glirel.py
34 35 36 37 38 39 40 41 42 | |
glirel_input(doc, segment, mentions)
¶
Align mentions to a tokenized segment; return token text, NER spans, and the entity key of each token span.
Mentions outside the segment are skipped, other boundaries expand to whole tokens, and two mentions on the same tokens keep the exact one, then the lower rank, then the earlier span.
Source code in src/knowledge_graph/clinical/glirel.py
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 | |
reference_mentions(annotation)
¶
Reference mentions keyed by their entity.
Source code in src/knowledge_graph/clinical/glirel.py
74 75 76 77 78 79 80 | |
predicted_mentions(annotation, predictions, threshold)
¶
Predicted mentions above the threshold; exact spans map to reference entities for scoring.
Source code in src/knowledge_graph/clinical/glirel.py
83 84 85 86 87 88 89 90 91 92 93 94 | |
predict_note(model, nlp, note_id, condition, ner)
¶
Relations of one note pooled over its inputs; mentions are the reference ones when ner is None.
Source code in src/knowledge_graph/clinical/glirel.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
predict(args)
¶
Relations of every note and input condition with one checkpoint, scored per condition.
Source code in src/knowledge_graph/clinical/glirel.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
training_examples(ids, nlp, condition='fixed256')
¶
Reference-mention training examples with multi-hot relation labels per directed mention pair.
Source code in src/knowledge_graph/clinical/glirel.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
example_loss(model, example)
¶
Source code in src/knowledge_graph/clinical/glirel.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
validation_scores(model, examples, gold)
¶
Source code in src/knowledge_graph/clinical/glirel.py
188 189 190 191 192 193 194 195 196 197 | |
finetune(args)
¶
Train on fixed256 inputs with reference mentions; keep the best pooled validation relation-F1 checkpoint.
Source code in src/knowledge_graph/clinical/glirel.py
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 | |
main()
¶
Source code in src/knowledge_graph/clinical/glirel.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |