def test_spatio_temporal_src_connectivity(): """Test spatio-temporal connectivity from source spaces""" tris = np.array([[0, 1, 2], [3, 4, 5]]) src = [dict(), dict()] connectivity = spatio_temporal_tris_connectivity(tris, 2) src[0]['use_tris'] = np.array([[0, 1, 2]]) src[1]['use_tris'] = np.array([[0, 1, 2]]) src[0]['vertno'] = np.array([0, 1, 2]) src[1]['vertno'] = np.array([0, 1, 2]) connectivity2 = spatio_temporal_src_connectivity(src, 2) assert_array_equal(connectivity.todense(), connectivity2.todense()) # add test for dist connectivity src[0]['dist'] = np.ones((3, 3)) - np.eye(3) src[1]['dist'] = np.ones((3, 3)) - np.eye(3) src[0]['vertno'] = [0, 1, 2] src[1]['vertno'] = [0, 1, 2] connectivity3 = spatio_temporal_src_connectivity(src, 2, dist=2) assert_array_equal(connectivity.todense(), connectivity3.todense()) # add test for source space connectivity with omitted vertices inverse_operator = read_inverse_operator(fname_inv) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') src_ = inverse_operator['src'] connectivity = spatio_temporal_src_connectivity(src_, n_times=2) assert len(w) == 1 a = connectivity.shape[0] / 2 b = sum([s['nuse'] for s in inverse_operator['src']]) assert_true(a == b) assert_equal(grade_to_tris(5).shape, [40960, 3])
def test_spatio_temporal_src_connectivity(): """Test spatio-temporal connectivity from source spaces.""" tris = np.array([[0, 1, 2], [3, 4, 5]]) src = [dict(), dict()] connectivity = spatio_temporal_tris_connectivity(tris, 2) src[0]['use_tris'] = np.array([[0, 1, 2]]) src[1]['use_tris'] = np.array([[0, 1, 2]]) src[0]['vertno'] = np.array([0, 1, 2]) src[1]['vertno'] = np.array([0, 1, 2]) src[0]['type'] = 'surf' src[1]['type'] = 'surf' connectivity2 = spatio_temporal_src_connectivity(src, 2) assert_array_equal(connectivity.todense(), connectivity2.todense()) # add test for dist connectivity src[0]['dist'] = np.ones((3, 3)) - np.eye(3) src[1]['dist'] = np.ones((3, 3)) - np.eye(3) src[0]['vertno'] = [0, 1, 2] src[1]['vertno'] = [0, 1, 2] src[0]['type'] = 'surf' src[1]['type'] = 'surf' connectivity3 = spatio_temporal_src_connectivity(src, 2, dist=2) assert_array_equal(connectivity.todense(), connectivity3.todense()) # add test for source space connectivity with omitted vertices inverse_operator = read_inverse_operator(fname_inv) src_ = inverse_operator['src'] with pytest.warns(RuntimeWarning, match='will have holes'): connectivity = spatio_temporal_src_connectivity(src_, n_times=2) a = connectivity.shape[0] / 2 b = sum([s['nuse'] for s in inverse_operator['src']]) assert (a == b) assert_equal(grade_to_tris(5).shape, [40960, 3])
def test_spatio_temporal_tris_connectivity(): """Test spatio-temporal connectivity from triangles""" tris = np.array([[0, 1, 2], [3, 4, 5]]) connectivity = spatio_temporal_tris_connectivity(tris, 2) x = [1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] components = stats.cluster_level._get_components(np.array(x), connectivity) # _get_components works differently now... old_fmt = [0, 0, -2, -2, -2, -2, 0, -2, -2, -2, -2, 1] new_fmt = np.array(old_fmt) new_fmt = [np.nonzero(new_fmt == v)[0] for v in np.unique(new_fmt[new_fmt >= 0])] assert_true(len(new_fmt), len(components)) for c, n in zip(components, new_fmt): assert_array_equal(c, n)
def perform_statistics_2(morphed_data, parameter_cache, vector, p_value=None): """Performs the statistical analysis using spatial_tris_connectivity. :param morphed_data: Morphed data obtained from morph_data function :param parameter_cache: Morphed parameter cache obtained from morph_data function. :param vector: Method to perform modelling ('sLORETA' etc.) :param p_value: Statistical p-value :return: clu, good_cluster_inds """ # Unpack parameter cache dictionary n_subjects = parameter_cache['n_subjects'] n_times = parameter_cache['n_times'] # Take on the absolute X = np.abs(morphed_data) # Obtain the paired contrast if vector is False: X = X[:, :, :, 0] - X[:, :, :, 1] # Dimension is (space, time, subjects) else: X = X[:, :, :, :, 0] - X[:, :, :, :, 1] # Dimension is (space, vector, time, subjects) print('Computing connectivity... ') connectivity_2 = mne.spatio_temporal_tris_connectivity( grade_to_tris(5), n_times) # Note that X needs to be a multi-dimensional array of shape [samples (subjects) x time x space] if vector is False: X = np.transpose(X, [2, 1, 0]) else: X = np.transpose(X, [3, 2, 1, 0]) ##### TO DOUBLE CHECK ##### # Perform the clustering p_threshold = p_value # 0.001 t_threshold = -stats.distributions.t.ppf(p_threshold / 2., n_subjects - 1) print('Clustering... ') T_obs, clusters, cluster_p_values, H0 = spatio_temporal_cluster_1samp_test( X, connectivity=connectivity_2, n_jobs=1, threshold=t_threshold) # Pack the outputs into tuple clu = (T_obs, clusters, cluster_p_values, H0) # Select the clusters that are sig. at p < p_value (Note this value is multiple-comparisons corrected) good_cluster_inds = np.where(cluster_p_values < p_value)[0] return clu, good_cluster_inds