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/units/unit.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""Units as products of atoms, each atom belonging to one base dimension.
|
|
2
|
+
|
|
3
|
+
Five base dimensions keep apart what must never be mixed: the drug's mass, the drug's activity
|
|
4
|
+
(insulin units, IU), volume, the patient's body mass, and time. Because body mass and drug mass are
|
|
5
|
+
different dimensions, mg/kg never cancels to a plain number, and because activity is its own
|
|
6
|
+
dimension, IU can never be converted to milligrams.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from collections.abc import Mapping
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from decimal import Decimal
|
|
14
|
+
from enum import StrEnum
|
|
15
|
+
from types import MappingProxyType
|
|
16
|
+
|
|
17
|
+
from doseframe.units.decimal_context import CONTEXT
|
|
18
|
+
from doseframe.units.errors import IncompatibleUnitError
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Dimension(StrEnum):
|
|
22
|
+
DRUG_MASS = "drug_mass"
|
|
23
|
+
DRUG_ACTIVITY = "drug_activity"
|
|
24
|
+
VOLUME = "volume"
|
|
25
|
+
BODY_MASS = "body_mass"
|
|
26
|
+
TIME = "time"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True, slots=True)
|
|
30
|
+
class Atom:
|
|
31
|
+
"""One unit symbol of a single dimension, with its exact size relative to the base unit."""
|
|
32
|
+
|
|
33
|
+
symbol: str
|
|
34
|
+
dimension: Dimension
|
|
35
|
+
scale: Decimal
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True, slots=True)
|
|
39
|
+
class Unit:
|
|
40
|
+
"""An immutable product of atoms with integer exponents, such as µg·kg⁻¹·min⁻¹.
|
|
41
|
+
|
|
42
|
+
Terms keep their order of appearance (numerator first), which gives a stable, readable symbol.
|
|
43
|
+
Each dimension appears in at most one term.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
terms: tuple[tuple[Atom, int], ...]
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def symbol(self) -> str:
|
|
50
|
+
numerator = [_term_symbol(atom, exp) for atom, exp in self.terms if exp > 0]
|
|
51
|
+
denominator = [_term_symbol(atom, -exp) for atom, exp in self.terms if exp < 0]
|
|
52
|
+
head = "·".join(numerator) if numerator else "1"
|
|
53
|
+
return "/".join([head, *denominator])
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def dimensions(self) -> Mapping[Dimension, int]:
|
|
57
|
+
return MappingProxyType({atom.dimension: exp for atom, exp in self.terms})
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def scale(self) -> tuple[Decimal, Decimal]:
|
|
61
|
+
"""The size of this unit relative to the base units, as an exact fraction.
|
|
62
|
+
|
|
63
|
+
Kept as numerator and denominator because sizes such as 1/1440 (per day in minutes) have
|
|
64
|
+
no exact decimal; dividing only once, at the end of a conversion, keeps results exact.
|
|
65
|
+
"""
|
|
66
|
+
numerator, denominator = Decimal(1), Decimal(1)
|
|
67
|
+
for atom, exp in self.terms:
|
|
68
|
+
power = CONTEXT.power(atom.scale, abs(exp))
|
|
69
|
+
if exp > 0:
|
|
70
|
+
numerator = CONTEXT.multiply(numerator, power)
|
|
71
|
+
else:
|
|
72
|
+
denominator = CONTEXT.multiply(denominator, power)
|
|
73
|
+
return numerator, denominator
|
|
74
|
+
|
|
75
|
+
def is_compatible(self, other: Unit) -> bool:
|
|
76
|
+
return dict(self.dimensions) == dict(other.dimensions)
|
|
77
|
+
|
|
78
|
+
def convert(self, value: Decimal, target: Unit) -> Decimal:
|
|
79
|
+
"""Convert a value to `target`; exact wherever the result has a finite decimal."""
|
|
80
|
+
if not self.is_compatible(target):
|
|
81
|
+
raise IncompatibleUnitError(f"cannot convert {self.symbol} to {target.symbol}")
|
|
82
|
+
source_num, source_den = self.scale
|
|
83
|
+
target_num, target_den = target.scale
|
|
84
|
+
top = CONTEXT.multiply(CONTEXT.multiply(value, source_num), target_den)
|
|
85
|
+
return CONTEXT.divide(top, CONTEXT.multiply(source_den, target_num))
|
|
86
|
+
|
|
87
|
+
def multiply(self, other: Unit) -> tuple[Unit, Decimal, Decimal]:
|
|
88
|
+
"""Multiply two units.
|
|
89
|
+
|
|
90
|
+
Returns the product unit and a factor for the value as numerator and denominator. Where
|
|
91
|
+
both units hold an atom of the same dimension, the second atom is expressed in the first
|
|
92
|
+
one's size, so µg/h divided by mg/ml gives ml/h with a factor of 1/1000 rather than a
|
|
93
|
+
mixed µg/mg.
|
|
94
|
+
"""
|
|
95
|
+
terms: dict[Dimension, tuple[Atom, int]] = {
|
|
96
|
+
atom.dimension: (atom, exp) for atom, exp in self.terms
|
|
97
|
+
}
|
|
98
|
+
order = [atom.dimension for atom, _ in self.terms]
|
|
99
|
+
numerator, denominator = Decimal(1), Decimal(1)
|
|
100
|
+
for atom, exp in other.terms:
|
|
101
|
+
if atom.dimension in terms:
|
|
102
|
+
kept, kept_exp = terms[atom.dimension]
|
|
103
|
+
up, down = (atom.scale, kept.scale) if exp > 0 else (kept.scale, atom.scale)
|
|
104
|
+
numerator = CONTEXT.multiply(numerator, CONTEXT.power(up, abs(exp)))
|
|
105
|
+
denominator = CONTEXT.multiply(denominator, CONTEXT.power(down, abs(exp)))
|
|
106
|
+
terms[atom.dimension] = (kept, kept_exp + exp)
|
|
107
|
+
else:
|
|
108
|
+
terms[atom.dimension] = (atom, exp)
|
|
109
|
+
order.append(atom.dimension)
|
|
110
|
+
combined = [terms[d] for d in order if terms[d][1] != 0]
|
|
111
|
+
positives = [t for t in combined if t[1] > 0]
|
|
112
|
+
negatives = sorted(
|
|
113
|
+
(t for t in combined if t[1] < 0), key=lambda t: _DENOMINATOR_ORDER[t[0].dimension]
|
|
114
|
+
)
|
|
115
|
+
return Unit(tuple(positives + negatives)), numerator, denominator
|
|
116
|
+
|
|
117
|
+
def inverse(self) -> Unit:
|
|
118
|
+
return Unit(tuple((atom, -exp) for atom, exp in reversed(self.terms)))
|
|
119
|
+
|
|
120
|
+
def __str__(self) -> str:
|
|
121
|
+
return self.symbol
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
DIMENSIONLESS = Unit(())
|
|
125
|
+
|
|
126
|
+
# Clinical reading order of a denominator: per kg, per ml, per time (µg/kg/min, not µg/min/kg).
|
|
127
|
+
_DENOMINATOR_ORDER = {
|
|
128
|
+
Dimension.BODY_MASS: 0,
|
|
129
|
+
Dimension.VOLUME: 1,
|
|
130
|
+
Dimension.DRUG_MASS: 2,
|
|
131
|
+
Dimension.DRUG_ACTIVITY: 3,
|
|
132
|
+
Dimension.TIME: 4,
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _term_symbol(atom: Atom, exp: int) -> str:
|
|
137
|
+
return atom.symbol if exp == 1 else f"{atom.symbol}^{exp}"
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Use cases: what a user can ask the system to do, and the ports they need from outside."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from doseframe.use_cases.clinical import (
|
|
6
|
+
CalculateRateUseCase,
|
|
7
|
+
CalculateSingleDoseUseCase,
|
|
8
|
+
CalculationRequest,
|
|
9
|
+
CheckRunningPerfusorUseCase,
|
|
10
|
+
MatchProtocolsUseCase,
|
|
11
|
+
ReverseCheckRequest,
|
|
12
|
+
SingleDoseRequest,
|
|
13
|
+
)
|
|
14
|
+
from doseframe.use_cases.gate import AvailabilityGate, VersionUnusableError, activate
|
|
15
|
+
from doseframe.use_cases.governance import (
|
|
16
|
+
ApprovalReasons,
|
|
17
|
+
Evaluation,
|
|
18
|
+
OpenChangeExistsError,
|
|
19
|
+
ProtocolOffice,
|
|
20
|
+
ProtocolOverview,
|
|
21
|
+
VersionDetail,
|
|
22
|
+
VersionOverview,
|
|
23
|
+
)
|
|
24
|
+
from doseframe.use_cases.ports import (
|
|
25
|
+
Clock,
|
|
26
|
+
NotFoundError,
|
|
27
|
+
ProtocolRepository,
|
|
28
|
+
RepositoryUnavailableError,
|
|
29
|
+
UserDirectory,
|
|
30
|
+
)
|
|
31
|
+
from doseframe.use_cases.results import (
|
|
32
|
+
CalculationResult,
|
|
33
|
+
ConcentrationSource,
|
|
34
|
+
ProtocolMatch,
|
|
35
|
+
ReverseCheckResult,
|
|
36
|
+
SingleDoseResult,
|
|
37
|
+
Unavailable,
|
|
38
|
+
UnusableReason,
|
|
39
|
+
VersionRef,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
__all__ = [
|
|
43
|
+
"ApprovalReasons",
|
|
44
|
+
"AvailabilityGate",
|
|
45
|
+
"CalculateRateUseCase",
|
|
46
|
+
"CalculateSingleDoseUseCase",
|
|
47
|
+
"CalculationRequest",
|
|
48
|
+
"CalculationResult",
|
|
49
|
+
"CheckRunningPerfusorUseCase",
|
|
50
|
+
"Clock",
|
|
51
|
+
"ConcentrationSource",
|
|
52
|
+
"Evaluation",
|
|
53
|
+
"MatchProtocolsUseCase",
|
|
54
|
+
"NotFoundError",
|
|
55
|
+
"OpenChangeExistsError",
|
|
56
|
+
"ProtocolMatch",
|
|
57
|
+
"ProtocolOffice",
|
|
58
|
+
"ProtocolOverview",
|
|
59
|
+
"ProtocolRepository",
|
|
60
|
+
"RepositoryUnavailableError",
|
|
61
|
+
"ReverseCheckRequest",
|
|
62
|
+
"ReverseCheckResult",
|
|
63
|
+
"SingleDoseRequest",
|
|
64
|
+
"SingleDoseResult",
|
|
65
|
+
"Unavailable",
|
|
66
|
+
"UnusableReason",
|
|
67
|
+
"UserDirectory",
|
|
68
|
+
"VersionDetail",
|
|
69
|
+
"VersionOverview",
|
|
70
|
+
"VersionRef",
|
|
71
|
+
"VersionUnusableError",
|
|
72
|
+
"activate",
|
|
73
|
+
]
|