コード例 #1
0
def test_cluster_summary():
    m1 = block_diag(np.ones((4, 4)), np.zeros((4, 4)), np.zeros((4, 4)))
    m2 = block_diag(np.zeros((4, 4)), np.ones((4, 4)), np.zeros((4, 4)))
    m3 = block_diag(np.zeros((4, 4)), np.zeros((4, 4)), np.ones((4, 4)))
    noisy = (m1 * 1 + m2 * 2 + m3 * 3) + np.random.randn(12, 12) * 0.1
    dat = Adjacency(noisy,
                    matrix_type="similarity",
                    labels=["C1"] * 4 + ["C2"] * 4 + ["C3"] * 4)

    clusters = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]
    cluster_mean = dat.cluster_summary(clusters=clusters)
    for i, j in zip(np.array([1, 2, 3]),
                    np.array([cluster_mean[x] for x in cluster_mean])):
        np.testing.assert_almost_equal(i, j, decimal=1)

    for i in dat.cluster_summary(clusters=clusters,
                                 summary="between").values():
        np.testing.assert_almost_equal(0, i, decimal=1)

    for i in dat.cluster_summary(clusters=clusters,
                                 summary="between").values():
        np.testing.assert_almost_equal(0, i, decimal=1)
コード例 #2
0
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 `.cluster_summary()` method.  You must specify a group variable to group the  data.  Here we use the labels.

print(dat.cluster_summary(clusters=dat.labels, summary="within",
                          metric="mean"))

#########################################################################
# 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"])

#########################################################################
# In addition to decomposing a single adjacency matrix, we can also estimate a model that predicts the variance over each voxel.  This is equivalent to a univariate regression in imaging analyses. Remember that just like in imaging these tests are non-independent and may require correcting for multiple comparisons.  Here we create some data that varies over matrices and identify pixels that follow a particular on-off-on pattern.  We plot the t-values that exceed 2.

from nltools.data import Design_Matrix