Debian Codegraph @

pint/facets/nonmultiplicative/definitions.py

raw
1 """
2 pint.facets.nonmultiplicative.definitions
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 :copyright: 2022 by Pint Authors, see AUTHORS for more details.
6 :license: BSD, see LICENSE for more details.
7 """
8
9 from __future__ import annotations
10
11 from dataclasses import dataclass
12
13 from ..._typing import Magnitude
14 from ...compat import HAS_NUMPY, exp, log
15 from ..plain import ScaleConverter
16
17
18 @dataclass(frozen=True)
19 class OffsetConverter(ScaleConverter):
20 """An affine transformation."""
21
22 offset: float
23
24 @property
25 def is_multiplicative(self):
26 return self.offset == 0
27
28 def to_reference(self, value: Magnitude, inplace: bool = False) -> Magnitude:
29 if inplace:
30 value *= self.scale
31 value += self.offset
32 else:
33 value = value * self.scale + self.offset
34
35 return value
36
37 def from_reference(self, value: Magnitude, inplace: bool = False) -> Magnitude:
38 if inplace:
39 value -= self.offset
40 value /= self.scale
41 else:
42 value = (value - self.offset) / self.scale
43
44 return value
45
46 @classmethod
47 def preprocess_kwargs(cls, **kwargs):
48 if "offset" in kwargs and kwargs["offset"] == 0:
49 return {"scale": kwargs["scale"]}
50 return None
51
52
53 @dataclass(frozen=True)
54 class LogarithmicConverter(ScaleConverter):
55 """Converts between linear units and logarithmic units, such as dB, octave, neper or pH.
56 Q_log = logfactor * log( Q_lin / scale ) / log(log_base)
57
58 Parameters
59 ----------
60 scale : float
61 unit of reference at denominator for logarithmic unit conversion
62 logbase : float
63 plain of logarithm used in the logarithmic unit conversion
64 logfactor : float
65 factor multiplied to logarithm for unit conversion
66 inplace : bool
67 controls if computation is done in place
68 """
69
70 # TODO: Can I use PintScalar here?
71 logbase: float
72 logfactor: float
73
74 @property
75 def is_multiplicative(self):
76 return False
77
78 @property
79 def is_logarithmic(self):
80 return True
81
82 def from_reference(self, value: Magnitude, inplace: bool = False) -> Magnitude:
83 """Converts value from the reference unit to the logarithmic unit
84
85 dBm <------ mW
86 y dBm = 10 log10( x / 1mW )
87 """
88 if inplace:
89 value /= self.scale
90 if HAS_NUMPY:
91 log(value, value)
92 else:
93 value = log(value)
94 value *= self.logfactor / log(self.logbase)
95 else:
96 value = self.logfactor * log(value / self.scale) / log(self.logbase)
97
98 return value
99
100 def to_reference(self, value: Magnitude, inplace: bool = False) -> Magnitude:
101 """Converts value to the reference unit from the logarithmic unit
102
103 dBm ------> mW
104 y dBm = 10 log10( x / 1mW )
105 """
106 if inplace:
107 value /= self.logfactor
108 value *= log(self.logbase)
109 if HAS_NUMPY:
110 exp(value, value)
111 else:
112 value = exp(value)
113 value *= self.scale
114 else:
115 value = self.scale * exp(log(self.logbase) * (value / self.logfactor))
116
117 return value