labfreed 0.0.8__py2.py3-none-any.whl → 0.0.10__py2.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.
@@ -1,32 +0,0 @@
1
- from math import floor, log10, pow
2
-
3
- def to_significant_digits_str(x:int|float, uncertainty:float|int) -> str:
4
- if uncertainty == None:
5
- if isinstance(x, float):
6
- Warning(f'Uncertainty was given as none. Returning unrounded number')
7
- return str(x)
8
- else:
9
- uncertainty = 1
10
-
11
-
12
- log_least_significant_digit = floor(log10(uncertainty))
13
- digits = -log_least_significant_digit
14
-
15
- x_significant = round(x, digits)
16
-
17
- if digits <= 0:
18
- return str(int(x_significant))
19
- else:
20
- return str(x_significant)
21
-
22
-
23
-
24
- if __name__ == "__main__":
25
- print(to_significant_digits_str(111111.1111111, 1000))
26
- print(to_significant_digits_str(111111.1111111, 100))
27
- print(to_significant_digits_str(111111.1111111, 10))
28
- print(to_significant_digits_str(111111.1111111, 1))
29
- print(to_significant_digits_str(111111.1111111, 0.1))
30
- print(to_significant_digits_str(111111.1111111, 0.01))
31
- print(to_significant_digits_str(111111.1111111, 0.001))
32
- print(to_significant_digits_str(111111.1111111, 0.0001))
@@ -1,143 +0,0 @@
1
- from typing import Tuple
2
- from typing_extensions import Annotated
3
- from pydantic import BaseModel, AfterValidator
4
- import quantities as pq
5
- from quantities import Quantity, UnitQuantity, units, dimensionless
6
- from .uncertainty import to_significant_digits_str
7
-
8
-
9
-
10
- def validate_unit(unit_name:str) -> str :
11
- """
12
- Pydantic validator function for the unit.
13
- Checks if the unit is a valid unit.
14
-
15
-
16
- Args:
17
- unit (str): unit symbol, e.g. 'kg'
18
-
19
- Returns:
20
- str: the input unit.
21
-
22
- Errors:
23
- raises an AssertionError if validation fails
24
- """
25
- if hasattr(pq, unit_name):
26
- return unit_name
27
- else:
28
- assert False
29
-
30
-
31
- class PydanticUncertainQuantity(BaseModel):
32
- data:int|float
33
- unit_name: Annotated[str, AfterValidator(validate_unit)]
34
- unit_symbol: str
35
- uncertainty:float|None=None
36
-
37
- @property
38
- def for_display(self):
39
- return self.__str__()
40
-
41
- def as_strings(self):
42
- unit_symbol = self.unit_symbol
43
- if unit_symbol == "dimensionless":
44
- unit_symbol = ""
45
- s = ''
46
-
47
- val_str = to_significant_digits_str(self.data, self.uncertainty)
48
- return f"{val_str}", f"{unit_symbol}", f"{val_str} {unit_symbol}"
49
-
50
-
51
-
52
- def __str__(self):
53
- unit_symbol = self.unit_symbol
54
- if unit_symbol == "dimensionless":
55
- unit_symbol = ""
56
-
57
- s = f"{to_significant_digits_str(self.data, self.uncertainty)} {unit_symbol}"
58
- return s
59
-
60
-
61
- unit_map = [
62
- ('MGM', units.milligram),
63
- ('GRM', units.gram),
64
- ('KGM', units.kilogram),
65
-
66
- ('CEL', units.celsius),
67
-
68
- ('LTR', units.liter),
69
- ('MLT', units.milliliter),
70
-
71
- ('C34', units.mole),
72
- ('D43',units.atomic_mass_unit),
73
-
74
- ('1', units.dimensionless),
75
- ('C62', units.dimensionless),
76
-
77
- ('BAR',units.bar),
78
- ('MBR',units.millibar),
79
- ('KBA',units.kilobar),
80
-
81
- ('RPM', units.rpm),
82
-
83
- ('HTZ', units.hertz),
84
- ('KHZ', units.kilohertz),
85
- ('MHZ',units.megahertz),
86
-
87
- ('SEC', units.second),
88
- ('MIN', units.minute),
89
- ('HUR', units.hour),
90
-
91
- ('MTR', units.meter)
92
-
93
- ]
94
-
95
-
96
- def quantity_from_UN_CEFACT(value:str, unit_UN_CEFACT) -> PydanticUncertainQuantity:
97
- """
98
- Maps units from https://unece.org/trade/documents/revision-17-annexes-i-iii
99
- to an object of the quantities library https://python-quantities.readthedocs.io/en/latest/index.html
100
- """
101
- # cast to numeric type. try int first, which will fail if string has no decimals.
102
- # nothing to worry yet: try floast next. if that fails the input was not a str representation of a number
103
- try:
104
- value_out = int(value)
105
- except ValueError:
106
- try:
107
- value_out = float(value)
108
- except ValueError as e:
109
- raise Exception(f'Input {value} is not a str representation of a number') from e
110
-
111
- d = {um[0]: um[1] for um in unit_map}
112
-
113
- unit = d.get(unit_UN_CEFACT)
114
- if not unit:
115
- raise NotImplementedError(f"lookup for unit {unit} not implemented")
116
- out = PydanticUncertainQuantity(data=value_out, unit_name=unit.name, unit_symbol=unit.symbol)
117
-
118
- return out
119
-
120
-
121
- def quantity_to_UN_CEFACT(value:PydanticUncertainQuantity ) -> Tuple[int|float, str]:
122
- d = {um[1].symbol: um[0] for um in unit_map}
123
-
124
- unit_un_cefact = d.get(value.unit_symbol)
125
- if not unit_un_cefact:
126
- raise NotImplementedError(f"lookup for unit {value.unit_symbol} not implemented")
127
- return value.data, unit_un_cefact
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
- if __name__ == "__main__":
138
- pass
139
-
140
-
141
-
142
-
143
-
@@ -1,19 +0,0 @@
1
- labfreed/__init__.py,sha256=tnLtz0mQ8ljO5N2ESqqI0ENkU-_cJi9_XwG0WkGZYKQ,87
2
- labfreed/validation.py,sha256=dG-SubAguub67RTvw51xlErkqXbzzq_5rwxJwWg6SfY,2869
3
- labfreed/DisplayNameExtension/DisplayNameExtension.py,sha256=FlT53u1EntpsLmho6GZtgIWBZBNWkl9STxzJBvojR6M,1033
4
- labfreed/DisplayNameExtension/base36.py,sha256=2lwmEMWm8qrFJkcrP-nMPwS0eCm2THhCJ3Vk-TdGQg0,2455
5
- labfreed/PAC_CAT/__init__.py,sha256=frcCV1k9oG9oKj3dpUqdJg1PxRT2RSN_XKdLCPjaYaY,2
6
- labfreed/PAC_CAT/data_model.py,sha256=hob-WNs2-633LmxQ7Ot3RBpcvStYFzdj20QDQZOQyqY,4306
7
- labfreed/PAC_ID/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- labfreed/PAC_ID/data_model.py,sha256=DKU9Rptl9DcxiUdFf28XMyy4YcGUG7-a1EWuEpIFXVc,7931
9
- labfreed/PAC_ID/parse.py,sha256=t38ABXZ0siUVDW5oEDmkF7uQ6iSX8Dbkeg-lWNlOgWA,5011
10
- labfreed/PAC_ID/serialize.py,sha256=0BhF7aXGlLpr312lkBvl1O5fXDFZeLLPgSBddO9Y86Q,1963
11
- labfreed/PAC_ID/well_known_segment_keys.py,sha256=zrzMvvS42urPpiwinI-IhHPgT3r86zEBl4TlEMOfzbU,338
12
- labfreed/TREXExtension/data_model.py,sha256=eT4KyQklTO6m-wA28KyJ8wzT8ONhG3fOB3JU6b19ScY,8011
13
- labfreed/TREXExtension/parse.py,sha256=OpRKu2FICAiOaaur6oxFY828xg1JEFj4fgPnXW79G4o,2118
14
- labfreed/TREXExtension/uncertainty.py,sha256=l3WxrLnWTQYfX28gFisXwDcVPvT8bCAd4q6Xl02dRdE,1117
15
- labfreed/TREXExtension/unit_utilities.py,sha256=WzrC1CZMgBccxADXFP6nLkMWVDqCCkNb3trPyt3BvF8,3826
16
- labfreed-0.0.8.dist-info/licenses/LICENSE,sha256=gHFOv9FRKHxO8cInP3YXyPoJnuNeqrvcHjaE_wPSsQ8,1100
17
- labfreed-0.0.8.dist-info/WHEEL,sha256=BXjIu84EnBiZ4HkNUBN93Hamt5EPQMQ6VkF7-VZ_Pu0,100
18
- labfreed-0.0.8.dist-info/METADATA,sha256=36ASUvZ6qHjozWU0zI9_jPyPx3YNIqdppd5DGUSHjIs,206
19
- labfreed-0.0.8.dist-info/RECORD,,