Formatter for subtypes of np.floating
| 903 | return x |
| 904 | |
| 905 | class FloatingFormat: |
| 906 | """ Formatter for subtypes of np.floating """ |
| 907 | def __init__(self, data, precision, floatmode, suppress_small, sign=False, |
| 908 | *, legacy=None): |
| 909 | # for backcompatibility, accept bools |
| 910 | if isinstance(sign, bool): |
| 911 | sign = '+' if sign else '-' |
| 912 | |
| 913 | self._legacy = legacy |
| 914 | if self._legacy <= 113: |
| 915 | # when not 0d, legacy does not support '-' |
| 916 | if data.shape != () and sign == '-': |
| 917 | sign = ' ' |
| 918 | |
| 919 | self.floatmode = floatmode |
| 920 | if floatmode == 'unique': |
| 921 | self.precision = None |
| 922 | else: |
| 923 | self.precision = precision |
| 924 | |
| 925 | self.precision = _none_or_positive_arg(self.precision, 'precision') |
| 926 | |
| 927 | self.suppress_small = suppress_small |
| 928 | self.sign = sign |
| 929 | self.exp_format = False |
| 930 | self.large_exponent = False |
| 931 | |
| 932 | self.fillFormat(data) |
| 933 | |
| 934 | def fillFormat(self, data): |
| 935 | # only the finite values are used to compute the number of digits |
| 936 | finite_vals = data[isfinite(data)] |
| 937 | |
| 938 | # choose exponential mode based on the non-zero finite values: |
| 939 | abs_non_zero = absolute(finite_vals[finite_vals != 0]) |
| 940 | if len(abs_non_zero) != 0: |
| 941 | max_val = np.max(abs_non_zero) |
| 942 | min_val = np.min(abs_non_zero) |
| 943 | with errstate(over='ignore'): # division can overflow |
| 944 | if max_val >= 1.e8 or (not self.suppress_small and |
| 945 | (min_val < 0.0001 or max_val/min_val > 1000.)): |
| 946 | self.exp_format = True |
| 947 | |
| 948 | # do a first pass of printing all the numbers, to determine sizes |
| 949 | if len(finite_vals) == 0: |
| 950 | self.pad_left = 0 |
| 951 | self.pad_right = 0 |
| 952 | self.trim = '.' |
| 953 | self.exp_size = -1 |
| 954 | self.unique = True |
| 955 | self.min_digits = None |
| 956 | elif self.exp_format: |
| 957 | trim, unique = '.', True |
| 958 | if self.floatmode == 'fixed' or self._legacy <= 113: |
| 959 | trim, unique = 'k', False |
| 960 | strs = (dragon4_scientific(x, precision=self.precision, |
| 961 | unique=unique, trim=trim, sign=self.sign == '+') |
| 962 | for x in finite_vals) |
no outgoing calls
no test coverage detected