Exemple #1
0
# ### Testing a Representation Hypothesis
#
# Ok, now that we have a sense of what the similarity of patterns look like in left motor cortex, let's create an adjacency matrix indicating a specific relationship between left hand finger tapping across the auditory and visual conditions. This type of adjacency matrix is one way in which we can test a specific hypotheses about the representational structure of the data across all images.
#
# As you can see this only includes edges for the motor-left auditory and motor-left visual conditions.

# In[109]:

motor = np.zeros((len(conditions), len(conditions)))
motor[np.diag_indices(len(conditions))] = 1
motor[1, 7] = 1
motor[7, 1] = 1
motor[2, 8] = 1
motor[8, 2] = 1
motor = Adjacency(motor, matrix_type='distance', labels=conditions)
motor.plot()

# Now let's search over all ROIs to see if any match this representational structure of left motor-cortex using the `similarity()` method from the `Adjacency` class. This function uses spearman rank correlations by default. This is probably a good idea as we are most interested in monotonic relationships between the two similarity matrices.
#
# The `similarity()` method also computes permutations within columns and rows by default. To speed up the analysis, we will set the number of permutations to zero (i.e., `n_permute=0`).

# In[110]:

motor_sim_r = []
for m in out_sim:
    s = m.similarity(motor, metric='spearman', n_permute=0)
    motor_sim_r.append(s['correlation'])

# This will return a vector of similarity scores for each ROI, we can plot the distribution of these 50 $\rho$ values.

# In[111]:
Exemple #2
0
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)
Exemple #3
0
                labels=['C1'] * 4 + ['C2'] * 4 + ['C3'] * 4)

#########################################################################
# Basic information about the object can be viewed by simply calling it.

print(dat)

#########################################################################
# Adjacency objects can easily be converted back into two-dimensional matrices with the `.squareform()` method.

dat.squareform()

#########################################################################
# Matrices can viewed as a heatmap using the `.plot()` method.

f = dat.plot()

#########################################################################
# The mean within a a grouping label can be calculated using the `.within_cluster_mean()` method.  You must specify a group variable to group the  data.  Here we use the labels.

print(dat.within_cluster_mean(clusters=dat.labels))

#########################################################################
# Regression
# ----------
#
# Adjacency objects can currently accommodate two different types of regression. Sometimes we might want to decompose an Adjacency matrix from a linear combination of other Adjacency matrices.  Other times we might want to perform a regression at each pixel in a stack of Adjacency matrices. Here we provide an example of each method.  We use the same data we generated above, but attempt to decompose it by each block of data.  We create the design matrix by simply concatenating the matrices we used to create the data object. The regress method returns a dictionary containing all of the relevant information from the regression. Here we show that the model recovers the average weight in each block.

X = Adjacency([m1, m2, m3], matrix_type='similarity')
stats = dat.regress(X)
print(stats['beta'])
Exemple #4
0
                labels=['C1'] * 4 + ['C2'] * 4 + ['C3'] * 4)

#########################################################################
# Basic information about the object can be viewed by simply calling it.

print(dat)

#########################################################################
# Adjacency objects can easily be converted back into two-dimensional matrices with the `.squareform()` method.

dat.squareform()

#########################################################################
# Matrices can viewed as a heatmap using the `.plot()` method.

dat.plot()

#########################################################################
# The mean within a a grouping label can be calculated using the `.within_cluster_mean()` method.  You must specify a group variable to group the  data.  Here we use the labels.

print(dat.within_cluster_mean(clusters=dat.labels))

#########################################################################
# Regression
# ----------
#
# Adjacency objects can currently accommodate two different types of regression. Sometimes we might want to decompose an Adjacency matrix from a linear combination of other Adjacency matrices.  Other times we might want to perform a regression at each pixel in a stack of Adjacency matrices. Here we provide an example of each method.  We use the same data we generated above, but attempt to decompose it by each block of data.  We create the design matrix by simply concatenating the matrices we used to create the data object. The regress method returns a dictionary containing all of the relevant information from the regression. Here we show that the model recovers the average weight in each block.

X = Adjacency([m1, m2, m3], matrix_type='similarity')
stats = dat.regress(X)
print(stats['beta'])