wimsta-formulas 1.0.1__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.
- wimsta_formulas/__init__.py +135 -0
- wimsta_formulas/data/bounds.json +50 -0
- wimsta_formulas/data/registry.json +1013 -0
- wimsta_formulas/data/vectors.json +3212 -0
- wimsta_formulas/formulas.py +229 -0
- wimsta_formulas/py.typed +0 -0
- wimsta_formulas-1.0.1.dist-info/METADATA +136 -0
- wimsta_formulas-1.0.1.dist-info/RECORD +13 -0
- wimsta_formulas-1.0.1.dist-info/WHEEL +5 -0
- wimsta_formulas-1.0.1.dist-info/licenses/LICENSE +202 -0
- wimsta_formulas-1.0.1.dist-info/licenses/LICENSE-DATA +396 -0
- wimsta_formulas-1.0.1.dist-info/licenses/NOTICE +8 -0
- wimsta_formulas-1.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""wimsta-formulas — the WIMSTA Formula Registry as code.
|
|
2
|
+
|
|
3
|
+
Computes single formulas from https://wimsta.com/registry/. It does not combine
|
|
4
|
+
formulas into a range: for that use the API, https://wimsta.com/api/calc.
|
|
5
|
+
Estimates for general information only, not medical advice.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
from importlib.resources import files
|
|
11
|
+
from typing import Any, Dict, List, Optional
|
|
12
|
+
|
|
13
|
+
from . import formulas as F
|
|
14
|
+
|
|
15
|
+
__all__ = ["registry", "REGISTRY_VERSION", "vectors", "get_formula", "list_formulas", "calculate", "FormulaInputError"]
|
|
16
|
+
__version__ = "1.0.1"
|
|
17
|
+
|
|
18
|
+
_data = files(__package__) / "data"
|
|
19
|
+
registry: Dict[str, Any] = json.loads((_data / "registry.json").read_text(encoding="utf-8"))
|
|
20
|
+
vectors: Dict[str, Any] = json.loads((_data / "vectors.json").read_text(encoding="utf-8"))
|
|
21
|
+
_BOUNDS: Dict[str, Dict[str, float]] = json.loads((_data / "bounds.json").read_text(encoding="utf-8"))
|
|
22
|
+
REGISTRY_VERSION: str = registry["registry_version"]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class FormulaInputError(ValueError):
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def get_formula(id: str) -> Optional[Dict[str, Any]]:
|
|
30
|
+
return next((f for f in registry["formulas"] if f["id"] == id), None)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def list_formulas(category: Optional[str] = None) -> List[Dict[str, Any]]:
|
|
34
|
+
return [f for f in registry["formulas"] if category is None or f["category"] == category]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _needs(id: str, sex: Optional[str]) -> List[str]:
|
|
38
|
+
male = sex == "male"
|
|
39
|
+
return {
|
|
40
|
+
"deurenberg": ["sex", "age", "height_cm", "weight_kg"],
|
|
41
|
+
"cun-bae": ["sex", "age", "height_cm", "weight_kg"],
|
|
42
|
+
"us-navy": ["sex", "height_cm", "neck_cm", "abdomen_cm"] if male else ["sex", "height_cm", "neck_cm", "waist_cm", "hip_cm"],
|
|
43
|
+
"rfm": ["sex", "height_cm", "waist_cm"],
|
|
44
|
+
"covert-bailey": (["sex", "age", "wrist_cm", "waist_cm", "hip_cm", "forearm_cm"] if male
|
|
45
|
+
else ["sex", "age", "wrist_cm", "hip_cm", "thigh_cm", "calf_cm"]),
|
|
46
|
+
"gallagher-ranges": ["sex", "age"],
|
|
47
|
+
"who-bmi": ["weight_kg", "height_cm"],
|
|
48
|
+
"trefethen-bmi": ["weight_kg", "height_cm"],
|
|
49
|
+
"ashwell-whtr": ["waist_cm", "height_cm"],
|
|
50
|
+
"tanaka": ["age"],
|
|
51
|
+
"gulati": ["sex", "age"],
|
|
52
|
+
"fox": ["age"],
|
|
53
|
+
"harris-benedict-1919": ["sex", "age", "height_cm", "weight_kg"],
|
|
54
|
+
"harris-benedict-1984": ["sex", "age", "height_cm", "weight_kg"],
|
|
55
|
+
"katch-mcardle": ["weight_kg", "body_fat_pct"],
|
|
56
|
+
"mifflin-st-jeor": ["sex", "age", "height_cm", "weight_kg"],
|
|
57
|
+
"nes": ["age"],
|
|
58
|
+
"londeree": ["age"],
|
|
59
|
+
"inbar": ["age"],
|
|
60
|
+
"devine": ["sex", "height_cm"],
|
|
61
|
+
"robinson": ["sex", "height_cm"],
|
|
62
|
+
"miller-ideal-weight": ["sex", "height_cm"],
|
|
63
|
+
"peterson": ["height_cm"],
|
|
64
|
+
"mosteller": ["height_cm", "weight_kg"],
|
|
65
|
+
"du-bois": ["height_cm", "weight_kg"],
|
|
66
|
+
"haycock": ["height_cm", "weight_kg"],
|
|
67
|
+
}[id]
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
_UNIT = {"body-fat": "%", "body-fat-ranges": "%", "bmi": "kg/m²", "waist-to-height": "ratio",
|
|
71
|
+
"max-heart-rate": "bpm", "bmr": "kcal/day", "ideal-weight": "kg", "bsa": "m²"}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def calculate(id: str, inputs: Dict[str, Any]) -> Dict[str, Any]:
|
|
75
|
+
"""Compute one registry formula. Inputs use the /api/calc names (sex, age, height_cm, weight_kg, ...)."""
|
|
76
|
+
entry = get_formula(id)
|
|
77
|
+
if entry is None:
|
|
78
|
+
raise FormulaInputError(f'unknown formula id "{id}"')
|
|
79
|
+
for k in _needs(id, inputs.get("sex")):
|
|
80
|
+
v = inputs.get(k)
|
|
81
|
+
if v is None:
|
|
82
|
+
raise FormulaInputError(f'{id}: missing input "{k}"')
|
|
83
|
+
if k == "sex":
|
|
84
|
+
if v not in ("female", "male"):
|
|
85
|
+
raise FormulaInputError(f'{id}: sex must be "female" or "male"')
|
|
86
|
+
continue
|
|
87
|
+
b = _BOUNDS[k]
|
|
88
|
+
if isinstance(v, bool) or not isinstance(v, (int, float)) or not (b["min"] <= v <= b["max"]):
|
|
89
|
+
raise FormulaInputError(f"{id}: {k} must be a number from {b['min']} to {b['max']}")
|
|
90
|
+
sex = inputs.get("sex") or "female"
|
|
91
|
+
m = {
|
|
92
|
+
"sex": sex, "age": inputs.get("age"), "height": inputs.get("height_cm"), "weight": inputs.get("weight_kg"),
|
|
93
|
+
"waist": inputs.get("waist_cm"), "abdomen": inputs.get("abdomen_cm"), "neck": inputs.get("neck_cm"),
|
|
94
|
+
"hip": inputs.get("hip_cm"), "wrist": inputs.get("wrist_cm"), "forearm": inputs.get("forearm_cm"),
|
|
95
|
+
"thigh": inputs.get("thigh_cm"), "calf": inputs.get("calf_cm"), "bodyFat": inputs.get("body_fat_pct"),
|
|
96
|
+
}
|
|
97
|
+
fn = {
|
|
98
|
+
"deurenberg": lambda: F.deurenberg(m),
|
|
99
|
+
"cun-bae": lambda: F.cun_bae(m),
|
|
100
|
+
"us-navy": lambda: F.us_navy(m),
|
|
101
|
+
"rfm": lambda: F.rfm(m),
|
|
102
|
+
"covert-bailey": lambda: F.covert_bailey(m),
|
|
103
|
+
"gallagher-ranges": lambda: F.gallagher_ranges(m["age"], sex),
|
|
104
|
+
"who-bmi": lambda: F.who_bmi(m["weight"], m["height"]),
|
|
105
|
+
"trefethen-bmi": lambda: F.trefethen_bmi(m["weight"], m["height"]),
|
|
106
|
+
"ashwell-whtr": lambda: F.ashwell_whtr(m["waist"], m["height"]),
|
|
107
|
+
"tanaka": lambda: F.tanaka(m["age"]),
|
|
108
|
+
"gulati": lambda: F.gulati(m["age"], sex),
|
|
109
|
+
"fox": lambda: F.fox(m["age"]),
|
|
110
|
+
"harris-benedict-1919": lambda: F.harris_benedict_1919(m),
|
|
111
|
+
"harris-benedict-1984": lambda: F.harris_benedict_1984(m),
|
|
112
|
+
"katch-mcardle": lambda: F.katch_mcardle(m),
|
|
113
|
+
"mifflin-st-jeor": lambda: F.mifflin_st_jeor(m),
|
|
114
|
+
"nes": lambda: F.nes(m["age"]),
|
|
115
|
+
"londeree": lambda: F.londeree(m["age"]),
|
|
116
|
+
"inbar": lambda: F.inbar(m["age"]),
|
|
117
|
+
"devine": lambda: F.devine(m["height"], sex),
|
|
118
|
+
"robinson": lambda: F.robinson(m["height"], sex),
|
|
119
|
+
"miller-ideal-weight": lambda: F.miller_ideal_weight(m["height"], sex),
|
|
120
|
+
"peterson": lambda: F.peterson(m["height"]),
|
|
121
|
+
"mosteller": lambda: F.mosteller(m["height"], m["weight"]),
|
|
122
|
+
"du-bois": lambda: F.du_bois(m["height"], m["weight"]),
|
|
123
|
+
"haycock": lambda: F.haycock(m["height"], m["weight"]),
|
|
124
|
+
}[id]
|
|
125
|
+
value = fn()
|
|
126
|
+
if value is None:
|
|
127
|
+
if id == "gulati":
|
|
128
|
+
raise FormulaInputError('gulati: derived in women only; not defined for sex "male"')
|
|
129
|
+
raise FormulaInputError(f"{id}: the equation is undefined for these inputs")
|
|
130
|
+
return {
|
|
131
|
+
"value": value,
|
|
132
|
+
"unit": _UNIT[entry["category"]],
|
|
133
|
+
"formula": {"id": entry["id"], "name": entry["name"], "source": entry["source"],
|
|
134
|
+
"canonical_page": entry.get("canonical_page") or registry["canonical_page"]},
|
|
135
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"age": {
|
|
3
|
+
"min": 18,
|
|
4
|
+
"max": 100
|
|
5
|
+
},
|
|
6
|
+
"height_cm": {
|
|
7
|
+
"min": 100,
|
|
8
|
+
"max": 250
|
|
9
|
+
},
|
|
10
|
+
"weight_kg": {
|
|
11
|
+
"min": 20,
|
|
12
|
+
"max": 350
|
|
13
|
+
},
|
|
14
|
+
"waist_cm": {
|
|
15
|
+
"min": 45,
|
|
16
|
+
"max": 160
|
|
17
|
+
},
|
|
18
|
+
"abdomen_cm": {
|
|
19
|
+
"min": 50,
|
|
20
|
+
"max": 170
|
|
21
|
+
},
|
|
22
|
+
"neck_cm": {
|
|
23
|
+
"min": 20,
|
|
24
|
+
"max": 60
|
|
25
|
+
},
|
|
26
|
+
"hip_cm": {
|
|
27
|
+
"min": 60,
|
|
28
|
+
"max": 180
|
|
29
|
+
},
|
|
30
|
+
"wrist_cm": {
|
|
31
|
+
"min": 10,
|
|
32
|
+
"max": 25
|
|
33
|
+
},
|
|
34
|
+
"forearm_cm": {
|
|
35
|
+
"min": 15,
|
|
36
|
+
"max": 45
|
|
37
|
+
},
|
|
38
|
+
"thigh_cm": {
|
|
39
|
+
"min": 30,
|
|
40
|
+
"max": 90
|
|
41
|
+
},
|
|
42
|
+
"calf_cm": {
|
|
43
|
+
"min": 20,
|
|
44
|
+
"max": 60
|
|
45
|
+
},
|
|
46
|
+
"body_fat_pct": {
|
|
47
|
+
"min": 3,
|
|
48
|
+
"max": 65
|
|
49
|
+
}
|
|
50
|
+
}
|