_binned_statistic.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. import builtins
  2. from warnings import catch_warnings, simplefilter
  3. import numpy as np
  4. from operator import index
  5. from collections import namedtuple
  6. from scipy._lib._array_api import xp_capabilities
  7. __all__ = ['binned_statistic',
  8. 'binned_statistic_2d',
  9. 'binned_statistic_dd']
  10. BinnedStatisticResult = namedtuple('BinnedStatisticResult',
  11. ('statistic', 'bin_edges', 'binnumber'))
  12. @xp_capabilities(np_only=True)
  13. def binned_statistic(x, values, statistic='mean',
  14. bins=10, range=None):
  15. """
  16. Compute a binned statistic for one or more sets of data.
  17. This is a generalization of a histogram function. A histogram divides
  18. the space into bins, and returns the count of the number of points in
  19. each bin. This function allows the computation of the sum, mean, median,
  20. or other statistic of the values (or set of values) within each bin.
  21. Parameters
  22. ----------
  23. x : (N,) array_like
  24. A sequence of values to be binned.
  25. values : (N,) array_like or list of (N,) array_like
  26. The data on which the statistic will be computed. This must be
  27. the same shape as `x`, or a set of sequences - each the same shape as
  28. `x`. If `values` is a set of sequences, the statistic will be computed
  29. on each independently.
  30. statistic : string or callable, optional
  31. The statistic to compute (default is 'mean').
  32. The following statistics are available:
  33. * 'mean' : compute the mean of values for points within each bin.
  34. Empty bins will be represented by NaN.
  35. * 'std' : compute the standard deviation within each bin. This
  36. is implicitly calculated with ddof=0.
  37. * 'median' : compute the median of values for points within each
  38. bin. Empty bins will be represented by NaN.
  39. * 'count' : compute the count of points within each bin. This is
  40. identical to an unweighted histogram. `values` array is not
  41. referenced.
  42. * 'sum' : compute the sum of values for points within each bin.
  43. This is identical to a weighted histogram.
  44. * 'min' : compute the minimum of values for points within each bin.
  45. Empty bins will be represented by NaN.
  46. * 'max' : compute the maximum of values for point within each bin.
  47. Empty bins will be represented by NaN.
  48. * function : a user-defined function which takes a 1D array of
  49. values, and outputs a single numerical statistic. This function
  50. will be called on the values in each bin. Empty bins will be
  51. represented by function([]), or NaN if this returns an error.
  52. bins : int or sequence of scalars, optional
  53. If `bins` is an int, it defines the number of equal-width bins in the
  54. given range (10 by default). If `bins` is a sequence, it defines the
  55. bin edges, including the rightmost edge, allowing for non-uniform bin
  56. widths. Values in `x` that are smaller than lowest bin edge are
  57. assigned to bin number 0, values beyond the highest bin are assigned to
  58. ``bins[-1]``. If the bin edges are specified, the number of bins will
  59. be, (nx = len(bins)-1).
  60. range : (float, float) or [(float, float)], optional
  61. The lower and upper range of the bins. If not provided, range
  62. is simply ``(x.min(), x.max())``. Values outside the range are
  63. ignored.
  64. Returns
  65. -------
  66. statistic : array
  67. The values of the selected statistic in each bin.
  68. bin_edges : array of dtype float
  69. Return the bin edges ``(length(statistic)+1)``.
  70. binnumber: 1-D ndarray of ints
  71. Indices of the bins (corresponding to `bin_edges`) in which each value
  72. of `x` belongs. Same length as `values`. A binnumber of `i` means the
  73. corresponding value is between (bin_edges[i-1], bin_edges[i]).
  74. See Also
  75. --------
  76. numpy.digitize, numpy.histogram, binned_statistic_2d, binned_statistic_dd
  77. Notes
  78. -----
  79. All but the last (righthand-most) bin is half-open. In other words, if
  80. `bins` is ``[1, 2, 3, 4]``, then the first bin is ``[1, 2)`` (including 1,
  81. but excluding 2) and the second ``[2, 3)``. The last bin, however, is
  82. ``[3, 4]``, which *includes* 4.
  83. .. versionadded:: 0.11.0
  84. Examples
  85. --------
  86. >>> import numpy as np
  87. >>> from scipy import stats
  88. >>> import matplotlib.pyplot as plt
  89. First some basic examples:
  90. Create two evenly spaced bins in the range of the given sample, and sum the
  91. corresponding values in each of those bins:
  92. >>> values = [1.0, 1.0, 2.0, 1.5, 3.0]
  93. >>> stats.binned_statistic([1, 1, 2, 5, 7], values, 'sum', bins=2)
  94. BinnedStatisticResult(statistic=array([4. , 4.5]),
  95. bin_edges=array([1., 4., 7.]), binnumber=array([1, 1, 1, 2, 2]))
  96. Multiple arrays of values can also be passed. The statistic is calculated
  97. on each set independently:
  98. >>> values = [[1.0, 1.0, 2.0, 1.5, 3.0], [2.0, 2.0, 4.0, 3.0, 6.0]]
  99. >>> stats.binned_statistic([1, 1, 2, 5, 7], values, 'sum', bins=2)
  100. BinnedStatisticResult(statistic=array([[4. , 4.5],
  101. [8. , 9. ]]), bin_edges=array([1., 4., 7.]),
  102. binnumber=array([1, 1, 1, 2, 2]))
  103. >>> stats.binned_statistic([1, 2, 1, 2, 4], np.arange(5), statistic='mean',
  104. ... bins=3)
  105. BinnedStatisticResult(statistic=array([1., 2., 4.]),
  106. bin_edges=array([1., 2., 3., 4.]),
  107. binnumber=array([1, 2, 1, 2, 3]))
  108. As a second example, we now generate some random data of sailing boat speed
  109. as a function of wind speed, and then determine how fast our boat is for
  110. certain wind speeds:
  111. >>> rng = np.random.default_rng()
  112. >>> windspeed = 8 * rng.random(500)
  113. >>> boatspeed = .3 * windspeed**.5 + .2 * rng.random(500)
  114. >>> bin_means, bin_edges, binnumber = stats.binned_statistic(windspeed,
  115. ... boatspeed, statistic='median', bins=[1,2,3,4,5,6,7])
  116. >>> plt.figure()
  117. >>> plt.plot(windspeed, boatspeed, 'b.', label='raw data')
  118. >>> plt.hlines(bin_means, bin_edges[:-1], bin_edges[1:], colors='g', lw=5,
  119. ... label='binned statistic of data')
  120. >>> plt.legend()
  121. Now we can use ``binnumber`` to select all datapoints with a windspeed
  122. below 1:
  123. >>> low_boatspeed = boatspeed[binnumber == 0]
  124. As a final example, we will use ``bin_edges`` and ``binnumber`` to make a
  125. plot of a distribution that shows the mean and distribution around that
  126. mean per bin, on top of a regular histogram and the probability
  127. distribution function:
  128. >>> x = np.linspace(0, 5, num=500)
  129. >>> x_pdf = stats.maxwell.pdf(x)
  130. >>> samples = stats.maxwell.rvs(size=10000)
  131. >>> bin_means, bin_edges, binnumber = stats.binned_statistic(x, x_pdf,
  132. ... statistic='mean', bins=25)
  133. >>> bin_width = (bin_edges[1] - bin_edges[0])
  134. >>> bin_centers = bin_edges[1:] - bin_width/2
  135. >>> plt.figure()
  136. >>> plt.hist(samples, bins=50, density=True, histtype='stepfilled',
  137. ... alpha=0.2, label='histogram of data')
  138. >>> plt.plot(x, x_pdf, 'r-', label='analytical pdf')
  139. >>> plt.hlines(bin_means, bin_edges[:-1], bin_edges[1:], colors='g', lw=2,
  140. ... label='binned statistic of data')
  141. >>> plt.plot((binnumber - 0.5) * bin_width, x_pdf, 'g.', alpha=0.5)
  142. >>> plt.legend(fontsize=10)
  143. >>> plt.show()
  144. """
  145. try:
  146. N = len(bins)
  147. except TypeError:
  148. N = 1
  149. if N != 1:
  150. bins = [np.asarray(bins, float)]
  151. if range is not None:
  152. if len(range) == 2:
  153. range = [range]
  154. medians, edges, binnumbers = binned_statistic_dd(
  155. [x], values, statistic, bins, range)
  156. return BinnedStatisticResult(medians, edges[0], binnumbers)
  157. BinnedStatistic2dResult = namedtuple('BinnedStatistic2dResult',
  158. ('statistic', 'x_edge', 'y_edge',
  159. 'binnumber'))
  160. @xp_capabilities(np_only=True)
  161. def binned_statistic_2d(x, y, values, statistic='mean',
  162. bins=10, range=None, expand_binnumbers=False):
  163. """
  164. Compute a bidimensional binned statistic for one or more sets of data.
  165. This is a generalization of a histogram2d function. A histogram divides
  166. the space into bins, and returns the count of the number of points in
  167. each bin. This function allows the computation of the sum, mean, median,
  168. or other statistic of the values (or set of values) within each bin.
  169. Parameters
  170. ----------
  171. x : (N,) array_like
  172. A sequence of values to be binned along the first dimension.
  173. y : (N,) array_like
  174. A sequence of values to be binned along the second dimension.
  175. values : (N,) array_like or list of (N,) array_like
  176. The data on which the statistic will be computed. This must be
  177. the same shape as `x`, or a list of sequences - each with the same
  178. shape as `x`. If `values` is such a list, the statistic will be
  179. computed on each independently.
  180. statistic : string or callable, optional
  181. The statistic to compute (default is 'mean').
  182. The following statistics are available:
  183. * 'mean' : compute the mean of values for points within each bin.
  184. Empty bins will be represented by NaN.
  185. * 'std' : compute the standard deviation within each bin. This
  186. is implicitly calculated with ddof=0.
  187. * 'median' : compute the median of values for points within each
  188. bin. Empty bins will be represented by NaN.
  189. * 'count' : compute the count of points within each bin. This is
  190. identical to an unweighted histogram. `values` array is not
  191. referenced.
  192. * 'sum' : compute the sum of values for points within each bin.
  193. This is identical to a weighted histogram.
  194. * 'min' : compute the minimum of values for points within each bin.
  195. Empty bins will be represented by NaN.
  196. * 'max' : compute the maximum of values for point within each bin.
  197. Empty bins will be represented by NaN.
  198. * function : a user-defined function which takes a 1D array of
  199. values, and outputs a single numerical statistic. This function
  200. will be called on the values in each bin. Empty bins will be
  201. represented by function([]), or NaN if this returns an error.
  202. bins : int or [int, int] or array_like or [array, array], optional
  203. The bin specification:
  204. * the number of bins for the two dimensions (nx = ny = bins),
  205. * the number of bins in each dimension (nx, ny = bins),
  206. * the bin edges for the two dimensions (x_edge = y_edge = bins),
  207. * the bin edges in each dimension (x_edge, y_edge = bins).
  208. If the bin edges are specified, the number of bins will be,
  209. (nx = len(x_edge)-1, ny = len(y_edge)-1).
  210. range : (2,2) array_like, optional
  211. The leftmost and rightmost edges of the bins along each dimension
  212. (if not specified explicitly in the `bins` parameters):
  213. [[xmin, xmax], [ymin, ymax]]. All values outside of this range will be
  214. considered outliers and not tallied in the histogram.
  215. expand_binnumbers : bool, optional
  216. 'False' (default): the returned `binnumber` is a shape (N,) array of
  217. linearized bin indices.
  218. 'True': the returned `binnumber` is 'unraveled' into a shape (2,N)
  219. ndarray, where each row gives the bin numbers in the corresponding
  220. dimension.
  221. See the `binnumber` returned value, and the `Examples` section.
  222. .. versionadded:: 0.17.0
  223. Returns
  224. -------
  225. statistic : (nx, ny) ndarray
  226. The values of the selected statistic in each two-dimensional bin.
  227. x_edge : (nx + 1) ndarray
  228. The bin edges along the first dimension.
  229. y_edge : (ny + 1) ndarray
  230. The bin edges along the second dimension.
  231. binnumber : (N,) array of ints or (2,N) ndarray of ints
  232. This assigns to each element of `sample` an integer that represents the
  233. bin in which this observation falls. The representation depends on the
  234. `expand_binnumbers` argument. See `Notes` for details.
  235. See Also
  236. --------
  237. numpy.digitize, numpy.histogram2d, binned_statistic, binned_statistic_dd
  238. Notes
  239. -----
  240. Binedges:
  241. All but the last (righthand-most) bin is half-open. In other words, if
  242. `bins` is ``[1, 2, 3, 4]``, then the first bin is ``[1, 2)`` (including 1,
  243. but excluding 2) and the second ``[2, 3)``. The last bin, however, is
  244. ``[3, 4]``, which *includes* 4.
  245. `binnumber`:
  246. This returned argument assigns to each element of `sample` an integer that
  247. represents the bin in which it belongs. The representation depends on the
  248. `expand_binnumbers` argument. If 'False' (default): The returned
  249. `binnumber` is a shape (N,) array of linearized indices mapping each
  250. element of `sample` to its corresponding bin (using row-major ordering).
  251. Note that the returned linearized bin indices are used for an array with
  252. extra bins on the outer binedges to capture values outside of the defined
  253. bin bounds.
  254. If 'True': The returned `binnumber` is a shape (2,N) ndarray where
  255. each row indicates bin placements for each dimension respectively. In each
  256. dimension, a binnumber of `i` means the corresponding value is between
  257. (D_edge[i-1], D_edge[i]), where 'D' is either 'x' or 'y'.
  258. .. versionadded:: 0.11.0
  259. Examples
  260. --------
  261. >>> from scipy import stats
  262. Calculate the counts with explicit bin-edges:
  263. >>> x = [0.1, 0.1, 0.1, 0.6]
  264. >>> y = [2.1, 2.6, 2.1, 2.1]
  265. >>> binx = [0.0, 0.5, 1.0]
  266. >>> biny = [2.0, 2.5, 3.0]
  267. >>> ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx, biny])
  268. >>> ret.statistic
  269. array([[2., 1.],
  270. [1., 0.]])
  271. The bin in which each sample is placed is given by the `binnumber`
  272. returned parameter. By default, these are the linearized bin indices:
  273. >>> ret.binnumber
  274. array([5, 6, 5, 9])
  275. The bin indices can also be expanded into separate entries for each
  276. dimension using the `expand_binnumbers` parameter:
  277. >>> ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx, biny],
  278. ... expand_binnumbers=True)
  279. >>> ret.binnumber
  280. array([[1, 1, 1, 2],
  281. [1, 2, 1, 1]])
  282. Which shows that the first three elements belong in the xbin 1, and the
  283. fourth into xbin 2; and so on for y.
  284. """
  285. # This code is based on np.histogram2d
  286. try:
  287. N = len(bins)
  288. except TypeError:
  289. N = 1
  290. if N != 1 and N != 2:
  291. xedges = yedges = np.asarray(bins, float)
  292. bins = [xedges, yedges]
  293. medians, edges, binnumbers = binned_statistic_dd(
  294. [x, y], values, statistic, bins, range,
  295. expand_binnumbers=expand_binnumbers)
  296. return BinnedStatistic2dResult(medians, edges[0], edges[1], binnumbers)
  297. BinnedStatisticddResult = namedtuple('BinnedStatisticddResult',
  298. ('statistic', 'bin_edges',
  299. 'binnumber'))
  300. def _bincount(x, weights):
  301. if np.iscomplexobj(weights):
  302. a = np.bincount(x, np.real(weights))
  303. b = np.bincount(x, np.imag(weights))
  304. z = a + b*1j
  305. else:
  306. z = np.bincount(x, weights)
  307. return z
  308. @xp_capabilities(np_only=True)
  309. def binned_statistic_dd(sample, values, statistic='mean',
  310. bins=10, range=None, expand_binnumbers=False,
  311. binned_statistic_result=None):
  312. """
  313. Compute a multidimensional binned statistic for a set of data.
  314. This is a generalization of a histogramdd function. A histogram divides
  315. the space into bins, and returns the count of the number of points in
  316. each bin. This function allows the computation of the sum, mean, median,
  317. or other statistic of the values within each bin.
  318. Parameters
  319. ----------
  320. sample : array_like
  321. Data to histogram passed as a sequence of N arrays of length D, or
  322. as an (N,D) array.
  323. values : (N,) array_like or list of (N,) array_like
  324. The data on which the statistic will be computed. This must be
  325. the same shape as `sample`, or a list of sequences - each with the
  326. same shape as `sample`. If `values` is such a list, the statistic
  327. will be computed on each independently.
  328. statistic : string or callable, optional
  329. The statistic to compute (default is 'mean').
  330. The following statistics are available:
  331. * 'mean' : compute the mean of values for points within each bin.
  332. Empty bins will be represented by NaN.
  333. * 'median' : compute the median of values for points within each
  334. bin. Empty bins will be represented by NaN.
  335. * 'count' : compute the count of points within each bin. This is
  336. identical to an unweighted histogram. `values` array is not
  337. referenced.
  338. * 'sum' : compute the sum of values for points within each bin.
  339. This is identical to a weighted histogram.
  340. * 'std' : compute the standard deviation within each bin. This
  341. is implicitly calculated with ddof=0. If the number of values
  342. within a given bin is 0 or 1, the computed standard deviation value
  343. will be 0 for the bin.
  344. * 'min' : compute the minimum of values for points within each bin.
  345. Empty bins will be represented by NaN.
  346. * 'max' : compute the maximum of values for point within each bin.
  347. Empty bins will be represented by NaN.
  348. * function : a user-defined function which takes a 1D array of
  349. values, and outputs a single numerical statistic. This function
  350. will be called on the values in each bin. Empty bins will be
  351. represented by function([]), or NaN if this returns an error.
  352. bins : sequence or positive int, optional
  353. The bin specification must be in one of the following forms:
  354. * A sequence of arrays describing the bin edges along each dimension.
  355. * The number of bins for each dimension (nx, ny, ... = bins).
  356. * The number of bins for all dimensions (nx = ny = ... = bins).
  357. range : sequence, optional
  358. A sequence of lower and upper bin edges to be used if the edges are
  359. not given explicitly in `bins`. Defaults to the minimum and maximum
  360. values along each dimension.
  361. expand_binnumbers : bool, optional
  362. 'False' (default): the returned `binnumber` is a shape (N,) array of
  363. linearized bin indices.
  364. 'True': the returned `binnumber` is 'unraveled' into a shape (D,N)
  365. ndarray, where each row gives the bin numbers in the corresponding
  366. dimension.
  367. See the `binnumber` returned value, and the `Examples` section of
  368. `binned_statistic_2d`.
  369. binned_statistic_result : binnedStatisticddResult
  370. Result of a previous call to the function in order to reuse bin edges
  371. and bin numbers with new values and/or a different statistic.
  372. To reuse bin numbers, `expand_binnumbers` must have been set to False
  373. (the default)
  374. .. versionadded:: 0.17.0
  375. Returns
  376. -------
  377. statistic : ndarray, shape(nx1, nx2, nx3,...)
  378. The values of the selected statistic in each two-dimensional bin.
  379. bin_edges : list of ndarrays
  380. A list of D arrays describing the (nxi + 1) bin edges for each
  381. dimension.
  382. binnumber : (N,) array of ints or (D,N) ndarray of ints
  383. This assigns to each element of `sample` an integer that represents the
  384. bin in which this observation falls. The representation depends on the
  385. `expand_binnumbers` argument. See `Notes` for details.
  386. See Also
  387. --------
  388. numpy.digitize, numpy.histogramdd, binned_statistic, binned_statistic_2d
  389. Notes
  390. -----
  391. Binedges:
  392. All but the last (righthand-most) bin is half-open in each dimension. In
  393. other words, if `bins` is ``[1, 2, 3, 4]``, then the first bin is
  394. ``[1, 2)`` (including 1, but excluding 2) and the second ``[2, 3)``. The
  395. last bin, however, is ``[3, 4]``, which *includes* 4.
  396. `binnumber`:
  397. This returned argument assigns to each element of `sample` an integer that
  398. represents the bin in which it belongs. The representation depends on the
  399. `expand_binnumbers` argument. If 'False' (default): The returned
  400. `binnumber` is a shape (N,) array of linearized indices mapping each
  401. element of `sample` to its corresponding bin (using row-major ordering).
  402. If 'True': The returned `binnumber` is a shape (D,N) ndarray where
  403. each row indicates bin placements for each dimension respectively. In each
  404. dimension, a binnumber of `i` means the corresponding value is between
  405. (bin_edges[D][i-1], bin_edges[D][i]), for each dimension 'D'.
  406. .. versionadded:: 0.11.0
  407. Examples
  408. --------
  409. >>> import numpy as np
  410. >>> from scipy import stats
  411. >>> import matplotlib.pyplot as plt
  412. >>> from mpl_toolkits.mplot3d import Axes3D
  413. Take an array of 600 (x, y) coordinates as an example.
  414. `binned_statistic_dd` can handle arrays of higher dimension `D`. But a plot
  415. of dimension `D+1` is required.
  416. >>> mu = np.array([0., 1.])
  417. >>> sigma = np.array([[1., -0.5],[-0.5, 1.5]])
  418. >>> multinormal = stats.multivariate_normal(mu, sigma)
  419. >>> data = multinormal.rvs(size=600, random_state=235412)
  420. >>> data.shape
  421. (600, 2)
  422. Create bins and count how many arrays fall in each bin:
  423. >>> N = 60
  424. >>> x = np.linspace(-3, 3, N)
  425. >>> y = np.linspace(-3, 4, N)
  426. >>> ret = stats.binned_statistic_dd(data, np.arange(600), bins=[x, y],
  427. ... statistic='count')
  428. >>> bincounts = ret.statistic
  429. Set the volume and the location of bars:
  430. >>> dx = x[1] - x[0]
  431. >>> dy = y[1] - y[0]
  432. >>> x, y = np.meshgrid(x[:-1]+dx/2, y[:-1]+dy/2)
  433. >>> z = 0
  434. >>> bincounts = bincounts.ravel()
  435. >>> x = x.ravel()
  436. >>> y = y.ravel()
  437. >>> fig = plt.figure()
  438. >>> ax = fig.add_subplot(111, projection='3d')
  439. >>> with np.errstate(divide='ignore'): # silence random axes3d warning
  440. ... ax.bar3d(x, y, z, dx, dy, bincounts)
  441. Reuse bin numbers and bin edges with new values:
  442. >>> ret2 = stats.binned_statistic_dd(data, -np.arange(600),
  443. ... binned_statistic_result=ret,
  444. ... statistic='mean')
  445. """
  446. known_stats = ['mean', 'median', 'count', 'sum', 'std', 'min', 'max']
  447. if not callable(statistic) and statistic not in known_stats:
  448. raise ValueError(f'invalid statistic {statistic!r}')
  449. try:
  450. bins = index(bins)
  451. except TypeError:
  452. # bins is not an integer
  453. pass
  454. # If bins was an integer-like object, now it is an actual Python int.
  455. # NOTE: for _bin_edges(), see e.g. gh-11365
  456. if isinstance(bins, int) and not np.isfinite(sample).all():
  457. raise ValueError(f'{sample!r} contains non-finite values.')
  458. # `Ndim` is the number of dimensions (e.g. `2` for `binned_statistic_2d`)
  459. # `Dlen` is the length of elements along each dimension.
  460. # This code is based on np.histogramdd
  461. try:
  462. # `sample` is an ND-array.
  463. Dlen, Ndim = sample.shape
  464. except (AttributeError, ValueError):
  465. # `sample` is a sequence of 1D arrays.
  466. sample = np.atleast_2d(sample).T
  467. Dlen, Ndim = sample.shape
  468. # Store initial shape of `values` to preserve it in the output
  469. values = np.asarray(values)
  470. input_shape = list(values.shape)
  471. # Make sure that `values` is 2D to iterate over rows
  472. values = np.atleast_2d(values)
  473. Vdim, Vlen = values.shape
  474. # Make sure `values` match `sample`
  475. if statistic != 'count' and Vlen != Dlen:
  476. raise AttributeError('The number of `values` elements must match the '
  477. 'length of each `sample` dimension.')
  478. try:
  479. M = len(bins)
  480. if M != Ndim:
  481. raise AttributeError('The dimension of bins must be equal '
  482. 'to the dimension of the sample x.')
  483. except TypeError:
  484. bins = Ndim * [bins]
  485. if binned_statistic_result is None:
  486. nbin, edges, dedges = _bin_edges(sample, bins, range)
  487. binnumbers = _bin_numbers(sample, nbin, edges, dedges)
  488. else:
  489. edges = binned_statistic_result.bin_edges
  490. nbin = np.array([len(edges[i]) + 1 for i in builtins.range(Ndim)])
  491. # +1 for outlier bins
  492. dedges = [np.diff(edges[i]) for i in builtins.range(Ndim)]
  493. binnumbers = binned_statistic_result.binnumber
  494. # Avoid overflow with double precision. Complex `values` -> `complex128`.
  495. result_type = np.result_type(values, np.float64)
  496. result = np.empty([Vdim, nbin.prod()], dtype=result_type)
  497. if statistic in {'mean', np.mean}:
  498. result.fill(np.nan)
  499. flatcount = _bincount(binnumbers, None)
  500. a = flatcount.nonzero()
  501. for vv in builtins.range(Vdim):
  502. flatsum = _bincount(binnumbers, values[vv])
  503. result[vv, a] = flatsum[a] / flatcount[a]
  504. elif statistic in {'std', np.std}:
  505. result.fill(np.nan)
  506. flatcount = _bincount(binnumbers, None)
  507. a = flatcount.nonzero()
  508. for vv in builtins.range(Vdim):
  509. flatsum = _bincount(binnumbers, values[vv])
  510. delta = values[vv] - flatsum[binnumbers] / flatcount[binnumbers]
  511. std = np.sqrt(
  512. _bincount(binnumbers, delta*np.conj(delta))[a] / flatcount[a]
  513. )
  514. result[vv, a] = std
  515. result = np.real(result)
  516. elif statistic == 'count':
  517. result = np.empty([Vdim, nbin.prod()], dtype=np.float64)
  518. result.fill(0)
  519. flatcount = _bincount(binnumbers, None)
  520. a = np.arange(len(flatcount))
  521. result[:, a] = flatcount[np.newaxis, :]
  522. elif statistic in {'sum', np.sum}:
  523. result.fill(0)
  524. for vv in builtins.range(Vdim):
  525. flatsum = _bincount(binnumbers, values[vv])
  526. a = np.arange(len(flatsum))
  527. result[vv, a] = flatsum
  528. elif statistic in {'median', np.median}:
  529. result.fill(np.nan)
  530. for vv in builtins.range(Vdim):
  531. i = np.lexsort((values[vv], binnumbers))
  532. _, j, counts = np.unique(binnumbers[i],
  533. return_index=True, return_counts=True)
  534. mid = j + (counts - 1) / 2
  535. mid_a = values[vv, i][np.floor(mid).astype(int)]
  536. mid_b = values[vv, i][np.ceil(mid).astype(int)]
  537. medians = (mid_a + mid_b) / 2
  538. result[vv, binnumbers[i][j]] = medians
  539. elif statistic in {'min', np.min}:
  540. result.fill(np.nan)
  541. for vv in builtins.range(Vdim):
  542. i = np.argsort(values[vv])[::-1] # Reversed so the min is last
  543. result[vv, binnumbers[i]] = values[vv, i]
  544. elif statistic in {'max', np.max}:
  545. result.fill(np.nan)
  546. for vv in builtins.range(Vdim):
  547. i = np.argsort(values[vv])
  548. result[vv, binnumbers[i]] = values[vv, i]
  549. elif callable(statistic):
  550. with np.errstate(invalid='ignore'), catch_warnings():
  551. simplefilter("ignore", RuntimeWarning)
  552. try:
  553. null = statistic([])
  554. except Exception:
  555. null = np.nan
  556. if np.iscomplexobj(null):
  557. result = result.astype(np.complex128)
  558. result.fill(null)
  559. try:
  560. _calc_binned_statistic(
  561. Vdim, binnumbers, result, values, statistic
  562. )
  563. except ValueError:
  564. result = result.astype(np.complex128)
  565. _calc_binned_statistic(
  566. Vdim, binnumbers, result, values, statistic
  567. )
  568. # Shape into a proper matrix
  569. result = result.reshape(np.append(Vdim, nbin))
  570. # Remove outliers (indices 0 and -1 for each bin-dimension).
  571. core = tuple([slice(None)] + Ndim * [slice(1, -1)])
  572. result = result[core]
  573. # Unravel binnumbers into an ndarray, each row the bins for each dimension
  574. if expand_binnumbers and Ndim > 1:
  575. binnumbers = np.asarray(np.unravel_index(binnumbers, nbin))
  576. if np.any(result.shape[1:] != nbin - 2):
  577. raise RuntimeError('Internal Shape Error')
  578. # Reshape to have output (`result`) match input (`values`) shape
  579. result = result.reshape(input_shape[:-1] + list(nbin-2))
  580. return BinnedStatisticddResult(result, edges, binnumbers)
  581. def _calc_binned_statistic(Vdim, bin_numbers, result, values, stat_func):
  582. unique_bin_numbers = np.unique(bin_numbers)
  583. for vv in builtins.range(Vdim):
  584. bin_map = _create_binned_data(bin_numbers, unique_bin_numbers,
  585. values, vv)
  586. for i in unique_bin_numbers:
  587. stat = stat_func(np.array(bin_map[i]))
  588. if np.iscomplexobj(stat) and not np.iscomplexobj(result):
  589. raise ValueError("The statistic function returns complex ")
  590. result[vv, i] = stat
  591. def _create_binned_data(bin_numbers, unique_bin_numbers, values, vv):
  592. """ Create hashmap of bin ids to values in bins
  593. key: bin number
  594. value: list of binned data
  595. """
  596. bin_map = dict()
  597. for i in unique_bin_numbers:
  598. bin_map[i] = []
  599. for i in builtins.range(len(bin_numbers)):
  600. bin_map[bin_numbers[i]].append(values[vv, i])
  601. return bin_map
  602. def _bin_edges(sample, bins=None, range=None):
  603. """ Create edge arrays
  604. """
  605. Dlen, Ndim = sample.shape
  606. nbin = np.empty(Ndim, int) # Number of bins in each dimension
  607. edges = Ndim * [None] # Bin edges for each dim (will be 2D array)
  608. dedges = Ndim * [None] # Spacing between edges (will be 2D array)
  609. # Select range for each dimension
  610. # Used only if number of bins is given.
  611. if range is None:
  612. smin = np.atleast_1d(np.array(sample.min(axis=0), float))
  613. smax = np.atleast_1d(np.array(sample.max(axis=0), float))
  614. else:
  615. if len(range) != Ndim:
  616. raise ValueError(
  617. f"range given for {len(range)} dimensions; {Ndim} required")
  618. smin = np.empty(Ndim)
  619. smax = np.empty(Ndim)
  620. for i in builtins.range(Ndim):
  621. if range[i][1] < range[i][0]:
  622. raise ValueError(
  623. f"In {f'dimension {i + 1} of ' if Ndim > 1 else ''}range,"
  624. " start must be <= stop")
  625. smin[i], smax[i] = range[i]
  626. # Make sure the bins have a finite width.
  627. for i in builtins.range(len(smin)):
  628. if smin[i] == smax[i]:
  629. smin[i] = smin[i] - .5
  630. smax[i] = smax[i] + .5
  631. # Preserve sample floating point precision in bin edges
  632. edges_dtype = (sample.dtype if np.issubdtype(sample.dtype, np.floating)
  633. else float)
  634. # Create edge arrays
  635. for i in builtins.range(Ndim):
  636. if np.isscalar(bins[i]):
  637. nbin[i] = bins[i] + 2 # +2 for outlier bins
  638. edges[i] = np.linspace(smin[i], smax[i], nbin[i] - 1,
  639. dtype=edges_dtype)
  640. else:
  641. edges[i] = np.asarray(bins[i], edges_dtype)
  642. nbin[i] = len(edges[i]) + 1 # +1 for outlier bins
  643. dedges[i] = np.diff(edges[i])
  644. nbin = np.asarray(nbin)
  645. return nbin, edges, dedges
  646. def _bin_numbers(sample, nbin, edges, dedges):
  647. """Compute the bin number each sample falls into, in each dimension
  648. """
  649. Dlen, Ndim = sample.shape
  650. sampBin = [
  651. np.digitize(sample[:, i], edges[i])
  652. for i in range(Ndim)
  653. ]
  654. # Using `digitize`, values that fall on an edge are put in the right bin.
  655. # For the rightmost bin, we want values equal to the right
  656. # edge to be counted in the last bin, and not as an outlier.
  657. for i in range(Ndim):
  658. # Find the rounding precision
  659. dedges_min = dedges[i].min()
  660. if dedges_min == 0:
  661. raise ValueError('The smallest edge difference is numerically 0.')
  662. decimal = int(-np.log10(dedges_min)) + 6
  663. # Find which points are on the rightmost edge.
  664. on_edge = np.where((sample[:, i] >= edges[i][-1]) &
  665. (np.around(sample[:, i], decimal) ==
  666. np.around(edges[i][-1], decimal)))[0]
  667. # Shift these points one bin to the left.
  668. sampBin[i][on_edge] -= 1
  669. # Compute the sample indices in the flattened statistic matrix.
  670. binnumbers = np.ravel_multi_index(sampBin, nbin)
  671. return binnumbers