MCPcopy Create free account
hub / github.com/dask/dask / moment_agg

Function moment_agg

dask/array/reductions.py:508–557  ·  view source on GitHub ↗
(
    pairs,
    order=2,
    ddof=0,
    dtype="f8",
    sum=np.sum,
    axis=None,
    computing_meta=False,
    **kwargs,
)

Source from the content-addressed store, hash-verified

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

Callers 2

reduction_aggregateMethod · 0.90
reduction_aggregateMethod · 0.90

Calls 7

deepmapFunction · 0.90
_concatenate2Function · 0.90
_moment_helperFunction · 0.85
divideFunction · 0.70
copyMethod · 0.45
sumMethod · 0.45
absMethod · 0.45

Tested by

no test coverage detected