labunits 0.1.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.
labunits/__init__.py
ADDED
|
File without changes
|
labunits/converters.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import math
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
_analytes_data = {}
|
|
6
|
+
|
|
7
|
+
type LoincNum = str
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _load_data() -> dict:
|
|
11
|
+
"""
|
|
12
|
+
Load data from the 'data' directory.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
dict: Data in the format {'analyte': {'value': float, 'units': str}}
|
|
16
|
+
"""
|
|
17
|
+
global _analytes_data
|
|
18
|
+
# check if data is already loaded
|
|
19
|
+
if _analytes_data:
|
|
20
|
+
return _analytes_data
|
|
21
|
+
|
|
22
|
+
# get data dir of labunits package using Pathlib
|
|
23
|
+
data_dir = Path(__file__).parent / "data"
|
|
24
|
+
|
|
25
|
+
with Path.open(data_dir / "analytes.json", "r") as file:
|
|
26
|
+
_analytes_data = json.load(file)
|
|
27
|
+
|
|
28
|
+
return _analytes_data
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _resolve_analyte_identifier(identifier: str) -> LoincNum:
|
|
32
|
+
"""Resolve various identifiers to LOINC code."""
|
|
33
|
+
data = _load_data()
|
|
34
|
+
|
|
35
|
+
# Direct LOINC lookup
|
|
36
|
+
if identifier in data:
|
|
37
|
+
return identifier
|
|
38
|
+
|
|
39
|
+
# Search by abbreviation or name
|
|
40
|
+
for loinc, info in data.items():
|
|
41
|
+
if ( # FIXME
|
|
42
|
+
info.get("abbreviation", "").lower() == identifier.lower()
|
|
43
|
+
or info.get("name", "").lower() == identifier.lower()
|
|
44
|
+
):
|
|
45
|
+
return loinc
|
|
46
|
+
|
|
47
|
+
raise ValueError(f"Unknown analyte identifier: {identifier}")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def si_unit(analyte: LoincNum | str) -> str:
|
|
51
|
+
"""Returns the SI unit for the given analyte.
|
|
52
|
+
|
|
53
|
+
Examples:
|
|
54
|
+
"mmol/L"
|
|
55
|
+
"µmol/L"
|
|
56
|
+
"""
|
|
57
|
+
loinc = _resolve_analyte_identifier(analyte)
|
|
58
|
+
return _analytes_data[loinc]["si_units"]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def traditional_unit(analyte: LoincNum | str) -> str:
|
|
62
|
+
"""Returns the traditional unit for the given analyte."""
|
|
63
|
+
loinc = _resolve_analyte_identifier(analyte)
|
|
64
|
+
return _analytes_data[loinc]["traditional_units"]
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def conversion_factor(analyte: LoincNum | str) -> float:
|
|
68
|
+
"""Returns the conversion factor for the given analyte.
|
|
69
|
+
|
|
70
|
+
Multiply with the factor to convert from traditional to SI, divide by it the
|
|
71
|
+
other way round.
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
float: Conversion factor
|
|
75
|
+
"""
|
|
76
|
+
loinc = _resolve_analyte_identifier(analyte)
|
|
77
|
+
return float(_analytes_data[loinc]["conversion_factor"])
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def to_si_unit(analyte: LoincNum | str, value: float) -> float:
|
|
81
|
+
"""
|
|
82
|
+
Convert value to SI unit.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
analyte: LOINC code (preferred), abbreviation, or full name
|
|
86
|
+
value (float): Concentration in traditional units
|
|
87
|
+
Returns:
|
|
88
|
+
float: value in SI units
|
|
89
|
+
"""
|
|
90
|
+
# Normalize to LOINC internally
|
|
91
|
+
loinc = _resolve_analyte_identifier(analyte)
|
|
92
|
+
data = _load_data()
|
|
93
|
+
factor = float(data[loinc]["conversion_factor"])
|
|
94
|
+
|
|
95
|
+
if math.isinf(value) or math.isnan(value):
|
|
96
|
+
return value
|
|
97
|
+
return value * factor
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def to_traditional_unit(analyte: str, value: float) -> float:
|
|
101
|
+
"""
|
|
102
|
+
Convert SI to value unit.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
analyte: LOINC code (preferred), abbreviation, or full name
|
|
106
|
+
value (float): Concentration in SI units
|
|
107
|
+
Returns:
|
|
108
|
+
float: value in traditional units
|
|
109
|
+
"""
|
|
110
|
+
loinc = _resolve_analyte_identifier(analyte)
|
|
111
|
+
data = _load_data()
|
|
112
|
+
factor = float(data[loinc]["conversion_factor"])
|
|
113
|
+
|
|
114
|
+
if math.isinf(value) or math.isnan(value):
|
|
115
|
+
return value
|
|
116
|
+
return value / factor
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: labunits
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Convert laboratory units between mmol/l und mg/dl based on the molar mass of the unit.
|
|
5
|
+
Author-email: Christian Gonzalez <office@nerdocs.at>
|
|
6
|
+
Project-URL: Homepage, https://github.com/nerdocs/labunits
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE.md
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# Labunits
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
This is a simple python library for laboratory units, and to convert between them.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Why another library with this content?
|
|
19
|
+
|
|
20
|
+
Oh - just because - there IS NONE. I couldn't believe that.
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### License
|
|
24
|
+
|
|
25
|
+
MIT. Just use it in your project and give credits.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
labunits/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
labunits/converters.py,sha256=xw6672DnbxuEJ5GPr3HN5YeIvoz0TZUPjaUBW9hgTRs,3095
|
|
3
|
+
labunits-0.1.0.dist-info/licenses/LICENSE.md,sha256=PPcvoP2uKoZilEpSLmGF9LoxdCYf9DaXbiyaDwiVQt4,1097
|
|
4
|
+
labunits-0.1.0.dist-info/METADATA,sha256=ENIgrSEd0wsRqoZoJlODRLZzZl0sUSMjXMdd7yk-v9c,640
|
|
5
|
+
labunits-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
6
|
+
labunits-0.1.0.dist-info/top_level.txt,sha256=zolV0gwuFhfmRhBzph7gx8MdTX2C6zhkOFfdQL4_rOA,9
|
|
7
|
+
labunits-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2025 <Christian González>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
labunits
|