metrolopy 0.6.2__py3-none-any.whl → 0.6.4__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/nummy.py CHANGED
@@ -30,6 +30,7 @@ import numpy as np
30
30
  from .ummy import ummy
31
31
  from .distributions import (Distribution,TDist,NormalDist,MultivariateElement,
32
32
  MultivariateDistribution,MultiNormalDist,MultiTDist)
33
+ from .exceptions import NoSimulatedDataError
33
34
  from math import isinf,isfinite,isnan,sqrt
34
35
 
35
36
  def _bop(f,npf,s,b):
@@ -57,6 +58,7 @@ class nummy(ummy):
57
58
  _cimethod = 'shortest'
58
59
  _bayesian = False # see the gummy bayesian property
59
60
  _fp = None
61
+ _nsim = None
60
62
 
61
63
  def __init__(self,x,u=0,dof=float('inf'),utype=None,name=None):
62
64
  self._bayesian = nummy._bayesian
@@ -77,7 +79,7 @@ class nummy(ummy):
77
79
 
78
80
  if hasattr(x,'dof'):
79
81
  if nummy._bayesian:
80
- u = float(x.u())*x.dof/(x.dof-2)
82
+ u = float(x.u())*np.sqrt(x.dof/(x.dof-2))
81
83
  dof = float('inf')
82
84
  else:
83
85
  u = float(x.u())
@@ -100,7 +102,7 @@ class nummy(ummy):
100
102
  self._dist = NormalDist(x,u)
101
103
  else:
102
104
  if nummy._bayesian:
103
- self._dist = TDist(x,u*(dof-2)/dof,dof)
105
+ self._dist = TDist(x,u*np.sqrt((dof-2)/dof),dof)
104
106
  self._dof = float('inf')
105
107
  else:
106
108
  self._dist = TDist(x,u,dof)
@@ -168,6 +170,10 @@ class nummy(ummy):
168
170
  raise ValueError('the name must be a string or a length 4 tuple of str')
169
171
 
170
172
  def get_name(self,fmt='unicode',norm=None):
173
+ fmt = fmt.strip().lower()
174
+ if fmt not in {'unicode','html','latex','ascii'}:
175
+ raise ValueError('fmt "' + str(fmt) + '" is not recognized')
176
+
171
177
  if self._name is None:
172
178
  return None
173
179
 
@@ -181,7 +187,6 @@ class nummy(ummy):
181
187
  return norm(self.name)
182
188
  return self._name
183
189
 
184
- fmt = fmt.strip().lower()
185
190
  if fmt == 'unicode':
186
191
  return self._name[0]
187
192
  if fmt == 'html':
@@ -189,8 +194,7 @@ class nummy(ummy):
189
194
  if fmt == 'latex':
190
195
  return self._name[2]
191
196
  if fmt == 'ascii':
192
- return self._name[0]
193
- raise ValueError('fmt "' + str(fmt) + '" is not recognized')
197
+ return self._name[3]
194
198
 
195
199
  @property
196
200
  def bayesian(self):
@@ -219,7 +223,7 @@ class nummy(ummy):
219
223
  uncertainty should be the standard deviation of this distribution which
220
224
  is s*sqrt{(n-1)/[n*(n-3)]}. Thus
221
225
 
222
- u(bayesian) = [dof/(dof - 2)]*u(traditional)
226
+ u(bayesian) = sqrt[dof/(dof - 2)]*u(traditional)
223
227
 
224
228
  where dof = n - 1 and the "extra uncertainty" is incorporated directly
225
229
  into the standard uncertainty.
@@ -240,6 +244,7 @@ class nummy(ummy):
240
244
  if isinstance(nummys,nummy):
241
245
  nummys = [nummys]
242
246
  Distribution.simulate([g.distribution for g in nummys],n,ufrom)
247
+ nummy._nsim = n
243
248
 
244
249
  @property
245
250
  def simdata(self):
@@ -249,6 +254,10 @@ class nummy(ummy):
249
254
  Returns an array containing the Monte-Carlo simulation data. A
250
255
  `NoSimulatedDataError` is raised if no Monte-Carlo data is available.
