metrolopy 0.6.5__py3-none-any.whl → 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.
metrolopy/indexed.py CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # module indexed
4
4
 
5
- # Copyright (C) 2019 National Research Council Canada
5
+ # Copyright (C) 2025 National Research Council Canada
6
6
  # Author: Harold Parks
7
7
 
8
8
  # This file is part of MetroloPy.
@@ -21,10 +21,11 @@
21
21
  # MetroloPy. If not, see <http://www.gnu.org/licenses/>.
22
22
 
23
23
  """
24
- indexed
24
+ A base class for the Unit and GummyConstant classes.
25
25
  """
26
26
 
27
27
  from importlib import import_module
28
+ from html import escape
28
29
 
29
30
 
30
31
  class _Builtin:
@@ -51,6 +52,8 @@ class Indexed:
51
52
  # _lib = {}
52
53
  # _open_lib = _builtin_lib
53
54
 
55
+ _case_sensitive = True
56
+
54
57
  @classmethod
55
58
  def alias(cls,alias,inst):
56
59
  """
@@ -65,6 +68,10 @@ class Indexed:
65
68
  A string referencing the `Indexed` instance that will be assigned
66
69
  the alias or the `Indexed` instance its self.
67
70
  """
71
+ if isinstance(alias,str):
72
+ if not cls._case_sensitive and ' ' in alias:
73
+ alias = alias.lower()
74
+ alias = alias.strip()
68
75
  inst = cls.get(inst)
69
76
  cls._open_lib[alias] = inst
70
77
  inst._add_alias(alias)
@@ -77,6 +84,10 @@ class Indexed:
77
84
  def get(cls,name,exception=True):
78
85
  if isinstance(name,cls):
79
86
  return name
87
+ if isinstance(name,str):
88
+ if not cls._case_sensitive and ' ' in name:
89
+ name = name.lower()
90
+ name = name.strip()
80
91
  c = cls._lib.get(name)
81
92
  if c is None:
82
93
  c = cls._builtin_lib.get(name)
@@ -116,7 +127,13 @@ class Indexed:
116
127
 
117
128
  def __init__(self,name,symbol=None,short_name=None,add_symbol=False,
118
129
  html_symbol=None,latex_symbol=None,ascii_symbol=None,
119
- description=None):
130
+ description=None,case_sensitive=True):
131
+
132
+ tolower = False
133
+ if isinstance(name,str):
134
+ if not self._case_sensitive and ' ' in name:
135
+ tolower = True
136
+ name = name.strip()
120
137
  self.name = name
121
138
  self.description=description
122
139
 
@@ -126,21 +143,42 @@ class Indexed:
126
143
  self.symbol = symbol
127
144
 
128
145
  if html_symbol is None:
129
- self.html_symbol = symbol
146
+ self.html_symbol = escape(symbol)
130
147
  else:
131
- self.html_symbol=html_symbol
148
+ self.html_symbol = html_symbol
132
149
  if latex_symbol is None:
133
- self.latex_symbol = self.format_latex(symbol)
150
+ sb = symbol.strip()
151
+ sc = {
152
+ '&': r'\&',
153
+ '%': r'\%',
154
+ '$': r'\$',
155
+ '#': r'\#',
156
+ '_': r'\_',
157
+ '{': r'\{',
158
+ '}': r'\}',
159
+ '~': r'\textasciitilde{}',
160
+ '^': r'\wedge{}',
161
+ '\\': r'\textbackslash{}',
162
+ '<': r'\textless{}',
163
+ '>': r'\textgreater{}',
164
+ ' ': r' \ '
165
+ }
166
+ self.latex_symbol = self.format_latex(''.join(sc[c] if c in sc
167
+ else c
168
+ for c in sb))
134
169
  else:
135
170
  self.latex_symbol = self.format_latex(latex_symbol)
136
171
  if ascii_symbol is None:
137
- self.ascii_symbol=symbol
172
+ self.ascii_symbol = ''.join(i if ord(i) < 128 else '_' for i in symbol)
138
173
  else:
139
174
  self.ascii_symbol=ascii_symbol
140
175
 
141
176
  self._aliases = set()
142
177
 
143
- self._open_lib[name] = self
178
+ if tolower:
179
+ self._open_lib[name.lower()] = self
180
+ else:
181
+ self._open_lib[name] = self
144
182
  if add_symbol:
145
183
  if symbol.strip() != name:
146
184
  self._open_lib[symbol.strip()] = self
