richvalues 4.2.5__tar.gz → 4.2.6__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.6
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.6"
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.6'
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,25 @@ 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, exp_dy = dy_.split('e')
319
+ exp_y_ = 0 if exp_y is None else exp_y
320
+ if int(exp_y_) > int(exp_dy):
321
+ dy = round_sf(float(dy)*10**int(exp_dy), n, np.inf, extra_sf_lim)
322
+ if '.' in dy:
323
+ dy = dy.split('.')[-1]
324
+ dy = '(' + dy + ')'
325
+ else:
326
+ dy = ''
327
+ if exp_y is not None:
328
+ y = '{}e{}'.format(y, exp_y)
323
329
  return y, dy
324
330
 
325
331
  def round_sf_uncs(x, dx, n=None, min_exp=None, max_dec=None, extra_sf_lim=None):
@@ -354,7 +360,7 @@ def round_sf_uncs(x, dx, n=None, min_exp=None, max_dec=None, extra_sf_lim=None):
354
360
  """
355
361
  n = set_default_value(n, 'number of significant figures')
356
362
  min_exp = set_default_value(min_exp, 'minimum exponent for scientific notation')
357
- max_dec = set_default_value(max_dec, 'maximum number of decimals')
363
+ max_dec = set_default_value(max_dec, 'maximum number of decimals to use parenthesis')
358
364
  extra_sf_lim = set_default_value(extra_sf_lim, 'limit for extra significant figure')
359
365
  dx1, dx2 = dx
360
366
  y1, dy1 = round_sf_unc(x, dx1, n, min_exp, max_dec, extra_sf_lim)
@@ -583,7 +589,7 @@ class RichValue():
583
589
  self.domain = domain
584
590
  self.num_sf = defaultparams['number of significant figures']
585
591
  self.min_exp = defaultparams['minimum exponent for scientific notation']
586
- self.max_dec = defaultparams['maximum number of decimals']
592
+ self.max_dec = defaultparams['maximum number of decimals to use parenthesis']
587
593
  self.extra_sf_lim = defaultparams['limit for extra significant figure']
588
594
  self.pdf_info = 'default'
589
595
  self.variables = variables
@@ -1218,6 +1224,7 @@ class RichValue():
1218
1224
  rvalue = RichValue(x, dx, domain=domain, is_int=self.is_int)
1219
1225
  rvalue.num_sf = self.num_sf
1220
1226
  rvalue.min_exp = self.min_exp
1227
+ rvalue.max_dec = self.max_dec
1221
1228
  rvalue.extra_sf_lim = self.extra_sf_lim
1222
1229
  rvalue.variables = self.variables
1223
1230
  expression = self.expression
@@ -1256,6 +1263,7 @@ class RichValue():
1256
1263
  rvalue.domain[0] = max(0, rvalue.domain[0])
1257
1264
  rvalue.num_sf = self.num_sf
1258
1265
  rvalue.min_exp = self.min_exp
1266
+ rvalue.max_dec = self.max_dec
1259
1267
  rvalue.extra_sf_lim = self.extra_sf_lim
1260
1268
  rvalue.variables = self.variables
1261
1269
  rvalue.expression = 'abs({})'.format(self.expression)
@@ -1501,6 +1509,7 @@ class RichValue():
1501
1509
  rvalue = RichValue(np.nan)
1502
1510
  rvalue.num_sf = self.num_sf
1503
1511
  rvalue.min_exp = self.min_exp
1512
+ rvalue.max_dec = self.max_dec
1504
1513
  rvalue.extra_sf_lim = self.extra_sf_lim
1505
1514
  else:
1506
1515
  vars_str = ','.join(variables)
@@ -2012,7 +2021,7 @@ class RichArray(np.ndarray):
2012
2021
  abbreviations = {'is integer': 'is_int',
2013
2022
  'number of significant figures': 'num_sf',
2014
2023
  'minimum exponent for scientific notation': 'min_exp',
2015
- 'maximum number of decimals': 'max_dec',
2024
+ 'maximum number of decimals to use parenthesis': 'max_dec',
2016
2025
  'limit for extra significant figure': 'extra_sf_lim'}
2017
2026
  attributes = ['domain'] + list(abbreviations.values())
2018
2027
  for entry in abbreviations:
@@ -2250,7 +2259,7 @@ class RichDataFrame(pd.DataFrame):
2250
2259
  abbreviations = {'is integer': 'is_int',
2251
2260
  'number of significant figures': 'num_sf',
2252
2261
  'minimum exponent for scientific notation': 'min_exp',
2253
- 'maximum number of decimals': 'max_dec',
2262
+ 'maximum number of decimals to use parenthesis': 'max_dec',
2254
2263
  'limit for extra significant figure': 'extra_sf_lim'}
2255
2264
  attributes = ['domain'] + list(abbreviations.values())
2256
2265
  for entry in abbreviations:
@@ -2991,6 +3000,7 @@ def add_rich_values(x, y):
2991
3000
  """Sum two rich values to get a new one."""
2992
3001
  num_sf = min(x.num_sf, y.num_sf)
2993
3002
  min_exp = round(np.mean([x.min_exp, y.min_exp]))
3003
+ max_dec = round(np.mean([x.max_dec, y.max_dec]))
2994
3004
  extra_sf_lim = max(x.extra_sf_lim, y.extra_sf_lim)
2995
3005
  is_int = x.is_int and y.is_int
2996
3006
  domain = [x.domain[0] + y.domain[0], x.domain[1] + y.domain[1]]
@@ -3008,6 +3018,7 @@ def add_rich_values(x, y):
3008
3018
  is_vectorizable=True)
3009
3019
  z.num_sf = num_sf
3010
3020
  z.min_exp = min_exp
3021
+ z.max_dec = max_dec
3011
3022
  z.extra_sf_lim = extra_sf_lim
3012
3023
  return z
3013
3024
 
@@ -3015,6 +3026,7 @@ def multiply_rich_values(x, y):
3015
3026
  """Multiply two rich values to get a new one."""
3016
3027
  num_sf = min(x.num_sf, y.num_sf)
3017
3028
  min_exp = round(np.mean([x.min_exp, y.min_exp]))
3029
+ max_dec = round(np.mean([x.max_dec, y.max_dec]))
3018
3030
  extra_sf_lim = max(x.extra_sf_lim, y.extra_sf_lim)
3019
3031
  is_int = x.is_int and y.is_int
3020
3032
  domain = propagate_domain(x.domain, y.domain, lambda a,b: a*b)
@@ -3033,6 +3045,7 @@ def multiply_rich_values(x, y):
3033
3045
  is_vectorizable=True)
3034
3046
  z.num_sf = num_sf
3035
3047
  z.min_exp = min_exp
3048
+ z.max_dec = max_dec
3036
3049
  z.extra_sf_lim = extra_sf_lim
3037
3050
  return z
3038
3051
 
@@ -3040,6 +3053,7 @@ def divide_rich_values(x, y):
3040
3053
  """Divide two rich values to get a new one."""
3041
3054
  num_sf = min(x.num_sf, y.num_sf)
3042
3055
  min_exp = round(np.mean([x.min_exp, y.min_exp]))
3056
+ max_dec = round(np.mean([x.max_dec, y.max_dec]))
3043
3057
  extra_sf_lim = max(x.extra_sf_lim, y.extra_sf_lim)
3044
3058
  is_int = x.is_int and y.is_int
3045
3059
  domain = propagate_domain(x.domain, y.domain, lambda a,b: a/b)
@@ -3081,6 +3095,7 @@ def divide_rich_values(x, y):
3081
3095
  is_vectorizable=True, sigmas=sigmas)
3082
3096
  z.num_sf = num_sf
3083
3097
  z.min_exp = min_exp
3098
+ z.max_dec = max_dec
3084
3099
  z.extra_sf_lim = extra_sf_lim
3085
3100
  return z
3086
3101
 
@@ -3214,7 +3229,8 @@ def less_equiv(x, y, sigmas_interval=None, sigmas_overlap=None):
3214
3229
  return output
3215
3230
 
3216
3231
  def rich_value(text=None, domain=None, is_int=None, pdf=None,
3217
- consider_intervs=True, use_default_extra_sf_lim=False):
3232
+ consider_intervs=True, use_default_max_dec=False,
3233
+ use_default_extra_sf_lim=False):
3218
3234
  """
