doseframe 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- doseframe/__init__.py +252 -0
- doseframe/calculation/__init__.py +34 -0
- doseframe/calculation/derivation.py +30 -0
- doseframe/calculation/errors.py +16 -0
- doseframe/calculation/infusion.py +27 -0
- doseframe/calculation/relation.py +129 -0
- doseframe/calculation/single_dose.py +54 -0
- doseframe/calculation/steps.py +20 -0
- doseframe/calculation/strategies.py +105 -0
- doseframe/calculation/strategy.py +37 -0
- doseframe/checks/__init__.py +20 -0
- doseframe/checks/chain.py +212 -0
- doseframe/checks/context.py +43 -0
- doseframe/findings/__init__.py +58 -0
- doseframe/protocols/__init__.py +103 -0
- doseframe/protocols/activation.py +51 -0
- doseframe/protocols/device.py +51 -0
- doseframe/protocols/diff.py +70 -0
- doseframe/protocols/errors.py +26 -0
- doseframe/protocols/limits.py +74 -0
- doseframe/protocols/model.py +40 -0
- doseframe/protocols/naming.py +126 -0
- doseframe/protocols/reference.py +163 -0
- doseframe/protocols/schemes.py +598 -0
- doseframe/protocols/single_dose.py +253 -0
- doseframe/protocols/version.py +690 -0
- doseframe/py.typed +0 -0
- doseframe/rounding/__init__.py +23 -0
- doseframe/rounding/policy.py +108 -0
- doseframe/units/__init__.py +49 -0
- doseframe/units/catalog.py +155 -0
- doseframe/units/decimal_context.py +17 -0
- doseframe/units/errors.py +27 -0
- doseframe/units/options.py +52 -0
- doseframe/units/quantity.py +144 -0
- doseframe/units/unit.py +137 -0
- doseframe/use_cases/__init__.py +73 -0
- doseframe/use_cases/clinical.py +735 -0
- doseframe/use_cases/gate.py +85 -0
- doseframe/use_cases/governance.py +460 -0
- doseframe/use_cases/ports.py +73 -0
- doseframe/use_cases/results.py +182 -0
- doseframe-0.2.0.dist-info/METADATA +79 -0
- doseframe-0.2.0.dist-info/RECORD +46 -0
- doseframe-0.2.0.dist-info/WHEEL +4 -0
- doseframe-0.2.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
"""Hot-spot 3: preparation schemes (Factory Method), stock solutions, and the preparation.
|
|
2
|
+
|
|
3
|
+
A scheme holds the approved preparation data of one protocol version. It knows which formula
|
|
4
|
+
belongs to it (`create_strategy`, the factory method), how the syringe is drawn up for a weight
|
|
5
|
+
(`prepare`), and whether its own data is complete and consistent (`validate`).
|
|
6
|
+
|
|
7
|
+
"ad 50 ml" means: fill up *to* a final volume of 50 ml, not 50 ml plus the drug.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from abc import ABC, abstractmethod
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from enum import StrEnum
|
|
15
|
+
from typing import ClassVar
|
|
16
|
+
|
|
17
|
+
from doseframe.calculation import (
|
|
18
|
+
CalculationStrategy,
|
|
19
|
+
DividedDailyDoseStrategy,
|
|
20
|
+
IndividualPreparationStrategy,
|
|
21
|
+
SingleDoseStrategy,
|
|
22
|
+
StandardConcentrationStrategy,
|
|
23
|
+
StructuralError,
|
|
24
|
+
TwoStepPreparationStrategy,
|
|
25
|
+
)
|
|
26
|
+
from doseframe.calculation.relation import require_positive
|
|
27
|
+
from doseframe.findings import UNIT_ROLE, Finding, Severity
|
|
28
|
+
from doseframe.units import Quantity, Role, Unit, UnitError, is_allowed
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True, slots=True)
|
|
32
|
+
class StockSolution:
|
|
33
|
+
"""An approved product or solution the drug is drawn from, such as "Suprarenin 1 mg/ml".
|
|
34
|
+
|
|
35
|
+
Several strengths of one drug exist (Midazolam 1 or 5 mg/ml, insulin U40 or U100); the chosen
|
|
36
|
+
one stays visible in every result, and the withdrawal volume follows from it.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
label: str
|
|
40
|
+
strength: Quantity
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Workflow(StrEnum):
|
|
44
|
+
"""The clinical workflow a scheme belongs to; a use case refuses a scheme of another one."""
|
|
45
|
+
|
|
46
|
+
INFUSION = "infusion"
|
|
47
|
+
SINGLE_DOSE = "single_dose"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class StepKind(StrEnum):
|
|
51
|
+
DRAW = "draw" # draw a volume of a solution into the syringe
|
|
52
|
+
FILL_UP = "fill_up" # fill up with diluent to the final volume
|
|
53
|
+
DILUTE = "dilute" # prepare an intermediate stock solution
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True, slots=True)
|
|
57
|
+
class PreparationStep:
|
|
58
|
+
kind: StepKind
|
|
59
|
+
substance: str
|
|
60
|
+
volume: Quantity
|
|
61
|
+
amount: Quantity | None = None
|
|
62
|
+
total_volume: Quantity | None = None
|
|
63
|
+
concentration: Quantity | None = None
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass(frozen=True, slots=True)
|
|
67
|
+
class Preparation:
|
|
68
|
+
"""How one syringe is drawn up, with every value carrying its unit."""
|
|
69
|
+
|
|
70
|
+
stock: StockSolution
|
|
71
|
+
drug_amount: Quantity
|
|
72
|
+
final_volume: Quantity
|
|
73
|
+
final_concentration: Quantity
|
|
74
|
+
steps: tuple[PreparationStep, ...]
|
|
75
|
+
intermediate_concentration: Quantity | None = None
|
|
76
|
+
shelf_life_note: str | None = None
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def drawn_volume(self) -> Quantity:
|
|
80
|
+
"""The volume of drug solution in the syringe (everything except the fill-up)."""
|
|
81
|
+
draws = [s.volume for s in self.steps if s.kind is StepKind.DRAW]
|
|
82
|
+
total = draws[0]
|
|
83
|
+
for volume in draws[1:]:
|
|
84
|
+
total = total + volume
|
|
85
|
+
return total
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@dataclass(frozen=True, slots=True)
|
|
89
|
+
class DosePreparation:
|
|
90
|
+
"""How one dose is drawn up from a made-up product, such as 110 mg from 100 mg/ml = 1.1 ml."""
|
|
91
|
+
|
|
92
|
+
stock: StockSolution
|
|
93
|
+
amount: Quantity
|
|
94
|
+
volume: Quantity
|
|
95
|
+
steps: tuple[PreparationStep, ...]
|
|
96
|
+
diluent: str | None = None
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
100
|
+
class PreparationScheme(ABC):
|
|
101
|
+
"""Abstract base of all preparation schemes. Subclasses declare their own fields."""
|
|
102
|
+
|
|
103
|
+
key: ClassVar[str]
|
|
104
|
+
workflow: ClassVar[Workflow]
|
|
105
|
+
|
|
106
|
+
diluent: str
|
|
107
|
+
stock_solutions: tuple[StockSolution, ...]
|
|
108
|
+
concentration_unit: Unit | None = None
|
|
109
|
+
"""The approved unit for showing the final concentration, such as µg/ml for 1.5 mg in 50 ml."""
|
|
110
|
+
|
|
111
|
+
def validate(self) -> list[Finding]:
|
|
112
|
+
"""Findings about the scheme's own data. A BLOCK here prevents approval (guard G1).
|
|
113
|
+
|
|
114
|
+
Subclasses call this as `PreparationScheme.validate(self)`: a zero-argument `super()`
|
|
115
|
+
fails inside slotted dataclasses on early Python 3.13 patch releases.
|
|
116
|
+
"""
|
|
117
|
+
findings: list[Finding] = []
|
|
118
|
+
if not self.diluent.strip():
|
|
119
|
+
findings.append(_missing("diluent"))
|
|
120
|
+
if not self.stock_solutions:
|
|
121
|
+
findings.append(_missing("stock_solutions"))
|
|
122
|
+
if self.concentration_unit is not None and not is_allowed(
|
|
123
|
+
self.concentration_unit, Role.CONCENTRATION
|
|
124
|
+
):
|
|
125
|
+
findings.append(
|
|
126
|
+
Finding(
|
|
127
|
+
UNIT_ROLE,
|
|
128
|
+
Severity.BLOCK,
|
|
129
|
+
f"{self.concentration_unit.symbol} is not a concentration unit",
|
|
130
|
+
{"field": "concentration_unit", "unit": self.concentration_unit.symbol},
|
|
131
|
+
)
|
|
132
|
+
)
|
|
133
|
+
labels = [s.label for s in self.stock_solutions]
|
|
134
|
+
if len(set(labels)) != len(labels):
|
|
135
|
+
findings.append(
|
|
136
|
+
Finding("duplicate_stock", Severity.BLOCK, "stock solution labels must be unique")
|
|
137
|
+
)
|
|
138
|
+
for stock in self.stock_solutions:
|
|
139
|
+
if not stock.label.strip():
|
|
140
|
+
findings.append(_missing("stock_solutions.label"))
|
|
141
|
+
findings += _check_quantity(stock.strength, Role.CONCENTRATION, "stock.strength")
|
|
142
|
+
return findings
|
|
143
|
+
|
|
144
|
+
def named_strength(self) -> Quantity | None:
|
|
145
|
+
"""The strength the standard protocol name shows (V3-12); None if the scheme has none."""
|
|
146
|
+
return None
|
|
147
|
+
|
|
148
|
+
def named_carrier(self) -> str | None:
|
|
149
|
+
"""The carrier the standard name shows: the fill-up solution of the syringe."""
|
|
150
|
+
return self.diluent.strip() or None
|
|
151
|
+
|
|
152
|
+
names_syringe: ClassVar[bool] = False
|
|
153
|
+
"""Whether the standard name shows the syringe instead of the carrier (single doses)."""
|
|
154
|
+
|
|
155
|
+
def stock(self, label: str) -> StockSolution:
|
|
156
|
+
"""The approved stock solution with this label."""
|
|
157
|
+
for stock in self.stock_solutions:
|
|
158
|
+
if stock.label == label:
|
|
159
|
+
return stock
|
|
160
|
+
raise StructuralError("unknown_stock", f"{label!r} is not an approved stock solution")
|
|
161
|
+
|
|
162
|
+
def shown(self, concentration: Quantity) -> Quantity:
|
|
163
|
+
"""A concentration in the approved display unit, or in drug unit per ml."""
|
|
164
|
+
if self.concentration_unit is not None:
|
|
165
|
+
return concentration.to(self.concentration_unit)
|
|
166
|
+
return concentration.to(f"{concentration.unit.terms[0][0].symbol}/ml")
|
|
167
|
+
|
|
168
|
+
def _require_known(self, stock: StockSolution) -> None:
|
|
169
|
+
if stock not in self.stock_solutions:
|
|
170
|
+
raise StructuralError(
|
|
171
|
+
"unknown_stock", f"{stock.label!r} is not an approved stock solution"
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
176
|
+
class InfusionScheme(PreparationScheme):
|
|
177
|
+
"""A syringe for a perfusor, filled up to a final volume; its strategy gives a pump rate."""
|
|
178
|
+
|
|
179
|
+
workflow: ClassVar[Workflow] = Workflow.INFUSION
|
|
180
|
+
|
|
181
|
+
final_volume: Quantity
|
|
182
|
+
|
|
183
|
+
@abstractmethod
|
|
184
|
+
def create_strategy(self) -> CalculationStrategy:
|
|
185
|
+
"""Factory method: the calculation strategy that belongs to this scheme."""
|
|
186
|
+
|
|
187
|
+
@abstractmethod
|
|
188
|
+
def prepare(self, weight: Quantity, stock: StockSolution) -> Preparation:
|
|
189
|
+
"""How the syringe is drawn up for this weight, from the chosen stock solution."""
|
|
190
|
+
|
|
191
|
+
def validate(self) -> list[Finding]:
|
|
192
|
+
return _check_quantity(self.final_volume, Role.VOLUME, "final_volume") + (
|
|
193
|
+
PreparationScheme.validate(self) # not super(): see PreparationScheme.validate
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
def reference_dose(self) -> Quantity | None:
|
|
197
|
+
"""The dose per kg at the reference rate, for schemes that have one."""
|
|
198
|
+
return None
|
|
199
|
+
|
|
200
|
+
def _per_kg_strength(self, factor: Quantity) -> Quantity:
|
|
201
|
+
"""A per-kg amount made up to the final volume, e.g. 5 IU/kg ad 50 ml = 0.1 IU/kg/ml."""
|
|
202
|
+
per_kg = factor / self.final_volume
|
|
203
|
+
if self.concentration_unit is None:
|
|
204
|
+
return per_kg
|
|
205
|
+
amount = self.concentration_unit.terms[0][0].symbol
|
|
206
|
+
try:
|
|
207
|
+
return per_kg.to(f"{amount}/kg/ml")
|
|
208
|
+
except UnitError:
|
|
209
|
+
return per_kg
|
|
210
|
+
|
|
211
|
+
def implied_preparation_weight(self, drug_amount: Quantity) -> Quantity | None:
|
|
212
|
+
"""The weight a syringe with this drug amount was prepared for, if the amount scales.
|
|
213
|
+
|
|
214
|
+
For a standard concentration the amount does not depend on weight, so there is none.
|
|
215
|
+
"""
|
|
216
|
+
return None
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
220
|
+
class StandardConcentrationScheme(InfusionScheme):
|
|
221
|
+
"""A fixed drug amount made up to the final volume: the same syringe for every patient."""
|
|
222
|
+
|
|
223
|
+
key: ClassVar[str] = "standard_concentration"
|
|
224
|
+
|
|
225
|
+
drug_amount: Quantity
|
|
226
|
+
|
|
227
|
+
@property
|
|
228
|
+
def final_concentration(self) -> Quantity:
|
|
229
|
+
return self.shown(self.drug_amount / self.final_volume)
|
|
230
|
+
|
|
231
|
+
def named_strength(self) -> Quantity | None:
|
|
232
|
+
return self.final_concentration
|
|
233
|
+
|
|
234
|
+
def create_strategy(self) -> CalculationStrategy:
|
|
235
|
+
return StandardConcentrationStrategy(self.final_concentration)
|
|
236
|
+
|
|
237
|
+
def prepare(self, weight: Quantity, stock: StockSolution) -> Preparation:
|
|
238
|
+
_require_weight(weight)
|
|
239
|
+
self._require_known(stock)
|
|
240
|
+
return _single_step(self, stock, self.drug_amount, self.final_concentration)
|
|
241
|
+
|
|
242
|
+
def validate(self) -> list[Finding]:
|
|
243
|
+
findings = InfusionScheme.validate(self) # not super(): see PreparationScheme.validate
|
|
244
|
+
findings += _check_quantity(self.drug_amount, Role.DRUG_AMOUNT, "drug_amount")
|
|
245
|
+
if findings:
|
|
246
|
+
return findings
|
|
247
|
+
for stock in self.stock_solutions:
|
|
248
|
+
findings += _check_draw(self.drug_amount, stock, self.final_volume)
|
|
249
|
+
return findings
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
253
|
+
class IndividualPreparationScheme(InfusionScheme):
|
|
254
|
+
"""A drug amount per kg of body weight made up to the final volume.
|
|
255
|
+
|
|
256
|
+
With the same factor and final volume, the reference rate (often 1 ml/h) gives the same dose
|
|
257
|
+
per kg for every weight. The factor is stored; the reference dose is derived from it, so the
|
|
258
|
+
two can never contradict each other (IN-03).
|
|
259
|
+
"""
|
|
260
|
+
|
|
261
|
+
key: ClassVar[str] = "individual_preparation"
|
|
262
|
+
|
|
263
|
+
preparation_factor: Quantity
|
|
264
|
+
reference_rate: Quantity | None = None
|
|
265
|
+
|
|
266
|
+
def named_strength(self) -> Quantity | None:
|
|
267
|
+
return self._per_kg_strength(self.preparation_factor)
|
|
268
|
+
|
|
269
|
+
def create_strategy(self) -> CalculationStrategy:
|
|
270
|
+
return IndividualPreparationStrategy(self.preparation_factor, self.final_volume)
|
|
271
|
+
|
|
272
|
+
def prepare(self, weight: Quantity, stock: StockSolution) -> Preparation:
|
|
273
|
+
_require_weight(weight)
|
|
274
|
+
self._require_known(stock)
|
|
275
|
+
amount = self.preparation_factor * weight
|
|
276
|
+
return _single_step(self, stock, amount, amount / self.final_volume)
|
|
277
|
+
|
|
278
|
+
def reference_dose(self) -> Quantity | None:
|
|
279
|
+
"""The dose per kg delivered at the reference rate, the same for every weight."""
|
|
280
|
+
if self.reference_rate is None:
|
|
281
|
+
return None
|
|
282
|
+
return self.preparation_factor * self.reference_rate / self.final_volume
|
|
283
|
+
|
|
284
|
+
def implied_preparation_weight(self, drug_amount: Quantity) -> Quantity | None:
|
|
285
|
+
return (drug_amount / self.preparation_factor).to("kg")
|
|
286
|
+
|
|
287
|
+
def validate(self) -> list[Finding]:
|
|
288
|
+
findings = InfusionScheme.validate(self) # not super(): see PreparationScheme.validate
|
|
289
|
+
findings += _check_quantity(
|
|
290
|
+
self.preparation_factor, Role.PREPARATION_FACTOR, "preparation_factor"
|
|
291
|
+
)
|
|
292
|
+
if self.reference_rate is not None:
|
|
293
|
+
findings += _check_quantity(self.reference_rate, Role.RATE, "reference_rate")
|
|
294
|
+
if findings:
|
|
295
|
+
return findings
|
|
296
|
+
for stock in self.stock_solutions:
|
|
297
|
+
findings += _check_amount_matches(self.preparation_factor, stock)
|
|
298
|
+
return findings
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
302
|
+
class TwoStepPreparationScheme(InfusionScheme):
|
|
303
|
+
"""A stock solution is diluted first, then a per-kg amount of it is made up to the final volume.
|
|
304
|
+
|
|
305
|
+
Step 1: `dilution_product_volume` of the product + `dilution_diluent_volume` of
|
|
306
|
+
`intermediate_diluent` gives the intermediate solution (for example U100 insulin 1 ml + 9 ml
|
|
307
|
+
= 10 IU/ml). Step 2: `preparation_factor` × weight is drawn from the intermediate solution and
|
|
308
|
+
filled up to the final volume. The shelf life is approved display text only.
|
|
309
|
+
"""
|
|
310
|
+
|
|
311
|
+
key: ClassVar[str] = "two_step_preparation"
|
|
312
|
+
|
|
313
|
+
dilution_product_volume: Quantity
|
|
314
|
+
dilution_diluent_volume: Quantity
|
|
315
|
+
intermediate_diluent: str
|
|
316
|
+
preparation_factor: Quantity
|
|
317
|
+
shelf_life_note: str = ""
|
|
318
|
+
reference_rate: Quantity | None = None
|
|
319
|
+
|
|
320
|
+
def named_strength(self) -> Quantity | None:
|
|
321
|
+
return self._per_kg_strength(self.preparation_factor)
|
|
322
|
+
|
|
323
|
+
def intermediate_concentration(self, stock: StockSolution) -> Quantity:
|
|
324
|
+
total = self.dilution_product_volume + self.dilution_diluent_volume
|
|
325
|
+
return stock.strength * self.dilution_product_volume / total
|
|
326
|
+
|
|
327
|
+
def create_strategy(self) -> CalculationStrategy:
|
|
328
|
+
return TwoStepPreparationStrategy(
|
|
329
|
+
self.preparation_factor,
|
|
330
|
+
self.final_volume,
|
|
331
|
+
self.intermediate_concentration(self.stock_solutions[0]),
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
def prepare(self, weight: Quantity, stock: StockSolution) -> Preparation:
|
|
335
|
+
_require_weight(weight)
|
|
336
|
+
self._require_known(stock)
|
|
337
|
+
intermediate = self.intermediate_concentration(stock)
|
|
338
|
+
intermediate_volume = self.dilution_product_volume + self.dilution_diluent_volume
|
|
339
|
+
amount = self.preparation_factor * weight
|
|
340
|
+
draw = amount / intermediate
|
|
341
|
+
_require_fits(draw, self.final_volume)
|
|
342
|
+
dilution = PreparationStep(
|
|
343
|
+
StepKind.DILUTE,
|
|
344
|
+
stock.label,
|
|
345
|
+
self.dilution_product_volume,
|
|
346
|
+
amount=stock.strength * self.dilution_product_volume,
|
|
347
|
+
total_volume=intermediate_volume,
|
|
348
|
+
concentration=intermediate,
|
|
349
|
+
)
|
|
350
|
+
diluent_step = PreparationStep(
|
|
351
|
+
StepKind.DILUTE, self.intermediate_diluent, self.dilution_diluent_volume
|
|
352
|
+
)
|
|
353
|
+
return Preparation(
|
|
354
|
+
stock=stock,
|
|
355
|
+
drug_amount=amount,
|
|
356
|
+
final_volume=self.final_volume,
|
|
357
|
+
final_concentration=self.shown(amount / self.final_volume),
|
|
358
|
+
steps=(
|
|
359
|
+
dilution,
|
|
360
|
+
diluent_step,
|
|
361
|
+
PreparationStep(StepKind.DRAW, "intermediate", draw, amount=amount),
|
|
362
|
+
PreparationStep(
|
|
363
|
+
StepKind.FILL_UP,
|
|
364
|
+
self.diluent,
|
|
365
|
+
self.final_volume - draw,
|
|
366
|
+
total_volume=self.final_volume,
|
|
367
|
+
),
|
|
368
|
+
),
|
|
369
|
+
intermediate_concentration=intermediate,
|
|
370
|
+
shelf_life_note=self.shelf_life_note or None,
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
def reference_dose(self) -> Quantity | None:
|
|
374
|
+
if self.reference_rate is None:
|
|
375
|
+
return None
|
|
376
|
+
return self.preparation_factor * self.reference_rate / self.final_volume
|
|
377
|
+
|
|
378
|
+
def implied_preparation_weight(self, drug_amount: Quantity) -> Quantity | None:
|
|
379
|
+
return (drug_amount / self.preparation_factor).to("kg")
|
|
380
|
+
|
|
381
|
+
def validate(self) -> list[Finding]:
|
|
382
|
+
findings = InfusionScheme.validate(self) # not super(): see PreparationScheme.validate
|
|
383
|
+
findings += _check_quantity(
|
|
384
|
+
self.dilution_product_volume, Role.VOLUME, "dilution_product_volume"
|
|
385
|
+
)
|
|
386
|
+
findings += _check_quantity(
|
|
387
|
+
self.dilution_diluent_volume, Role.VOLUME, "dilution_diluent_volume"
|
|
388
|
+
)
|
|
389
|
+
findings += _check_quantity(
|
|
390
|
+
self.preparation_factor, Role.PREPARATION_FACTOR, "preparation_factor"
|
|
391
|
+
)
|
|
392
|
+
if self.reference_rate is not None:
|
|
393
|
+
findings += _check_quantity(self.reference_rate, Role.RATE, "reference_rate")
|
|
394
|
+
if not self.intermediate_diluent.strip():
|
|
395
|
+
findings.append(_missing("intermediate_diluent"))
|
|
396
|
+
if findings:
|
|
397
|
+
return findings
|
|
398
|
+
if len(self.stock_solutions) != 1:
|
|
399
|
+
findings.append(
|
|
400
|
+
Finding(
|
|
401
|
+
"two_step_single_stock",
|
|
402
|
+
Severity.BLOCK,
|
|
403
|
+
"a two-step preparation names exactly one product for its stock solution",
|
|
404
|
+
)
|
|
405
|
+
)
|
|
406
|
+
for stock in self.stock_solutions:
|
|
407
|
+
findings += _check_amount_matches(self.preparation_factor, stock)
|
|
408
|
+
return findings
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
412
|
+
class SingleDoseScheme(PreparationScheme):
|
|
413
|
+
"""A divided daily dose drawn neat from a made-up product (for example 100 mg/ml).
|
|
414
|
+
|
|
415
|
+
`diluent` names the solvent the product is made up with; the dose itself is not diluted.
|
|
416
|
+
"""
|
|
417
|
+
|
|
418
|
+
key: ClassVar[str] = "single_dose"
|
|
419
|
+
workflow: ClassVar[Workflow] = Workflow.SINGLE_DOSE
|
|
420
|
+
names_syringe: ClassVar[bool] = True
|
|
421
|
+
|
|
422
|
+
def named_strength(self) -> Quantity | None:
|
|
423
|
+
"""The product's strength when every approved product has the same one."""
|
|
424
|
+
strengths = {s.strength for s in self.stock_solutions}
|
|
425
|
+
return next(iter(strengths)) if len(strengths) == 1 else None
|
|
426
|
+
|
|
427
|
+
def named_carrier(self) -> str | None:
|
|
428
|
+
return None # the solvent makes up the product; the dose itself is not diluted
|
|
429
|
+
|
|
430
|
+
doses_per_day: int
|
|
431
|
+
|
|
432
|
+
def create_strategy(self) -> SingleDoseStrategy:
|
|
433
|
+
return DividedDailyDoseStrategy(self.doses_per_day)
|
|
434
|
+
|
|
435
|
+
def prepare_dose(self, amount: Quantity, stock: StockSolution) -> DosePreparation:
|
|
436
|
+
self._require_known(stock)
|
|
437
|
+
require_positive(amount, Role.DRUG_AMOUNT, "amount")
|
|
438
|
+
mismatch = _check_amount_matches(amount, stock)
|
|
439
|
+
if mismatch:
|
|
440
|
+
raise StructuralError("stock_does_not_match", mismatch[0].message)
|
|
441
|
+
volume = (amount / stock.strength).to("ml")
|
|
442
|
+
step = PreparationStep(StepKind.DRAW, stock.label, volume, amount=amount)
|
|
443
|
+
return DosePreparation(stock, amount, volume, (step,), self.diluent)
|
|
444
|
+
|
|
445
|
+
def validate(self) -> list[Finding]:
|
|
446
|
+
findings = PreparationScheme.validate(self) # not super(): see PreparationScheme.validate
|
|
447
|
+
if type(self.doses_per_day) is not int or self.doses_per_day < 1:
|
|
448
|
+
findings.append(
|
|
449
|
+
Finding(
|
|
450
|
+
"not_positive",
|
|
451
|
+
Severity.BLOCK,
|
|
452
|
+
"doses_per_day must be a positive integer",
|
|
453
|
+
{"field": "doses_per_day"},
|
|
454
|
+
)
|
|
455
|
+
)
|
|
456
|
+
return findings
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
# --- helpers --------------------------------------------------------------------------------
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
def _single_step(
|
|
463
|
+
scheme: InfusionScheme, stock: StockSolution, amount: Quantity, concentration: Quantity
|
|
464
|
+
) -> Preparation:
|
|
465
|
+
try:
|
|
466
|
+
draw = amount / stock.strength
|
|
467
|
+
volume = draw.to("ml")
|
|
468
|
+
except UnitError as exc:
|
|
469
|
+
raise StructuralError(
|
|
470
|
+
"stock_does_not_match", f"{stock.strength} does not fit a drug amount of {amount}"
|
|
471
|
+
) from exc
|
|
472
|
+
_require_fits(volume, scheme.final_volume)
|
|
473
|
+
steps = [PreparationStep(StepKind.DRAW, stock.label, volume, amount=amount)]
|
|
474
|
+
fill = scheme.final_volume - volume
|
|
475
|
+
if fill.is_positive():
|
|
476
|
+
steps.append(
|
|
477
|
+
PreparationStep(
|
|
478
|
+
StepKind.FILL_UP, scheme.diluent, fill, total_volume=scheme.final_volume
|
|
479
|
+
)
|
|
480
|
+
)
|
|
481
|
+
return Preparation(
|
|
482
|
+
stock=stock,
|
|
483
|
+
drug_amount=amount,
|
|
484
|
+
final_volume=scheme.final_volume,
|
|
485
|
+
final_concentration=scheme.shown(concentration),
|
|
486
|
+
steps=tuple(steps),
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
def _require_weight(weight: Quantity) -> None:
|
|
491
|
+
if not is_allowed(weight.unit, Role.WEIGHT):
|
|
492
|
+
raise StructuralError(UNIT_ROLE, f"{weight.unit.symbol} is not a weight")
|
|
493
|
+
if not weight.is_positive():
|
|
494
|
+
raise StructuralError("not_positive", f"weight must be greater than zero, got {weight}")
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
def _require_fits(drawn: Quantity, final_volume: Quantity) -> None:
|
|
498
|
+
"""Drawing more than the final volume is impossible; drawing exactly all of it is "pur"."""
|
|
499
|
+
if drawn > final_volume:
|
|
500
|
+
raise StructuralError(
|
|
501
|
+
"overdraw", f"drawn volume {drawn} is larger than the final volume {final_volume}"
|
|
502
|
+
)
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
def _check_quantity(q: Quantity, role: Role, name: str) -> list[Finding]:
|
|
506
|
+
if not is_allowed(q.unit, role):
|
|
507
|
+
return [
|
|
508
|
+
Finding(
|
|
509
|
+
UNIT_ROLE,
|
|
510
|
+
Severity.BLOCK,
|
|
511
|
+
f"{name}: {q.unit.symbol} is not allowed as {role.value}",
|
|
512
|
+
{"field": name, "unit": q.unit.symbol},
|
|
513
|
+
)
|
|
514
|
+
]
|
|
515
|
+
if not q.is_positive():
|
|
516
|
+
return [
|
|
517
|
+
Finding(
|
|
518
|
+
"not_positive",
|
|
519
|
+
Severity.BLOCK,
|
|
520
|
+
f"{name} must be greater than zero",
|
|
521
|
+
{"field": name, "value": str(q)},
|
|
522
|
+
)
|
|
523
|
+
]
|
|
524
|
+
return []
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
def _check_draw(amount: Quantity, stock: StockSolution, final_volume: Quantity) -> list[Finding]:
|
|
528
|
+
found = _check_amount_matches(amount, stock)
|
|
529
|
+
if found:
|
|
530
|
+
return found
|
|
531
|
+
drawn = (amount / stock.strength).to("ml")
|
|
532
|
+
if drawn > final_volume:
|
|
533
|
+
return [
|
|
534
|
+
Finding(
|
|
535
|
+
"overdraw",
|
|
536
|
+
Severity.BLOCK,
|
|
537
|
+
f"{stock.label}: drawn volume {drawn} is larger than the final volume",
|
|
538
|
+
{"stock": stock.label, "drawn": str(drawn), "final_volume": str(final_volume)},
|
|
539
|
+
)
|
|
540
|
+
]
|
|
541
|
+
return []
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
def _check_amount_matches(amount: Quantity, stock: StockSolution) -> list[Finding]:
|
|
545
|
+
"""The stock's drug unit must fit the drug amount: IU never with mg (AT-03)."""
|
|
546
|
+
amount_dimension = amount.unit.terms[0][0].dimension
|
|
547
|
+
stock_dimension = stock.strength.unit.terms[0][0].dimension
|
|
548
|
+
if amount_dimension is stock_dimension:
|
|
549
|
+
return []
|
|
550
|
+
return [
|
|
551
|
+
Finding(
|
|
552
|
+
"stock_does_not_match",
|
|
553
|
+
Severity.BLOCK,
|
|
554
|
+
f"{stock.label} ({stock.strength.unit.symbol}) does not fit {amount.unit.symbol}",
|
|
555
|
+
{"stock": stock.label},
|
|
556
|
+
)
|
|
557
|
+
]
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
def _missing(name: str) -> Finding:
|
|
561
|
+
return Finding("missing_field", Severity.BLOCK, f"{name} is required", {"field": name})
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
class SchemeRegistry:
|
|
565
|
+
"""Maps a scheme's `key` to its class, so stored protocol data can name its scheme.
|
|
566
|
+
|
|
567
|
+
A hospital that brings its own scheme registers it here; no code branches on the kind.
|
|
568
|
+
"""
|
|
569
|
+
|
|
570
|
+
def __init__(self) -> None:
|
|
571
|
+
self._schemes: dict[str, type[PreparationScheme]] = {}
|
|
572
|
+
|
|
573
|
+
def register(self, scheme: type[PreparationScheme]) -> None:
|
|
574
|
+
if scheme.key in self._schemes:
|
|
575
|
+
raise ValueError(f"scheme {scheme.key!r} is already registered")
|
|
576
|
+
self._schemes[scheme.key] = scheme
|
|
577
|
+
|
|
578
|
+
def get(self, key: str) -> type[PreparationScheme]:
|
|
579
|
+
try:
|
|
580
|
+
return self._schemes[key]
|
|
581
|
+
except KeyError:
|
|
582
|
+
raise StructuralError("unknown_scheme", f"no preparation scheme {key!r}") from None
|
|
583
|
+
|
|
584
|
+
def keys(self) -> frozenset[str]:
|
|
585
|
+
return frozenset(self._schemes)
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
def default_schemes() -> SchemeRegistry:
|
|
589
|
+
"""The schemes shipped with the framework."""
|
|
590
|
+
registry = SchemeRegistry()
|
|
591
|
+
for scheme in (
|
|
592
|
+
StandardConcentrationScheme,
|
|
593
|
+
IndividualPreparationScheme,
|
|
594
|
+
TwoStepPreparationScheme,
|
|
595
|
+
SingleDoseScheme,
|
|
596
|
+
):
|
|
597
|
+
registry.register(scheme)
|
|
598
|
+
return registry
|