@@ -150,8 +188,15 @@ class Indexed:
150
188
  self._open_lib[ascii_symbol.strip()] = self
151
189
  self._aliases.add(ascii_symbol.strip())
152
190
  if short_name is not None:
153
- self._open_lib[short_name] = self
191
+ tolower = False
192
+ if isinstance(short_name,str):
193
+ if not self._case_sensitive and ' ' in short_name:
194
+ tolower = True
195
+ short_name = short_name.strip()
154
196
  self.short_name = short_name
197
+ if tolower:
198
+ short_name = short_name.lower()
199
+ self._open_lib[short_name] = self
155
200
  self._aliases.add(short_name)
156
201
  else:
157
202
  if add_symbol:
@@ -185,22 +230,26 @@ class Indexed:
185
230
  def _add_alias(self,alias):
186
231
  self._aliases.add(alias)
187
232
 
188
- def tostring(self,fmt=None,**kwds):
233
+ def tostring(self,fmt=None,strip=True,**kwds):
189
234
  """
190
235
  Returns a string containing the symbol for the instance in the format
191
236
  given by the keyword `fmt` which may be set to a string the values
192
237
  'html', 'latex', 'ascii' or 'unicode'.
193
238
  """
194
239
  if fmt is None or fmt == 'unicode':
195
- return self.symbol
196
- if fmt is None:
197
- return self.symbol
198
- if fmt == 'html':
199
- return self.html_symbol
200
- if fmt == 'latex':
201
- return self.latex_symbol
202
- if fmt == 'ascii':
203
- return self.ascii_symbol
204
- raise ValueError('format ' + str(fmt) + ' is not recognized')
240
+ ret = self.symbol
241
+ elif fmt is None:
242
+ ret = self.symbol
243
+ elif fmt == 'html':
244
+ ret = self.html_symbol
245
+ elif fmt == 'latex':
246
+ ret = self.latex_symbol
247
+ elif fmt == 'ascii':
248
+ ret = self.ascii_symbol
249
+ else:
250
+ raise ValueError('format ' + str(fmt) + ' is not recognized')
251
+ if strip:
252
+ ret = ret.strip()
253
+ return ret
205
254
 
206
255
 
metrolopy/logunit.py CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # module logunit
4
4
 
5
- # Copyright (C) 2019 National Research Council Canada
5
+ # Copyright (C) 2025 National Research Council Canada
6
6
  # Author: Harold Parks
7
7
 
8
8
  # This file is part of MetroloPy.
metrolopy/mean.py CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # module mean
4
4
 
5
- # Copyright (C) 2019 National Research Council Canada
5
+ # Copyright (C) 2025 National Research Council Canada
6
6
  # Author: Harold Parks
7
7
 
8
8
  # This file is part of MetroloPy.
@@ -26,7 +26,6 @@ Some miscellaneous functions that are useful for uncertainty analysis.
26
26
 
27
27
  import numpy as np
28
28
  from .gummy import gummy
29
- from .distributions import TDist
30
29
  import datetime
31
30
 
32
31
  def autocorrelation(x):
@@ -182,7 +181,7 @@ def wmean(x,chi_correct=False):
182
181
 
183
182
  return ret
184
183
 
185
- def mean(x,n_sigma_trim=3,unit=1,ignore_nan=True,use_n_eff=None):
184
+ def mean(x,n_sigma_trim=3,unit=1,ignore_nan=True,use_n_eff=None,utype=None):
186
185
  """
187
186
  Returns a gummy representing the mean of a float array.
188
187
 
@@ -232,11 +231,11 @@ def mean(x,n_sigma_trim=3,unit=1,ignore_nan=True,use_n_eff=None):
232
231
  if dof < 1:
233
232
  dof = 1
234
233
 
235
- u = float(np.std(x,ddof=1))/np.sqrt(n)
234
+ u = float(np.std(x,ddof=1)/np.sqrt(n))
236
235
  if gummy.bayesian:
237
236
  u *= np.sqrt(dof/(dof-2))
238
237
 
239
- return gummy(m,u,dof=dof,unit=unit)
238
+ return gummy(m,u,dof=dof,unit=unit,utype=utype)
240
239
 
241
240
  def sigma_trim(x, n_sigma = 3):
242
241
  """
@@ -284,7 +283,7 @@ def delta_diff(x):
284
283
  z[0::2] = -z[0::2]
285
284
  return z
286
285
 