3219
3235
  Convert the input text to a rich value.
3220
3236
 
@@ -3236,6 +3252,10 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3236
3252
  If the PDF is given, this variable determines if we consider the
3237
3253
  possibility that the rich value is an upper/lower limit or a finite
3238
3254
  interval of values. By default, it is True.
3255
+ use_default_max_dec : bool, optional
3256
+ If True, the default maximum number of decimals to show uncertainties
3257
+ between parenthesis will be used instead of inferring it from the input
3258
+ text. This will reduce the computation time a little bit.
3239
3259
  use_default_extra_sf_lim : bool, optional
3240
3260
  If True, the default limit for extra significant figure will be used
3241
3261
  instead of inferring it from the input text. This will reduce the
@@ -3261,6 +3281,7 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3261
3281
 
3262
3282
  input_domain = copy.copy(domain)
3263
3283
  default_num_sf = defaultparams['number of significant figures']
3284
+ default_max_dec = defaultparams['maximum number of decimals to use parenthesis']
3264
3285
  default_extra_sf_lim = defaultparams['limit for extra significant figure']
3265
3286
  abbreviations = {'inf': 'np.inf', 'tau': 'math.tau', 'pi': 'np.pi',
3266
3287
  'nan': 'np.nan', 'NaN': 'np.nan', 'none': 'np.nan'}
