Computes the bins used internally by `histogram`. Parameters ========== a : ndarray Ravelled data array bins, range Forwarded arguments from `histogram`. weights : ndarray, optional Ravelled weights array, or None Returns ======= bin_edg
(a, bins, range, weights)
| 354 | |
| 355 | |
| 356 | def _get_bin_edges(a, bins, range, weights): |
| 357 | """ |
| 358 | Computes the bins used internally by `histogram`. |
| 359 | |
| 360 | Parameters |
| 361 | ========== |
| 362 | a : ndarray |
| 363 | Ravelled data array |
| 364 | bins, range |
| 365 | Forwarded arguments from `histogram`. |
| 366 | weights : ndarray, optional |
| 367 | Ravelled weights array, or None |
| 368 | |
| 369 | Returns |
| 370 | ======= |
| 371 | bin_edges : ndarray |
| 372 | Array of bin edges |
| 373 | uniform_bins : (Number, Number, int): |
| 374 | The upper bound, lowerbound, and number of bins, used in the optimized |
| 375 | implementation of `histogram` that works on uniform bins. |
| 376 | """ |
| 377 | # parse the overloaded bins argument |
| 378 | n_equal_bins = None |
| 379 | bin_edges = None |
| 380 | |
| 381 | if isinstance(bins, str): |
| 382 | bin_name = bins |
| 383 | # if `bins` is a string for an automatic method, |
| 384 | # this will replace it with the number of bins calculated |
| 385 | if bin_name not in _hist_bin_selectors: |
| 386 | raise ValueError( |
| 387 | f"{bin_name!r} is not a valid estimator for `bins`") |
| 388 | if weights is not None: |
| 389 | raise TypeError("Automated estimation of the number of " |
| 390 | "bins is not supported for weighted data") |
| 391 | |
| 392 | first_edge, last_edge = _get_outer_edges(a, range) |
| 393 | |
| 394 | # truncate the range if needed |
| 395 | if range is not None: |
| 396 | keep = (a >= first_edge) |
| 397 | keep &= (a <= last_edge) |
| 398 | if not np.logical_and.reduce(keep): |
| 399 | a = a[keep] |
| 400 | |
| 401 | if a.size == 0: |
| 402 | n_equal_bins = 1 |
| 403 | else: |
| 404 | # Do not call selectors on empty arrays |
| 405 | width = _hist_bin_selectors[bin_name](a, (first_edge, last_edge)) |
| 406 | if width: |
| 407 | if np.issubdtype(a.dtype, np.integer) and width < 1: |
| 408 | width = 1 |
| 409 | delta = _unsigned_subtract(last_edge, first_edge) |
| 410 | n_equal_bins = int(np.ceil(delta / width)) |
| 411 | else: |
| 412 | # Width can be zero for some estimators, e.g. FD when |
| 413 | # the IQR of the data is zero. |
no test coverage detected
searching dependent graphs…