Beispiel #1
0
def test_nPerFtr2n():

    da = xr.DataArray(1.5 * np.ones((2, 3, 4)),
                      dims=('px', 'r', 'Sigma_id'),
                      coords=dict(px=[1, 2]))
    py = xr.DataArray(2 * np.ones((2, 3, 4)),
                      dims=('px', 'r', 'Sigma_id'),
                      coords=dict(px=[1, 2])).astype(int)

    assert_raises(NotImplementedError, nPerFtr2n, da.sel(px=1), py)

    n_req = nPerFtr2n(da, py)
    assert n_req.name == 'n_required'
    assert (n_req.dims == np.array(['ptot', 'r', 'Sigma_id'])).all()

    assert np.allclose(n_req.sel(ptot=3).values, 3 * 1.5)
    assert np.allclose(n_req.sel(ptot=4).values, 4 * 1.5)

    ###

    da['py'] = py
    n_req_2 = nPerFtr2n(
        da
    )  # n_req_2 should be identical to n_req except that it has an additional attribute py
    del n_req_2['py']
    assert_xr_allclose(n_req, n_req_2)
Beispiel #2
0
def test_scores_pairwise_spearmansim_stats():
    np.random.seed(0)
    scores = np.array([[.1, .2], [1, 2]])
    ds = xr.Dataset(dict(
        x_test_scores=xr.DataArray(scores, dims=('rep', 'test_sample')),
        y_test_scores=xr.DataArray(scores, dims=('rep', 'test_sample')),
    ))
    scores_pairwise_spearmansim_stats(ds, qs=(.025, .5, .975))
    stats_true = xr.DataArray(np.ones(4), dims=('stat',), coords=dict(stat=['q2.5%', 'q50.0%', 'q97.5%', 'mean']))
    assert_xr_allclose(ds.x_scores_pairwise_spearmansim_stats, stats_true)
    assert_xr_allclose(ds.y_scores_pairwise_spearmansim_stats, stats_true)
Beispiel #3
0
def test_weights_pairwise_cossim_stats():
    np.random.seed(0)
    weights = np.linalg.qr(np.random.normal(size=(3, 3)))[0]
    ds = xr.Dataset(dict(
        x_weights=xr.DataArray(weights, dims=('rep', 'x_feature')),
        y_weights=xr.DataArray(weights, dims=('rep', 'y_feature')),
    ))
    weights_pairwise_cossim_stats(ds, qs=(.025, .5, .975))
    stats_true = xr.DataArray(np.zeros(4), dims=('stat',), coords=dict(stat=['q2.5%', 'q50.0%', 'q97.5%', 'mean']))
    assert_xr_allclose(ds.x_weights_pairwise_cossim_stats, stats_true)
    assert_xr_allclose(ds.y_weights_pairwise_cossim_stats, stats_true)
Beispiel #4
0
def test_power():
    ds = xr.Dataset(dict(
        between_assocs=xr.DataArray(np.linspace(0, 1, 10).reshape(1, -1), dims=('dummy', 'rep')),
        between_assocs_perm=xr.DataArray(.05 * np.ones((1, 10, 100)), dims=('dummy', 'rep', 'perm')),
    ))

    alpha = 0.05
    power(ds, alpha=alpha)

    p_values = np.array([1] + [.01]*9)
    true_power = xr.DataArray(
        np.array([(p_values < alpha).mean()]),
        dims=('dummy',)
    )

    assert_xr_allclose(ds.power, true_power)
Beispiel #5
0
def assert_qty_allclose(a, b, check_attrs=True, **kwargs):
    from xarray import DataArray
    from xarray.testing import assert_allclose as assert_xr_allclose

    from .reporting.utils import Quantity, AttrSeries

    a = Quantity(a)
    b = Quantity(b)

    # check type-specific allclose
    if Quantity is AttrSeries:
        # we may
        assert_series_equal(a, b, **kwargs)
    elif Quantity is DataArray:
        assert_xr_allclose(a, b, **kwargs)

    # check attributes are equal
    if check_attrs:
        assert a.attrs == b.attrs
Beispiel #6
0
def assert_qty_allclose(a, b, check_attrs=True, **kwargs):
    """Assert that Quantity objects *a* and *b* have numerically close values.

    When Quantity is AttrSeries, *a* and *b* are first passed through
    :meth:`as_quantity`.
    """
    from xarray import DataArray
    from xarray.testing import assert_allclose as assert_xr_allclose

    from .reporting.quantity import AttrSeries, Quantity, as_quantity

    if Quantity is AttrSeries:
        # Convert pd.Series automatically
        a = as_quantity(a) if isinstance(a, (pd.Series, DataArray)) else a
        b = as_quantity(b) if isinstance(b, (pd.Series, DataArray)) else b

        assert_series_equal(a, b, **kwargs)
    elif Quantity is DataArray:  # pragma: no cover
        kwargs.pop('check_dtype', None)
        assert_xr_allclose(a, b, **kwargs)

    # check attributes are equal
    if check_attrs:
        assert a.attrs == b.attrs
