Formatter for subtypes of np.floating
| 983 | return x |
| 984 | |
| 985 | class FloatingFormat: |
| 986 | """ Formatter for subtypes of np.floating """ |
| 987 | def __init__(self, data, precision, floatmode, suppress_small, sign=False, |
| 988 | *, legacy=None): |
| 989 | # for backcompatibility, accept bools |
| 990 | if isinstance(sign, bool): |
| 991 | sign = '+' if sign else '-' |
| 992 | |
| 993 | self._legacy = legacy |
| 994 | if self._legacy <= 113: |
| 995 | # when not 0d, legacy does not support '-' |
| 996 | if data.shape != () and sign == '-': |
| 997 | sign = ' ' |
| 998 | |
| 999 | self.floatmode = floatmode |
| 1000 | if floatmode == 'unique': |
| 1001 | self.precision = None |
| 1002 | else: |
| 1003 | self.precision = precision |
| 1004 | |
| 1005 | self.precision = _none_or_positive_arg(self.precision, 'precision') |
| 1006 | |
| 1007 | self.suppress_small = suppress_small |
| 1008 | self.sign = sign |
| 1009 | self.exp_format = False |
| 1010 | self.large_exponent = False |
| 1011 | self.fillFormat(data) |
| 1012 | |
| 1013 | def fillFormat(self, data): |
| 1014 | # only the finite values are used to compute the number of digits |
| 1015 | finite_vals = data[isfinite(data)] |
| 1016 | |
| 1017 | # choose exponential mode based on the non-zero finite values: |
| 1018 | abs_non_zero = absolute(finite_vals[finite_vals != 0]) |
| 1019 | if len(abs_non_zero) != 0: |
| 1020 | max_val = np.max(abs_non_zero) |
| 1021 | min_val = np.min(abs_non_zero) |
| 1022 | if self._legacy <= 202: |
| 1023 | exp_cutoff_max = 1.e8 |
| 1024 | else: |
| 1025 | # consider data type while deciding the max cutoff for exp format |
| 1026 | exp_cutoff_max = 10.**min(8, np.finfo(data.dtype).precision) |
| 1027 | with errstate(over='ignore'): # division can overflow |
| 1028 | if max_val >= exp_cutoff_max or (not self.suppress_small and |
| 1029 | (min_val < 0.0001 or max_val / min_val > 1000.)): |
| 1030 | self.exp_format = True |
| 1031 | |
| 1032 | # do a first pass of printing all the numbers, to determine sizes |
| 1033 | if len(finite_vals) == 0: |
| 1034 | self.pad_left = 0 |
| 1035 | self.pad_right = 0 |
| 1036 | self.trim = '.' |
| 1037 | self.exp_size = -1 |
| 1038 | self.unique = True |
| 1039 | self.min_digits = None |
| 1040 | elif self.exp_format: |
| 1041 | trim, unique = '.', True |
| 1042 | if self.floatmode == 'fixed' or self._legacy <= 113: |
no outgoing calls
no test coverage detected
searching dependent graphs…