t = stats['t'].plot(vmin=2) plt.title('Significant Pixels', fontsize=18) ######################################################################### # Similarity/Distance # ------------------- # # We can calculate similarity between two Adjacency matrices using `.similiarity()`. stats = dat.similarity(m1) print(stats) ######################################################################### # We can also calculate the distance between multiple matrices contained within a single Adjacency object. Any distance metric is available from the `sci-kit learn <http://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.pairwise_distances.html>`_ by specifying the `method` flag. This outputs an Adjacency matrix. In the example below we see that several matrices are more similar to each other (i.e., when the signal is on). Remember that the nodes here now represent each matrix from the original distance matrix. dist = data.distance(method='correlation') f = dist.plot() ######################################################################### # Similarity matrices can be converted to and from Distance matrices using `.similarity_to_distance()` and `.distance_to_similarity()`. f = dist.distance_to_similarity().plot() ######################################################################### # Multidimensional Scaling # ------------------------ # # We can perform additional analyses on distance matrices such as multidimensional scaling. Here we provide an example to create a 3D multidimensional scaling plot of our data to see if the on and off matrices might naturally group together. dat = Adjacency(noisy, matrix_type='similarity',
t = stats['t'].plot(vmin=2) plt.title('Significant Pixels', fontsize=18) ######################################################################### # Similarity/Distance # ------------------- # # We can calculate similarity between two Adjacency matrices using `.similiarity()`. stats = dat.similarity(m1) print(stats) ######################################################################### # We can also calculate the distance between multiple matrices contained within a single Adjacency object. Any distance metric is available from the `sci-kit learn <http://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.pairwise_distances.html>`_ by specifying the `method` flag. This outputs an Adjacency matrix. In the example below we see that several matrices are more similar to each other (i.e., when the signal is on). Remember that the nodes here now represent each matrix from the original distance matrix. dist = data.distance(metric='correlation') dist.plot() ######################################################################### # Similarity matrices can be converted to and from Distance matrices using `.similarity_to_distance()` and `.distance_to_similarity()`. dist.distance_to_similarity().plot() ######################################################################### # Multidimensional Scaling # ------------------------ # # We can perform additional analyses on distance matrices such as multidimensional scaling. Here we provide an example to create a 3D multidimensional scaling plot of our data to see if the on and off matrices might naturally group together. dat = Adjacency(noisy, matrix_type='similarity',
def test_adjacency(tmpdir): n = 10 sim = np.random.multivariate_normal([0,0,0,0],[[1, 0.8, 0.1, 0.4], [0.8, 1, 0.6, 0.1], [0.1, 0.6, 1, 0.3], [0.4, 0.1, 0.3, 1]], 100) data = pairwise_distances(sim.T, metric='correlation') dat_all = [] for t in range(n): tmp = data dat_all.append(tmp) sim_directed = np.array([[1, 0.5, 0.3, 0.4], [0.8, 1, 0.2, 0.1], [0.7, 0.6, 1, 0.5], [0.85, 0.4, 0.3, 1]]) labels = ['v_%s' % (x+1) for x in range(sim.shape[1])] dat_single = Adjacency(dat_all[0], labels=labels) dat_multiple = Adjacency(dat_all, labels=labels) dat_directed = Adjacency(sim_directed, matrix_type='directed', labels=labels) # Test automatic distance/similarity detection assert dat_single.matrix_type is 'distance' dat_single2 = Adjacency(1-data) assert dat_single2.matrix_type is 'similarity' assert not dat_directed.issymmetric assert dat_single.issymmetric # Test length assert len(dat_multiple) == dat_multiple.data.shape[0] assert len(dat_multiple[0]) == 1 # Test Indexing assert len(dat_multiple[0]) == 1 assert len(dat_multiple[0:4]) == 4 assert len(dat_multiple[0, 2, 3]) == 3 # Test basic arithmetic assert(dat_directed+5).data[0] == dat_directed.data[0]+5 assert(dat_directed-.5).data[0] == dat_directed.data[0]-.5 assert(dat_directed*5).data[0] == dat_directed.data[0]*5 assert np.all(np.isclose((dat_directed+dat_directed).data, (dat_directed*2).data)) assert np.all(np.isclose((dat_directed*2-dat_directed).data, dat_directed.data)) # Test copy assert np.all(dat_multiple.data == dat_multiple.copy().data) # Test squareform & iterable assert len(dat_multiple.squareform()) == len(dat_multiple) assert dat_single.squareform().shape == data.shape assert dat_directed.squareform().shape == sim_directed.shape # Test write dat_multiple.write(os.path.join(str(tmpdir.join('Test.csv'))), method='long') dat_multiple2 = Adjacency(os.path.join(str(tmpdir.join('Test.csv'))), matrix_type='distance_flat') dat_directed.write(os.path.join(str(tmpdir.join('Test.csv'))), method='long') dat_directed2 = Adjacency(os.path.join(str(tmpdir.join('Test.csv'))), matrix_type='directed_flat') assert np.all(np.isclose(dat_multiple.data, dat_multiple2.data)) assert np.all(np.isclose(dat_directed.data, dat_directed2.data)) # Test mean assert isinstance(dat_multiple.mean(axis=0), Adjacency) assert len(dat_multiple.mean(axis=0)) == 1 assert len(dat_multiple.mean(axis=1)) == len(np.mean(dat_multiple.data, axis=1)) # Test std assert isinstance(dat_multiple.std(axis=0), Adjacency) assert len(dat_multiple.std(axis=0)) == 1 assert len(dat_multiple.std(axis=1)) == len(np.std(dat_multiple.data, axis=1)) # Test similarity assert len(dat_multiple.similarity( dat_single.squareform())) == len(dat_multiple) assert len(dat_multiple.similarity(dat_single.squareform(), metric='pearson')) == len(dat_multiple) assert len(dat_multiple.similarity(dat_single.squareform(), metric='kendall')) == len(dat_multiple) # Test distance assert isinstance(dat_multiple.distance(), Adjacency) assert dat_multiple.distance().square_shape()[0] == len(dat_multiple) # Test ttest mn, p = dat_multiple.ttest() assert len(mn) == 1 assert len(p) == 1 assert mn.shape()[0] == dat_multiple.shape()[1] assert p.shape()[0] == dat_multiple.shape()[1] # Test Threshold assert np.sum(dat_directed.threshold(upper=.8).data == 0) == 10 assert dat_directed.threshold(upper=.8, binarize=True).data[0] assert np.sum(dat_directed.threshold(upper='70%', binarize=True).data) == 5 assert np.sum(dat_directed.threshold(lower=.4, binarize=True).data) == 6 # Test to_graph() assert isinstance(dat_directed.to_graph(), nx.DiGraph) assert isinstance(dat_single.to_graph(), nx.Graph) # Test Append a = Adjacency() a = a.append(dat_single) assert a.shape() == dat_single.shape() a = a.append(a) assert a.shape() == (2, 6) n_samples = 3 b = dat_multiple.bootstrap('mean', n_samples=n_samples) assert isinstance(b['Z'], Adjacency) b = dat_multiple.bootstrap('std', n_samples=n_samples) assert isinstance(b['Z'], Adjacency) # Test plot f = dat_single.plot() assert isinstance(f, plt.Figure) f = dat_multiple.plot() assert isinstance(f, plt.Figure) # Test plot_mds f = dat_single.plot_mds() assert isinstance(f, plt.Figure)
t = stats["t"].plot(vmin=2) plt.title("Significant Pixels", fontsize=18) ######################################################################### # Similarity/Distance # ------------------- # # We can calculate similarity between two Adjacency matrices using `.similiarity()`. stats = dat.similarity(m1) print(stats) ######################################################################### # We can also calculate the distance between multiple matrices contained within a single Adjacency object. Any distance metric is available from the `sci-kit learn <http://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.pairwise_distances.html>`_ by specifying the `method` flag. This outputs an Adjacency matrix. In the example below we see that several matrices are more similar to each other (i.e., when the signal is on). Remember that the nodes here now represent each matrix from the original distance matrix. dist = data.distance(metric="correlation") dist.plot() ######################################################################### # Similarity matrices can be converted to and from Distance matrices using `.similarity_to_distance()` and `.distance_to_similarity()`. dist.distance_to_similarity(metric="correlation").plot() ######################################################################### # Multidimensional Scaling # ------------------------ # # We can perform additional analyses on distance matrices such as multidimensional scaling. Here we provide an example to create a 3D multidimensional scaling plot of our data to see if the on and off matrices might naturally group together. dist = data.distance(metric="correlation") dist.labels = ["On"] * 5 + ["Off"] * 5 + ["On"] * 5