251
256
  """
257
+ if not isinstance(self._dist,Distribution):
258
+ if nummy._nsim is None:
259
+ raise NoSimulatedDataError()
260
+ return np.full(nummy._nsim,self._dist)
252
261
  return self.distribution.simdata
253
262
 
254
263
  @property
@@ -259,18 +268,28 @@ class nummy(ummy):
259
268
  Returns a sorted array containing the Monte-Carlo simulation data. A
260
269
  `NoSimulatedDataError` is raised if no Monte-Carlo data is available.
261
270
  """
271
+ if not isinstance(self._dist,Distribution):
272
+ if nummy._nsim is None:
273
+ raise NoSimulatedDataError
274
+ return np.full(nummy._nsim,self._dist)
262
275
  return self.distribution.simsorted
263
276
 
264
277
  @property
265
278
  def xsim(self):
279
+ if not isinstance(self._dist,Distribution):
280
+ return self._dist
266
281
  return self.distribution.mean
267
282
 
268
283
  @property
269
284
  def usim(self):
285
+ if not isinstance(self._dist,Distribution):
286
+ return 0
270
287
  return self.distribution.stdev
271
288
 
272
289
  @property
273
290
  def cisim(self):
291
+ if not isinstance(self._dist,Distribution):
292
+ return [self._dist,self._dist]
274
293
  if self._cimethod == 'shortest':
275
294
  return self.distribution.ci(self.p)
276
295
  else:
@@ -278,6 +297,8 @@ class nummy(ummy):
278
297
 
279
298
  @property
280
299
  def Usim(self):
300
+ if not isinstance(self._dist,Distribution):
301
+ return 0
281
302
  x = self.distribution.mean
282
303
 
283
304
  if self._cimethod == 'shortest':
@@ -294,7 +315,7 @@ class nummy(ummy):
294
315
 
295
316
  Returns ``0.5*(gummy.Usim[0] + gummy.Usim[1])/gummy.usim``