@@ -3344,8 +3365,8 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3344
3365
  d = len(x.split('.')[1]) if '.' in x else 0
3345
3366
  d1 = len(dx1.split('.')[1]) if '.' in dx1 else 0
3346
3367
  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)))
3368
+ dx1 = str(float(dx1)*10**(-(d-d1)))
3369
+ dx2 = str(float(dx2)*10**(-(d-d2)))
3349
3370
  elif '+/-' in text:
3350
3371
  x, dx = x_dx.split('+/-')
3351
3372
  text = '{}-{}+{} {}'.format(x, dx, dx, e)
@@ -3373,6 +3394,11 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3373
3394
  x = parse_value(x)
3374
3395
  dx1 = parse_value(dx1)
3375
3396
  dx2 = parse_value(dx2)
3397
+ if not use_default_max_dec:
3398
+ num_dec = len(x.split('.')[1].split('e')[0]) if '.' in x else 0
3399
+ max_dec = num_dec - 1
3400
+ else:
3401
+ max_dec = default_max_dec
3376
3402
  if not use_default_extra_sf_lim:
3377
3403
  if (not (is_lolim or is_uplim)
3378
3404
  and not (eval(dx1) == eval(dx2) == 0)):
@@ -3404,7 +3430,7 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3404
3430
  base = float('{:e}'.format(eval(val)).split('e')[0])
3405
3431
  if base <= default_extra_sf_lim:
3406
3432
  if num_sf < default_num_sf + 1:
3407
- extra_sf_lim = base - 1e-8
3433
+ extra_sf_lim = base - 1e-8
3408
3434
  else:
3409
3435
  extra_sf_lim = default_extra_sf_lim
3410
3436
  x = x.replace('e0','')
@@ -3416,27 +3442,29 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3416
3442
  else:
3417
3443
  text = text.replace(' --','--').replace('-- ','--')
3418
3444
  text1, text2 = text.split('--')
