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
doseframe/__init__.py
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"""doseframe: a transparent, unit-safe dose calculation framework for perfusors.
|
|
2
|
+
|
|
3
|
+
Not a medical device. The names exported here are the public API, versioned semantically:
|
|
4
|
+
a change to a frozen spot is a major version, a new default plug-in a minor version.
|
|
5
|
+
|
|
6
|
+
Hot-spots, where a hospital plugs in its own behaviour:
|
|
7
|
+
1. `CalculationStrategy` how to calculate (Strategy)
|
|
8
|
+
2. `RoundingPolicy` how to round (Strategy)
|
|
9
|
+
3. `PreparationScheme` how the syringe is prepared (Factory Method)
|
|
10
|
+
4. `Check` extra checks (Chain of Responsibility)
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from doseframe.calculation import (
|
|
16
|
+
CalculationStep,
|
|
17
|
+
CalculationStrategy,
|
|
18
|
+
Derivation,
|
|
19
|
+
DividedDailyDoseStrategy,
|
|
20
|
+
IndividualPreparationStrategy,
|
|
21
|
+
InfusedEstimate,
|
|
22
|
+
SingleDoseStrategy,
|
|
23
|
+
StandardConcentrationStrategy,
|
|
24
|
+
StructuralError,
|
|
25
|
+
TwoStepPreparationStrategy,
|
|
26
|
+
deviation_pct,
|
|
27
|
+
)
|
|
28
|
+
from doseframe.checks import Check, CheckChain, CheckContext, CheckMode, LimitCheck, UnitCheck
|
|
29
|
+
from doseframe.findings import Finding, Severity, blocking
|
|
30
|
+
from doseframe.protocols import (
|
|
31
|
+
Actor,
|
|
32
|
+
CaseDirection,
|
|
33
|
+
CaseResult,
|
|
34
|
+
DeviceProfile,
|
|
35
|
+
DeviceProfileChange,
|
|
36
|
+
DosePreparation,
|
|
37
|
+
EventKind,
|
|
38
|
+
FieldChange,
|
|
39
|
+
GovernanceError,
|
|
40
|
+
GuardFailedError,
|
|
41
|
+
IllegalTransitionError,
|
|
42
|
+
IndividualPreparationScheme,
|
|
43
|
+
InfusionScheme,
|
|
44
|
+
LifecycleEvent,
|
|
45
|
+
Limit,
|
|
46
|
+
LimitTarget,
|
|
47
|
+
Medication,
|
|
48
|
+
Operation,
|
|
49
|
+
PermissionDeniedError,
|
|
50
|
+
Preparation,
|
|
51
|
+
PreparationScheme,
|
|
52
|
+
PreparationStep,
|
|
53
|
+
Protocol,
|
|
54
|
+
ProtocolTitle,
|
|
55
|
+
ProtocolVersion,
|
|
56
|
+
Range,
|
|
57
|
+
ReferenceCase,
|
|
58
|
+
SchemeRegistry,
|
|
59
|
+
SingleDoseOutcome,
|
|
60
|
+
SingleDoseScheme,
|
|
61
|
+
SingleDoseSetup,
|
|
62
|
+
StandardConcentrationScheme,
|
|
63
|
+
StateName,
|
|
64
|
+
StepKind,
|
|
65
|
+
StockSolution,
|
|
66
|
+
TwoStepPreparationScheme,
|
|
67
|
+
UserRole,
|
|
68
|
+
VersionContent,
|
|
69
|
+
WeightScope,
|
|
70
|
+
Workflow,
|
|
71
|
+
default_schemes,
|
|
72
|
+
evaluate_single_dose,
|
|
73
|
+
usual_range,
|
|
74
|
+
)
|
|
75
|
+
from doseframe.protocols.single_dose import DoseOption
|
|
76
|
+
from doseframe.rounding import (
|
|
77
|
+
ChooseStep,
|
|
78
|
+
NearestStepRounding,
|
|
79
|
+
NoAutoRounding,
|
|
80
|
+
RoundingPolicy,
|
|
81
|
+
RoundingPolicyRegistry,
|
|
82
|
+
UnknownRoundingPolicyError,
|
|
83
|
+
default_registry,
|
|
84
|
+
)
|
|
85
|
+
from doseframe.units import (
|
|
86
|
+
DoseframeError,
|
|
87
|
+
IncompatibleUnitError,
|
|
88
|
+
Quantity,
|
|
89
|
+
Role,
|
|
90
|
+
Unit,
|
|
91
|
+
UnitError,
|
|
92
|
+
UnitOptions,
|
|
93
|
+
UnitRoleError,
|
|
94
|
+
UnknownUnitError,
|
|
95
|
+
dose_unit_options,
|
|
96
|
+
format_decimal,
|
|
97
|
+
is_allowed,
|
|
98
|
+
parse_unit,
|
|
99
|
+
require_role,
|
|
100
|
+
result_units,
|
|
101
|
+
unit,
|
|
102
|
+
)
|
|
103
|
+
from doseframe.use_cases import (
|
|
104
|
+
ApprovalReasons,
|
|
105
|
+
AvailabilityGate,
|
|
106
|
+
CalculateRateUseCase,
|
|
107
|
+
CalculateSingleDoseUseCase,
|
|
108
|
+
CalculationRequest,
|
|
109
|
+
CalculationResult,
|
|
110
|
+
CheckRunningPerfusorUseCase,
|
|
111
|
+
Clock,
|
|
112
|
+
ConcentrationSource,
|
|
113
|
+
Evaluation,
|
|
114
|
+
MatchProtocolsUseCase,
|
|
115
|
+
NotFoundError,
|
|
116
|
+
OpenChangeExistsError,
|
|
117
|
+
ProtocolMatch,
|
|
118
|
+
ProtocolOffice,
|
|
119
|
+
ProtocolOverview,
|
|
120
|
+
ProtocolRepository,
|
|
121
|
+
RepositoryUnavailableError,
|
|
122
|
+
ReverseCheckRequest,
|
|
123
|
+
ReverseCheckResult,
|
|
124
|
+
SingleDoseRequest,
|
|
125
|
+
SingleDoseResult,
|
|
126
|
+
Unavailable,
|
|
127
|
+
UnusableReason,
|
|
128
|
+
UserDirectory,
|
|
129
|
+
VersionDetail,
|
|
130
|
+
VersionOverview,
|
|
131
|
+
VersionRef,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
__version__ = "0.2.0"
|
|
135
|
+
|
|
136
|
+
__all__ = [
|
|
137
|
+
"Actor",
|
|
138
|
+
"ApprovalReasons",
|
|
139
|
+
"AvailabilityGate",
|
|
140
|
+
"CalculateRateUseCase",
|
|
141
|
+
"CalculateSingleDoseUseCase",
|
|
142
|
+
"CalculationRequest",
|
|
143
|
+
"CalculationResult",
|
|
144
|
+
"CalculationStep",
|
|
145
|
+
"CalculationStrategy",
|
|
146
|
+
"CaseDirection",
|
|
147
|
+
"CaseResult",
|
|
148
|
+
"Check",
|
|
149
|
+
"CheckChain",
|
|
150
|
+
"CheckContext",
|
|
151
|
+
"CheckMode",
|
|
152
|
+
"CheckRunningPerfusorUseCase",
|
|
153
|
+
"ChooseStep",
|
|
154
|
+
"Clock",
|
|
155
|
+
"ConcentrationSource",
|
|
156
|
+
"Derivation",
|
|
157
|
+
"DeviceProfile",
|
|
158
|
+
"DeviceProfileChange",
|
|
159
|
+
"DividedDailyDoseStrategy",
|
|
160
|
+
"DoseOption",
|
|
161
|
+
"DosePreparation",
|
|
162
|
+
"DoseframeError",
|
|
163
|
+
"Evaluation",
|
|
164
|
+
"EventKind",
|
|
165
|
+
"FieldChange",
|
|
166
|
+
"Finding",
|
|
167
|
+
"GovernanceError",
|
|
168
|
+
"GuardFailedError",
|
|
169
|
+
"IllegalTransitionError",
|
|
170
|
+
"IncompatibleUnitError",
|
|
171
|
+
"IndividualPreparationScheme",
|
|
172
|
+
"IndividualPreparationStrategy",
|
|
173
|
+
"InfusedEstimate",
|
|
174
|
+
"InfusionScheme",
|
|
175
|
+
"LifecycleEvent",
|
|
176
|
+
"Limit",
|
|
177
|
+
"LimitCheck",
|
|
178
|
+
"LimitTarget",
|
|
179
|
+
"MatchProtocolsUseCase",
|
|
180
|
+
"Medication",
|
|
181
|
+
"NearestStepRounding",
|
|
182
|
+
"NoAutoRounding",
|
|
183
|
+
"NotFoundError",
|
|
184
|
+
"OpenChangeExistsError",
|
|
185
|
+
"Operation",
|
|
186
|
+
"PermissionDeniedError",
|
|
187
|
+
"Preparation",
|
|
188
|
+
"PreparationScheme",
|
|
189
|
+
"PreparationStep",
|
|
190
|
+
"Protocol",
|
|
191
|
+
"ProtocolMatch",
|
|
192
|
+
"ProtocolOffice",
|
|
193
|
+
"ProtocolOverview",
|
|
194
|
+
"ProtocolRepository",
|
|
195
|
+
"ProtocolTitle",
|
|
196
|
+
"ProtocolVersion",
|
|
197
|
+
"Quantity",
|
|
198
|
+
"Range",
|
|
199
|
+
"ReferenceCase",
|
|
200
|
+
"RepositoryUnavailableError",
|
|
201
|
+
"ReverseCheckRequest",
|
|
202
|
+
"ReverseCheckResult",
|
|
203
|
+
"Role",
|
|
204
|
+
"RoundingPolicy",
|
|
205
|
+
"RoundingPolicyRegistry",
|
|
206
|
+
"SchemeRegistry",
|
|
207
|
+
"Severity",
|
|
208
|
+
"SingleDoseOutcome",
|
|
209
|
+
"SingleDoseRequest",
|
|
210
|
+
"SingleDoseResult",
|
|
211
|
+
"SingleDoseScheme",
|
|
212
|
+
"SingleDoseSetup",
|
|
213
|
+
"SingleDoseStrategy",
|
|
214
|
+
"StandardConcentrationScheme",
|
|
215
|
+
"StandardConcentrationStrategy",
|
|
216
|
+
"StateName",
|
|
217
|
+
"StepKind",
|
|
218
|
+
"StockSolution",
|
|
219
|
+
"StructuralError",
|
|
220
|
+
"TwoStepPreparationScheme",
|
|
221
|
+
"TwoStepPreparationStrategy",
|
|
222
|
+
"Unavailable",
|
|
223
|
+
"Unit",
|
|
224
|
+
"UnitCheck",
|
|
225
|
+
"UnitError",
|
|
226
|
+
"UnitOptions",
|
|
227
|
+
"UnitRoleError",
|
|
228
|
+
"UnknownRoundingPolicyError",
|
|
229
|
+
"UnknownUnitError",
|
|
230
|
+
"UnusableReason",
|
|
231
|
+
"UserDirectory",
|
|
232
|
+
"UserRole",
|
|
233
|
+
"VersionContent",
|
|
234
|
+
"VersionDetail",
|
|
235
|
+
"VersionOverview",
|
|
236
|
+
"VersionRef",
|
|
237
|
+
"WeightScope",
|
|
238
|
+
"Workflow",
|
|
239
|
+
"blocking",
|
|
240
|
+
"default_registry",
|
|
241
|
+
"default_schemes",
|
|
242
|
+
"deviation_pct",
|
|
243
|
+
"dose_unit_options",
|
|
244
|
+
"evaluate_single_dose",
|
|
245
|
+
"format_decimal",
|
|
246
|
+
"is_allowed",
|
|
247
|
+
"parse_unit",
|
|
248
|
+
"require_role",
|
|
249
|
+
"result_units",
|
|
250
|
+
"unit",
|
|
251
|
+
"usual_range",
|
|
252
|
+
]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Calculation: the strategy interface (hot-spot 1), the default strategies, and their results."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from doseframe.calculation.derivation import CalculationStep, Derivation
|
|
6
|
+
from doseframe.calculation.errors import StructuralError
|
|
7
|
+
from doseframe.calculation.infusion import InfusedEstimate, estimate_infused
|
|
8
|
+
from doseframe.calculation.relation import deviation_pct, dose_to_rate, rate_to_dose
|
|
9
|
+
from doseframe.calculation.single_dose import DividedDailyDoseStrategy, SingleDoseStrategy
|
|
10
|
+
from doseframe.calculation.steps import neighbouring_steps
|
|
11
|
+
from doseframe.calculation.strategies import (
|
|
12
|
+
IndividualPreparationStrategy,
|
|
13
|
+
StandardConcentrationStrategy,
|
|
14
|
+
TwoStepPreparationStrategy,
|
|
15
|
+
)
|
|
16
|
+
from doseframe.calculation.strategy import CalculationStrategy
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"CalculationStep",
|
|
20
|
+
"CalculationStrategy",
|
|
21
|
+
"Derivation",
|
|
22
|
+
"DividedDailyDoseStrategy",
|
|
23
|
+
"IndividualPreparationStrategy",
|
|
24
|
+
"InfusedEstimate",
|
|
25
|
+
"SingleDoseStrategy",
|
|
26
|
+
"StandardConcentrationStrategy",
|
|
27
|
+
"StructuralError",
|
|
28
|
+
"TwoStepPreparationStrategy",
|
|
29
|
+
"deviation_pct",
|
|
30
|
+
"dose_to_rate",
|
|
31
|
+
"estimate_infused",
|
|
32
|
+
"neighbouring_steps",
|
|
33
|
+
"rate_to_dose",
|
|
34
|
+
]
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""A value together with the steps that produced it, for the visible calculation path."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
from doseframe.units import Quantity
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True, slots=True)
|
|
11
|
+
class CalculationStep:
|
|
12
|
+
"""One step of a calculation: ``operands[0] <operation> operands[1] = result``.
|
|
13
|
+
|
|
14
|
+
`key` names the step for the interface ("dose_for_weight", "per_hour", "to_volume", ...).
|
|
15
|
+
For a unit conversion, `operation` is ``"convert"`` and there is a single operand.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
key: str
|
|
19
|
+
operation: str
|
|
20
|
+
operands: tuple[Quantity, ...]
|
|
21
|
+
result: Quantity
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True, slots=True)
|
|
25
|
+
class Derivation:
|
|
26
|
+
value: Quantity
|
|
27
|
+
steps: tuple[CalculationStep, ...] = ()
|
|
28
|
+
|
|
29
|
+
def then(self, step: CalculationStep) -> Derivation:
|
|
30
|
+
return Derivation(step.result, (*self.steps, step))
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Errors for inputs that cannot describe a real preparation or pump setting."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from doseframe.units import DoseframeError
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class StructuralError(DoseframeError):
|
|
9
|
+
"""A rule that follows from arithmetic was broken. Always blocks, never configurable.
|
|
10
|
+
|
|
11
|
+
`code` is stable and becomes the code of the BLOCK finding shown to the user.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def __init__(self, code: str, message: str) -> None:
|
|
15
|
+
super().__init__(message)
|
|
16
|
+
self.code = code
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""How much a running perfusor has delivered: an estimate at a constant rate (V2-05)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
from doseframe.calculation.relation import require_positive
|
|
8
|
+
from doseframe.units import Quantity, Role
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True, slots=True)
|
|
12
|
+
class InfusedEstimate:
|
|
13
|
+
"""Assumes the pump ran at this rate the whole time; it is not measured delivery."""
|
|
14
|
+
|
|
15
|
+
volume: Quantity
|
|
16
|
+
drug_amount: Quantity
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def estimate_infused(
|
|
20
|
+
rate: Quantity, running_for: Quantity, concentration: Quantity
|
|
21
|
+
) -> InfusedEstimate:
|
|
22
|
+
require_positive(rate, Role.RATE, "rate")
|
|
23
|
+
require_positive(running_for, Role.DURATION, "running_for")
|
|
24
|
+
require_positive(concentration, Role.CONCENTRATION, "concentration")
|
|
25
|
+
volume = (rate * running_for).to("ml")
|
|
26
|
+
amount = (volume * concentration).to(concentration.unit.terms[0][0].symbol)
|
|
27
|
+
return InfusedEstimate(volume, amount)
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"""The relation between dose and pump rate, shared by every preparation scheme.
|
|
2
|
+
|
|
3
|
+
weight-based dose: rate = dose × weight / concentration
|
|
4
|
+
absolute dose: rate = dose / concentration
|
|
5
|
+
|
|
6
|
+
Whether the weight enters follows from the dose unit (it has a "per kg"), never from a flag or a
|
|
7
|
+
medication name. Every step keeps its unit and is recorded for the calculation path.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from decimal import Decimal
|
|
13
|
+
|
|
14
|
+
from doseframe.calculation.derivation import CalculationStep, Derivation
|
|
15
|
+
from doseframe.calculation.errors import StructuralError
|
|
16
|
+
from doseframe.units import (
|
|
17
|
+
CONTEXT,
|
|
18
|
+
Dimension,
|
|
19
|
+
IncompatibleUnitError,
|
|
20
|
+
Quantity,
|
|
21
|
+
Role,
|
|
22
|
+
Unit,
|
|
23
|
+
is_weight_based_dose,
|
|
24
|
+
require_role,
|
|
25
|
+
unit,
|
|
26
|
+
)
|
|
27
|
+
from doseframe.units.catalog import H
|
|
28
|
+
|
|
29
|
+
ML_PER_H = unit("ml/h")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def dose_to_rate(dose: Quantity, weight: Quantity, concentration: Quantity) -> Derivation:
|
|
33
|
+
require_positive(dose, Role.DOSE, "dose")
|
|
34
|
+
require_positive(weight, Role.WEIGHT, "weight")
|
|
35
|
+
require_positive(concentration, Role.CONCENTRATION, "concentration")
|
|
36
|
+
|
|
37
|
+
derivation = Derivation(dose)
|
|
38
|
+
amount_per_time = dose
|
|
39
|
+
if is_weight_based_dose(dose.unit):
|
|
40
|
+
amount_per_time = dose * weight
|
|
41
|
+
derivation = derivation.then(
|
|
42
|
+
CalculationStep("dose_for_weight", "×", (dose, weight), amount_per_time)
|
|
43
|
+
)
|
|
44
|
+
per_hour_unit = Unit(((amount_per_time.unit.terms[0][0], 1), (H, -1)))
|
|
45
|
+
if amount_per_time.unit != per_hour_unit:
|
|
46
|
+
per_hour = amount_per_time.to(per_hour_unit)
|
|
47
|
+
derivation = derivation.then(
|
|
48
|
+
CalculationStep("per_hour", "convert", (amount_per_time,), per_hour)
|
|
49
|
+
)
|
|
50
|
+
amount_per_time = per_hour
|
|
51
|
+
rate = amount_per_time / concentration
|
|
52
|
+
if not rate.unit.is_compatible(ML_PER_H):
|
|
53
|
+
raise IncompatibleUnitError(
|
|
54
|
+
f"{dose.unit.symbol} and {concentration.unit.symbol} do not give a rate"
|
|
55
|
+
)
|
|
56
|
+
rate = rate.to(ML_PER_H)
|
|
57
|
+
return derivation.then(
|
|
58
|
+
CalculationStep("to_volume", "÷", (amount_per_time, concentration), rate)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def rate_to_dose(
|
|
63
|
+
rate: Quantity, weight: Quantity, concentration: Quantity, dose_unit: Unit
|
|
64
|
+
) -> Derivation:
|
|
65
|
+
require_positive(rate, Role.RATE, "rate")
|
|
66
|
+
require_positive(weight, Role.WEIGHT, "weight")
|
|
67
|
+
require_positive(concentration, Role.CONCENTRATION, "concentration")
|
|
68
|
+
require_role(dose_unit, Role.DOSE)
|
|
69
|
+
|
|
70
|
+
amount_per_hour = rate * concentration
|
|
71
|
+
if not amount_per_hour.unit.is_compatible(Unit(((dose_unit.terms[0][0], 1), (H, -1)))):
|
|
72
|
+
raise IncompatibleUnitError(
|
|
73
|
+
f"{concentration.unit.symbol} does not give a dose in {dose_unit.symbol}"
|
|
74
|
+
)
|
|
75
|
+
derivation = Derivation(rate).then(
|
|
76
|
+
CalculationStep("to_amount", "×", (rate, concentration), amount_per_hour)
|
|
77
|
+
)
|
|
78
|
+
value = amount_per_hour
|
|
79
|
+
if is_weight_based_dose(dose_unit):
|
|
80
|
+
value = amount_per_hour / weight
|
|
81
|
+
derivation = derivation.then(
|
|
82
|
+
CalculationStep("per_weight", "÷", (amount_per_hour, weight), value)
|
|
83
|
+
)
|
|
84
|
+
dose = value.to(dose_unit)
|
|
85
|
+
if dose.unit == value.unit:
|
|
86
|
+
return derivation
|
|
87
|
+
# One unit per step (V3-02): time first, then body weight, then the drug amount. A single
|
|
88
|
+
# change keeps its one step, named for the prescribed unit as before.
|
|
89
|
+
target = {atom.dimension: (atom, exp) for atom, exp in dose_unit.terms}
|
|
90
|
+
terms = list(value.unit.terms)
|
|
91
|
+
changes = [
|
|
92
|
+
i
|
|
93
|
+
for i in reversed(range(len(terms)))
|
|
94
|
+
if target.get(terms[i][0].dimension, terms[i]) != terms[i]
|
|
95
|
+
]
|
|
96
|
+
if len(changes) <= 1:
|
|
97
|
+
return derivation.then(CalculationStep("to_dose_unit", "convert", (value,), dose))
|
|
98
|
+
current = value
|
|
99
|
+
for i in changes:
|
|
100
|
+
terms[i] = target[terms[i][0].dimension]
|
|
101
|
+
following = current.to(Unit(tuple(terms)))
|
|
102
|
+
key = _CONVERSION_KEYS[terms[i][0].dimension]
|
|
103
|
+
derivation = derivation.then(CalculationStep(key, "convert", (current,), following))
|
|
104
|
+
current = following
|
|
105
|
+
return derivation
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
_CONVERSION_KEYS = {
|
|
109
|
+
Dimension.TIME: "to_time_unit",
|
|
110
|
+
Dimension.BODY_MASS: "to_weight_unit",
|
|
111
|
+
Dimension.DRUG_MASS: "to_amount_unit",
|
|
112
|
+
Dimension.DRUG_ACTIVITY: "to_amount_unit",
|
|
113
|
+
Dimension.VOLUME: "to_volume_unit",
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def require_positive(q: Quantity, role: Role, name: str) -> None:
|
|
118
|
+
"""The unit must fit the role, and the value must be greater than zero."""
|
|
119
|
+
require_role(q.unit, role)
|
|
120
|
+
if not q.is_positive():
|
|
121
|
+
raise StructuralError("not_positive", f"{name} must be greater than zero, got {q}")
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def deviation_pct(actual: Quantity, target: Quantity) -> Decimal:
|
|
125
|
+
"""(actual − target) / target × 100. Undefined for a target of zero, which is refused."""
|
|
126
|
+
if not target.is_positive():
|
|
127
|
+
raise StructuralError("not_positive", f"deviation needs a positive target, got {target}")
|
|
128
|
+
difference = (actual - target).value
|
|
129
|
+
return CONTEXT.multiply(CONTEXT.divide(difference, target.value), Decimal(100))
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""The divided daily dose (V2-09): one interface, one default implementation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from decimal import Decimal
|
|
7
|
+
from typing import Protocol, runtime_checkable
|
|
8
|
+
|
|
9
|
+
from doseframe.calculation.derivation import CalculationStep, Derivation
|
|
10
|
+
from doseframe.calculation.errors import StructuralError
|
|
11
|
+
from doseframe.calculation.relation import require_positive
|
|
12
|
+
from doseframe.units import DIMENSIONLESS, Quantity, Role, is_weight_based_dose, unit
|
|
13
|
+
from doseframe.units.catalog import D
|
|
14
|
+
|
|
15
|
+
ONE_DAY = Quantity.of("1", "d")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@runtime_checkable
|
|
19
|
+
class SingleDoseStrategy(Protocol):
|
|
20
|
+
"""Turns a daily dose into the amount of one administration."""
|
|
21
|
+
|
|
22
|
+
def amount_per_administration(self, daily_dose: Quantity, weight: Quantity) -> Derivation: ...
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True, slots=True)
|
|
26
|
+
class DividedDailyDoseStrategy:
|
|
27
|
+
"""Daily dose (per kg or absolute) × weight × 1 d ÷ administrations per day."""
|
|
28
|
+
|
|
29
|
+
doses_per_day: int
|
|
30
|
+
|
|
31
|
+
def amount_per_administration(self, daily_dose: Quantity, weight: Quantity) -> Derivation:
|
|
32
|
+
require_positive(daily_dose, Role.DOSE, "daily_dose")
|
|
33
|
+
require_positive(weight, Role.WEIGHT, "weight")
|
|
34
|
+
if daily_dose.unit.terms[-1] != (D, -1):
|
|
35
|
+
raise StructuralError("not_a_daily_dose", f"{daily_dose.unit.symbol} is not per day")
|
|
36
|
+
if type(self.doses_per_day) is not int or self.doses_per_day < 1:
|
|
37
|
+
raise StructuralError("not_positive", "at least one administration per day")
|
|
38
|
+
derivation = Derivation(daily_dose)
|
|
39
|
+
per_day = daily_dose
|
|
40
|
+
if is_weight_based_dose(daily_dose.unit):
|
|
41
|
+
per_day = daily_dose * weight
|
|
42
|
+
derivation = derivation.then(
|
|
43
|
+
CalculationStep("dose_for_weight", "×", (daily_dose, weight), per_day)
|
|
44
|
+
)
|
|
45
|
+
amount_unit = unit(per_day.unit.terms[0][0].symbol)
|
|
46
|
+
daily_amount = (per_day * ONE_DAY).to(amount_unit)
|
|
47
|
+
derivation = derivation.then(
|
|
48
|
+
CalculationStep("daily_amount", "×", (per_day, ONE_DAY), daily_amount)
|
|
49
|
+
)
|
|
50
|
+
count = Quantity(Decimal(self.doses_per_day), DIMENSIONLESS)
|
|
51
|
+
amount = daily_amount / count
|
|
52
|
+
return derivation.then(
|
|
53
|
+
CalculationStep("per_administration", "÷", (daily_amount, count), amount)
|
|
54
|
+
)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""Device increments shared by dose and pump calculations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from doseframe.calculation.errors import StructuralError
|
|
6
|
+
from doseframe.units import CONTEXT, Quantity
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def neighbouring_steps(value: Quantity, step: Quantity) -> tuple[Quantity, ...]:
|
|
10
|
+
"""Return adjacent multiples in the value's unit, retaining zero below the first step."""
|
|
11
|
+
if not step.is_positive() or value.value < 0:
|
|
12
|
+
raise StructuralError(
|
|
13
|
+
"not_positive", "a device step must be positive and its value non-negative"
|
|
14
|
+
)
|
|
15
|
+
count, remainder = CONTEXT.divmod(value.to(step.unit).value, step.value)
|
|
16
|
+
if remainder == 0:
|
|
17
|
+
return (value,)
|
|
18
|
+
lower = CONTEXT.multiply(count, step.value)
|
|
19
|
+
upper = CONTEXT.add(lower, step.value)
|
|
20
|
+
return (Quantity(lower, step.unit).to(value.unit), Quantity(upper, step.unit).to(value.unit))
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""The default calculation strategies shipped with the framework.
|
|
2
|
+
|
|
3
|
+
They share the dose-rate relation and differ in where the final concentration comes from: fixed
|
|
4
|
+
for a standard concentration, scaled with the patient's weight for an individual preparation.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
|
|
11
|
+
from doseframe.calculation import relation
|
|
12
|
+
from doseframe.calculation.derivation import CalculationStep, Derivation
|
|
13
|
+
from doseframe.units import Quantity, Role, Unit
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class _RelationStrategy:
|
|
17
|
+
"""Shared dose-rate conversion; subclasses supply `concentration_for`."""
|
|
18
|
+
|
|
19
|
+
def concentration_for(self, weight: Quantity) -> Derivation:
|
|
20
|
+
raise NotImplementedError
|
|
21
|
+
|
|
22
|
+
def dose_to_rate(
|
|
23
|
+
self, dose: Quantity, weight: Quantity, concentration: Quantity | None = None
|
|
24
|
+
) -> Derivation:
|
|
25
|
+
used = concentration if concentration is not None else self.concentration_for(weight).value
|
|
26
|
+
return relation.dose_to_rate(dose, weight, used)
|
|
27
|
+
|
|
28
|
+
def rate_to_dose(
|
|
29
|
+
self,
|
|
30
|
+
rate: Quantity,
|
|
31
|
+
weight: Quantity,
|
|
32
|
+
dose_unit: Unit,
|
|
33
|
+
concentration: Quantity | None = None,
|
|
34
|
+
) -> Derivation:
|
|
35
|
+
used = concentration if concentration is not None else self.concentration_for(weight).value
|
|
36
|
+
return relation.rate_to_dose(rate, weight, used, dose_unit)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True, slots=True)
|
|
40
|
+
class StandardConcentrationStrategy(_RelationStrategy):
|
|
41
|
+
"""The syringe is always the same; the rate follows weight and dose."""
|
|
42
|
+
|
|
43
|
+
final_concentration: Quantity
|
|
44
|
+
|
|
45
|
+
def concentration_for(self, weight: Quantity) -> Derivation:
|
|
46
|
+
relation.require_positive(weight, Role.WEIGHT, "weight")
|
|
47
|
+
return Derivation(self.final_concentration)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass(frozen=True, slots=True)
|
|
51
|
+
class IndividualPreparationStrategy(_RelationStrategy):
|
|
52
|
+
"""The drug amount in the syringe is scaled with the weight: factor × weight / final volume."""
|
|
53
|
+
|
|
54
|
+
preparation_factor: Quantity
|
|
55
|
+
final_volume: Quantity
|
|
56
|
+
|
|
57
|
+
def concentration_for(self, weight: Quantity) -> Derivation:
|
|
58
|
+
relation.require_positive(weight, Role.WEIGHT, "weight")
|
|
59
|
+
amount = self.preparation_factor * weight
|
|
60
|
+
concentration = amount / self.final_volume
|
|
61
|
+
return (
|
|
62
|
+
Derivation(self.preparation_factor)
|
|
63
|
+
.then(CalculationStep("drug_amount", "×", (self.preparation_factor, weight), amount))
|
|
64
|
+
.then(
|
|
65
|
+
CalculationStep(
|
|
66
|
+
"final_concentration", "÷", (amount, self.final_volume), concentration
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass(frozen=True, slots=True)
|
|
73
|
+
class TwoStepPreparationStrategy(_RelationStrategy):
|
|
74
|
+
"""Like the individual preparation, with the drug drawn from an intermediate stock solution.
|
|
75
|
+
|
|
76
|
+
The intermediate concentration is part of the calculation path, so a second person can see
|
|
77
|
+
which stock the per-kg amount comes from.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
preparation_factor: Quantity
|
|
81
|
+
final_volume: Quantity
|
|
82
|
+
intermediate_concentration: Quantity
|
|
83
|
+
|
|
84
|
+
def concentration_for(self, weight: Quantity) -> Derivation:
|
|
85
|
+
relation.require_positive(weight, Role.WEIGHT, "weight")
|
|
86
|
+
amount = self.preparation_factor * weight
|
|
87
|
+
stock_volume = amount / self.intermediate_concentration
|
|
88
|
+
concentration = amount / self.final_volume
|
|
89
|
+
return (
|
|
90
|
+
Derivation(self.preparation_factor)
|
|
91
|
+
.then(CalculationStep("drug_amount", "×", (self.preparation_factor, weight), amount))
|
|
92
|
+
.then(
|
|
93
|
+
CalculationStep(
|
|
94
|
+
"stock_volume",
|
|
95
|
+
"÷",
|
|
96
|
+
(amount, self.intermediate_concentration),
|
|
97
|
+
stock_volume,
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
.then(
|
|
101
|
+
CalculationStep(
|
|
102
|
+
"final_concentration", "÷", (amount, self.final_volume), concentration
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
)
|