@slow_test
def test_permutation_connectivity_equiv():
    """Test cluster level permutations with and without connectivity
    """
    try:
        try:
            from sklearn.feature_extraction.image import grid_to_graph
        except __HOLE__:
            from scikits.learn.feature_extraction.image import grid_to_graph
    except ImportError:
        return
    rng = np.random.RandomState(0)
    # subjects, time points, spatial points
    n_time = 2
    n_space = 4
    X = rng.randn(6, n_time, n_space)
    # add some significant points
    X[:, :, 0:2] += 10  # span two time points and two spatial points
    X[:, 1, 3] += 20  # span one time point
    max_steps = [1, 1, 1, 2]
    # This will run full algorithm in two ways, then the ST-algorithm in 2 ways
    # All of these should give the same results
    conns = [None, grid_to_graph(n_time, n_space),
             grid_to_graph(1, n_space), grid_to_graph(1, n_space)]
    stat_map = None
    thresholds = [2, dict(start=1.5, step=1.0)]
    sig_counts = [2, 5]
    sdps = [0, 0.05, 0.05]
    ots = ['mask', 'mask', 'indices']
    stat_fun = partial(ttest_1samp_no_p, sigma=1e-3)
    for thresh, count in zip(thresholds, sig_counts):
        cs = None
        ps = None
        for max_step, conn in zip(max_steps, conns):
            for sdp, ot in zip(sdps, ots):
                t, clusters, p, H0 = \
                    permutation_cluster_1samp_test(
                        X, threshold=thresh, connectivity=conn, n_jobs=2,
                        max_step=max_step, stat_fun=stat_fun,
                        step_down_p=sdp, out_type=ot)
                # make sure our output datatype is correct
                if ot == 'mask':
                    assert_true(isinstance(clusters[0], np.ndarray))
                    assert_true(clusters[0].dtype == bool)
                    assert_array_equal(clusters[0].shape, X.shape[1:])
                else:  # ot == 'indices'
                    assert_true(isinstance(clusters[0], tuple))

                # make sure all comparisons were done; for TFCE, no perm
                # should come up empty
                if count == 8:
                    assert_true(not np.any(H0 == 0))
                inds = np.where(p < 0.05)[0]
                assert_true(len(inds) == count)
                this_cs = [clusters[ii] for ii in inds]
                this_ps = p[inds]
                this_stat_map = np.zeros((n_time, n_space), dtype=bool)
                for ci, c in enumerate(this_cs):
                    if isinstance(c, tuple):
                        this_c = np.zeros((n_time, n_space), bool)
                        for x, y in zip(c[0], c[1]):
                            this_stat_map[x, y] = True
                            this_c[x, y] = True
                        this_cs[ci] = this_c
                        c = this_c
                    this_stat_map[c] = True
                if cs is None:
                    ps = this_ps
                    cs = this_cs
                if stat_map is None:
                    stat_map = this_stat_map
                assert_array_equal(ps, this_ps)
                assert_true(len(cs) == len(this_cs))
                for c1, c2 in zip(cs, this_cs):
                    assert_array_equal(c1, c2)
                assert_array_equal(stat_map, this_stat_map)
dataset/ETHPy150Open mne-tools/mne-python/mne/stats/tests/test_cluster_level.py/test_permutation_connectivity_equiv