3419
- x1, _, _, _, _, _, me1, el1 = parse_as_rich_value(text1)
3420
- x2, _, _, _, _, _, me2, el2 = parse_as_rich_value(text2)
3445
+ x1, _, _, _, _, _, me1, md1, el1 = parse_as_rich_value(text1)
3446
+ x2, _, _, _, _, _, me2, md2, el2 = parse_as_rich_value(text2)
3421
3447
  main = [x1, x2]
3422
3448
  unc = 0
3423
3449
  is_lolim, is_uplim, is_range = False, False, True
3424
3450
  min_exp = round(np.mean([me1, me2]))
3451
+ max_dec = round(np.mean([md1, md2]))
3425
3452
  extra_sf_lim = max(el1, el2)
3426
3453
  return (main, unc, is_lolim, is_uplim, is_range, domain,
3427
- min_exp, extra_sf_lim)
3454
+ min_exp, max_dec, extra_sf_lim)
3428
3455
 
3429
3456
  if pdf is None:
3430
3457
  text = str(text)
3431
3458
  is_complex = 'j' in text
3432
3459
  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)
3460
+ (main, unc, is_lolim, is_uplim, is_range, domain, min_exp,
3461
+ max_dec, extra_sf_lim) = parse_as_rich_value(text)
3435
3462
  if input_domain is not None:
3436
3463
  domain = input_domain
3437
3464
  rvalue = RichValue(main, unc, is_lolim, is_uplim,
3438
3465
  is_range, domain, is_int)
3439
3466
  rvalue.min_exp = min_exp
3467
+ rvalue.max_dec = max_dec
3440
3468
  rvalue.extra_sf_lim = extra_sf_lim
3441
3469
  else:
3442
3470
  if '+/-' in text or '-' in text[1:] and '+' in text[1:]:
@@ -3454,7 +3482,8 @@ def rich_value(text=None, domain=None, is_int=None, pdf=None,
3454
3482
  text_imag = text_imag[1:]
3455
3483
  if text_imag[-1] == ')':
3456
3484
  text_imag = text_imag[:-1]
3457
- args = (domain, is_int, pdf, use_default_extra_sf_lim)
3485
+ args = (domain, is_int, pdf,
3486
+ use_default_max_dec, use_default_extra_sf_lim)
3458
3487
  real = rich_value(text_real, *args)
3459
3488
  imag = rich_value(text_imag, *args)
3460
3489
  else:
@@ -4785,6 +4814,7 @@ def function_with_rich_values(function, args, unc_function=None,
4785
4814
  len_samples = int(len(args)**0.5 * defaultparams['size of samples'])
4786
4815
  num_sf = int(np.median([arg.num_sf for arg in args]))
4787
4816
  min_exp = round(np.mean([arg.min_exp for arg in args]))
4817
+ max_dec = round(np.mean([arg.max_dec for arg in args]))
4788
4818
  extra_sf_lim = max([arg.extra_sf_lim for arg in args])
4789
4819
 
4790
4820
  if consider_intervs is None:
@@ -4922,6 +4952,7 @@ def function_with_rich_values(function, args, unc_function=None,
4922
4952
  rval_k = ComplexRichValue(real_k, imag_k)
4923
4953
  rval_k.num_sf = num_sf
4924
4954
  rval_k.min_exp = min_exp
4955
+ rval_k.max_dec = max_dec
4925
4956
  rval_k.extra_sf_lim = extra_sf_lim
4926
4957
  output += [rval_k]
4927
4958
 
@@ -4949,6 +4980,7 @@ def function_with_rich_values(function, args, unc_function=None,
4949
4980
  num_reps_lims, save_pdf)
4950
4981
  rval_k.num_sf = num_sf
4951
4982
  rval_k.min_exp = min_exp
4983
+ rval_k.max_dec = max_dec
4952
4984
  rval_k.extra_sf_lim = extra_sf_lim
4953
4985
  if type(input_function) is str:
4954
4986
  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.6
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.6',
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