Private function for the computation of covariance and correlation coefficients.
(x, y=None, rowvar=True, allow_masked=True)
| 1519 | |
| 1520 | |
| 1521 | def _covhelper(x, y=None, rowvar=True, allow_masked=True): |
| 1522 | """ |
| 1523 | Private function for the computation of covariance and correlation |
| 1524 | coefficients. |
| 1525 | |
| 1526 | """ |
| 1527 | x = ma.array(x, ndmin=2, copy=True, dtype=float) |
| 1528 | xmask = ma.getmaskarray(x) |
| 1529 | # Quick exit if we can't process masked data |
| 1530 | if not allow_masked and xmask.any(): |
| 1531 | raise ValueError("Cannot process masked data.") |
| 1532 | # |
| 1533 | if x.shape[0] == 1: |
| 1534 | rowvar = True |
| 1535 | # Make sure that rowvar is either 0 or 1 |
| 1536 | rowvar = int(bool(rowvar)) |
| 1537 | axis = 1 - rowvar |
| 1538 | if rowvar: |
| 1539 | tup = (slice(None), None) |
| 1540 | else: |
| 1541 | tup = (None, slice(None)) |
| 1542 | # |
| 1543 | if y is None: |
| 1544 | # Check if we can guarantee that the integers in the (N - ddof) |
| 1545 | # normalisation can be accurately represented with single-precision |
| 1546 | # before computing the dot product. |
| 1547 | if x.shape[0] > 2 ** 24 or x.shape[1] > 2 ** 24: |
| 1548 | xnm_dtype = np.float64 |
| 1549 | else: |
| 1550 | xnm_dtype = np.float32 |
| 1551 | xnotmask = np.logical_not(xmask).astype(xnm_dtype) |
| 1552 | else: |
| 1553 | y = array(y, copy=False, ndmin=2, dtype=float) |
| 1554 | ymask = ma.getmaskarray(y) |
| 1555 | if not allow_masked and ymask.any(): |
| 1556 | raise ValueError("Cannot process masked data.") |
| 1557 | if xmask.any() or ymask.any(): |
| 1558 | if y.shape == x.shape: |
| 1559 | # Define some common mask |
| 1560 | common_mask = np.logical_or(xmask, ymask) |
| 1561 | if common_mask is not nomask: |
| 1562 | xmask = x._mask = y._mask = ymask = common_mask |
| 1563 | x._sharedmask = False |
| 1564 | y._sharedmask = False |
| 1565 | x = ma.concatenate((x, y), axis) |
| 1566 | # Check if we can guarantee that the integers in the (N - ddof) |
| 1567 | # normalisation can be accurately represented with single-precision |
| 1568 | # before computing the dot product. |
| 1569 | if x.shape[0] > 2 ** 24 or x.shape[1] > 2 ** 24: |
| 1570 | xnm_dtype = np.float64 |
| 1571 | else: |
| 1572 | xnm_dtype = np.float32 |
| 1573 | xnotmask = np.logical_not(np.concatenate((xmask, ymask), axis)).astype( |
| 1574 | xnm_dtype |
| 1575 | ) |
| 1576 | x -= x.mean(axis=rowvar)[tup] |
| 1577 | return (x, xnotmask, rowvar) |
| 1578 |
searching dependent graphs…