Ejemplo n.º 1
0
def test_linear_trend():
    # TODO implement a test for nans
    data = dsa.from_array(np.random.random([10, 2, 2]), chunks=(10, 1, 1))
    t = range(10)
    x = range(2)
    y = range(2)
    data_da = xr.DataArray(
        data,
        dims=["time", "x", "y"],
        coords={
            "time": ("time", t),
            "x": ("x", x),
            "y": ("y", y)
        },
    )

    fit_da = linear_trend(data_da, "time")

    for xi in x:
        for yi in y:
            x_fit = t
            y_fit = data[:, xi, yi]
            fit = np.array(stats.linregress(x_fit, y_fit))
            test = np.array([
                fit_da.sel(x=xi, y=yi)[param].data for param in
                ["slope", "intercept", "r_value", "p_value", "std_err"]
            ])
            assert np.allclose(fit, test)

    # Test with other timedim (previously was not caught)
    data = dsa.from_array(np.random.random([2, 10, 2]), chunks=(1, 10, 1))
    data_da = xr.DataArray(
        data,
        dims=["x", "time", "y"],
        coords={
            "x": ("x", x),
            "time": ("time", t),
            "y": ("y", y)
        },
    )

    fit_da = linear_trend(data_da, "time")

    for xi in x:
        for yi in y:
            x_fit = t
            y_fit = data[xi, :, yi]
            fit = np.array(stats.linregress(x_fit, y_fit))
            test = test = np.array([
                fit_da.sel(x=xi, y=yi)[param].data for param in
                ["slope", "intercept", "r_value", "p_value", "std_err"]
            ])
            assert np.allclose(fit, test)
Ejemplo n.º 2
0
def slope(da):
    """returns slope per century"""
    assert len(da.time) < 300  # make sure the data is annual
    assert len(
        da.time
    ) > 95  # make sure this covers most of the century allows for small adjustments
    reg = linear_trend(da.sel(time=trend_slice), 'time')
    return reg.slope * 100, reg.p_value
Ejemplo n.º 3
0
def test_linear_trend():
    #TODO implement a test for nans
    data = dsa.from_array(np.random.random([10, 2, 2]), chunks=(10, 1, 1))
    t = range(10)
    x = range(2)
    y = range(2)
    data_da = xr.DataArray(data,
                           dims=['time', 'x', 'y'],
                           coords={
                               'time': ('time', t),
                               'x': ('x', x),
                               'y': ('y', y)
                           })

    fit_da = linear_trend(data_da, 'time')

    for xi in x:
        for yi in y:
            x_fit = t
            y_fit = data[:, xi, yi]
            fit = np.array(stats.linregress(x_fit, y_fit))
            test = fit_da.sel(x=xi, y=yi).data
            assert np.allclose(fit, test)

    # Test with other timedim (previously was not caught)
    data = dsa.from_array(np.random.random([2, 10, 2]), chunks=(1, 10, 1))
    data_da = xr.DataArray(data,
                           dims=['x', 'time', 'y'],
                           coords={
                               'x': ('x', x),
                               'time': ('time', t),
                               'y': ('y', y)
                           })

    fit_da = linear_trend(data_da, 'time')

    for xi in x:
        for yi in y:
            x_fit = t
            y_fit = data[xi, :, yi]
            fit = np.array(stats.linregress(x_fit, y_fit))
            assert np.allclose(fit, fit_da.sel(x=xi, y=yi).data)