Validate weights array. We assume, weights is not None.
(weights, a, axis)
| 418 | |
| 419 | |
| 420 | def _weights_are_valid(weights, a, axis): |
| 421 | """Validate weights array. |
| 422 | |
| 423 | We assume, weights is not None. |
| 424 | """ |
| 425 | wgt = np.asanyarray(weights) |
| 426 | |
| 427 | # Sanity checks |
| 428 | if a.shape != wgt.shape: |
| 429 | if axis is None: |
| 430 | raise TypeError( |
| 431 | "Axis must be specified when shapes of a and weights " |
| 432 | "differ.") |
| 433 | if wgt.shape != tuple(a.shape[ax] for ax in axis): |
| 434 | raise ValueError( |
| 435 | "Shape of weights must be consistent with " |
| 436 | "shape of a along specified axis.") |
| 437 | |
| 438 | # setup wgt to broadcast along axis |
| 439 | wgt = wgt.transpose(np.argsort(axis)) |
| 440 | wgt = wgt.reshape(tuple((s if ax in axis else 1) |
| 441 | for ax, s in enumerate(a.shape))) |
| 442 | return wgt |
| 443 | |
| 444 | |
| 445 | def _average_dispatcher(a, axis=None, weights=None, returned=None, *, |
no test coverage detected
searching dependent graphs…