287
- def delta_diff_mean(x,n_sigma=None,unit=1):
286
+ def delta_diff_mean(x,n_sigma=None,unit=1,utype=None):
288
287
  """
289
288
  Returns a gummy representing the mean value and uncertainty of a delta
290
289
  type difference taken on the data. A delta type difference removes a
@@ -334,7 +333,7 @@ def delta_diff_mean(x,n_sigma=None,unit=1):
334
333
  if gummy.bayesian:
335
334
  u *= np.sqrt(dof/(dof-2))
336
335
 
337
- return gummy(mn,u,dof=dof,unit=unit)
336
+ return gummy(float(mn),u=float(u),dof=dof,unit=unit,utype=utype)
338
337
 
339
338
  def delta_sum(x):
340
339
  """Returns a list containing differences of the type:
@@ -354,7 +353,7 @@ def delta_sum(x):
354
353
  z[0::2] = -z[0::2]
355
354
  return delta_diff(z)/2.0
356
355
 
357
- def delta_sum_mean(x,n_sigma=None,unit=1):
356
+ def delta_sum_mean(x,n_sigma=None,unit=1,utype=None):
358
357
  """
359
358
  Returns a gummy representing the mean value
360
359
  and uncertainty of a delta type difference taken on the data. A delta type
@@ -383,7 +382,7 @@ def delta_sum_mean(x,n_sigma=None,unit=1):
383
382
  """
384
383
  z = np.asanyarray(x)
385
384
  z[0::2] = -z[0::2]
386
- return delta_diff_mean(z,n_sigma,unit)/2.0
385
+ return delta_diff_mean(z,n_sigma,unit=unit,utype=utype)/2.0
387
386
 
388
387
  def mean_datetime(*params):
389
388
  if len(params) == 1:
metrolopy/miscunits.py CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # module miscunits
4
4
 
5
- # Copyright (C) 2019 National Research Council Canada
5
+ # Copyright (C) 2025 National Research Council Canada
6
6
  # Author: Harold Parks
7
7
 
8
8
  # This file is part of MetroloPy.
@@ -29,10 +29,9 @@ non-SI units accepted for use with the SI not in the siunits module, some
29
29
  obsolete SI units, and a few other units.
30
30
  """
31
31
 
32
- from numpy import sqrt,log,log2
33
- from .gummy import gummy,_lg10
34
- from .ummy import MFraction
35
- from .unit import Unit,Conversion
32
+ from numpy import sqrt,log,log2,log10
33
+ from .gummy import gummy
34
+ from .unit import Unit,Conversion,MFraction
36
35
  from .prefixedunit import PrefixedUnit,BinaryPrefixedUnit
37
36
  from .logunit import LogUnit,LogConversion
38
37
  from .siunits import (_kg,_V,_ohm,_s,_K,_J,_m,_d,_min,_h,_W,_Hz,_C,_A,_N,_cm,
@@ -40,6 +39,22 @@ from .siunits import (_kg,_V,_ohm,_s,_K,_J,_m,_d,_min,_h,_W,_Hz,_C,_A,_N,_cm,
40
39
  from .constcom import (dalton,KJ,RK,KJ90,RK90,e,c,solar_mass,jupiter_mass,
41
40
  earth_mass,hbar,a0,me,mp,G,alph,k,pi,euler)
42
41
 
42
+ def _lg10(x):
43
+ try:
44
+ from mpmath import mpf
45
+ from mpmath import log10 as mlog10
46
+ except:
47
+ mlog10 = mpf = None
48
+
49
+ if isinstance(x,mpf):
50
+ return mlog10(x)
51
+ if hasattr(x,'log10'):
52
+ return x.log10()
53
+ try:
54
+ return log10(x)
55
+ except:
56
+ return log10(float(x))
57
+
43
58
  with Unit._builtin():
44
59
 
45
60
  # non-SI accepted for use with the SI, SI Brochure, 9th ed
@@ -146,7 +161,7 @@ with Unit._builtin():
146
161
  add_symbol=True,html_symbol='<i>E</i><sub>h</sub>',latex_symbol='\t\tE_{\\mathrm{h}}',
147
162
  description='natural, atomic unit of energy')
148
163
  Unit.alias('Ha',_Eh)
149
- Unit('reduced compton wavelength','\u019b(C)',
164
+ Unit('reduced Compton wavelength','\u019b(C)',
150
165
  Conversion(_hbar*_me**-1*_c**-1,1),order=0,add_symbol=True,
151
166
  html_symbol='\u019b<sub>C</sub>',latex_symbol='\t\t\u019b_{\\mathrm{C}}',
152
167
  ascii_symbol='lambda(C)',