richvalues 4.2.5__tar.gz → 4.2.7__tar.gz

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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: richvalues
3
- Version: 4.2.5
3
+ Version: 4.2.7
4
4
  Summary: Python library for working with uncertainties and upper/lower limits
5
5
  Home-page: https://github.com/andresmegias/richvalues/
6
6
  Author: Andrés Megías Toledano
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "richvalues"
7
- version = "4.2.5"
7
+ version = "4.2.7"
8
8
  description = "Python library for working with uncertainties and upper/lower limits"
9
9
  license = {file="LICENSE"}
10
10
  authors = [{name="Andrés Megías Toledano"}]
@@ -37,7 +37,7 @@ IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37
37
  POSSIBILITY OF SUCH DAMAGE.
38
38
  """
39
39
 
40
- __version__ = '4.2.5'
40
+ __version__ = '4.2.7'
41
41
  __author__ = 'Andrés Megías Toledano'
42
42
 
43
43
  import copy
@@ -59,7 +59,7 @@ defaultparams = {
59
59
  'size of samples': int(8e3),
60
60
  'number of significant figures': 1,
61
61
  'minimum exponent for scientific notation': 4,
62
- 'maximum number of decimals': 5,
62
+ 'maximum number of decimals to use parenthesis': 5,
63
63
  'limit for extra significant figure': 2.5,
64
64
  'use extra significant figure for exact values': True,
65
65
  'use extra significant figure for finite intervals': True,
@@ -211,7 +211,7 @@ def round_sf_unc(x, dx, n=None, min_exp=None, max_dec=None, extra_sf_lim=None):
211
211
  """
212
212
  n = set_default_value(n, 'number of significant figures')
213
213
  min_exp = set_default_value(min_exp, 'minimum exponent for scientific notation')
214
- max_dec = set_default_value(max_dec, 'maximum number of decimals')
214
+ max_dec = set_default_value(max_dec, 'maximum number of decimals to use parenthesis')
215
215
  extra_sf_lim = set_default_value(extra_sf_lim, 'limit for extra significant figure')
216
216
  use_exp = True
