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,253 @@
|
|
|
1
|
+
"""One evaluation of a divided daily dose, shared by approval and the bedside (P-03)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from decimal import Decimal
|
|
7
|
+
from typing import Protocol
|
|
8
|
+
|
|
9
|
+
from doseframe.calculation import Derivation, deviation_pct
|
|
10
|
+
from doseframe.findings import Finding, Severity, blocking
|
|
11
|
+
from doseframe.protocols.device import DeviceProfile
|
|
12
|
+
from doseframe.protocols.schemes import DosePreparation, SingleDoseScheme, StockSolution
|
|
13
|
+
from doseframe.units import DoseframeError, Quantity, Unit, is_weight_based_dose, unit
|
|
14
|
+
|
|
15
|
+
ML = unit("ml")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Rounds(Protocol):
|
|
19
|
+
def round(self, value: Quantity, device: DeviceProfile | None) -> Quantity | None: ...
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True, slots=True)
|
|
23
|
+
class SingleDoseSetup:
|
|
24
|
+
"""The protocol's rounding rule with its syringe profile."""
|
|
25
|
+
|
|
26
|
+
rounding: Rounds
|
|
27
|
+
device: DeviceProfile | None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True, slots=True)
|
|
31
|
+
class DoseOption:
|
|
32
|
+
amount: Quantity | None
|
|
33
|
+
volume: Quantity
|
|
34
|
+
deviation_pct: Decimal | None
|
|
35
|
+
delivered_daily_dose: Quantity | None
|
|
36
|
+
preparation: DosePreparation | None
|
|
37
|
+
findings: tuple[Finding, ...]
|
|
38
|
+
|
|
39
|
+
def is_usable(self) -> bool:
|
|
40
|
+
return self.preparation is not None and not blocking(self.findings)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True, slots=True)
|
|
44
|
+
class SingleDoseOutcome:
|
|
45
|
+
exact: Derivation | None
|
|
46
|
+
exact_volume: Quantity | None
|
|
47
|
+
options: tuple[DoseOption, ...]
|
|
48
|
+
offers_choice: bool
|
|
49
|
+
findings: tuple[Finding, ...]
|
|
50
|
+
|
|
51
|
+
# Keep the single-result accessors for existing nearest-step clients.
|
|
52
|
+
@property
|
|
53
|
+
def legacy_option(self) -> DoseOption | None:
|
|
54
|
+
return self.options[0] if self.options and not self.offers_choice else None
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def rounded(self) -> Quantity | None:
|
|
58
|
+
return self.legacy_option.amount if self.legacy_option else None
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def rounded_volume(self) -> Quantity | None:
|
|
62
|
+
return self.legacy_option.volume if self.legacy_option else None
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def deviation_pct(self) -> Decimal | None:
|
|
66
|
+
return self.legacy_option.deviation_pct if self.legacy_option else None
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def preparation(self) -> DosePreparation | None:
|
|
70
|
+
return self.legacy_option.preparation if self.legacy_option else None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def syringe_findings(device: DeviceProfile | None) -> list[Finding]:
|
|
74
|
+
"""A drawable dose needs a positive step in ml and a capacity in ml (P-04)."""
|
|
75
|
+
if device is None:
|
|
76
|
+
return [Finding("syringe_profile_missing", Severity.BLOCK, "no syringe profile on file")]
|
|
77
|
+
step = device.step_size
|
|
78
|
+
capacity = device.settable_range.upper if device.settable_range else None
|
|
79
|
+
minimum = device.settable_range.lower if device.settable_range else None
|
|
80
|
+
ok = (
|
|
81
|
+
step is not None
|
|
82
|
+
and step.unit.is_compatible(ML)
|
|
83
|
+
and step.is_positive()
|
|
84
|
+
and capacity is not None
|
|
85
|
+
and capacity.unit.is_compatible(ML)
|
|
86
|
+
and capacity >= step
|
|
87
|
+
and (minimum is None or (minimum.unit.is_compatible(ML) and minimum <= capacity))
|
|
88
|
+
)
|
|
89
|
+
if ok:
|
|
90
|
+
return []
|
|
91
|
+
return [
|
|
92
|
+
Finding(
|
|
93
|
+
"syringe_profile_incomplete",
|
|
94
|
+
Severity.BLOCK,
|
|
95
|
+
f"{device.name}: step and capacity in ml are required",
|
|
96
|
+
{"device": device.name},
|
|
97
|
+
)
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def deviation_limit_findings(maximum: Decimal | None) -> list[Finding]:
|
|
102
|
+
"""The maximum rounding deviation must be present and above 0 up to 100 %."""
|
|
103
|
+
if maximum is None:
|
|
104
|
+
return [
|
|
105
|
+
Finding(
|
|
106
|
+
"rounding_deviation_missing",
|
|
107
|
+
Severity.BLOCK,
|
|
108
|
+
"a maximum rounding deviation is required",
|
|
109
|
+
)
|
|
110
|
+
]
|
|
111
|
+
if not (maximum.is_finite() and Decimal(0) < maximum <= Decimal(100)):
|
|
112
|
+
return [
|
|
113
|
+
Finding(
|
|
114
|
+
"rounding_deviation_invalid",
|
|
115
|
+
Severity.BLOCK,
|
|
116
|
+
"the maximum rounding deviation is above 0 and at most 100 %",
|
|
117
|
+
{"maximum": str(maximum)},
|
|
118
|
+
)
|
|
119
|
+
]
|
|
120
|
+
return []
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def rounding_deviation_findings(deviation: Decimal, maximum: Decimal | None) -> list[Finding]:
|
|
124
|
+
"""Up to the maximum nothing, above it WARN, above twice it BLOCK (V2-18)."""
|
|
125
|
+
if (
|
|
126
|
+
maximum is None or not maximum.is_finite() or abs(deviation) <= maximum
|
|
127
|
+
): # invalid: validate()
|
|
128
|
+
return []
|
|
129
|
+
severity = Severity.BLOCK if abs(deviation) > 2 * maximum else Severity.WARN
|
|
130
|
+
return [
|
|
131
|
+
Finding(
|
|
132
|
+
"rounding_deviation_high",
|
|
133
|
+
severity,
|
|
134
|
+
f"rounding changes the dose by {deviation:.1f} %",
|
|
135
|
+
{"deviation_pct": f"{deviation:.1f}", "maximum": str(maximum)},
|
|
136
|
+
)
|
|
137
|
+
]
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def evaluate_single_dose( # noqa: PLR0917
|
|
141
|
+
scheme: SingleDoseScheme,
|
|
142
|
+
stock: StockSolution,
|
|
143
|
+
daily_dose: Quantity,
|
|
144
|
+
weight: Quantity,
|
|
145
|
+
setup: SingleDoseSetup | None,
|
|
146
|
+
max_deviation: Decimal | None,
|
|
147
|
+
) -> SingleDoseOutcome:
|
|
148
|
+
invalid = deviation_limit_findings(max_deviation)
|
|
149
|
+
if invalid:
|
|
150
|
+
return _stop(*invalid)
|
|
151
|
+
if setup is None:
|
|
152
|
+
return _stop(
|
|
153
|
+
Finding(
|
|
154
|
+
"rounding_unavailable", Severity.BLOCK, "no rounding rule with a syringe profile"
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
config = syringe_findings(setup.device)
|
|
158
|
+
if config or setup.device is None or setup.device.settable_range is None:
|
|
159
|
+
return _stop(*config)
|
|
160
|
+
offers_choice = bool(getattr(setup.rounding, "offers_choice", False))
|
|
161
|
+
try:
|
|
162
|
+
exact = scheme.create_strategy().amount_per_administration(daily_dose, weight)
|
|
163
|
+
exact_volume = scheme.prepare_dose(exact.value, stock).volume
|
|
164
|
+
if offers_choice:
|
|
165
|
+
candidates = getattr(setup.rounding, "candidates", None)
|
|
166
|
+
volumes = candidates(exact_volume, setup.device) if candidates is not None else ()
|
|
167
|
+
else:
|
|
168
|
+
rounded = setup.rounding.round(exact_volume, setup.device)
|
|
169
|
+
volumes = () if rounded is None else (rounded,)
|
|
170
|
+
options = tuple(
|
|
171
|
+
_evaluate_option(
|
|
172
|
+
scheme,
|
|
173
|
+
stock,
|
|
174
|
+
exact.value,
|
|
175
|
+
volume,
|
|
176
|
+
weight,
|
|
177
|
+
daily_dose.unit,
|
|
178
|
+
setup.device,
|
|
179
|
+
max_deviation,
|
|
180
|
+
)
|
|
181
|
+
for volume in volumes
|
|
182
|
+
)
|
|
183
|
+
except DoseframeError as exc:
|
|
184
|
+
return _stop(Finding(getattr(exc, "code", "invalid_input"), Severity.BLOCK, str(exc)))
|
|
185
|
+
if not options:
|
|
186
|
+
finding = Finding("rounding_unavailable", Severity.BLOCK, "the rounding rule gave no value")
|
|
187
|
+
return SingleDoseOutcome(exact, exact_volume, (), offers_choice, (finding,))
|
|
188
|
+
return SingleDoseOutcome(
|
|
189
|
+
exact, exact_volume, options, offers_choice, () if offers_choice else options[0].findings
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _evaluate_option( # noqa: PLR0917
|
|
194
|
+
scheme: SingleDoseScheme,
|
|
195
|
+
stock: StockSolution,
|
|
196
|
+
exact: Quantity,
|
|
197
|
+
volume: Quantity,
|
|
198
|
+
weight: Quantity,
|
|
199
|
+
dose_unit: Unit,
|
|
200
|
+
device: DeviceProfile,
|
|
201
|
+
max_deviation: Decimal | None,
|
|
202
|
+
) -> DoseOption:
|
|
203
|
+
findings = _drawable_findings(volume, device)
|
|
204
|
+
if findings:
|
|
205
|
+
return DoseOption(None, volume, None, None, None, findings)
|
|
206
|
+
amount = (volume * stock.strength).to(exact.unit)
|
|
207
|
+
deviation = deviation_pct(amount, exact)
|
|
208
|
+
findings = tuple(rounding_deviation_findings(deviation, max_deviation))
|
|
209
|
+
delivered = delivered_daily_dose(amount, scheme.doses_per_day, weight, dose_unit)
|
|
210
|
+
preparation = scheme.prepare_dose(amount, stock)
|
|
211
|
+
return DoseOption(amount, volume, deviation, delivered, preparation, findings)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _drawable_findings(volume: Quantity, device: DeviceProfile) -> tuple[Finding, ...]:
|
|
215
|
+
assert device.settable_range is not None
|
|
216
|
+
minimum, capacity = device.settable_range.lower, device.settable_range.upper
|
|
217
|
+
if not volume.is_positive():
|
|
218
|
+
return (
|
|
219
|
+
Finding("not_drawable", Severity.BLOCK, "the dose is too small to draw accurately"),
|
|
220
|
+
)
|
|
221
|
+
if minimum is not None and volume < minimum:
|
|
222
|
+
return (
|
|
223
|
+
Finding(
|
|
224
|
+
"below_syringe_minimum",
|
|
225
|
+
Severity.BLOCK,
|
|
226
|
+
f"{volume} is less than the syringe can set ({minimum})",
|
|
227
|
+
{"value": str(volume), "minimum": str(minimum)},
|
|
228
|
+
),
|
|
229
|
+
)
|
|
230
|
+
if capacity is not None and volume > capacity:
|
|
231
|
+
return (
|
|
232
|
+
Finding(
|
|
233
|
+
"above_syringe_capacity",
|
|
234
|
+
Severity.BLOCK,
|
|
235
|
+
f"{volume} is more than the syringe holds ({capacity})",
|
|
236
|
+
{"value": str(volume), "capacity": str(capacity)},
|
|
237
|
+
),
|
|
238
|
+
)
|
|
239
|
+
return ()
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def delivered_daily_dose(
|
|
243
|
+
amount: Quantity, doses_per_day: int, weight: Quantity, dose_unit: Unit
|
|
244
|
+
) -> Quantity:
|
|
245
|
+
"""The daily dose that the rounded amount delivers, in the protocol's unit."""
|
|
246
|
+
per_day = amount * doses_per_day / Quantity.of("1", "d")
|
|
247
|
+
if is_weight_based_dose(dose_unit):
|
|
248
|
+
per_day = per_day / weight
|
|
249
|
+
return per_day.to(dose_unit)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _stop(*findings: Finding) -> SingleDoseOutcome:
|
|
253
|
+
return SingleDoseOutcome(None, None, (), False, tuple(findings))
|