arrayprint.py 61 KB

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