Example #1
0
    def _svd(self, left, right):
        """
        Singular Value Decomposition of left and right field.

        **Arguments:**
        *array*
        left field `xarray.DataArray`;
        right field `xarray.DataArray`;

        **Return:**
        *array*
           U: collumns are sigular vectors of the left field;
           V: collumns are sigular vectors of the right field;
           s: sigular values of covariance matirx of left and right field.

        """

        Cxy = xr.cov(left, right, dim='time')
        U, s, V = np.linalg.svd(Cxy, full_matrices=False)
        # U column is eigenvectors for left, V column is for right
        V = V.T

        self._U = U
        self._V = V
        self._s = s
Example #2
0
def test_autocov(da_a, dim):
    # Testing that the autocovariance*(N-1) is ~=~ to the variance matrix
    # 1. Ignore the nans
    valid_values = da_a.notnull()
    da_a = da_a.where(valid_values)
    expected = ((da_a - da_a.mean(dim=dim))**2).sum(dim=dim, skipna=False)
    actual = xr.cov(da_a, da_a, dim=dim) * (valid_values.sum(dim) - 1)
    assert_allclose(actual, expected)
Example #3
0
def test_cov(da_a, da_b, dim, ddof):
    if dim is not None:

        def np_cov_ind(ts1, ts2, a, x):
            # Ensure the ts are aligned and missing values ignored
            ts1, ts2 = broadcast(ts1, ts2)
            valid_values = ts1.notnull() & ts2.notnull()

            # While dropping isn't ideal here, numpy will return nan
            # if any segment contains a NaN.
            ts1 = ts1.where(valid_values)
            ts2 = ts2.where(valid_values)

            return np.ma.cov(
                np.ma.masked_invalid(ts1.sel(a=a, x=x).data.flatten()),
                np.ma.masked_invalid(ts2.sel(a=a, x=x).data.flatten()),
                ddof=ddof,
            )[0, 1]

        expected = np.zeros((3, 4))
        for a in [0, 1, 2]:
            for x in [0, 1, 2, 3]:
                expected[a, x] = np_cov_ind(da_a, da_b, a=a, x=x)
        actual = xr.cov(da_a, da_b, dim=dim, ddof=ddof)
        assert_allclose(actual, expected)

    else:

        def np_cov(ts1, ts2):
            # Ensure the ts are aligned and missing values ignored
            ts1, ts2 = broadcast(ts1, ts2)
            valid_values = ts1.notnull() & ts2.notnull()

            ts1 = ts1.where(valid_values)
            ts2 = ts2.where(valid_values)

            return np.ma.cov(
                np.ma.masked_invalid(ts1.data.flatten()),
                np.ma.masked_invalid(ts2.data.flatten()),
                ddof=ddof,
            )[0, 1]

        expected = np_cov(da_a, da_b)
        actual = xr.cov(da_a, da_b, dim=dim, ddof=ddof)
        assert_allclose(actual, expected)
Example #4
0
def test_autocov(da_a, dim):
    # Testing that the autocovariance*(N-1) is ~=~ to the variance matrix
    # 1. Ignore the nans
    valid_values = da_a.notnull()
    # Because we're using ddof=1, this requires > 1 value in each sample
    da_a = da_a.where(valid_values.sum(dim=dim) > 1)
    expected = ((da_a - da_a.mean(dim=dim)) ** 2).sum(dim=dim, skipna=True, min_count=1)
    actual = xr.cov(da_a, da_a, dim=dim) * (valid_values.sum(dim) - 1)
    assert_allclose(actual, expected)
Example #5
0
def test_cov(da_a, da_b, dim, ddof):
    if dim is not None:

        def np_cov_ind(ts1, ts2, a, x):
            # Ensure the ts are aligned and missing values ignored
            ts1, ts2 = broadcast(ts1, ts2)
            valid_values = ts1.notnull() & ts2.notnull()

            ts1 = ts1.where(valid_values)
            ts2 = ts2.where(valid_values)

            return np.cov(
                ts1.sel(a=a, x=x).data.flatten(),
                ts2.sel(a=a, x=x).data.flatten(),
                ddof=ddof,
            )[0, 1]

        expected = np.zeros((3, 4))
        for a in [0, 1, 2]:
            for x in [0, 1, 2, 3]:
                expected[a, x] = np_cov_ind(da_a, da_b, a=a, x=x)
        actual = xr.cov(da_a, da_b, dim=dim, ddof=ddof)
        assert_allclose(actual, expected)

    else:

        def np_cov(ts1, ts2):
            # Ensure the ts are aligned and missing values ignored
            ts1, ts2 = broadcast(ts1, ts2)
            valid_values = ts1.notnull() & ts2.notnull()

            ts1 = ts1.where(valid_values)
            ts2 = ts2.where(valid_values)

            return np.cov(ts1.data.flatten(), ts2.data.flatten(), ddof=ddof)[0,
                                                                             1]

        expected = np_cov(da_a, da_b)
        actual = xr.cov(da_a, da_b, dim=dim, ddof=ddof)
        assert_allclose(actual, expected)
Example #6
0
def test_covcorr_consistency(da_a, da_b, dim):
    # Testing that xr.corr and xr.cov are consistent with each other
    # 1. Broadcast the two arrays
    da_a, da_b = broadcast(da_a, da_b)
    # 2. Ignore the nans
    valid_values = da_a.notnull() & da_b.notnull()
    da_a = da_a.where(valid_values)
    da_b = da_b.where(valid_values)

    expected = xr.cov(da_a, da_b, dim=dim,
                      ddof=0) / (da_a.std(dim=dim) * da_b.std(dim=dim))
    actual = xr.corr(da_a, da_b, dim=dim)
    assert_allclose(actual, expected)
plt.title('Correlation between dCO2 and windspeed',fontsize=16)




plt.tight_layout()
plt.show()


import sys
sys.exit()

# %%
#pCO22
covariance=xr.cov(co2,pco2_intrp,dim='time')
corr=xr.corr(co2,pco2_intrp,dim='time')
#corr=xr.corr(co2,newprod,dim='time')

fig=plt.figure(figsize=(20,12))
ax1=fig.add_subplot(3,2,1)
m=plot_basemap()
lo,la=np.meshgrid(newprod.lon.values,newprod.lat.values)
lo1,la1=m(lo,la)

span=max(abs(covariance.min()),covariance.max()).round(3)
m.contourf(lo1,la1,covariance,cmap='bwr',levels=np.arange(-span,span+(span/20),span/20))#,levels=np.arange(-0.001,0.0012,0.0002))
#m.contourf(lo1,la1,covariance,cmap='bwr',levels=np.arange(-0.0015,0.0016,0.0001),extend='max')#span+(span/20),span/20))#,levels=np.arange(-0.001,0.0012,0.0002))

plt.colorbar()
plt.title('Covariance between pCO2 and new production',fontsize=16)