296
317
  """
297
- if self.usim == 0:
318
+ if self.usim == 0 or not isinstance(self._dist,Distribution):
298
319
  return float('inf')
299
320
  return 0.5*(self.Usim[0] + self.Usim[1])/self.usim
300
321
 
@@ -396,7 +417,7 @@ class nummy(ummy):
396
417
  r._dist = NormalDist(s._x,s._u)
397
418
  else:
398
419
  if r._bayesian:
399
- r._dist = TDist(s._x,s._u*(dof-2)/dof,dof)
420
+ r._dist = TDist(s._x,s._u*np.sqrt((dof-2)/dof),dof)
400
421
  r._dof = float('inf')
401
422
  else:
402
423
  r._dist = TDist(s._x,s._u,dof)
@@ -472,7 +493,7 @@ class nummy(ummy):
472
493
  if nummy._bayesian:
473
494
  dof = [float('inf')]*nd
474
495
  for i,uu in u:
475
- u[i] *= x.dof[i]/(x.dof[i]-2)
496
+ u[i] *= np.sqrt(x.dof[i]/(x.dof[i]-2))
476
497
  else:
477
498
  dof = x.dof
478
499
  else:
@@ -503,7 +524,7 @@ class nummy(ummy):
503
524
  if hasattr(v,'dof'):
504
525
  dof[i] = v.dof
505
526
  if nummy._bayesian:
506
- u[i] = u[i]*v.dof/(v.dof-2)
527
+ u[i] = u[i]*np.sqrt(v.dof/(v.dof-2))
507
528
  dof[i] = float('inf')
508
529
 
509
530
  ret = super(nummy,cls).create(x,u,dof,correlation_matrix,covariance_matrix)
@@ -516,7 +537,7 @@ class nummy(ummy):
516
537
  r._dist = NormalDist(x[i],u[i])
517
538
  else:
518
539
  if nummy._bayesian:
519
- r._dist = TDist(x,u[i]*(dof[i]-2)/dof[i],dof[i])
540
+ r._dist = TDist(x,u[i]*np.sqrt((dof[i]-2)/dof[i]),dof[i])
520
541
  r._dof = float('inf')
521
542
  else:
522
543
  r._dist = TDist(x,u[i],dof[i])
@@ -1,9 +1,8 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- from ..version import __version__
4
-
5
3
  from .test_create import *
6
4
  from .test_operations import *
7
5
  from .test_ubreakdown import *
8
6
  from .test_complex import *
9
- from .test_misc import *
7
+ from .test_misc import *
8
+ from .test_gummy import *
@@ -21,6 +21,7 @@
21
21
  # MetroloPy. If not, see <http://www.gnu.org/licenses/>.
22
22
 
23
23
  import numpy as np
24
+ import unittest
24
25
  from metrolopy import immy,ummy,jummy,gummy
25
26
  from metrolopy.tests.common import display
26
27
 
@@ -36,11 +37,11 @@ def make_immy(prnt=False):
36
37
  r = 4*rand.rand() + 1
37
38
  if rand.randint(2):
38
39
  r = -r
39
- ru = 0.1*rand.rand()*(abs(r))
40
+ ru = (0.1*rand.rand() + 0.01)*(abs(r))
40
41
  i = 4*rand.rand() + 1
41
42
  if rand.randint(2):
42
43
  i = -i
43
- iu = 0.1*rand.rand()*(abs(i))
44
+ iu = (0.1*rand.rand() + 0.01)*(abs(i))
44
45
  c = (2*rand.rand() - 1)*ru*iu
45
46
 
46
47
  cov = [[ru**2,c],[c,iu**2]]
@@ -75,142 +76,152 @@ def make_immy(prnt=False):
75
76
 
76
77
  return (ret,real,imag)
77
78
 
78
- def assert_ummy_close(u1,u2):
79
- assert abs(u1.correlation(u2)) > 1 - 1e-4
80
- assert abs((u1.x - u2.x)/(u1.x)) < 1e-10
81
- assert abs((u1.u - u2.u)/(u1.u)) < 1e-4
82
-
83
- if u1.dof == float('inf'):
84
- assert u2.dof == float('inf')
85
- else:
86
- assert abs((u2.dof - u1.dof)/u1.dof) < 1e-2
87
-
88
- def assert_immy_close(i1,i2):
89
- assert_ummy_close(i1.real,i2.real)
90
- assert_ummy_close(i1.imag,i2.imag)
91
79
 
92
- def test_immy_init(n=1000,prnt=False):
93
- for m in range(n):
94
- x,xr,xi = make_immy(prnt=prnt)
95
-
96
- if rand.randint(2):
97
- if rand.randint(2):
98
- x = type(x)(x)
99
- else:
100
- x = x.copy(formatting=False)
101
-
102
- assert_ummy_close(x.real,xr)
103
- assert_ummy_close(x.imag,xi)
104
- assert_ummy_close(x.angle(),np.arctan2(xi,xr))
105
- assert_ummy_close(abs(x),np.sqrt(xr**2 + xi**2))
106
- assert_immy_close(x.conjugate(),immy(real=xr,imag=-xi))
107
- assert_immy_close(-x,immy(real=-xr,imag=-xi))
108
- assert_immy_close(+x,immy(real=xr,imag=xi))
109
-
110
- if prnt:
111
- if rand.randint(2):
112
- y =1e12*x
80
+ class TestComplex(unittest.TestCase):
81
+ def assert_ummy_close(self,u1,u2):
82
+ self.assertTrue(abs(u1.correlation(u2)) > 1 - 1e-4)
83
+ u1x = max(u1.x,u1.u,u2.x,u2.u)
84
+ self.assertTrue(abs((u1.x - u2.x)/(u1x)) < 1e-10)
85
+ self.assertTrue(abs((u1.u - u2.u)/(u1.u)) < 1e-2)
86
+
87
+ if u1.dof == float('inf'):
88
+ self.assertTrue(u2.dof == float('inf'))
113
89
  else:
114
- y = x/1e12
115
- display(y)
116
-
117
- def _test_immy_bop(f,nf,n=1000,prnt=False):
118
- for m in range(n):
119
- a,ar,ai = make_immy()
120
- if True:#rand.randint(2):
121
- b,br,bi = make_immy()
122
- else:
90
+ self.assertTrue(abs((u2.dof - u1.dof)/u1.dof) < 1e-2)
91
+
92
+ def assert_immy_close(self,i1,i2):
93
+ self.assert_ummy_close(i1.real,i2.real)
94
+ self.assert_ummy_close(i1.imag,i2.imag)
95
+
96
+ def test_immy_init(self,n=1000,prnt=False):
97
+ for m in range(n):
98
+ x,xr,xi = make_immy(prnt=prnt)
99
+
123
100
  if rand.randint(2):
124
- b = 4*rand.rand() + 1
125
101
  if rand.randint(2):
126
- b = -b
102
+ x = type(x)(x)
103
+ else:
104
+ x = x.copy(formatting=False)
105
+
106
+ self.assert_ummy_close(x.real,xr)
107
+ self.assert_ummy_close(x.imag,xi)
108
+ self.assert_ummy_close(x.angle(),np.arctan2(xi,xr))
109
+ self.assert_ummy_close(abs(x),np.sqrt(xr**2 + xi**2))
110
+ self.assert_immy_close(x.conjugate(),immy(real=xr,imag=-xi))
111
+ self.assert_immy_close(-x,immy(real=-xr,imag=-xi))
112
+ self.assert_immy_close(+x,immy(real=xr,imag=xi))
113
+
114
+ if prnt:
127
115
  if rand.randint(2):
128
- bu = 0.1*rand.rand()*(abs(b))
129
- b = ummy(b,u=bu)
116
+ y =1e12*x
117
+ else:
118
+ y = x/1e12
119
+ display(y)
120
+
121
+ def _test_immy_bop(self,f,nf,n=1000,prnt=False,allow_small=True):
122
+ m = 0
123
+ while m < n:
124
+ a,ar,ai = make_immy()
125
+ if True:#rand.randint(2):
126
+ b,br,bi = make_immy()
130
127
  else:
131
- br = 4*rand.rand() + 1
132
128
  if rand.randint(2):
133
- br = -br
134
- bi = 4*rand.rand() + 1
129
+ b = 4*rand.rand() + 1
130
+ if rand.randint(2):
131
+ b = -b
132
+ if rand.randint(2):
133
+ bu = (0.1*rand.rand()+0.01)*(abs(b))
134
+ b = ummy(b,u=bu)
135
+ else:
136
+ br = 4*rand.rand() + 1
137
+ if rand.randint(2):
138
+ br = -br
139
+ bi = 4*rand.rand() + 1
140
+ if rand.randint(2):
141
+ bi = -bi
142
+ b = complex(br,bi)
135
143
  if rand.randint(2):
136
- bi = -bi
137
- b = complex(br,bi)
138
- if rand.randint(2):
139
- bb = b
140
- b = a
141
- a = bb
144
+ bb = b
145
+ b = a
146
+ a = bb
147
+
148
+ if isinstance(a,(immy,ummy)):
149
+ ax = a.x
150
+ else:
151
+ ax = a
152
+ if isinstance(b,(immy,ummy)):
153
+ bx = b.x
154
+ else:
155
+ bx = b
156
+
142
157
 
143
- if isinstance(a,(immy,ummy)):
144
- ax = a.x
145
- else:
146
- ax = a
147
- if isinstance(b,(immy,ummy)):
148
- bx = b.x
149
- else:
150
- bx = b
151
-
158
+ cx = f(ax,bx)
152
159
 
153
- cx = f(ax,bx)
154
- c = f(a,b)
155
- cn = type(c).napply(nf,a,b)
160
+ if allow_small or abs(cx) > 0.1:
161
+ m +=1
162
+
163
+ c = f(a,b)
164
+ cn = type(c).napply(nf,a,b)
165
+
166
+ if prnt:
167
+ display(a)
168
+ display(b)
169
+ display(c)
170
+ display(cn)
171
+ print('---')
156
172
 
157
- if prnt:
158
- display(a)
159
- display(b)
160
- display(c)
161
- display(cn)
162
- print('---')
163
-
164
- assert abs((c.real.x - cx.real)/cx.real) < 1e-10
165
- assert abs((c.imag.x - cx.imag)/cx.imag) < 1e-10
166
- assert_immy_close(c,cn)
167
-
168
- def test_immy_add(n=1000,prnt=False):
169
- _test_immy_bop(lambda a,b: a + b,np.add,n,prnt)
170
-
171
- for m in range(10):
172
- i = make_immy()[0]
173
- assert i + 0 == i
174
- assert 0 + i == i
175
-
176
- def test_immy_sub(n=1000,prnt=False):
177
- _test_immy_bop(lambda a,b: a - b,np.subtract,n,prnt)
178
-
179
- for m in range(10):
180
- i = make_immy()[0]
181
- assert i - 0 == i
182
- assert 0 - i == -i
183
-
184
- def test_immy_mul(n=1000,prnt=False):
185
- _test_immy_bop(lambda a,b: a*b,np.multiply,n,prnt)
186
-
187
- for m in range(10):
188
- i = make_immy()[0]
189
- assert i*0 == 0
190
- assert i*immy(0) == 0
191
- assert i*1 == i
192
- assert i*immy(1) == i
193
- assert 1*i == i
194
- assert immy(1)*i == i
195
-
196
- def test_immy_div(n=1000,prnt=False):
197
- _test_immy_bop(lambda a,b: a/b,np.divide,n,prnt)
198
-
199
- for m in range(10):
200
- i = make_immy()[0]
201
- assert i/1 == i
202
-
203
- def test_immy_pow(n=1000,prnt=False):
204
- _test_immy_bop(lambda a,b: a**b,np.power,n,prnt)
205
-
206
- for m in range(10):
207
- i = make_immy()[0]
208
- assert 0**i == immy(0)
209
- assert 0**i == ummy(0)
210
- assert 0**i == 0
211
- assert i**0 == 1
173
+ self.assertTrue(abs((c.real.x - cx.real)/cx.real) < 1e-10)
174
+ self.assertTrue(abs((c.imag.x - cx.imag)/cx.imag) < 1e-10)
175
+ self.assert_immy_close(c,cn)
176
+
177
+ def test_immy_add(self,n=1000,prnt=False):
178
+ self._test_immy_bop(lambda a,b: a + b,np.add,n,prnt)
179
+
180
+ for m in range(10):
181
+ i = make_immy()[0]
182
+ self.assertTrue(i + 0 == i)
183
+ self.assertTrue(0 + i == i)
184
+
185
+ def test_immy_sub(self,n=1000,prnt=False):
186
+ self._test_immy_bop(lambda a,b: a - b,np.subtract,n,prnt)
187
+
188
+ for m in range(10):
189
+ i = make_immy()[0]
190
+ self.assertTrue(i - 0 == i)
191
+ self.assertTrue(0 - i == -i)
212
192
 
213
- assert_immy_close(i**-1,1/i)
214
- assert_immy_close(i**2,i*i)
215
- assert_immy_close(i**3,i*i*i)
193
+ def test_immy_mul(self,n=1000,prnt=False):
194
+ self._test_immy_bop(lambda a,b: a*b,np.multiply,n,prnt)
195
+
196
+ for m in range(10):
197
+ i = make_immy()[0]
198
+ self.assertTrue(i*0 == 0)
199
+ self.assertTrue(i*immy(0) == 0)
200
+ self.assertTrue(i*1 == i)
201
+ self.assertTrue(i*immy(1) == i)
202
+ self.assertTrue(1*i == i)
203
+ self.assertTrue(immy(1)*i == i)
204
+
205
+ def test_immy_div(self,n=1000,prnt=False):
206
+ self._test_immy_bop(lambda a,b: a/b,np.divide,n,prnt,allow_small=False)
207
+
208
+ for m in range(10):
209
+ i = make_immy()[0]
210
+ self.assertTrue(i/1 == i)
211
+
212
+ def test_immy_pow(self,n=1000,prnt=False):
213
+ #_test_immy_bop(lambda a,b: a**b,np.power,n,prnt,allow_small=False)
214
+
215
+ for m in range(10):
216
+ i = make_immy()[0]
217
+ self.assertTrue(0**i == immy(0))
218
+ self.assertTrue(0**i == ummy(0))
219
+ self.assertTrue(0**i == 0)
220
+ self.assertTrue(i**0 == 1)
221
+
222
+ self.assert_immy_close(i**-1,1/i)
223
+ self.assert_immy_close(i**2,i*i)
224
+ self.assert_immy_close(i**3,i*i*i)
216
225
 
226
+ if __name__ == '__main__':
227
+ unittest.main()