(
pairs,
order=2,
ddof=0,
dtype="f8",
sum=np.sum,
axis=None,
computing_meta=False,
**kwargs,
)
| 509 | |
| 510 | |
| 511 | def moment_agg( |
| 512 | pairs, |
| 513 | order=2, |
| 514 | ddof=0, |
| 515 | dtype="f8", |
| 516 | sum=np.sum, |
| 517 | axis=None, |
| 518 | computing_meta=False, |
| 519 | **kwargs, |
| 520 | ): |
| 521 | if not isinstance(pairs, list): |
| 522 | pairs = [pairs] |
| 523 | |
| 524 | kwargs["dtype"] = dtype |
| 525 | # To properly handle ndarrays, the original dimensions need to be kept for |
| 526 | # part of the calculation. |
| 527 | keepdim_kw = kwargs.copy() |
| 528 | keepdim_kw["keepdims"] = True |
| 529 | keepdim_kw["dtype"] = None |
| 530 | |
| 531 | ns = deepmap(lambda pair: pair["n"], pairs) if not computing_meta else pairs |
| 532 | ns = _concatenate2(ns, axes=axis) |
| 533 | n = ns.sum(axis=axis, **keepdim_kw) |
| 534 | |
| 535 | if computing_meta: |
| 536 | return n |
| 537 | |
| 538 | totals = _concatenate2(deepmap(lambda pair: pair["total"], pairs), axes=axis) |
| 539 | Ms = _concatenate2(deepmap(lambda pair: pair["M"], pairs), axes=axis) |
| 540 | |
| 541 | mu = divide(totals.sum(axis=axis, **keepdim_kw), n) |
| 542 | |
| 543 | with np.errstate(divide="ignore", invalid="ignore"): |
| 544 | if np.issubdtype(totals.dtype, np.complexfloating): |
| 545 | inner_term = np.abs(divide(totals, ns) - mu) |
| 546 | else: |
| 547 | inner_term = divide(totals, ns, dtype=dtype) - mu |
| 548 | inner_term = np.where(ns == 0, 0, inner_term) |
| 549 | M = _moment_helper(Ms, ns, inner_term, order, sum, axis, kwargs) |
| 550 | |
| 551 | denominator = n.sum(axis=axis, **kwargs) - ddof |
| 552 | |
| 553 | # taking care of the edge case with empty or all-nans array with ddof > 0 |
| 554 | if isinstance(denominator, Number): |
| 555 | if denominator < 0: |
| 556 | denominator = np.nan |
| 557 | elif denominator is not np.ma.masked: |
| 558 | denominator[denominator < 0] = np.nan |
| 559 | |
| 560 | return divide(M, denominator, dtype=dtype) |
| 561 | |
| 562 | |
| 563 | def moment( |
no test coverage detected
searching dependent graphs…