MCPcopy
hub / github.com/pydata/xarray / expected_weighted

Function expected_weighted

xarray/tests/test_weighted.py:525–561  ·  view source on GitHub ↗

Generate expected result using ``*`` and ``sum``. This is checked against the result of da.weighted which uses ``dot``

(da, weights, dim, skipna, operation)

Source from the content-addressed store, hash-verified

523
524
525def expected_weighted(da, weights, dim, skipna, operation):
526 """
527 Generate expected result using ``*`` and ``sum``. This is checked against
528 the result of da.weighted which uses ``dot``
529 """
530
531 weighted_sum = (da * weights).sum(dim=dim, skipna=skipna)
532
533 if operation == "sum":
534 return weighted_sum
535
536 masked_weights = weights.where(da.notnull())
537 sum_of_weights = masked_weights.sum(dim=dim, skipna=True)
538 valid_weights = sum_of_weights != 0
539 sum_of_weights = sum_of_weights.where(valid_weights)
540
541 if operation == "sum_of_weights":
542 return sum_of_weights
543
544 weighted_mean = weighted_sum / sum_of_weights
545
546 if operation == "mean":
547 return weighted_mean
548
549 demeaned = da - weighted_mean
550 sum_of_squares = ((demeaned**2) * weights).sum(dim=dim, skipna=skipna)
551
552 if operation == "sum_of_squares":
553 return sum_of_squares
554
555 var = sum_of_squares / sum_of_weights
556
557 if operation == "var":
558 return var
559
560 if operation == "std":
561 return np.sqrt(var)
562
563
564def check_weighted_operations(data, weights, dim, skipna):

Callers 1

Calls 3

sumMethod · 0.45
whereMethod · 0.45
notnullMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…