[pypy-commit] pypy numpy-refactor: reimport arrayprint.py, probably broken for now
fijal
noreply at buildbot.pypy.org
Wed Sep 5 22:19:14 CEST 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-refactor
Changeset: r57165:cb2dcc139f6a
Date: 2012-09-05 22:19 +0200
http://bitbucket.org/pypy/pypy/changeset/cb2dcc139f6a/
Log: reimport arrayprint.py, probably broken for now
diff --git a/lib_pypy/numpypy/core/arrayprint.py b/lib_pypy/numpypy/core/arrayprint.py
--- a/lib_pypy/numpypy/core/arrayprint.py
+++ b/lib_pypy/numpypy/core/arrayprint.py
@@ -14,9 +14,9 @@
import sys
import _numpypy as _nt
-from _numpypy import maximum, minimum, absolute, not_equal, isinf, isnan, isna
+from _numpypy import maximum, minimum, absolute, not_equal, isnan, isinf
#from _numpypy import format_longfloat, datetime_as_string, datetime_data
-from .fromnumeric import ravel
+from fromnumeric import ravel
def product(x, y): return x*y
@@ -29,7 +29,6 @@
_line_width = 75
_nan_str = 'nan'
_inf_str = 'inf'
-_na_str = 'NA'
_formatter = None # formatting function for array elements
if sys.version_info[0] >= 3:
@@ -37,7 +36,7 @@
def set_printoptions(precision=None, threshold=None, edgeitems=None,
linewidth=None, suppress=None,
- nanstr=None, infstr=None, nastr=None,
+ nanstr=None, infstr=None,
formatter=None):
"""
Set printing options.
@@ -65,8 +64,6 @@
String representation of floating point not-a-number (default nan).
infstr : str, optional
String representation of floating point infinity (default inf).
- nastr : str, optional
- String representation of NA missing value (default NA).
formatter : dict of callables, optional
If not None, the keys should indicate the type(s) that the respective
formatting function applies to. Callables should return a string.
@@ -144,7 +141,7 @@
global _summaryThreshold, _summaryEdgeItems, _float_output_precision, \
_line_width, _float_output_suppress_small, _nan_str, _inf_str, \
- _na_str, _formatter
+ _formatter
if linewidth is not None:
_line_width = linewidth
if threshold is not None:
@@ -159,8 +156,6 @@
_nan_str = nanstr
if infstr is not None:
_inf_str = infstr
- if nastr is not None:
- _na_str = nastr
_formatter = formatter
def get_printoptions():
@@ -195,7 +190,6 @@
suppress=_float_output_suppress_small,
nanstr=_nan_str,
infstr=_inf_str,
- nastr=_na_str,
formatter=_formatter)
return d
@@ -219,19 +213,14 @@
return b
def _boolFormatter(x):
- if isna(x):
- return str(x).replace('NA', _na_str, 1)
- elif x:
+ if x:
return ' True'
else:
return 'False'
def repr_format(x):
- if isna(x):
- return str(x).replace('NA', _na_str, 1)
- else:
- return repr(x)
+ return repr(x)
def _array2string(a, max_line_width, precision, suppress_small, separator=' ',
prefix="", formatter=None):
@@ -259,11 +248,11 @@
'int' : IntegerFormat(data),
'float' : FloatFormat(data, precision, suppress_small),
'longfloat' : LongFloatFormat(precision),
- #'complexfloat' : ComplexFormat(data, precision,
- # suppress_small),
- #'longcomplexfloat' : LongComplexFormat(precision),
- #'datetime' : DatetimeFormat(data),
- #'timedelta' : TimedeltaFormat(data),
+ 'complexfloat' : ComplexFormat(data, precision,
+ suppress_small),
+ 'longcomplexfloat' : LongComplexFormat(precision),
+ 'datetime' : DatetimeFormat(data),
+ 'timedelta' : TimedeltaFormat(data),
'numpystr' : repr_format,
'str' : str}
@@ -300,15 +289,15 @@
if issubclass(dtypeobj, _nt.bool_):
format_function = formatdict['bool']
elif issubclass(dtypeobj, _nt.integer):
- #if issubclass(dtypeobj, _nt.timedelta64):
- # format_function = formatdict['timedelta']
- #else:
- format_function = formatdict['int']
+ if issubclass(dtypeobj, _nt.timedelta64):
+ format_function = formatdict['timedelta']
+ else:
+ format_function = formatdict['int']
elif issubclass(dtypeobj, _nt.floating):
- #if issubclass(dtypeobj, _nt.longfloat):
- # format_function = formatdict['longfloat']
- #else:
- format_function = formatdict['float']
+ if issubclass(dtypeobj, _nt.longfloat):
+ format_function = formatdict['longfloat']
+ else:
+ format_function = formatdict['float']
elif issubclass(dtypeobj, _nt.complexfloating):
if issubclass(dtypeobj, _nt.clongfloat):
format_function = formatdict['longcomplexfloat']
@@ -437,20 +426,17 @@
if a.shape == ():
x = a.item()
- if isna(x):
- lst = str(x).replace('NA', _na_str, 1)
- else:
- try:
- lst = a._format(x)
- msg = "The `_format` attribute is deprecated in Numpy " \
- "2.0 and will be removed in 2.1. Use the " \
- "`formatter` kw instead."
- import warnings
- warnings.warn(msg, DeprecationWarning)
- except AttributeError:
- if isinstance(x, tuple):
- x = _convert_arrays(x)
- lst = style(x)
+ try:
+ lst = a._format(x)
+ msg = "The `_format` attribute is deprecated in Numpy " \
+ "2.0 and will be removed in 2.1. Use the " \
+ "`formatter` kw instead."
+ import warnings
+ warnings.warn(msg, DeprecationWarning)
+ except AttributeError:
+ if isinstance(x, tuple):
+ x = _convert_arrays(x)
+ lst = style(x)
elif reduce(product, a.shape) == 0:
# treat as a null array if any of shape elements == 0
lst = "[]"
@@ -542,38 +528,33 @@
self.exp_format = False
self.large_exponent = False
self.max_str_len = 0
- #try:
- self.fillFormat(data)
- #except (TypeError, NotImplementedError):
+ try:
+ self.fillFormat(data)
+ except (TypeError, NotImplementedError):
# if reduce(data) fails, this instance will not be called, just
# instantiated in formatdict.
- #pass
+ pass
def fillFormat(self, data):
import numeric as _nc
- # XXX pypy unimplemented
- #errstate = _nc.seterr(all='ignore')
+ errstate = _nc.seterr(all='ignore')
try:
- special = isnan(data) | isinf(data) | isna(data)
- special[isna(data)] = False
+ special = isnan(data) | isinf(data)
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)
+ max_val = maximum.reduce(non_zero)
+ min_val = minimum.reduce(non_zero)
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)
+ _nc.seterr(**errstate)
if self.exp_format:
self.large_exponent = 0 < min_val < 1e-99 or max_val >= 1e100
@@ -594,11 +575,10 @@
precision = 0
precision = min(self.precision, precision)
self.max_str_len = len(str(int(max_val))) + precision + 2
- if special.any():
+ if _nc.any(special):
self.max_str_len = max(self.max_str_len,
len(_nan_str),
- len(_inf_str)+1,
- len(_na_str))
+ len(_inf_str)+1)
if self.sign:
format = '%#+'
else:
@@ -610,11 +590,9 @@
def __call__(self, x, strip_zeros=True):
import numeric as _nc
- #err = _nc.seterr(invalid='ignore')
+ err = _nc.seterr(invalid='ignore')
try:
- if isna(x):
- return self.special_fmt % (str(x).replace('NA', _na_str, 1),)
- elif isnan(x):
+ if isnan(x):
if self.sign:
return self.special_fmt % ('+' + _nan_str,)
else:
@@ -628,8 +606,7 @@
else:
return self.special_fmt % ('-' + _inf_str,)
finally:
- pass
- #_nc.seterr(**err)
+ _nc.seterr(**err)
s = self.format % x
if self.large_exponent:
@@ -658,10 +635,10 @@
class IntegerFormat(object):
def __init__(self, data):
try:
- max_str_len = max(len(str(maximum.reduce(data, skipna=True))),
- len(str(minimum.reduce(data, skipna=True))))
+ max_str_len = max(len(str(maximum.reduce(data))),
+ len(str(minimum.reduce(data))))
self.format = '%' + str(max_str_len) + 'd'
- except TypeError, NotImplementedError:
+ except (TypeError, NotImplementedError):
# if reduce(data) fails, this instance will not be called, just
# instantiated in formatdict.
pass
@@ -670,9 +647,7 @@
pass
def __call__(self, x):
- if isna(x):
- return str(x).replace('NA', _na_str, 1)
- elif _MININT < x < _MAXINT:
+ if _MININT < x < _MAXINT:
return self.format % x
else:
return "%s" % x
@@ -685,9 +660,7 @@
self.sign = sign
def __call__(self, x):
- if isna(x):
- return str(x).replace('NA', _na_str, 1)
- elif isnan(x):
+ if isnan(x):
if self.sign:
return '+' + _nan_str
else:
@@ -715,12 +688,9 @@
self.imag_format = LongFloatFormat(precision, sign=True)
def __call__(self, x):
- if isna(x):
- return str(x).replace('NA', _na_str, 1)
- else:
- r = self.real_format(x.real)
- i = self.imag_format(x.imag)
- return r + i + 'j'
+ r = self.real_format(x.real)
+ i = self.imag_format(x.imag)
+ return r + i + 'j'
class ComplexFormat(object):
@@ -730,17 +700,14 @@
sign=True)
def __call__(self, x):
- if isna(x):
- return str(x).replace('NA', _na_str, 1)
+ r = self.real_format(x.real, strip_zeros=False)
+ i = self.imag_format(x.imag, strip_zeros=False)
+ if not self.imag_format.exp_format:
+ z = i.rstrip('0')
+ i = z + 'j' + ' '*(len(i)-len(z))
else:
- r = self.real_format(x.real, strip_zeros=False)
- i = self.imag_format(x.imag, strip_zeros=False)
- if not self.imag_format.exp_format:
- z = i.rstrip('0')
- i = z + 'j' + ' '*(len(i)-len(z))
- else:
- i = i + 'j'
- return r + i
+ i = i + 'j'
+ return r + i
class DatetimeFormat(object):
def __init__(self, x, unit=None,
@@ -765,13 +732,10 @@
self.casting = casting
def __call__(self, x):
- if isna(x):
- return str(x).replace('NA', _na_str, 1)
- else:
- return "'%s'" % datetime_as_string(x,
- unit=self.unit,
- timezone=self.timezone,
- casting=self.casting)
+ return "'%s'" % datetime_as_string(x,
+ unit=self.unit,
+ timezone=self.timezone,
+ casting=self.casting)
class TimedeltaFormat(object):
def __init__(self, data):
@@ -782,8 +746,5 @@
self.format = '%' + str(max_str_len) + 'd'
def __call__(self, x):
- if isna(x):
- return str(x).replace('NA', _na_str, 1)
- else:
- return self.format % x.astype('i8')
+ return self.format % x.astype('i8')
More information about the pypy-commit
mailing list