def __call__(self, x, strip_zeros=True): import numeric as _nc err = _nc.seterr(invalid='ignore') try: if isnan(x): if self.sign: return self.special_fmt % ('+' + _nan_str,) else: return self.special_fmt % (_nan_str,) elif isinf(x): if x > 0: if self.sign: return self.special_fmt % ('+' + _inf_str,) else: return self.special_fmt % (_inf_str,) else: return self.special_fmt % ('-' + _inf_str,) finally: _nc.seterr(**err) s = self.format % x if self.large_exponent: # 3-digit exponent expsign = s[-3] if expsign == '+' or expsign == '-': s = s[1:-2] + '0' + s[-2:] elif self.exp_format: # 2-digit exponent if s[-3] == '0': s = ' ' + s[:-3] + s[-2:] elif strip_zeros: z = s.rstrip('0') s = z + ' '*(len(s)-len(z)) return s
def fillFormat(self, data): import numeric as _nc # XXX pypy unimplemented #errstate = _nc.seterr(all='ignore') try: special = isnan(data) | isinf(data) | isna(data) special[isna(data)] = False valid = not_equal(data, 0) & ~special valid[isna(data)] = False non_zero = absolute(data.compress(valid)) if len(non_zero) == 0: max_val = 0. min_val = 0. else: max_val = maximum.reduce(non_zero, skipna=True) min_val = minimum.reduce(non_zero, skipna=True) if max_val >= 1.e8: self.exp_format = True if not self.suppress_small and (min_val < 0.0001 or max_val/min_val > 1000.): self.exp_format = True finally: pass # XXX pypy unimplemented #_nc.seterr(**errstate) if self.exp_format: self.large_exponent = 0 < min_val < 1e-99 or max_val >= 1e100 self.max_str_len = 8 + self.precision if self.large_exponent: self.max_str_len += 1 if self.sign: format = '%+' else: format = '%' format = format + '%d.%de' % (self.max_str_len, self.precision) else: format = '%%.%df' % (self.precision,) if len(non_zero): precision = max([_digits(x, self.precision, format) for x in non_zero]) else: precision = 0 precision = min(self.precision, precision) self.max_str_len = len(str(int(max_val))) + precision + 2 if special.any(): self.max_str_len = max(self.max_str_len, len(_nan_str), len(_inf_str)+1, len(_na_str)) if self.sign: format = '%#+' else: format = '%#' format = format + '%d.%df' % (self.max_str_len, precision) self.special_fmt = '%%%ds' % (self.max_str_len,) self.format = format
def fillFormat(self, data): import numeric as _nc # XXX pypy unimplemented #errstate = _nc.seterr(all='ignore') try: special = isnan(data) | isinf(data) | isna(data) special[isna(data)] = False valid = not_equal(data, 0) & ~special valid[isna(data)] = False non_zero = absolute(data.compress(valid)) if len(non_zero) == 0: max_val = 0. min_val = 0. else: max_val = maximum.reduce(non_zero, skipna=True) min_val = minimum.reduce(non_zero, skipna=True) if max_val >= 1.e8: self.exp_format = True if not self.suppress_small and (min_val < 0.0001 or max_val / min_val > 1000.): self.exp_format = True finally: pass # XXX pypy unimplemented #_nc.seterr(**errstate) if self.exp_format: self.large_exponent = 0 < min_val < 1e-99 or max_val >= 1e100 self.max_str_len = 8 + self.precision if self.large_exponent: self.max_str_len += 1 if self.sign: format = '%+' else: format = '%' format = format + '%d.%de' % (self.max_str_len, self.precision) else: format = '%%.%df' % (self.precision, ) if len(non_zero): precision = max( [_digits(x, self.precision, format) for x in non_zero]) else: precision = 0 precision = min(self.precision, precision) self.max_str_len = len(str(int(max_val))) + precision + 2 if special.any(): self.max_str_len = max(self.max_str_len, len(_nan_str), len(_inf_str) + 1, len(_na_str)) if self.sign: format = '%#+' else: format = '%#' format = format + '%d.%df' % (self.max_str_len, precision) self.special_fmt = '%%%ds' % (self.max_str_len, ) self.format = format
def test_isnan_isinf(self): from _numpypy import isnan, isinf, float64, array assert isnan(float("nan")) assert isnan(float64(float("nan"))) assert not isnan(3) assert isinf(float("inf")) assert not isnan(3.5) assert not isinf(3.5) assert not isnan(float("inf")) assert not isinf(float("nan")) assert (isnan(array([0.2, float("inf"), float("nan")])) == [False, False, True]).all() assert (isinf(array([0.2, float("inf"), float("nan")])) == [False, True, False]).all() assert isinf(array([0.2])).dtype.kind == "b"
def __call__(self, x): if isnan(x): if self.sign: return '+' + _nan_str else: return ' ' + _nan_str elif isinf(x): if x > 0: if self.sign: return '+' + _inf_str else: return ' ' + _inf_str else: return '-' + _inf_str elif x >= 0: if self.sign: return '+' + format_longfloat(x, self.precision) else: return ' ' + format_longfloat(x, self.precision) else: return format_longfloat(x, self.precision)
def __call__(self, x): if isna(x): return str(x).replace('NA', _na_str, 1) elif isnan(x): if self.sign: return '+' + _nan_str else: return ' ' + _nan_str elif isinf(x): if x > 0: if self.sign: return '+' + _inf_str else: return ' ' + _inf_str else: return '-' + _inf_str elif x >= 0: if self.sign: return '+' + format_longfloat(x, self.precision) else: return ' ' + format_longfloat(x, self.precision) else: return format_longfloat(x, self.precision)
def __call__(self, x, strip_zeros=True): import numeric as _nc #err = _nc.seterr(invalid='ignore') try: if isna(x): return self.special_fmt % (str(x).replace('NA', _na_str, 1), ) elif isnan(x): if self.sign: return self.special_fmt % ('+' + _nan_str, ) else: return self.special_fmt % (_nan_str, ) elif isinf(x): if x > 0: if self.sign: return self.special_fmt % ('+' + _inf_str, ) else: return self.special_fmt % (_inf_str, ) else: return self.special_fmt % ('-' + _inf_str, ) finally: pass #_nc.seterr(**err) s = self.format % x if self.large_exponent: # 3-digit exponent expsign = s[-3] if expsign == '+' or expsign == '-': s = s[1:-2] + '0' + s[-2:] elif self.exp_format: # 2-digit exponent if s[-3] == '0': s = ' ' + s[:-3] + s[-2:] elif strip_zeros: z = s.rstrip('0') s = z + ' ' * (len(s) - len(z)) return s