m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1664 lines
60 KiB

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