Beispiel #7
0
def test_weights_pc_cossim(MockPCA):

    np.random.seed(0)
    X = np.random.normal(size=(10000, 2))
    Y = X
    weights = xr.DataArray([
        [1, 0],
        [1. / np.sqrt(2), 1. / np.sqrt(2)],
        [3 / 5, 4 / 5],
    ],
                           dims=('mode', 'x_feature')).T
    results = xr.Dataset(
        dict(x_weights=weights,
             y_weights=weights.rename(x_feature='y_feature')))

    weights_pc_cossim(None, X, Y, None, None, None, None, results)

    target_da = xr.DataArray(
        [[1, 1. / np.sqrt(2), 3. / 5], [0, 1. / np.sqrt(2), 4. / 5]],
        dims=('x_feature', 'mode'))

    assert_xr_allclose(results.x_weights_pc_cossim, target_da)
    assert_xr_allclose(results.y_weights_pc_cossim,
                       target_da.rename(x_feature='y_feature'))
Beispiel #8
0
def test_calc_max_n_required():
    assert_raises(ValueError, calc_max_n_required)
    a = xr.DataArray(np.arange(3), dims=('dummy', ))
    b = xr.DataArray(np.arange(3)[::-1], dims=('dummy', ))
    mx_n_req = calc_max_n_required(2 * a, 2 * b, a, b)
    assert_xr_allclose(mx_n_req, xr.DataArray([4, 2, 4], dims=('dummy', )))
Beispiel #9
0
def test_analyze_dataset():
    def addon_test(estr, X, Y, Xorig, Yorig, x_align_ref, y_align_ref, results,
                   **kwargs):
        results['addon_var'] = 3. / 8

    estr = SVDPLS()
    X = np.arange(10).reshape(5, 2)
    Y = X

    result = gemmr.sample_analysis.analyze_dataset(estr,
                                                   X,
                                                   Y,
                                                   addons=[addon_test])

    assert np.isclose(result.between_corrs_sample, 1.)
    assert np.isclose(result.addon_var, 3. / 8)

    estr.fit(X, Y)
    assert np.isclose(estr.assocs_[0], result.between_assocs)

    assert np.isclose(
        np.cov(estr.x_scores_[:, 0], estr.y_scores_[:, 0])[0, 1],
        result.between_covs_sample)

    tgt_weights = xr.DataArray([[1. / np.sqrt(2)] * 2],
                               dims=('mode', 'x_feature'),
                               coords=dict(x_feature=np.arange(2)),
                               name='x_weights').T
    tgt_loadings = xr.DataArray(np.ones((1, 2), dtype=float),
                                dims=('mode', 'x_orig_feature'),
                                coords=dict(x_orig_feature=np.arange(2)),
                                name='x_loadings').T
    assert_xr_allclose(result.x_weights, tgt_weights)
    assert_xr_allclose(result.x_loadings, tgt_loadings)

    tgt_weights = tgt_weights.rename('y_weights').rename(x_feature='y_feature')
    tgt_loadings = tgt_loadings.rename('y_loadings').rename(
        x_orig_feature='y_orig_feature')

    assert_xr_allclose(result.y_weights, tgt_weights)
    assert_xr_allclose(result.y_loadings, tgt_loadings)

    ###

    assert np.all(result.x_weights.values > 0)
    assert np.all(result.y_weights.values > 0)
    # i.e. if we now use rerun analyze_dataset with ``?_align_ref`` the weights should be negative

    result = gemmr.sample_analysis.analyze_dataset(estr,
                                                   X,
                                                   Y,
                                                   x_align_ref=-np.eye(2),
                                                   y_align_ref=-np.eye(2))

    ###

    class MockEstr():
        def fit(self, X, Y):
            raise ValueError()

    mock_estr = MockEstr()
    result = gemmr.sample_analysis.analyze_dataset(mock_estr,
                                                   X,
                                                   Y,
                                                   addons=[addon_test])

    da_nan = xr.DataArray(np.nan * np.empty((2, 1)),
                          dims=('x_feature', 'mode'),
                          coords=dict(x_feature=np.arange(2)))
    target_result = xr.Dataset(
        dict(between_assocs=np.nan,
             between_covs_sample=np.nan,
             between_corrs_sample=np.nan,
             addon_var=3. / 8,
             x_weights=da_nan,
             y_weights=da_nan.rename(x_feature='y_feature'),
             x_loadings=da_nan.rename(x_feature='x_orig_feature'),
             y_loadings=da_nan.rename(x_feature='y_orig_feature')))
    assert_xr_equal(result, target_result)