arrayprint.py 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  1. """Array printing function
  2. $Id: arrayprint.py,v 1.9 2005/09/13 13:58:44 teoliphant Exp $
  3. """
  4. from __future__ import division, absolute_import, print_function
  5. __all__ = ["array2string", "array_str", "array_repr", "set_string_function",
  6. "set_printoptions", "get_printoptions", "printoptions",
  7. "format_float_positional", "format_float_scientific"]
  8. __docformat__ = 'restructuredtext'
  9. #
  10. # Written by Konrad Hinsen <hinsenk@ere.umontreal.ca>
  11. # last revision: 1996-3-13
  12. # modified by Jim Hugunin 1997-3-3 for repr's and str's (and other details)
  13. # and by Perry Greenfield 2000-4-1 for numarray
  14. # and by Travis Oliphant 2005-8-22 for numpy
  15. # Note: Both scalartypes.c.src and arrayprint.py implement strs for numpy
  16. # scalars but for different purposes. scalartypes.c.src has str/reprs for when
  17. # the scalar is printed on its own, while arrayprint.py has strs for when
  18. # scalars are printed inside an ndarray. Only the latter strs are currently
  19. # user-customizable.
  20. import sys
  21. import functools
  22. import numbers
  23. if sys.version_info[0] >= 3:
  24. try:
  25. from _thread import get_ident
  26. except ImportError:
  27. from _dummy_thread import get_ident
  28. else:
  29. try:
  30. from thread import get_ident
  31. except ImportError:
  32. from dummy_thread import get_ident
  33. import numpy as np
  34. from . import numerictypes as _nt
  35. from .umath import absolute, not_equal, isnan, isinf, isfinite, isnat
  36. from . import multiarray
  37. from .multiarray import (array, dragon4_positional, dragon4_scientific,
  38. datetime_as_string, datetime_data, ndarray,
  39. set_legacy_print_mode)
  40. from .fromnumeric import ravel, any
  41. from .numeric import concatenate, asarray, errstate
  42. from .numerictypes import (longlong, intc, int_, float_, complex_, bool_,
  43. flexible)
  44. from .overrides import array_function_dispatch, set_module
  45. import warnings
  46. import contextlib
  47. _format_options = {
  48. 'edgeitems': 3, # repr N leading and trailing items of each dimension
  49. 'threshold': 1000, # total items > triggers array summarization
  50. 'floatmode': 'maxprec',
  51. 'precision': 8, # precision of floating point representations
  52. 'suppress': False, # suppress printing small floating values in exp format
  53. 'linewidth': 75,
  54. 'nanstr': 'nan',
  55. 'infstr': 'inf',
  56. 'sign': '-',
  57. 'formatter': None,
  58. 'legacy': False}
  59. def _make_options_dict(precision=None, threshold=None, edgeitems=None,
  60. linewidth=None, suppress=None, nanstr=None, infstr=None,
  61. sign=None, formatter=None, floatmode=None, legacy=None):
  62. """ make a dictionary out of the non-None arguments, plus sanity checks """
  63. options = {k: v for k, v in locals().items() if v is not None}
  64. if suppress is not None:
  65. options['suppress'] = bool(suppress)
  66. modes = ['fixed', 'unique', 'maxprec', 'maxprec_equal']
  67. if floatmode not in modes + [None]:
  68. raise ValueError("floatmode option must be one of " +
  69. ", ".join('"{}"'.format(m) for m in modes))
  70. if sign not in [None, '-', '+', ' ']:
  71. raise ValueError("sign option must be one of ' ', '+', or '-'")
  72. if legacy not in [None, False, '1.13']:
  73. warnings.warn("legacy printing option can currently only be '1.13' or "
  74. "`False`", stacklevel=3)
  75. if threshold is not None:
  76. # forbid the bad threshold arg suggested by stack overflow, gh-12351
  77. if not isinstance(threshold, numbers.Number) or np.isnan(threshold):
  78. raise ValueError("threshold must be numeric and non-NAN, try "
  79. "sys.maxsize for untruncated representation")
  80. return options
  81. @set_module('numpy')
  82. def set_printoptions(precision=None, threshold=None, edgeitems=None,
  83. linewidth=None, suppress=None, nanstr=None, infstr=None,
  84. formatter=None, sign=None, floatmode=None, **kwarg):
  85. """
  86. Set printing options.
  87. These options determine the way floating point numbers, arrays and
  88. other NumPy objects are displayed.
  89. Parameters
  90. ----------
  91. precision : int or None, optional
  92. Number of digits of precision for floating point output (default 8).
  93. May be `None` if `floatmode` is not `fixed`, to print as many digits as
  94. necessary to uniquely specify the value.
  95. threshold : int, optional
  96. Total number of array elements which trigger summarization
  97. rather than full repr (default 1000).
  98. edgeitems : int, optional
  99. Number of array items in summary at beginning and end of
  100. each dimension (default 3).
  101. linewidth : int, optional
  102. The number of characters per line for the purpose of inserting
  103. line breaks (default 75).
  104. suppress : bool, optional
  105. If True, always print floating point numbers using fixed point
  106. notation, in which case numbers equal to zero in the current precision
  107. will print as zero. If False, then scientific notation is used when
  108. absolute value of the smallest number is < 1e-4 or the ratio of the
  109. maximum absolute value to the minimum is > 1e3. The default is False.
  110. nanstr : str, optional
  111. String representation of floating point not-a-number (default nan).
  112. infstr : str, optional
  113. String representation of floating point infinity (default inf).
  114. sign : string, either '-', '+', or ' ', optional
  115. Controls printing of the sign of floating-point types. If '+', always
  116. print the sign of positive values. If ' ', always prints a space
  117. (whitespace character) in the sign position of positive values. If
  118. '-', omit the sign character of positive values. (default '-')
  119. formatter : dict of callables, optional
  120. If not None, the keys should indicate the type(s) that the respective
  121. formatting function applies to. Callables should return a string.
  122. Types that are not specified (by their corresponding keys) are handled
  123. by the default formatters. Individual types for which a formatter
  124. can be set are:
  125. - 'bool'
  126. - 'int'
  127. - 'timedelta' : a `numpy.timedelta64`
  128. - 'datetime' : a `numpy.datetime64`
  129. - 'float'
  130. - 'longfloat' : 128-bit floats
  131. - 'complexfloat'
  132. - 'longcomplexfloat' : composed of two 128-bit floats
  133. - 'numpystr' : types `numpy.string_` and `numpy.unicode_`
  134. - 'object' : `np.object_` arrays
  135. - 'str' : all other strings
  136. Other keys that can be used to set a group of types at once are:
  137. - 'all' : sets all types
  138. - 'int_kind' : sets 'int'
  139. - 'float_kind' : sets 'float' and 'longfloat'
  140. - 'complex_kind' : sets 'complexfloat' and 'longcomplexfloat'
  141. - 'str_kind' : sets 'str' and 'numpystr'
  142. floatmode : str, optional
  143. Controls the interpretation of the `precision` option for
  144. floating-point types. Can take the following values:
  145. * 'fixed': Always print exactly `precision` fractional digits,
  146. even if this would print more or fewer digits than
  147. necessary to specify the value uniquely.
  148. * 'unique': Print the minimum number of fractional digits necessary
  149. to represent each value uniquely. Different elements may
  150. have a different number of digits. The value of the
  151. `precision` option is ignored.
  152. * 'maxprec': Print at most `precision` fractional digits, but if
  153. an element can be uniquely represented with fewer digits
  154. only print it with that many.
  155. * 'maxprec_equal': Print at most `precision` fractional digits,
  156. but if every element in the array can be uniquely
  157. represented with an equal number of fewer digits, use that
  158. many digits for all elements.
  159. legacy : string or `False`, optional
  160. If set to the string `'1.13'` enables 1.13 legacy printing mode. This
  161. approximates numpy 1.13 print output by including a space in the sign
  162. position of floats and different behavior for 0d arrays. If set to
  163. `False`, disables legacy mode. Unrecognized strings will be ignored
  164. with a warning for forward compatibility.
  165. .. versionadded:: 1.14.0
  166. See Also
  167. --------
  168. get_printoptions, set_string_function, array2string
  169. Notes
  170. -----
  171. `formatter` is always reset with a call to `set_printoptions`.
  172. Examples
  173. --------
  174. Floating point precision can be set:
  175. >>> np.set_printoptions(precision=4)
  176. >>> print(np.array([1.123456789]))
  177. [ 1.1235]
  178. Long arrays can be summarised:
  179. >>> np.set_printoptions(threshold=5)
  180. >>> print(np.arange(10))
  181. [0 1 2 ..., 7 8 9]
  182. Small results can be suppressed:
  183. >>> eps = np.finfo(float).eps
  184. >>> x = np.arange(4.)
  185. >>> x**2 - (x + eps)**2
  186. array([ -4.9304e-32, -4.4409e-16, 0.0000e+00, 0.0000e+00])
  187. >>> np.set_printoptions(suppress=True)
  188. >>> x**2 - (x + eps)**2
  189. array([-0., -0., 0., 0.])
  190. A custom formatter can be used to display array elements as desired:
  191. >>> np.set_printoptions(formatter={'all':lambda x: 'int: '+str(-x)})
  192. >>> x = np.arange(3)
  193. >>> x
  194. array([int: 0, int: -1, int: -2])
  195. >>> np.set_printoptions() # formatter gets reset
  196. >>> x
  197. array([0, 1, 2])
  198. To put back the default options, you can use:
  199. >>> np.set_printoptions(edgeitems=3,infstr='inf',
  200. ... linewidth=75, nanstr='nan', precision=8,
  201. ... suppress=False, threshold=1000, formatter=None)
  202. """
  203. legacy = kwarg.pop('legacy', None)
  204. if kwarg:
  205. msg = "set_printoptions() got unexpected keyword argument '{}'"
  206. raise TypeError(msg.format(kwarg.popitem()[0]))
  207. opt = _make_options_dict(precision, threshold, edgeitems, linewidth,
  208. suppress, nanstr, infstr, sign, formatter,
  209. floatmode, legacy)
  210. # formatter is always reset
  211. opt['formatter'] = formatter
  212. _format_options.update(opt)
  213. # set the C variable for legacy mode
  214. if _format_options['legacy'] == '1.13':
  215. set_legacy_print_mode(113)
  216. # reset the sign option in legacy mode to avoid confusion
  217. _format_options['sign'] = '-'
  218. elif _format_options['legacy'] is False:
  219. set_legacy_print_mode(0)
  220. @set_module('numpy')
  221. def get_printoptions():
  222. """
  223. Return the current print options.
  224. Returns
  225. -------
  226. print_opts : dict
  227. Dictionary of current print options with keys
  228. - precision : int
  229. - threshold : int
  230. - edgeitems : int
  231. - linewidth : int
  232. - suppress : bool
  233. - nanstr : str
  234. - infstr : str
  235. - formatter : dict of callables
  236. - sign : str
  237. For a full description of these options, see `set_printoptions`.
  238. See Also
  239. --------
  240. set_printoptions, set_string_function
  241. """
  242. return _format_options.copy()
  243. @set_module('numpy')
  244. @contextlib.contextmanager
  245. def printoptions(*args, **kwargs):
  246. """Context manager for setting print options.
  247. Set print options for the scope of the `with` block, and restore the old
  248. options at the end. See `set_printoptions` for the full description of
  249. available options.
  250. Examples
  251. --------
  252. >>> with np.printoptions(precision=2):
  253. ... print(np.array([2.0])) / 3
  254. [0.67]
  255. The `as`-clause of the `with`-statement gives the current print options:
  256. >>> with np.printoptions(precision=2) as opts:
  257. ... assert_equal(opts, np.get_printoptions())
  258. See Also
  259. --------
  260. set_printoptions, get_printoptions
  261. """
  262. opts = np.get_printoptions()
  263. try:
  264. np.set_printoptions(*args, **kwargs)
  265. yield np.get_printoptions()
  266. finally:
  267. np.set_printoptions(**opts)
  268. def _leading_trailing(a, edgeitems, index=()):
  269. """
  270. Keep only the N-D corners (leading and trailing edges) of an array.
  271. Should be passed a base-class ndarray, since it makes no guarantees about
  272. preserving subclasses.
  273. """
  274. axis = len(index)
  275. if axis == a.ndim:
  276. return a[index]
  277. if a.shape[axis] > 2*edgeitems:
  278. return concatenate((
  279. _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
  280. _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
  281. ), axis=axis)
  282. else:
  283. return _leading_trailing(a, edgeitems, index + np.index_exp[:])
  284. def _object_format(o):
  285. """ Object arrays containing lists should be printed unambiguously """
  286. if type(o) is list:
  287. fmt = 'list({!r})'
  288. else:
  289. fmt = '{!r}'
  290. return fmt.format(o)
  291. def repr_format(x):
  292. return repr(x)
  293. def str_format(x):
  294. return str(x)
  295. def _get_formatdict(data, **opt):
  296. prec, fmode = opt['precision'], opt['floatmode']
  297. supp, sign = opt['suppress'], opt['sign']
  298. legacy = opt['legacy']
  299. # wrapped in lambdas to avoid taking a code path with the wrong type of data
  300. formatdict = {
  301. 'bool': lambda: BoolFormat(data),
  302. 'int': lambda: IntegerFormat(data),
  303. 'float': lambda:
  304. FloatingFormat(data, prec, fmode, supp, sign, legacy=legacy),
  305. 'longfloat': lambda:
  306. FloatingFormat(data, prec, fmode, supp, sign, legacy=legacy),
  307. 'complexfloat': lambda:
  308. ComplexFloatingFormat(data, prec, fmode, supp, sign, legacy=legacy),
  309. 'longcomplexfloat': lambda:
  310. ComplexFloatingFormat(data, prec, fmode, supp, sign, legacy=legacy),
  311. 'datetime': lambda: DatetimeFormat(data, legacy=legacy),
  312. 'timedelta': lambda: TimedeltaFormat(data),
  313. 'object': lambda: _object_format,
  314. 'void': lambda: str_format,
  315. 'numpystr': lambda: repr_format,
  316. 'str': lambda: str}
  317. # we need to wrap values in `formatter` in a lambda, so that the interface
  318. # is the same as the above values.
  319. def indirect(x):
  320. return lambda: x
  321. formatter = opt['formatter']
  322. if formatter is not None:
  323. fkeys = [k for k in formatter.keys() if formatter[k] is not None]
  324. if 'all' in fkeys:
  325. for key in formatdict.keys():
  326. formatdict[key] = indirect(formatter['all'])
  327. if 'int_kind' in fkeys:
  328. for key in ['int']:
  329. formatdict[key] = indirect(formatter['int_kind'])
  330. if 'float_kind' in fkeys:
  331. for key in ['float', 'longfloat']:
  332. formatdict[key] = indirect(formatter['float_kind'])
  333. if 'complex_kind' in fkeys:
  334. for key in ['complexfloat', 'longcomplexfloat']:
  335. formatdict[key] = indirect(formatter['complex_kind'])
  336. if 'str_kind' in fkeys:
  337. for key in ['numpystr', 'str']:
  338. formatdict[key] = indirect(formatter['str_kind'])
  339. for key in formatdict.keys():
  340. if key in fkeys:
  341. formatdict[key] = indirect(formatter[key])
  342. return formatdict
  343. def _get_format_function(data, **options):
  344. """
  345. find the right formatting function for the dtype_
  346. """
  347. dtype_ = data.dtype
  348. dtypeobj = dtype_.type
  349. formatdict = _get_formatdict(data, **options)
  350. if issubclass(dtypeobj, _nt.bool_):
  351. return formatdict['bool']()
  352. elif issubclass(dtypeobj, _nt.integer):
  353. if issubclass(dtypeobj, _nt.timedelta64):
  354. return formatdict['timedelta']()
  355. else:
  356. return formatdict['int']()
  357. elif issubclass(dtypeobj, _nt.floating):
  358. if issubclass(dtypeobj, _nt.longfloat):
  359. return formatdict['longfloat']()
  360. else:
  361. return formatdict['float']()
  362. elif issubclass(dtypeobj, _nt.complexfloating):
  363. if issubclass(dtypeobj, _nt.clongfloat):
  364. return formatdict['longcomplexfloat']()
  365. else:
  366. return formatdict['complexfloat']()
  367. elif issubclass(dtypeobj, (_nt.unicode_, _nt.string_)):
  368. return formatdict['numpystr']()
  369. elif issubclass(dtypeobj, _nt.datetime64):
  370. return formatdict['datetime']()
  371. elif issubclass(dtypeobj, _nt.object_):
  372. return formatdict['object']()
  373. elif issubclass(dtypeobj, _nt.void):
  374. if dtype_.names is not None:
  375. return StructuredVoidFormat.from_data(data, **options)
  376. else:
  377. return formatdict['void']()
  378. else:
  379. return formatdict['numpystr']()
  380. def _recursive_guard(fillvalue='...'):
  381. """
  382. Like the python 3.2 reprlib.recursive_repr, but forwards *args and **kwargs
  383. Decorates a function such that if it calls itself with the same first
  384. argument, it returns `fillvalue` instead of recursing.
  385. Largely copied from reprlib.recursive_repr
  386. """
  387. def decorating_function(f):
  388. repr_running = set()
  389. @functools.wraps(f)
  390. def wrapper(self, *args, **kwargs):
  391. key = id(self), get_ident()
  392. if key in repr_running:
  393. return fillvalue
  394. repr_running.add(key)
  395. try:
  396. return f(self, *args, **kwargs)
  397. finally:
  398. repr_running.discard(key)
  399. return wrapper
  400. return decorating_function
  401. # gracefully handle recursive calls, when object arrays contain themselves
  402. @_recursive_guard()
  403. def _array2string(a, options, separator=' ', prefix=""):
  404. # The formatter __init__s in _get_format_function cannot deal with
  405. # subclasses yet, and we also need to avoid recursion issues in
  406. # _formatArray with subclasses which return 0d arrays in place of scalars
  407. data = asarray(a)
  408. if a.shape == ():
  409. a = data
  410. if a.size > options['threshold']:
  411. summary_insert = "..."
  412. data = _leading_trailing(data, options['edgeitems'])
  413. else:
  414. summary_insert = ""
  415. # find the right formatting function for the array
  416. format_function = _get_format_function(data, **options)
  417. # skip over "["
  418. next_line_prefix = " "
  419. # skip over array(
  420. next_line_prefix += " "*len(prefix)
  421. lst = _formatArray(a, format_function, options['linewidth'],
  422. next_line_prefix, separator, options['edgeitems'],
  423. summary_insert, options['legacy'])
  424. return lst
  425. def _array2string_dispatcher(
  426. a, max_line_width=None, precision=None,
  427. suppress_small=None, separator=None, prefix=None,
  428. style=None, formatter=None, threshold=None,
  429. edgeitems=None, sign=None, floatmode=None, suffix=None,
  430. **kwarg):
  431. return (a,)
  432. @array_function_dispatch(_array2string_dispatcher, module='numpy')
  433. def array2string(a, max_line_width=None, precision=None,
  434. suppress_small=None, separator=' ', prefix="",
  435. style=np._NoValue, formatter=None, threshold=None,
  436. edgeitems=None, sign=None, floatmode=None, suffix="",
  437. **kwarg):
  438. """
  439. Return a string representation of an array.
  440. Parameters
  441. ----------
  442. a : array_like
  443. Input array.
  444. max_line_width : int, optional
  445. The maximum number of columns the string should span. Newline
  446. characters splits the string appropriately after array elements.
  447. precision : int or None, optional
  448. Floating point precision. Default is the current printing
  449. precision (usually 8), which can be altered using `set_printoptions`.
  450. suppress_small : bool, optional
  451. Represent very small numbers as zero. A number is "very small" if it
  452. is smaller than the current printing precision.
  453. separator : str, optional
  454. Inserted between elements.
  455. prefix : str, optional
  456. suffix: str, optional
  457. The length of the prefix and suffix strings are used to respectively
  458. align and wrap the output. An array is typically printed as::
  459. prefix + array2string(a) + suffix
  460. The output is left-padded by the length of the prefix string, and
  461. wrapping is forced at the column ``max_line_width - len(suffix)``.
  462. It should be noted that the content of prefix and suffix strings are
  463. not included in the output.
  464. style : _NoValue, optional
  465. Has no effect, do not use.
  466. .. deprecated:: 1.14.0
  467. formatter : dict of callables, optional
  468. If not None, the keys should indicate the type(s) that the respective
  469. formatting function applies to. Callables should return a string.
  470. Types that are not specified (by their corresponding keys) are handled
  471. by the default formatters. Individual types for which a formatter
  472. can be set are:
  473. - 'bool'
  474. - 'int'
  475. - 'timedelta' : a `numpy.timedelta64`
  476. - 'datetime' : a `numpy.datetime64`
  477. - 'float'
  478. - 'longfloat' : 128-bit floats
  479. - 'complexfloat'
  480. - 'longcomplexfloat' : composed of two 128-bit floats
  481. - 'void' : type `numpy.void`
  482. - 'numpystr' : types `numpy.string_` and `numpy.unicode_`
  483. - 'str' : all other strings
  484. Other keys that can be used to set a group of types at once are:
  485. - 'all' : sets all types
  486. - 'int_kind' : sets 'int'
  487. - 'float_kind' : sets 'float' and 'longfloat'
  488. - 'complex_kind' : sets 'complexfloat' and 'longcomplexfloat'
  489. - 'str_kind' : sets 'str' and 'numpystr'
  490. threshold : int, optional
  491. Total number of array elements which trigger summarization
  492. rather than full repr.
  493. edgeitems : int, optional
  494. Number of array items in summary at beginning and end of
  495. each dimension.
  496. sign : string, either '-', '+', or ' ', optional
  497. Controls printing of the sign of floating-point types. If '+', always
  498. print the sign of positive values. If ' ', always prints a space
  499. (whitespace character) in the sign position of positive values. If
  500. '-', omit the sign character of positive values.
  501. floatmode : str, optional
  502. Controls the interpretation of the `precision` option for
  503. floating-point types. Can take the following values:
  504. - 'fixed': Always print exactly `precision` fractional digits,
  505. even if this would print more or fewer digits than
  506. necessary to specify the value uniquely.
  507. - 'unique': Print the minimum number of fractional digits necessary
  508. to represent each value uniquely. Different elements may
  509. have a different number of digits. The value of the
  510. `precision` option is ignored.
  511. - 'maxprec': Print at most `precision` fractional digits, but if
  512. an element can be uniquely represented with fewer digits
  513. only print it with that many.
  514. - 'maxprec_equal': Print at most `precision` fractional digits,
  515. but if every element in the array can be uniquely
  516. represented with an equal number of fewer digits, use that
  517. many digits for all elements.
  518. legacy : string or `False`, optional
  519. If set to the string `'1.13'` enables 1.13 legacy printing mode. This
  520. approximates numpy 1.13 print output by including a space in the sign
  521. position of floats and different behavior for 0d arrays. If set to
  522. `False`, disables legacy mode. Unrecognized strings will be ignored
  523. with a warning for forward compatibility.
  524. .. versionadded:: 1.14.0
  525. Returns
  526. -------
  527. array_str : str
  528. String representation of the array.
  529. Raises
  530. ------
  531. TypeError
  532. if a callable in `formatter` does not return a string.
  533. See Also
  534. --------
  535. array_str, array_repr, set_printoptions, get_printoptions
  536. Notes
  537. -----
  538. If a formatter is specified for a certain type, the `precision` keyword is
  539. ignored for that type.
  540. This is a very flexible function; `array_repr` and `array_str` are using
  541. `array2string` internally so keywords with the same name should work
  542. identically in all three functions.
  543. Examples
  544. --------
  545. >>> x = np.array([1e-16,1,2,3])
  546. >>> print(np.array2string(x, precision=2, separator=',',
  547. ... suppress_small=True))
  548. [ 0., 1., 2., 3.]
  549. >>> x = np.arange(3.)
  550. >>> np.array2string(x, formatter={'float_kind':lambda x: "%.2f" % x})
  551. '[0.00 1.00 2.00]'
  552. >>> x = np.arange(3)
  553. >>> np.array2string(x, formatter={'int':lambda x: hex(x)})
  554. '[0x0L 0x1L 0x2L]'
  555. """
  556. legacy = kwarg.pop('legacy', None)
  557. if kwarg:
  558. msg = "array2string() got unexpected keyword argument '{}'"
  559. raise TypeError(msg.format(kwarg.popitem()[0]))
  560. overrides = _make_options_dict(precision, threshold, edgeitems,
  561. max_line_width, suppress_small, None, None,
  562. sign, formatter, floatmode, legacy)
  563. options = _format_options.copy()
  564. options.update(overrides)
  565. if options['legacy'] == '1.13':
  566. if style is np._NoValue:
  567. style = repr
  568. if a.shape == () and a.dtype.names is None:
  569. return style(a.item())
  570. elif style is not np._NoValue:
  571. # Deprecation 11-9-2017 v1.14
  572. warnings.warn("'style' argument is deprecated and no longer functional"
  573. " except in 1.13 'legacy' mode",
  574. DeprecationWarning, stacklevel=3)
  575. if options['legacy'] != '1.13':
  576. options['linewidth'] -= len(suffix)
  577. # treat as a null array if any of shape elements == 0
  578. if a.size == 0:
  579. return "[]"
  580. return _array2string(a, options, separator, prefix)
  581. def _extendLine(s, line, word, line_width, next_line_prefix, legacy):
  582. needs_wrap = len(line) + len(word) > line_width
  583. if legacy != '1.13':
  584. s# don't wrap lines if it won't help
  585. if len(line) <= len(next_line_prefix):
  586. needs_wrap = False
  587. if needs_wrap:
  588. s += line.rstrip() + "\n"
  589. line = next_line_prefix
  590. line += word
  591. return s, line
  592. def _formatArray(a, format_function, line_width, next_line_prefix,
  593. separator, edge_items, summary_insert, legacy):
  594. """formatArray is designed for two modes of operation:
  595. 1. Full output
  596. 2. Summarized output
  597. """
  598. def recurser(index, hanging_indent, curr_width):
  599. """
  600. By using this local function, we don't need to recurse with all the
  601. arguments. Since this function is not created recursively, the cost is
  602. not significant
  603. """
  604. axis = len(index)
  605. axes_left = a.ndim - axis
  606. if axes_left == 0:
  607. return format_function(a[index])
  608. # when recursing, add a space to align with the [ added, and reduce the
  609. # length of the line by 1
  610. next_hanging_indent = hanging_indent + ' '
  611. if legacy == '1.13':
  612. next_width = curr_width
  613. else:
  614. next_width = curr_width - len(']')
  615. a_len = a.shape[axis]
  616. show_summary = summary_insert and 2*edge_items < a_len
  617. if show_summary:
  618. leading_items = edge_items
  619. trailing_items = edge_items
  620. else:
  621. leading_items = 0
  622. trailing_items = a_len
  623. # stringify the array with the hanging indent on the first line too
  624. s = ''
  625. # last axis (rows) - wrap elements if they would not fit on one line
  626. if axes_left == 1:
  627. # the length up until the beginning of the separator / bracket
  628. if legacy == '1.13':
  629. elem_width = curr_width - len(separator.rstrip())
  630. else:
  631. elem_width = curr_width - max(len(separator.rstrip()), len(']'))
  632. line = hanging_indent
  633. for i in range(leading_items):
  634. word = recurser(index + (i,), next_hanging_indent, next_width)
  635. s, line = _extendLine(
  636. s, line, word, elem_width, hanging_indent, legacy)
  637. line += separator
  638. if show_summary:
  639. s, line = _extendLine(
  640. s, line, summary_insert, elem_width, hanging_indent, legacy)
  641. if legacy == '1.13':
  642. line += ", "
  643. else:
  644. line += separator
  645. for i in range(trailing_items, 1, -1):
  646. word = recurser(index + (-i,), next_hanging_indent, next_width)
  647. s, line = _extendLine(
  648. s, line, word, elem_width, hanging_indent, legacy)
  649. line += separator
  650. if legacy == '1.13':
  651. # width of the separator is not considered on 1.13
  652. elem_width = curr_width
  653. word = recurser(index + (-1,), next_hanging_indent, next_width)
  654. s, line = _extendLine(
  655. s, line, word, elem_width, hanging_indent, legacy)
  656. s += line
  657. # other axes - insert newlines between rows
  658. else:
  659. s = ''
  660. line_sep = separator.rstrip() + '\n'*(axes_left - 1)
  661. for i in range(leading_items):
  662. nested = recurser(index + (i,), next_hanging_indent, next_width)
  663. s += hanging_indent + nested + line_sep
  664. if show_summary:
  665. if legacy == '1.13':
  666. # trailing space, fixed nbr of newlines, and fixed separator
  667. s += hanging_indent + summary_insert + ", \n"
  668. else:
  669. s += hanging_indent + summary_insert + line_sep
  670. for i in range(trailing_items, 1, -1):
  671. nested = recurser(index + (-i,), next_hanging_indent,
  672. next_width)
  673. s += hanging_indent + nested + line_sep
  674. nested = recurser(index + (-1,), next_hanging_indent, next_width)
  675. s += hanging_indent + nested
  676. # remove the hanging indent, and wrap in []
  677. s = '[' + s[len(hanging_indent):] + ']'
  678. return s
  679. try:
  680. # invoke the recursive part with an initial index and prefix
  681. return recurser(index=(),
  682. hanging_indent=next_line_prefix,
  683. curr_width=line_width)
  684. finally:
  685. # recursive closures have a cyclic reference to themselves, which
  686. # requires gc to collect (gh-10620). To avoid this problem, for
  687. # performance and PyPy friendliness, we break the cycle:
  688. recurser = None
  689. def _none_or_positive_arg(x, name):
  690. if x is None:
  691. return -1
  692. if x < 0:
  693. raise ValueError("{} must be >= 0".format(name))
  694. return x
  695. class FloatingFormat(object):
  696. """ Formatter for subtypes of np.floating """
  697. def __init__(self, data, precision, floatmode, suppress_small, sign=False,
  698. **kwarg):
  699. # for backcompatibility, accept bools
  700. if isinstance(sign, bool):
  701. sign = '+' if sign else '-'
  702. self._legacy = kwarg.get('legacy', False)
  703. if self._legacy == '1.13':
  704. # when not 0d, legacy does not support '-'
  705. if data.shape != () and sign == '-':
  706. sign = ' '
  707. self.floatmode = floatmode
  708. if floatmode == 'unique':
  709. self.precision = None
  710. else:
  711. self.precision = precision
  712. self.precision = _none_or_positive_arg(self.precision, 'precision')
  713. self.suppress_small = suppress_small
  714. self.sign = sign
  715. self.exp_format = False
  716. self.large_exponent = False
  717. self.fillFormat(data)
  718. def fillFormat(self, data):
  719. # only the finite values are used to compute the number of digits
  720. finite_vals = data[isfinite(data)]
  721. # choose exponential mode based on the non-zero finite values:
  722. abs_non_zero = absolute(finite_vals[finite_vals != 0])
  723. if len(abs_non_zero) != 0:
  724. max_val = np.max(abs_non_zero)
  725. min_val = np.min(abs_non_zero)
  726. with errstate(over='ignore'): # division can overflow
  727. if max_val >= 1.e8 or (not self.suppress_small and
  728. (min_val < 0.0001 or max_val/min_val > 1000.)):
  729. self.exp_format = True
  730. # do a first pass of printing all the numbers, to determine sizes
  731. if len(finite_vals) == 0:
  732. self.pad_left = 0
  733. self.pad_right = 0
  734. self.trim = '.'
  735. self.exp_size = -1
  736. self.unique = True
  737. elif self.exp_format:
  738. trim, unique = '.', True
  739. if self.floatmode == 'fixed' or self._legacy == '1.13':
  740. trim, unique = 'k', False
  741. strs = (dragon4_scientific(x, precision=self.precision,
  742. unique=unique, trim=trim, sign=self.sign == '+')
  743. for x in finite_vals)
  744. frac_strs, _, exp_strs = zip(*(s.partition('e') for s in strs))
  745. int_part, frac_part = zip(*(s.split('.') for s in frac_strs))
  746. self.exp_size = max(len(s) for s in exp_strs) - 1
  747. self.trim = 'k'
  748. self.precision = max(len(s) for s in frac_part)
  749. # for back-compat with np 1.13, use 2 spaces & sign and full prec
  750. if self._legacy == '1.13':
  751. self.pad_left = 3
  752. else:
  753. # this should be only 1 or 2. Can be calculated from sign.
  754. self.pad_left = max(len(s) for s in int_part)
  755. # pad_right is only needed for nan length calculation
  756. self.pad_right = self.exp_size + 2 + self.precision
  757. self.unique = False
  758. else:
  759. # first pass printing to determine sizes
  760. trim, unique = '.', True
  761. if self.floatmode == 'fixed':
  762. trim, unique = 'k', False
  763. strs = (dragon4_positional(x, precision=self.precision,
  764. fractional=True,
  765. unique=unique, trim=trim,
  766. sign=self.sign == '+')
  767. for x in finite_vals)
  768. int_part, frac_part = zip(*(s.split('.') for s in strs))
  769. if self._legacy == '1.13':
  770. self.pad_left = 1 + max(len(s.lstrip('-+')) for s in int_part)
  771. else:
  772. self.pad_left = max(len(s) for s in int_part)
  773. self.pad_right = max(len(s) for s in frac_part)
  774. self.exp_size = -1
  775. if self.floatmode in ['fixed', 'maxprec_equal']:
  776. self.precision = self.pad_right
  777. self.unique = False
  778. self.trim = 'k'
  779. else:
  780. self.unique = True
  781. self.trim = '.'
  782. if self._legacy != '1.13':
  783. # account for sign = ' ' by adding one to pad_left
  784. if self.sign == ' ' and not any(np.signbit(finite_vals)):
  785. self.pad_left += 1
  786. # if there are non-finite values, may need to increase pad_left
  787. if data.size != finite_vals.size:
  788. neginf = self.sign != '-' or any(data[isinf(data)] < 0)
  789. nanlen = len(_format_options['nanstr'])
  790. inflen = len(_format_options['infstr']) + neginf
  791. offset = self.pad_right + 1 # +1 for decimal pt
  792. self.pad_left = max(self.pad_left, nanlen - offset, inflen - offset)
  793. def __call__(self, x):
  794. if not np.isfinite(x):
  795. with errstate(invalid='ignore'):
  796. if np.isnan(x):
  797. sign = '+' if self.sign == '+' else ''
  798. ret = sign + _format_options['nanstr']
  799. else: # isinf
  800. sign = '-' if x < 0 else '+' if self.sign == '+' else ''
  801. ret = sign + _format_options['infstr']
  802. return ' '*(self.pad_left + self.pad_right + 1 - len(ret)) + ret
  803. if self.exp_format:
  804. return dragon4_scientific(x,
  805. precision=self.precision,
  806. unique=self.unique,
  807. trim=self.trim,
  808. sign=self.sign == '+',
  809. pad_left=self.pad_left,
  810. exp_digits=self.exp_size)
  811. else:
  812. return dragon4_positional(x,
  813. precision=self.precision,
  814. unique=self.unique,
  815. fractional=True,
  816. trim=self.trim,
  817. sign=self.sign == '+',
  818. pad_left=self.pad_left,
  819. pad_right=self.pad_right)
  820. # for back-compatibility, we keep the classes for each float type too
  821. class FloatFormat(FloatingFormat):
  822. def __init__(self, *args, **kwargs):
  823. warnings.warn("FloatFormat has been replaced by FloatingFormat",
  824. DeprecationWarning, stacklevel=2)
  825. super(FloatFormat, self).__init__(*args, **kwargs)
  826. class LongFloatFormat(FloatingFormat):
  827. def __init__(self, *args, **kwargs):
  828. warnings.warn("LongFloatFormat has been replaced by FloatingFormat",
  829. DeprecationWarning, stacklevel=2)
  830. super(LongFloatFormat, self).__init__(*args, **kwargs)
  831. @set_module('numpy')
  832. def format_float_scientific(x, precision=None, unique=True, trim='k',
  833. sign=False, pad_left=None, exp_digits=None):
  834. """
  835. Format a floating-point scalar as a decimal string in scientific notation.
  836. Provides control over rounding, trimming and padding. Uses and assumes
  837. IEEE unbiased rounding. Uses the "Dragon4" algorithm.
  838. Parameters
  839. ----------
  840. x : python float or numpy floating scalar
  841. Value to format.
  842. precision : non-negative integer or None, optional
  843. Maximum number of digits to print. May be None if `unique` is
  844. `True`, but must be an integer if unique is `False`.
  845. unique : boolean, optional
  846. If `True`, use a digit-generation strategy which gives the shortest
  847. representation which uniquely identifies the floating-point number from
  848. other values of the same type, by judicious rounding. If `precision`
  849. was omitted, print all necessary digits, otherwise digit generation is
  850. cut off after `precision` digits and the remaining value is rounded.
  851. If `False`, digits are generated as if printing an infinite-precision
  852. value and stopping after `precision` digits, rounding the remaining
  853. value.
  854. trim : one of 'k', '.', '0', '-', optional
  855. Controls post-processing trimming of trailing digits, as follows:
  856. * 'k' : keep trailing zeros, keep decimal point (no trimming)
  857. * '.' : trim all trailing zeros, leave decimal point
  858. * '0' : trim all but the zero before the decimal point. Insert the
  859. zero if it is missing.
  860. * '-' : trim trailing zeros and any trailing decimal point
  861. sign : boolean, optional
  862. Whether to show the sign for positive values.
  863. pad_left : non-negative integer, optional
  864. Pad the left side of the string with whitespace until at least that
  865. many characters are to the left of the decimal point.
  866. exp_digits : non-negative integer, optional
  867. Pad the exponent with zeros until it contains at least this many digits.
  868. If omitted, the exponent will be at least 2 digits.
  869. Returns
  870. -------
  871. rep : string
  872. The string representation of the floating point value
  873. See Also
  874. --------
  875. format_float_positional
  876. Examples
  877. --------
  878. >>> np.format_float_scientific(np.float32(np.pi))
  879. '3.1415927e+00'
  880. >>> s = np.float32(1.23e24)
  881. >>> np.format_float_scientific(s, unique=False, precision=15)
  882. '1.230000071797338e+24'
  883. >>> np.format_float_scientific(s, exp_digits=4)
  884. '1.23e+0024'
  885. """
  886. precision = _none_or_positive_arg(precision, 'precision')
  887. pad_left = _none_or_positive_arg(pad_left, 'pad_left')
  888. exp_digits = _none_or_positive_arg(exp_digits, 'exp_digits')
  889. return dragon4_scientific(x, precision=precision, unique=unique,
  890. trim=trim, sign=sign, pad_left=pad_left,
  891. exp_digits=exp_digits)
  892. @set_module('numpy')
  893. def format_float_positional(x, precision=None, unique=True,
  894. fractional=True, trim='k', sign=False,
  895. pad_left=None, pad_right=None):
  896. """
  897. Format a floating-point scalar as a decimal string in positional notation.
  898. Provides control over rounding, trimming and padding. Uses and assumes
  899. IEEE unbiased rounding. Uses the "Dragon4" algorithm.
  900. Parameters
  901. ----------
  902. x : python float or numpy floating scalar
  903. Value to format.
  904. precision : non-negative integer or None, optional
  905. Maximum number of digits to print. May be None if `unique` is
  906. `True`, but must be an integer if unique is `False`.
  907. unique : boolean, optional
  908. If `True`, use a digit-generation strategy which gives the shortest
  909. representation which uniquely identifies the floating-point number from
  910. other values of the same type, by judicious rounding. If `precision`
  911. was omitted, print out all necessary digits, otherwise digit generation
  912. is cut off after `precision` digits and the remaining value is rounded.
  913. If `False`, digits are generated as if printing an infinite-precision
  914. value and stopping after `precision` digits, rounding the remaining
  915. value.
  916. fractional : boolean, optional
  917. If `True`, the cutoff of `precision` digits refers to the total number
  918. of digits after the decimal point, including leading zeros.
  919. If `False`, `precision` refers to the total number of significant
  920. digits, before or after the decimal point, ignoring leading zeros.
  921. trim : one of 'k', '.', '0', '-', optional
  922. Controls post-processing trimming of trailing digits, as follows:
  923. * 'k' : keep trailing zeros, keep decimal point (no trimming)
  924. * '.' : trim all trailing zeros, leave decimal point
  925. * '0' : trim all but the zero before the decimal point. Insert the
  926. zero if it is missing.
  927. * '-' : trim trailing zeros and any trailing decimal point
  928. sign : boolean, optional
  929. Whether to show the sign for positive values.
  930. pad_left : non-negative integer, optional
  931. Pad the left side of the string with whitespace until at least that
  932. many characters are to the left of the decimal point.
  933. pad_right : non-negative integer, optional
  934. Pad the right side of the string with whitespace until at least that
  935. many characters are to the right of the decimal point.
  936. Returns
  937. -------
  938. rep : string
  939. The string representation of the floating point value
  940. See Also
  941. --------
  942. format_float_scientific
  943. Examples
  944. --------
  945. >>> np.format_float_positional(np.float32(np.pi))
  946. '3.1415927'
  947. >>> np.format_float_positional(np.float16(np.pi))
  948. '3.14'
  949. >>> np.format_float_positional(np.float16(0.3))
  950. '0.3'
  951. >>> np.format_float_positional(np.float16(0.3), unique=False, precision=10)
  952. '0.3000488281'
  953. """
  954. precision = _none_or_positive_arg(precision, 'precision')
  955. pad_left = _none_or_positive_arg(pad_left, 'pad_left')
  956. pad_right = _none_or_positive_arg(pad_right, 'pad_right')
  957. return dragon4_positional(x, precision=precision, unique=unique,
  958. fractional=fractional, trim=trim,
  959. sign=sign, pad_left=pad_left,
  960. pad_right=pad_right)
  961. class IntegerFormat(object):
  962. def __init__(self, data):
  963. if data.size > 0:
  964. max_str_len = max(len(str(np.max(data))),
  965. len(str(np.min(data))))
  966. else:
  967. max_str_len = 0
  968. self.format = '%{}d'.format(max_str_len)
  969. def __call__(self, x):
  970. return self.format % x
  971. class BoolFormat(object):
  972. def __init__(self, data, **kwargs):
  973. # add an extra space so " True" and "False" have the same length and
  974. # array elements align nicely when printed, except in 0d arrays
  975. self.truestr = ' True' if data.shape != () else 'True'
  976. def __call__(self, x):
  977. return self.truestr if x else "False"
  978. class ComplexFloatingFormat(object):
  979. """ Formatter for subtypes of np.complexfloating """
  980. def __init__(self, x, precision, floatmode, suppress_small,
  981. sign=False, **kwarg):
  982. # for backcompatibility, accept bools
  983. if isinstance(sign, bool):
  984. sign = '+' if sign else '-'
  985. floatmode_real = floatmode_imag = floatmode
  986. if kwarg.get('legacy', False) == '1.13':
  987. floatmode_real = 'maxprec_equal'
  988. floatmode_imag = 'maxprec'
  989. self.real_format = FloatingFormat(x.real, precision, floatmode_real,
  990. suppress_small, sign=sign, **kwarg)
  991. self.imag_format = FloatingFormat(x.imag, precision, floatmode_imag,
  992. suppress_small, sign='+', **kwarg)
  993. def __call__(self, x):
  994. r = self.real_format(x.real)
  995. i = self.imag_format(x.imag)
  996. # add the 'j' before the terminal whitespace in i
  997. sp = len(i.rstrip())
  998. i = i[:sp] + 'j' + i[sp:]
  999. return r + i
  1000. # for back-compatibility, we keep the classes for each complex type too
  1001. class ComplexFormat(ComplexFloatingFormat):
  1002. def __init__(self, *args, **kwargs):
  1003. warnings.warn(
  1004. "ComplexFormat has been replaced by ComplexFloatingFormat",
  1005. DeprecationWarning, stacklevel=2)
  1006. super(ComplexFormat, self).__init__(*args, **kwargs)
  1007. class LongComplexFormat(ComplexFloatingFormat):
  1008. def __init__(self, *args, **kwargs):
  1009. warnings.warn(
  1010. "LongComplexFormat has been replaced by ComplexFloatingFormat",
  1011. DeprecationWarning, stacklevel=2)
  1012. super(LongComplexFormat, self).__init__(*args, **kwargs)
  1013. class _TimelikeFormat(object):
  1014. def __init__(self, data):
  1015. non_nat = data[~isnat(data)]
  1016. if len(non_nat) > 0:
  1017. # Max str length of non-NaT elements
  1018. max_str_len = max(len(self._format_non_nat(np.max(non_nat))),
  1019. len(self._format_non_nat(np.min(non_nat))))
  1020. else:
  1021. max_str_len = 0
  1022. if len(non_nat) < data.size:
  1023. # data contains a NaT
  1024. max_str_len = max(max_str_len, 5)
  1025. self._format = '%{}s'.format(max_str_len)
  1026. self._nat = "'NaT'".rjust(max_str_len)
  1027. def _format_non_nat(self, x):
  1028. # override in subclass
  1029. raise NotImplementedError
  1030. def __call__(self, x):
  1031. if isnat(x):
  1032. return self._nat
  1033. else:
  1034. return self._format % self._format_non_nat(x)
  1035. class DatetimeFormat(_TimelikeFormat):
  1036. def __init__(self, x, unit=None, timezone=None, casting='same_kind',
  1037. legacy=False):
  1038. # Get the unit from the dtype
  1039. if unit is None:
  1040. if x.dtype.kind == 'M':
  1041. unit = datetime_data(x.dtype)[0]
  1042. else:
  1043. unit = 's'
  1044. if timezone is None:
  1045. timezone = 'naive'
  1046. self.timezone = timezone
  1047. self.unit = unit
  1048. self.casting = casting
  1049. self.legacy = legacy
  1050. # must be called after the above are configured
  1051. super(DatetimeFormat, self).__init__(x)
  1052. def __call__(self, x):
  1053. if self.legacy == '1.13':
  1054. return self._format_non_nat(x)
  1055. return super(DatetimeFormat, self).__call__(x)
  1056. def _format_non_nat(self, x):
  1057. return "'%s'" % datetime_as_string(x,
  1058. unit=self.unit,
  1059. timezone=self.timezone,
  1060. casting=self.casting)
  1061. class TimedeltaFormat(_TimelikeFormat):
  1062. def _format_non_nat(self, x):
  1063. return str(x.astype('i8'))
  1064. class SubArrayFormat(object):
  1065. def __init__(self, format_function):
  1066. self.format_function = format_function
  1067. def __call__(self, arr):
  1068. if arr.ndim <= 1:
  1069. return "[" + ", ".join(self.format_function(a) for a in arr) + "]"
  1070. return "[" + ", ".join(self.__call__(a) for a in arr) + "]"
  1071. class StructuredVoidFormat(object):
  1072. """
  1073. Formatter for structured np.void objects.
  1074. This does not work on structured alias types like np.dtype(('i4', 'i2,i2')),
  1075. as alias scalars lose their field information, and the implementation
  1076. relies upon np.void.__getitem__.
  1077. """
  1078. def __init__(self, format_functions):
  1079. self.format_functions = format_functions
  1080. @classmethod
  1081. def from_data(cls, data, **options):
  1082. """
  1083. This is a second way to initialize StructuredVoidFormat, using the raw data
  1084. as input. Added to avoid changing the signature of __init__.
  1085. """
  1086. format_functions = []
  1087. for field_name in data.dtype.names:
  1088. format_function = _get_format_function(data[field_name], **options)
  1089. if data.dtype[field_name].shape != ():
  1090. format_function = SubArrayFormat(format_function)
  1091. format_functions.append(format_function)
  1092. return cls(format_functions)
  1093. def __call__(self, x):
  1094. str_fields = [
  1095. format_function(field)
  1096. for field, format_function in zip(x, self.format_functions)
  1097. ]
  1098. if len(str_fields) == 1:
  1099. return "({},)".format(str_fields[0])
  1100. else:
  1101. return "({})".format(", ".join(str_fields))
  1102. # for backwards compatibility
  1103. class StructureFormat(StructuredVoidFormat):
  1104. def __init__(self, *args, **kwargs):
  1105. # NumPy 1.14, 2018-02-14
  1106. warnings.warn(
  1107. "StructureFormat has been replaced by StructuredVoidFormat",
  1108. DeprecationWarning, stacklevel=2)
  1109. super(StructureFormat, self).__init__(*args, **kwargs)
  1110. def _void_scalar_repr(x):
  1111. """
  1112. Implements the repr for structured-void scalars. It is called from the
  1113. scalartypes.c.src code, and is placed here because it uses the elementwise
  1114. formatters defined above.
  1115. """
  1116. return StructuredVoidFormat.from_data(array(x), **_format_options)(x)
  1117. _typelessdata = [int_, float_, complex_, bool_]
  1118. if issubclass(intc, int):
  1119. _typelessdata.append(intc)
  1120. if issubclass(longlong, int):
  1121. _typelessdata.append(longlong)
  1122. def dtype_is_implied(dtype):
  1123. """
  1124. Determine if the given dtype is implied by the representation of its values.
  1125. Parameters
  1126. ----------
  1127. dtype : dtype
  1128. Data type
  1129. Returns
  1130. -------
  1131. implied : bool
  1132. True if the dtype is implied by the representation of its values.
  1133. Examples
  1134. --------
  1135. >>> np.core.arrayprint.dtype_is_implied(int)
  1136. True
  1137. >>> np.array([1, 2, 3], int)
  1138. array([1, 2, 3])
  1139. >>> np.core.arrayprint.dtype_is_implied(np.int8)
  1140. False
  1141. >>> np.array([1, 2, 3], np.int8)
  1142. array([1, 2, 3], dtype=np.int8)
  1143. """
  1144. dtype = np.dtype(dtype)
  1145. if _format_options['legacy'] == '1.13' and dtype.type == bool_:
  1146. return False
  1147. # not just void types can be structured, and names are not part of the repr
  1148. if dtype.names is not None:
  1149. return False
  1150. return dtype.type in _typelessdata
  1151. def dtype_short_repr(dtype):
  1152. """
  1153. Convert a dtype to a short form which evaluates to the same dtype.
  1154. The intent is roughly that the following holds
  1155. >>> from numpy import *
  1156. >>> assert eval(dtype_short_repr(dt)) == dt
  1157. """
  1158. if dtype.names is not None:
  1159. # structured dtypes give a list or tuple repr
  1160. return str(dtype)
  1161. elif issubclass(dtype.type, flexible):
  1162. # handle these separately so they don't give garbage like str256
  1163. return "'%s'" % str(dtype)
  1164. typename = dtype.name
  1165. # quote typenames which can't be represented as python variable names
  1166. if typename and not (typename[0].isalpha() and typename.isalnum()):
  1167. typename = repr(typename)
  1168. return typename
  1169. def _array_repr_implementation(
  1170. arr, max_line_width=None, precision=None, suppress_small=None,
  1171. array2string=array2string):
  1172. """Internal version of array_repr() that allows overriding array2string."""
  1173. if max_line_width is None:
  1174. max_line_width = _format_options['linewidth']
  1175. if type(arr) is not ndarray:
  1176. class_name = type(arr).__name__
  1177. else:
  1178. class_name = "array"
  1179. skipdtype = dtype_is_implied(arr.dtype) and arr.size > 0
  1180. prefix = class_name + "("
  1181. suffix = ")" if skipdtype else ","
  1182. if (_format_options['legacy'] == '1.13' and
  1183. arr.shape == () and not arr.dtype.names):
  1184. lst = repr(arr.item())
  1185. elif arr.size > 0 or arr.shape == (0,):
  1186. lst = array2string(arr, max_line_width, precision, suppress_small,
  1187. ', ', prefix, suffix=suffix)
  1188. else: # show zero-length shape unless it is (0,)
  1189. lst = "[], shape=%s" % (repr(arr.shape),)
  1190. arr_str = prefix + lst + suffix
  1191. if skipdtype:
  1192. return arr_str
  1193. dtype_str = "dtype={})".format(dtype_short_repr(arr.dtype))
  1194. # compute whether we should put dtype on a new line: Do so if adding the
  1195. # dtype would extend the last line past max_line_width.
  1196. # Note: This line gives the correct result even when rfind returns -1.
  1197. last_line_len = len(arr_str) - (arr_str.rfind('\n') + 1)
  1198. spacer = " "
  1199. if _format_options['legacy'] == '1.13':
  1200. if issubclass(arr.dtype.type, flexible):
  1201. spacer = '\n' + ' '*len(class_name + "(")
  1202. elif last_line_len + len(dtype_str) + 1 > max_line_width:
  1203. spacer = '\n' + ' '*len(class_name + "(")
  1204. return arr_str + spacer + dtype_str
  1205. def _array_repr_dispatcher(
  1206. arr, max_line_width=None, precision=None, suppress_small=None):
  1207. return (arr,)
  1208. @array_function_dispatch(_array_repr_dispatcher, module='numpy')
  1209. def array_repr(arr, max_line_width=None, precision=None, suppress_small=None):
  1210. """
  1211. Return the string representation of an array.
  1212. Parameters
  1213. ----------
  1214. arr : ndarray
  1215. Input array.
  1216. max_line_width : int, optional
  1217. The maximum number of columns the string should span. Newline
  1218. characters split the string appropriately after array elements.
  1219. precision : int, optional
  1220. Floating point precision. Default is the current printing precision
  1221. (usually 8), which can be altered using `set_printoptions`.
  1222. suppress_small : bool, optional
  1223. Represent very small numbers as zero, default is False. Very small
  1224. is defined by `precision`, if the precision is 8 then
  1225. numbers smaller than 5e-9 are represented as zero.
  1226. Returns
  1227. -------
  1228. string : str
  1229. The string representation of an array.
  1230. See Also
  1231. --------
  1232. array_str, array2string, set_printoptions
  1233. Examples
  1234. --------
  1235. >>> np.array_repr(np.array([1,2]))
  1236. 'array([1, 2])'
  1237. >>> np.array_repr(np.ma.array([0.]))
  1238. 'MaskedArray([ 0.])'
  1239. >>> np.array_repr(np.array([], np.int32))
  1240. 'array([], dtype=int32)'
  1241. >>> x = np.array([1e-6, 4e-7, 2, 3])
  1242. >>> np.array_repr(x, precision=6, suppress_small=True)
  1243. 'array([ 0.000001, 0. , 2. , 3. ])'
  1244. """
  1245. return _array_repr_implementation(
  1246. arr, max_line_width, precision, suppress_small)
  1247. _guarded_str = _recursive_guard()(str)
  1248. def _array_str_implementation(
  1249. a, max_line_width=None, precision=None, suppress_small=None,
  1250. array2string=array2string):
  1251. """Internal version of array_str() that allows overriding array2string."""
  1252. if (_format_options['legacy'] == '1.13' and
  1253. a.shape == () and not a.dtype.names):
  1254. return str(a.item())
  1255. # the str of 0d arrays is a special case: It should appear like a scalar,
  1256. # so floats are not truncated by `precision`, and strings are not wrapped
  1257. # in quotes. So we return the str of the scalar value.
  1258. if a.shape == ():
  1259. # obtain a scalar and call str on it, avoiding problems for subclasses
  1260. # for which indexing with () returns a 0d instead of a scalar by using
  1261. # ndarray's getindex. Also guard against recursive 0d object arrays.
  1262. return _guarded_str(np.ndarray.__getitem__(a, ()))
  1263. return array2string(a, max_line_width, precision, suppress_small, ' ', "")
  1264. def _array_str_dispatcher(
  1265. a, max_line_width=None, precision=None, suppress_small=None):
  1266. return (a,)
  1267. @array_function_dispatch(_array_str_dispatcher, module='numpy')
  1268. def array_str(a, max_line_width=None, precision=None, suppress_small=None):
  1269. """
  1270. Return a string representation of the data in an array.
  1271. The data in the array is returned as a single string. This function is
  1272. similar to `array_repr`, the difference being that `array_repr` also
  1273. returns information on the kind of array and its data type.
  1274. Parameters
  1275. ----------
  1276. a : ndarray
  1277. Input array.
  1278. max_line_width : int, optional
  1279. Inserts newlines if text is longer than `max_line_width`. The
  1280. default is, indirectly, 75.
  1281. precision : int, optional
  1282. Floating point precision. Default is the current printing precision
  1283. (usually 8), which can be altered using `set_printoptions`.
  1284. suppress_small : bool, optional
  1285. Represent numbers "very close" to zero as zero; default is False.
  1286. Very close is defined by precision: if the precision is 8, e.g.,
  1287. numbers smaller (in absolute value) than 5e-9 are represented as
  1288. zero.
  1289. See Also
  1290. --------
  1291. array2string, array_repr, set_printoptions
  1292. Examples
  1293. --------
  1294. >>> np.array_str(np.arange(3))
  1295. '[0 1 2]'
  1296. """
  1297. return _array_str_implementation(
  1298. a, max_line_width, precision, suppress_small)
  1299. # needed if __array_function__ is disabled
  1300. _array2string_impl = getattr(array2string, '__wrapped__', array2string)
  1301. _default_array_str = functools.partial(_array_str_implementation,
  1302. array2string=_array2string_impl)
  1303. _default_array_repr = functools.partial(_array_repr_implementation,
  1304. array2string=_array2string_impl)
  1305. def set_string_function(f, repr=True):
  1306. """
  1307. Set a Python function to be used when pretty printing arrays.
  1308. Parameters
  1309. ----------
  1310. f : function or None
  1311. Function to be used to pretty print arrays. The function should expect
  1312. a single array argument and return a string of the representation of
  1313. the array. If None, the function is reset to the default NumPy function
  1314. to print arrays.
  1315. repr : bool, optional
  1316. If True (default), the function for pretty printing (``__repr__``)
  1317. is set, if False the function that returns the default string
  1318. representation (``__str__``) is set.
  1319. See Also
  1320. --------
  1321. set_printoptions, get_printoptions
  1322. Examples
  1323. --------
  1324. >>> def pprint(arr):
  1325. ... return 'HA! - What are you going to do now?'
  1326. ...
  1327. >>> np.set_string_function(pprint)
  1328. >>> a = np.arange(10)
  1329. >>> a
  1330. HA! - What are you going to do now?
  1331. >>> print(a)
  1332. [0 1 2 3 4 5 6 7 8 9]
  1333. We can reset the function to the default:
  1334. >>> np.set_string_function(None)
  1335. >>> a
  1336. array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  1337. `repr` affects either pretty printing or normal string representation.
  1338. Note that ``__repr__`` is still affected by setting ``__str__``
  1339. because the width of each array element in the returned string becomes
  1340. equal to the length of the result of ``__str__()``.
  1341. >>> x = np.arange(4)
  1342. >>> np.set_string_function(lambda x:'random', repr=False)
  1343. >>> x.__str__()
  1344. 'random'
  1345. >>> x.__repr__()
  1346. 'array([ 0, 1, 2, 3])'
  1347. """
  1348. if f is None:
  1349. if repr:
  1350. return multiarray.set_string_function(_default_array_repr, 1)
  1351. else:
  1352. return multiarray.set_string_function(_default_array_str, 0)
  1353. else:
  1354. return multiarray.set_string_function(f, repr)
  1355. set_string_function(_default_array_str, 0)
  1356. set_string_function(_default_array_repr, 1)