|
5 | 5 |
|
6 | 6 | __all__ = ['slicer', 'keys', 'argmax_nd', 'argmin_nd', 'indexed', 'argclip', 'amap', |
7 | 7 | 'atleast_nd', 'assign_first', 'assign_last', 'dyadic', 'altcumsum', 'altcumprod', |
8 | | - 'extend_linspace', 'extend_geomspace', 'round_sigfig', 'unique_sigfig', |
| 8 | + 'extend_linspace', 'extend_geomspace', 'round_signif', 'almost_unique', |
9 | 9 | 'siground', 'DictToClass', 'DefaultDictToClass'] |
10 | 10 |
|
11 | 11 |
|
@@ -335,40 +335,50 @@ def extend_geomspace(x, min=None, max=None): |
335 | 335 | return np.exp(extend_linspace(np.log(x), ln_min, ln_max)) |
336 | 336 |
|
337 | 337 |
|
338 | | -def round_sigfig(x, n=6): |
| 338 | +def round_signif(x, decimals): |
339 | 339 | """ |
340 | | - Round array for given significant figures. |
341 | | - Added: 2022-10-19. |
| 340 | + Round to the given number of significant figures. |
| 341 | + ref: Scott Gigante, https://stackoverflow.com/a/59888924/2144720 |
342 | 342 |
|
343 | | - x: |
344 | | - Input number or array. |
345 | | - n: |
346 | | - Number of digit |
| 343 | + Added: 2022-10-19, Updated: 2022-10-20 |
| 344 | +
|
| 345 | + x : array_like |
| 346 | + Input data. |
| 347 | + decimals : int, optional |
| 348 | + Number of decimal places to round to. |
347 | 349 | """ |
348 | | - xarr = np.asfarray(x) |
349 | | - str = np.array2string(xarr, separator=',', formatter={'float_kind': lambda x: f"{x:.{n}e}"}) |
350 | | - xnew = eval(f"np.array({str}, dtype=xarr.dtype)") |
351 | | - if np.isscalar(x): |
352 | | - return xnew.item() |
353 | | - else: |
354 | | - return xnew |
| 350 | + x = np.asfarray(x) |
| 351 | + x_pos = np.where(np.isfinite(x) & (x != 0), np.abs(x), 10**(decimals)) |
| 352 | + mags = 10**(decimals - np.floor(np.log10(x_pos))) |
| 353 | + return np.around(x * mags) / mags |
355 | 354 |
|
| 355 | + # obsolete: |
| 356 | + # x = np.asfarray(x) |
| 357 | + # str = np.array2string(x, separator=',', formatter={'float_kind': lambda x: f"{x:.{decimals}e}"}) |
| 358 | + # return eval(f"np.array({str}, dtype=x.dtype)") |
356 | 359 |
|
357 | | -def unique_sigfig(x, n=6, **kwargs): |
| 360 | + |
| 361 | +def almost_unique(x, nrel=10, nabs=None, **kwargs): |
358 | 362 | """ |
359 | | - Find the unique elements of an array for given significant figures. |
| 363 | + Find the unique elements of an array for given precision. |
360 | 364 | Added: 2022-10-19. |
361 | 365 |
|
362 | 366 | x: |
363 | 367 | Input number or array. |
364 | | - n: |
365 | | - Number of digit |
| 368 | + nrel: |
| 369 | + Number of decimals in scientific notation (significant figures - 1). |
| 370 | + nabs: |
| 371 | + Number of absolute decimals. |
366 | 372 | kwargs: |
367 | 373 | np.unique arguments, including 'return_index', 'return_inverse', |
368 | 374 | 'return_counts', 'axis' |
369 | 375 | """ |
370 | | - xnew = round_sigfig(x, n=n) |
371 | | - return np.unique(xnew, **kwargs) |
| 376 | + if nrel is not None: |
| 377 | + x = round_signif(x, decimals=nrel) |
| 378 | + if nabs is not None: |
| 379 | + x = np.around(x, decimals=nabs) |
| 380 | + |
| 381 | + return np.unique(x, **kwargs) |
372 | 382 |
|
373 | 383 |
|
374 | 384 | def siground(x, n): |
|
0 commit comments