217
217
  if ((float(x) > float(dx)
@@ -307,19 +307,26 @@ def round_sf_unc(x, dx, n=None, min_exp=None, max_dec=None, extra_sf_lim=None):
307
307
  d = len(y.split('e')[0].split('.')[-1]) if '.' in y else 0
308
308
  if max_dec == 0 or d > max_dec and ')' not in dy:
309
309
  if 'e' in y:
310
- y, exp = y.split('e')
310
+ y, exp_y = y.split('e')
311
311
  dy, _ = dy.split('e')
312
312
  else:
313
- exp = None
314
- d = len(y.split('.')[-1]) if '.' in y else 0
315
- d_ = len(dy.split('.')[-1]) if '.' in dy else 0
316
- dy_ = round_sf(float(dy)*10**(d-d_), n, 0, extra_sf_lim)
317
- dy, exp_ = dy_.split('e')
318
- if int(exp_) > 0:
319
- dy = round_sf(float(dy)*10**int(exp_), n, np.inf, extra_sf_lim)
320
- dy = '(' + dy + ')'
321
- if exp is not None:
322
- y = '{}e{}'.format(y, exp)
313
+ exp_y = None
314
+ if dy != '0':
315
+ d = len(y.split('.')[-1]) if '.' in y else 0
316
+ d_ = len(dy.split('.')[-1]) if '.' in dy else 0
317
+ dy_ = round_sf(float(dy)*10**(d-d_), n, 0, extra_sf_lim)
318
+ dy = dy_.split('e')[0]
319
+ exp_dy = dy_.split('e')[1] if 'e' in dy_ else 0
320
+ exp_y_ = 0 if exp_y is None else exp_y
321
+ if int(exp_y_) > int(exp_dy):
322
+ dy = round_sf(float(dy)*10**int(exp_dy), n, np.inf, extra_sf_lim)
323
+ if '.' in dy:
324
+ dy = dy.split('.')[-1]
325
+ dy = '(' + dy + ')'
326
+ else:
327
+ dy = ''
328
+ if exp_y is not None:
329
+ y = '{}e{}'.format(y, exp_y)
323
330
  return y, dy
324
331
 
325
332
  def round_sf_uncs(x, dx, n=None, min_exp=None, max_dec=None, extra_sf_lim=None):
@@ -354,7 +361,7 @@ def round_sf_uncs(x, dx, n=None, min_exp=None, max_dec=None, extra_sf_lim=None):
354
361
  """
355
362
  n = set_default_value(n, 'number of significant figures')
356
363
  min_exp = set_default_value(min_exp, 'minimum exponent for scientific notation')
357
- max_dec = set_default_value(max_dec, 'maximum number of decimals')
364
+ max_dec = set_default_value(max_dec, 'maximum number of decimals to use parenthesis')
358
365
  extra_sf_lim = set_default_value(extra_sf_lim, 'limit for extra significant figure')
359
366
  dx1, dx2 = dx
360
367
  y1, dy1 = round_sf_unc(x, dx1, n, min_exp, max_dec, extra_sf_lim)
@@ -583,7 +590,7 @@ class RichValue():
583
590
  self.domain = domain
584
591
  self.num_sf = defaultparams['number of significant figures']
585
592
  self.min_exp = defaultparams['minimum exponent for scientific notation']
586
- self.max_dec = defaultparams['maximum number of decimals']
593
+ self.max_dec = defaultparams['maximum number of decimals to use parenthesis']
587
594
  self.extra_sf_lim = defaultparams['limit for extra significant figure']
588
595
  self.pdf_info = 'default'
589
596
  self.variables = variables
@@ -922,6 +929,8 @@ class RichValue():
922
929
  dy2, _ = dy2.split('e')
923
930
  if ')' not in dy1:
924
931
  if dy1 == dy2:
932
+ if dy1 == '':
933
+ dy1 = dy2 = '0'
925
934
  if float(dy1) != 0:
926
935
  text = '{}+/-{} e{}'.format(y, dy1, a)
927
936
  else:
@@ -1218,6 +1227,7 @@ class RichValue():
1218
1227
  rvalue = RichValue(x, dx, domain=domain, is_int=self.is_int)
1219
1228
  rvalue.num_sf = self.num_sf
1220
1229
  rvalue.min_exp = self.min_exp
1230
+ rvalue.max_dec = self.max_dec
1221
1231
  rvalue.extra_sf_lim = self.extra_sf_lim
1222
1232
  rvalue.variables = self.variables
1223
1233
  expression = self.expression
@@ -1256,6 +1266,7 @@ class RichValue():
1256
1266
  rvalue.domain[0] = max(0, rvalue.domain[0])
1257
1267
  rvalue.num_sf = self.num_sf
1258
1268
  rvalue.min_exp = self.min_exp
1269
+ rvalue.max_dec = self.max_dec
1259
1270
  rvalue.extra_sf_lim = self.extra_sf_lim
1260
1271
  rvalue.variables = self.variables
1261
1272
  rvalue.expression = 'abs({})'.format(self.expression)
@@ -1501,6 +1512,7 @@ class RichValue():
1501
1512
  rvalue = RichValue(np.nan)
1502
1513
  rvalue.num_sf = self.num_sf
1503
1514
  rvalue.min_exp = self.min_exp
1515
+ rvalue.max_dec = self.max_dec
1504
1516
  rvalue.extra_sf_lim = self.extra_sf_lim
1505
1517
  else:
1506
1518
  vars_str = ','.join(variables)
@@ -2012,7 +2024,7 @@ class RichArray(np.ndarray):
2012
2024
  abbreviations = {'is integer': 'is_int',
2013
2025
  'number of significant figures': 'num_sf',
2014
2026
  'minimum exponent for scientific notation': 'min_exp',
2015
- 'maximum number of decimals': 'max_dec',
2027
+ 'maximum number of decimals to use parenthesis': 'max_dec',
2016
2028
  'limit for extra significant figure': 'extra_sf_lim'}
2017
2029
  attributes = ['domain'] + list(abbreviations.values())
2018
2030
  for entry in abbreviations:
@@ -2250,7 +2262,7 @@ class RichDataFrame(pd.DataFrame):
2250
2262
  abbreviations = {'is integer': 'is_int',
2251
2263
  'number of significant figures': 'num_sf',
2252
2264
  'minimum exponent for scientific notation': 'min_exp',
2253
- 'maximum number of decimals': 'max_dec',
2265
+ 'maximum number of decimals to use parenthesis': 'max_dec',
2254
2266
  'limit for extra significant figure': 'extra_sf_lim'}
2255
2267
  attributes = ['domain'] + list(abbreviations.values())
2256
2268
  for entry in abbreviations:
@@ -2991,6 +3003,7 @@ def add_rich_values(x, y):
2991
3003
  """Sum two rich values to get a new one."""
2992
3004
  num_sf = min(x.num_sf, y.num_sf)
2993
3005
  min_exp = round(np.mean([x.min_exp, y.min_exp]))
3006
+ max_dec = round(np.mean([x.max_dec, y.max_dec]))
2994
3007
  extra_sf_lim = max(x.extra_sf_lim, y.extra_sf_lim)
2995
3008
  is_int = x.is_int and y.is_int
2996
3009
  domain = [x.domain[0] + y.domain[0], x.domain[1] + y.domain[1]]
@@ -3008,6 +3021,7 @@ def add_rich_values(x, y):
3008
3021
  is_vectorizable=True)
3009
3022
  z.num_sf = num_sf
3010
3023
  z.min_exp = min_exp
3024
+ z.max_dec = max_dec
3011
3025
  z.extra_sf_lim = extra_sf_lim
3012
3026
  return z
3013
3027
 
@@ -3015,6 +3029,7 @@ def multiply_rich_values(x, y):
3015
3029
  """Multiply two rich values to get a new one."""
3016
3030
  num_sf = min(x.num_sf, y.num_sf)
3017
3031
  min_exp = round(np.mean([x.min_exp, y.min_exp]))
3032
+ max_dec = round(np.mean([x.max_dec, y.max_dec]))
3018
3033
  extra_sf_lim = max(x.extra_sf_lim, y.extra_sf_lim)
3019
3034
  is_int = x.is_int and y.is_int
3020
3035
  domain = propagate_domain(x.domain, y.domain, lambda a,b: a*b)
@@ -3033,6 +3048,7 @@ def multiply_rich_values(x, y):
3033
3048
  is_vectorizable=True)
3034
3049
  z.num_sf = num_sf
3035
3050
  z.min_exp = min_exp
3051
+ z.max_dec = max_dec
3036
3052
  z.extra_sf_lim = extra_sf_lim
3037
3053
  return z
3038
3054
 
@@ -3040,6 +3056,7 @@ def divide_rich_values(x, y):
3040
3056
  """Divide two rich values to get a new one."""
3041
3057
  num_sf = min(x.num_sf, y.num_sf)
3042
3058
  min_exp = round(np.mean([x.min_exp, y.min_exp]))
3059
+ max_dec = round(np.mean([x.max_dec, y.max_dec]))
3043
3060
  extra_sf_lim = max(x.extra_sf_lim, y.extra_sf_lim)
3044
3061
  is_int = x.is_int and y.is_int
3045
3062
  domain = propagate_domain(x.domain, y.domain, lambda a,b: a/b)
@@ -3081,6 +3098,7 @@ def divide_rich_values(x, y):
3081
3098
  is_vectorizable=True, sigmas=sigmas)
3082
3099
  z.num_sf = num_sf
3083
3100
  z.min_exp = min_exp
3101
+ z.max_dec = max_dec
3084
3102
  z.extra_sf_lim = extra_sf_lim
3085
3103
  return z
3086
3104
 
@@ -3214,7 +3232,8 @@ def less_equiv(x, y, sigmas_interval=None, sigmas_overlap=None):
3214
3232
  return output
3215
3233
 
3216
3234
  def rich_value(text=None, domain=None, is_int=None, pdf=None,
3217
- consider_intervs=True, use_default_extra_sf_lim=False):
3235
+ consider_intervs=True, use_default_max_dec=False,
3236
+ use_default_extra_sf_lim=False):
3218
3237
  """
3219
3238
  Convert the input text to a rich value.
3220
3239
 
@@ -3236,6 +3255,10 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3236
3255
  If the PDF is given, this variable determines if we consider the
3237
3256
  possibility that the rich value is an upper/lower limit or a finite
3238
3257
  interval of values. By default, it is True.
3258
+ use_default_max_dec : bool, optional
3259
+ If True, the default maximum number of decimals to show uncertainties
3260
+ between parenthesis will be used instead of inferring it from the input
3261
+ text. This will reduce the computation time a little bit.
3239
3262
  use_default_extra_sf_lim : bool, optional
3240
3263
  If True, the default limit for extra significant figure will be used
3241
3264
  instead of inferring it from the input text. This will reduce the
@@ -3261,6 +3284,7 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3261
3284
 
3262
3285
  input_domain = copy.copy(domain)
3263
3286
  default_num_sf = defaultparams['number of significant figures']
3287
+ default_max_dec = defaultparams['maximum number of decimals to use parenthesis']
3264
3288
  default_extra_sf_lim = defaultparams['limit for extra significant figure']
3265
3289
  abbreviations = {'inf': 'np.inf', 'tau': 'math.tau', 'pi': 'np.pi',
3266
3290
  'nan': 'np.nan', 'NaN': 'np.nan', 'none': 'np.nan'}
@@ -3344,8 +3368,8 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3344
3368
  d = len(x.split('.')[1]) if '.' in x else 0
3345
3369
  d1 = len(dx1.split('.')[1]) if '.' in dx1 else 0
3346
3370
  d2 = len(dx1.split('.')[1]) if '.' in dx2 else 0
3347
- dx1 = '{:f}'.format(float(dx1)*10**(-(d-d1)))
3348
- dx2 = '{:f}'.format(float(dx2)*10**(-(d-d2)))
3371
+ dx1 = str(float(dx1)*10**(-(d-d1)))
3372
+ dx2 = str(float(dx2)*10**(-(d-d2)))
3349
3373
  elif '+/-' in text:
3350
3374
  x, dx = x_dx.split('+/-')
3351
3375
  text = '{}-{}+{} {}'.format(x, dx, dx, e)
@@ -3373,6 +3397,11 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3373
3397
  x = parse_value(x)
3374
3398
  dx1 = parse_value(dx1)
3375
3399
  dx2 = parse_value(dx2)
3400
+ if not use_default_max_dec:
3401
+ num_dec = len(x.split('.')[1].split('e')[0]) if '.' in x else 0
3402
+ max_dec = num_dec - 1
3403
+ else:
3404
+ max_dec = default_max_dec
3376
3405
  if not use_default_extra_sf_lim:
3377
3406
  if (not (is_lolim or is_uplim)
3378
3407
  and not (eval(dx1) == eval(dx2) == 0)):
@@ -3404,7 +3433,7 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3404
3433
  base = float('{:e}'.format(eval(val)).split('e')[0])
3405
3434
  if base <= default_extra_sf_lim:
3406
3435
  if num_sf < default_num_sf + 1:
3407
- extra_sf_lim = base - 1e-8
3436
+ extra_sf_lim = base - 1e-8
3408
3437
  else:
3409
3438
  extra_sf_lim = default_extra_sf_lim
3410
3439
  x = x.replace('e0','')
@@ -3416,27 +3445,29 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3416
3445
  else:
3417
3446
  text = text.replace(' --','--').replace('-- ','--')
3418
3447
  text1, text2 = text.split('--')
3419
- x1, _, _, _, _, _, me1, el1 = parse_as_rich_value(text1)
3420
- x2, _, _, _, _, _, me2, el2 = parse_as_rich_value(text2)
3448
+ x1, _, _, _, _, _, me1, md1, el1 = parse_as_rich_value(text1)
3449
+ x2, _, _, _, _, _, me2, md2, el2 = parse_as_rich_value(text2)
3421
3450
  main = [x1, x2]
3422
3451
  unc = 0
3423
3452
  is_lolim, is_uplim, is_range = False, False, True
3424
3453
  min_exp = round(np.mean([me1, me2]))
3454
+ max_dec = round(np.mean([md1, md2]))
3425
3455
  extra_sf_lim = max(el1, el2)
3426
3456
  return (main, unc, is_lolim, is_uplim, is_range, domain,
3427
- min_exp, extra_sf_lim)
3457
+ min_exp, max_dec, extra_sf_lim)
3428
3458
 
3429
3459
  if pdf is None:
3430
3460
  text = str(text)
3431
3461
  is_complex = 'j' in text
3432
3462
  if not is_complex:
3433
- (main, unc, is_lolim, is_uplim, is_range, domain,
3434
- min_exp, extra_sf_lim) = parse_as_rich_value(text)
3463
+ (main, unc, is_lolim, is_uplim, is_range, domain, min_exp,
3464
+ max_dec, extra_sf_lim) = parse_as_rich_value(text)
3435
3465
  if input_domain is not None:
3436
3466
  domain = input_domain
3437
3467
  rvalue = RichValue(main, unc, is_lolim, is_uplim,
3438
3468
  is_range, domain, is_int)
3439
3469
  rvalue.min_exp = min_exp
3470
+ rvalue.max_dec = max_dec
3440
3471
  rvalue.extra_sf_lim = extra_sf_lim
3441
3472
  else:
3442
3473
  if '+/-' in text or '-' in text[1:] and '+' in text[1:]:
@@ -3454,7 +3485,8 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3454
3485
  text_imag = text_imag[1:]
3455
3486
  if text_imag[-1] == ')':
3456
3487
  text_imag = text_imag[:-1]
3457
- args = (domain, is_int, pdf, use_default_extra_sf_lim)
3488
+ args = (domain, is_int, pdf,
3489
+ use_default_max_dec, use_default_extra_sf_lim)
3458
3490
  real = rich_value(text_real, *args)
3459
3491
  imag = rich_value(text_imag, *args)
3460
3492
  else:
@@ -4785,6 +4817,7 @@ def function_with_rich_values(function, args, unc_function=None,
4785
4817
  len_samples = int(len(args)**0.5 * defaultparams['size of samples'])
4786
4818
  num_sf = int(np.median([arg.num_sf for arg in args]))
4787
4819
  min_exp = round(np.mean([arg.min_exp for arg in args]))
4820
+ max_dec = round(np.mean([arg.max_dec for arg in args]))
4788
4821
  extra_sf_lim = max([arg.extra_sf_lim for arg in args])
4789
4822
 
4790
4823
  if consider_intervs is None:
@@ -4922,6 +4955,7 @@ def function_with_rich_values(function, args, unc_function=None,
4922
4955
  rval_k = ComplexRichValue(real_k, imag_k)
4923
4956
  rval_k.num_sf = num_sf
4924
4957
  rval_k.min_exp = min_exp
4958
+ rval_k.max_dec = max_dec
4925
4959
  rval_k.extra_sf_lim = extra_sf_lim
4926
4960
  output += [rval_k]
4927
4961
 
@@ -4949,6 +4983,7 @@ def function_with_rich_values(function, args, unc_function=None,
4949
4983
  num_reps_lims, save_pdf)
4950
4984
  rval_k.num_sf = num_sf
4951
4985
  rval_k.min_exp = min_exp
4986
+ rval_k.max_dec = max_dec
4952
4987
  rval_k.extra_sf_lim = extra_sf_lim
4953
4988
  if type(input_function) is str:
4954
4989
  if type(rval_k) is RichValue:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: richvalues
3
- Version: 4.2.5
3
+ Version: 4.2.7
4
4
  Summary: Python library for working with uncertainties and upper/lower limits
5
5
  Home-page: https://github.com/andresmegias/richvalues/
6
6
  Author: Andrés Megías Toledano
@@ -5,7 +5,7 @@ with open('README.md', 'r') as file:
5
5
 
6
6
  setuptools.setup(
7
7
  name = 'richvalues',
8
- version = '4.2.5',
8
+ version = '4.2.7',
9
9
  license = 'BSD-3-Clause',
10
10
  author = 'Andrés Megías Toledano',
11
11
  description = 'Python library for working with uncertainties and upper/lower limits',
File without changes
File without changes
File without changes