コード例 #1
0
def test_similarity_matrix_permutation():
    dat = np.random.multivariate_normal([2, 6], [[.5, 2], [.5, 3]], 190)
    x = Adjacency(dat[:, 0])
    y = Adjacency(dat[:, 1])
    stats = x.similarity(y, perm_type='2d', n_permute=1000)
    assert (stats['correlation'] > .4) & (stats['correlation'] < .85) & (stats['p'] < .001)
    stats = x.similarity(y, perm_type=None)
    assert (stats['correlation'] > .4) & (stats['correlation'] < .85)
コード例 #2
0
def test_cluster_mean():
    test_dat = Adjacency(block_diag(np.ones((4, 4)),
                                    np.ones((4, 4)) * 2,
                                    np.ones((4, 4)) * 3),
                         matrix_type='similarity')
    test_labels = np.concatenate([np.ones(4) * x for x in range(1, 4)])
    out = test_dat.within_cluster_mean(clusters=test_labels)
    assert np.sum(np.array([1, 2, 3]) - np.array([out[x] for x in out])) == 0
コード例 #3
0
def test_similarity_matrix_permutation():
    dat = np.random.multivariate_normal([2, 6], [[0.5, 2], [0.5, 3]], 190)
    x = Adjacency(dat[:, 0])
    y = Adjacency(dat[:, 1])
    stats = x.similarity(y, perm_type="2d", n_permute=1000)
    assert ((stats["correlation"] > 0.4)
            & (stats["correlation"] < 0.85)
            & (stats["p"] < 0.001))
    stats = x.similarity(y, perm_type=None)
    assert (stats["correlation"] > 0.4) & (stats["correlation"] < 0.85)
コード例 #4
0
def test_read_and_write_directed(sim_adjacency_directed, tmpdir):
    sim_adjacency_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(sim_adjacency_directed.data, dat_directed2.data))

    # Load Path
    dat_directed2 = Adjacency(Path(tmpdir.join("Test.csv")),
                              matrix_type="directed_flat")
    assert np.all(np.isclose(sim_adjacency_directed.data, dat_directed2.data))
コード例 #5
0
ファイル: conftest.py プロジェクト: sinodanishspain/nltools
def sim_adjacency_directed():
    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_directed.shape[1])]
    return Adjacency(sim_directed, matrix_type='directed', labels=labels)
コード例 #6
0
ファイル: conftest.py プロジェクト: sinodanishspain/nltools
def sim_adjacency_single():
    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')
    labels = ['v_%s' % (x+1) for x in range(sim.shape[1])]
    return Adjacency(data, labels=labels)
コード例 #7
0
def test_write_multiple(sim_adjacency_multiple, tmpdir):
    sim_adjacency_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')
    assert np.all(np.isclose(sim_adjacency_multiple.data, dat_multiple2.data))

    # Test i/o for hdf5
    sim_adjacency_multiple.write(os.path.join(str(tmpdir.join('test_write.h5'))))
    b = Adjacency(os.path.join(tmpdir.join('test_write.h5')))
    for k in ['Y', 'matrix_type', 'is_single_matrix', 'issymmetric', 'data']:
        if k == 'data':
            assert np.allclose(b.__dict__[k], sim_adjacency_multiple.__dict__[k])
        elif k == 'Y':
            assert all(b.__dict__[k].eq(sim_adjacency_multiple.__dict__[k]).values)
        else:
            assert b.__dict__[k] == sim_adjacency_multiple.__dict__[k]
コード例 #8
0
def test_regression():
    # Test Adjacency Regression
    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)))
    Y = Adjacency(m1 * 1 + m2 * 2 + m3 * 3, matrix_type="similarity")
    X = Adjacency([m1, m2, m3], matrix_type="similarity")

    stats = Y.regress(X)
    assert np.allclose(stats["beta"], np.array([1, 2, 3]))

    # Test Design_Matrix Regression
    n = 10
    d = Adjacency(
        [
            block_diag(
                np.ones((4, 4)) + np.random.randn(4, 4) * 0.1, np.zeros(
                    (8, 8))) for _ in range(n)
        ],
        matrix_type="similarity",
    )
    X = Design_Matrix(np.ones(n))
    stats = d.regress(X)
    out = stats["beta"].cluster_summary(clusters=["Group1"] * 4 +
                                        ["Group2"] * 8,
                                        summary="within")
    assert np.allclose(
        np.array([out["Group1"], out["Group2"]]), np.array([1, 0]),
        rtol=1e-01)  # np.allclose(np.sum(stats['beta']-np.array([1,2,3])),0)
コード例 #9
0
def test_social_relations_model():
    data = Adjacency(
        np.array([
            [np.nan, 8, 5, 10],
            [7, np.nan, 7, 6],
            [8, 7, np.nan, 5],
            [4, 5, 0, np.nan],
        ]),
        matrix_type="directed",
    )
    data2 = data.append(data)
    results1 = data.social_relations_model()
    assert isinstance(data.social_relations_model(), pd.Series)
    assert isinstance(data2.social_relations_model(), pd.DataFrame)
    assert len(results1["actor_effect"]) == data.square_shape()[0]
    assert results1["relationship_effect"].shape == data.square_shape()
    np.testing.assert_approx_equal(results1["actor_variance"],
                                   3.33,
                                   significant=2)
    np.testing.assert_approx_equal(results1["partner_variance"],
                                   0.66,
                                   significant=2)
    np.testing.assert_approx_equal(results1["relationship_variance"],
                                   3.33,
                                   significant=2)
    np.testing.assert_approx_equal(results1["actor_partner_correlation"],
                                   0.22,
                                   significant=2)
    np.testing.assert_approx_equal(results1["dyadic_reciprocity_correlation"],
                                   0.2,
                                   significant=2)
コード例 #10
0
def test_regression():
    # Test Adjacency Regression
    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)))
    Y = Adjacency(m1 * 1 + m2 * 2 + m3 * 3, matrix_type='similarity')
    X = Adjacency([m1, m2, m3], matrix_type='similarity')

    stats = Y.regress(X)
    assert np.allclose(stats['beta'], np.array([1, 2, 3]))

    # Test Design_Matrix Regression
    n = 10
    d = Adjacency([
        block_diag(
            np.ones((4, 4)) + np.random.randn(4, 4) * .1, np.zeros((8, 8)))
        for x in range(n)
    ],
                  matrix_type='similarity')
    X = Design_Matrix(np.ones(n))
    stats = d.regress(X)
    out = stats['beta'].within_cluster_mean(clusters=['Group1'] * 4 +
                                            ['Group2'] * 8)
    assert np.allclose(
        np.array([out['Group1'], out['Group2']]), np.array([1, 0]),
        rtol=1e-01)  # np.allclose(np.sum(stats['beta']-np.array([1,2,3])),0)
コード例 #11
0
def test_write_multiple(sim_adjacency_multiple, tmpdir):
    sim_adjacency_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")
    assert np.all(np.isclose(sim_adjacency_multiple.data, dat_multiple2.data))

    # Test i/o for hdf5
    sim_adjacency_multiple.write(
        os.path.join(str(tmpdir.join("test_write.h5"))))
    b = Adjacency(os.path.join(tmpdir.join("test_write.h5")))
    for k in ["Y", "matrix_type", "is_single_matrix", "issymmetric", "data"]:
        if k == "data":
            assert np.allclose(b.__dict__[k],
                               sim_adjacency_multiple.__dict__[k])
        elif k == "Y":
            assert all(b.__dict__[k].eq(
                sim_adjacency_multiple.__dict__[k]).values)
        else:
            assert b.__dict__[k] == sim_adjacency_multiple.__dict__[k]
コード例 #12
0
ファイル: conftest.py プロジェクト: sinodanishspain/nltools
def sim_adjacency_multiple():
    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)
    labels = ['v_%s' % (x+1) for x in range(sim.shape[1])]
    return Adjacency(dat_all, labels=labels)
コード例 #13
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)
コード例 #14
0
def test_directed_similarity():
    dat = np.random.multivariate_normal([2, 6], [[.5, 2], [.5, 3]], 400)
    x = Adjacency(dat[:, 0].reshape(20, 20), matrix_type='directed')
    y = Adjacency(dat[:, 1].reshape(20, 20), matrix_type='directed')
    # Ignore diagonal
    stats = x.similarity(y, perm_type='1d', ignore_diagonal=True, n_permute=1000)
    assert (stats['correlation'] > .4) & (stats['correlation'] < .85) & (stats['p'] < .001)
    # Use diagonal
    stats = x.similarity(y, perm_type=None, ignore_diagonal=False)
    assert (stats['correlation'] > .4) & (stats['correlation'] < .85)
    # Error out but make usre TypeError is the reason why
    try:
        x.similarity(y, perm_type='2d')
    except TypeError as e:
        pass 
    try:
        x.similarity(y, perm_type='jackknife')
    except TypeError as e:
        pass
コード例 #15
0
    def distance(self, method='euclidean', **kwargs):
        """ Calculate distance between rows within a Fex() instance.

            Args:
                method: type of distance metric (can use any scikit learn or
                        sciypy metric)

            Returns:
                dist: Outputs a 2D distance matrix.

        """

        return Adjacency(pairwise_distances(self, metric=method, **kwargs),
                         matrix_type='Distance')
コード例 #16
0
def test_directed_similarity():
    dat = np.random.multivariate_normal([2, 6], [[0.5, 2], [0.5, 3]], 400)
    x = Adjacency(dat[:, 0].reshape(20, 20), matrix_type="directed")
    y = Adjacency(dat[:, 1].reshape(20, 20), matrix_type="directed")
    # Ignore diagonal
    stats = x.similarity(y,
                         perm_type="1d",
                         ignore_diagonal=True,
                         n_permute=1000)
    assert ((stats["correlation"] > 0.4)
            & (stats["correlation"] < 0.85)
            & (stats["p"] < 0.001))
    # Use diagonal
    stats = x.similarity(y, perm_type=None, ignore_diagonal=False)
    assert (stats["correlation"] > 0.4) & (stats["correlation"] < 0.85)
    # Error out but make usre TypeError is the reason why
    try:
        x.similarity(y, perm_type="2d")
    except TypeError as _:  # noqa
        pass
コード例 #17
0
# visualize which regions are more central in this analysis.

from sklearn.metrics import pairwise_distances
from nltools.data import Adjacency
from nltools.mask import roi_to_brain
import pandas as pd
import numpy as np

sub_list = data.X['SubjectID'].unique()

# perform matrix multiplication to compute linear contrast for each subject
lin_contrast = []
for sub in sub_list:
    lin_contrast.append(data[data.X['SubjectID'] == sub] * np.array([1, -1,  0])) 

# concatenate list of Brain_Data instances into a single instance
lin_contrast = Brain_Data(lin_contrast) 

# Compute correlation distance between each ROI
dist = Adjacency(pairwise_distances(lin_contrast.extract_roi(mask), metric='correlation'), matrix_type='distance')

# Threshold functional connectivity and convert to Adjacency Matrix. Plot as heatmap
dist.threshold(upper=.4, binarize=True).plot()

# Convert Adjacency matrix to networkX instance
g = dist.threshold(upper=.4, binarize=True).to_graph()

# Compute degree centrality and convert back into Brain_Data instance.
degree_centrality = roi_to_brain(pd.Series(dict(g.degree())), mask_x)

degree_centrality.plot()
コード例 #18
0
def test_append(sim_adjacency_single):
    a = Adjacency()
    a = a.append(sim_adjacency_single)
    assert a.shape() == sim_adjacency_single.shape()
    a = a.append(a)
    assert a.shape() == (2, 6)
コード例 #19
0
                          for i in labels]) == 'threat').astype(int)
unique_run = np.array([
    re.search('(?<=-d)[12]', i).group() +
    re.search('(?<=_Run-)[0-9]', i).group() for i in beta_map_fnames_ordered
]).astype(float)
for n, i in enumerate(np.unique(unique_run)):
    unique_run[unique_run == i] = n

# Set up RSMs #

# Covariates #

# Matrix representing the unique run - used to ensure we ignore trials from the same run
unique_run_matrix = distance_matrix(unique_run[now_or_later, np.newaxis],
                                    unique_run[now_or_later, np.newaxis]) == 0
unique_run_matrix = Adjacency(unique_run_matrix)

# Session
session_matrix = distance_matrix(
    np.array(sessions).astype(float)[now_or_later, np.newaxis],
    np.array(sessions).astype(float)[now_or_later, np.newaxis])
plt.figure(figsize=(8, 8), dpi=100)
sns.heatmap(session_matrix, square=True)
session_matrix = Adjacency(session_matrix, matrix_type='similarity')
session_matrix.data = scale(session_matrix.data)

# Run
run_matrix = distance_matrix(
    np.array(runs).astype(float)[now_or_later, np.newaxis],
    np.array(runs).astype(float)[now_or_later, np.newaxis]) == 0
plt.figure(figsize=(8, 8), dpi=100)
コード例 #20
0
def test_sum():
    n = 10
    a = Adjacency(np.ones((n, n)), matrix_type="directed")
    assert a.sum() == n**2
    a = Adjacency([a, a])
    assert a.sum().data.sum() == (n**2) * 2

    a = Adjacency(np.ones((n, n)), matrix_type="similarity")
    assert a.sum() == n * (n - 1) / 2
    a = Adjacency([a, a])
    assert a.sum().data.sum() == n * (n - 1)

    a = Adjacency(np.ones((n, n)), matrix_type="distance")
    assert a.sum() == n * (n - 1) / 2
    a = Adjacency([a, a])
    assert a.sum().data.sum() == n * (n - 1)
コード例 #21
0
ファイル: Connectivity.py プロジェクト: ljchang/dartbrains
# In[37]:

roi_corr = 1 - pairwise_distances(rois, metric='correlation')

sns.heatmap(roi_corr, square=True, vmin=-1, vmax=1, cmap='RdBu_r')

# Now we need to convert this correlation matrix into a graph and calculate a centrality measure. We will use the `Adjacency` class from nltools as it has many functions that are useful for working with this type of data, including casting these type of matrices into networkx graph objects.
#
# We will be using the [networkx](https://networkx.github.io/documentation/stable/) python toolbox to work with graphs and compute different metrics of the graph.
#
# Let's calculate degree centrality, which is the total number of nodes each node is connected with. Unfortunately, many graph theory metrics require working with adjacency matrices, which are binary matrices indicating the presence of an edge or not. To create this, we will simply apply an arbitrary threshold to our correlation matrix.

# In[38]:

a = Adjacency(roi_corr,
              matrix_type='similarity',
              labels=[x for x in range(50)])
a_thresholded = a.threshold(upper=.6, binarize=True)

a_thresholded.plot()

# Okay, now that we have a thresholded binary matrix, let's cast our data into a networkx object and calculate the degree centrality of each ROI and make a quick plot of the graph.

# In[41]:

plt.figure(figsize=(20, 15))
G = a_thresholded.to_graph()
pos = nx.kamada_kawai_layout(G)
node_and_degree = G.degree()
nx.draw_networkx_edges(G, pos, width=3, alpha=.2)
nx.draw_networkx_labels(G, pos, font_size=14, font_color='darkslategray')
コード例 #22
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)
コード例 #23
0
ファイル: Connectivity.py プロジェクト: DevXl/dartbrains
Now that we have specified our 50 nodes, we need to calculate the edges of the graph. We will be using pearson correlations. We will be using the `pairwise_distances` function from scikit-learn as it is much faster than most other correlation measures. We will then convert the distance metric into similarities by subtracting all of the values from 1.

Let's visualize the resulting correlation matrix as a heatmap using seaborn.

roi_corr = 1 - pairwise_distances(rois, metric='correlation')

sns.heatmap(roi_corr, square=True, vmin=-1, vmax=1, cmap='RdBu_r')

Now we need to convert this correlation matrix into a graph and calculate a centrality measure. We will use the `Adjacency` class from nltools as it has many functions that are useful for working with this type of data, including casting these type of matrices into networkx graph objects.

We will be using the [networkx](https://networkx.github.io/documentation/stable/) python toolbox to work with graphs and compute different metrics of the graph.

Let's calculate degree centrality, which is the total number of nodes each node is connected with. Unfortunately, many graph theory metrics require working with adjacency matrices, which are binary matrices indicating the presence of an edge or not. To create this, we will simply apply an arbitrary threshold to our correlation matrix.

a = Adjacency(roi_corr, matrix_type='similarity', labels=[x for x in range(50)])
a_thresholded = a.threshold(upper=.6, binarize=True)

a_thresholded.plot()

Okay, now that we have a thresholded binary matrix, let's cast our data into a networkx object and calculate the degree centrality of each ROI and make a quick plot of the graph.

plt.figure(figsize=(20,15))
G = a_thresholded.to_graph()
pos = nx.kamada_kawai_layout(G)
node_and_degree = G.degree()
nx.draw_networkx_edges(G, pos, width=3, alpha=.2)
nx.draw_networkx_labels(G, pos, font_size=14, font_color='darkslategray')

nx.draw_networkx_nodes(G, pos, nodelist=list(dict(node_and_degree).keys()),
                       node_size=[x[1]*100 for x in node_and_degree],
コード例 #24
0
#########################################################################
#  Load Data
# ----------
#
# Similar to the Brain_Data class, Adjacency instances can be initialized by passing in a numpy array or pandas data frame, or a path to a csv file or list of files.  Here we will generate some fake data to demonstrate how to use this class.  In addition to data, you must indicate the type of matrix.  Currently, you can specify `['similarity','distance','directed']`.  Similarity matrices are symmetrical with typically ones along diagonal, Distance matrices are symmetrical with zeros along diagonal, and Directed graph matrices are not symmetrical.  Symmetrical matrices only store the upper triangle. The Adjacency class can also accommodate labels, but does not require them.

from nltools.data import Adjacency
from scipy.linalg import block_diag
import numpy as np

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))) * 3
noisy = (m1 * 1 + m2 * 2 + m3 * 3) + np.random.randn(12, 12) * .1
dat = Adjacency(noisy,
                matrix_type='similarity',
                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.
コード例 #25
0
def test_write_multiple(sim_adjacency_multiple, tmpdir):
    sim_adjacency_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')
    assert np.all(np.isclose(sim_adjacency_multiple.data, dat_multiple2.data))
コード例 #26
0
def test_write_directed(sim_adjacency_directed, tmpdir):
    sim_adjacency_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(sim_adjacency_directed.data, dat_directed2.data))
コード例 #27
0
def test_type_single(sim_adjacency_single):
    assert sim_adjacency_single.matrix_type == "distance"
    dat_single2 = Adjacency(1 - sim_adjacency_single.squareform())
    assert dat_single2.matrix_type == "similarity"
    assert sim_adjacency_single.issymmetric
コード例 #28
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.
コード例 #29
0
#########################################################################
#  Load Data
# ----------
#
# Similar to the Brain_Data class, Adjacency instances can be initialized by passing in a numpy array or pandas data frame, or a path to a csv file or list of files.  Here we will generate some fake data to demonstrate how to use this class.  In addition to data, you must indicate the type of matrix.  Currently, you can specify `['similarity','distance','directed']`.  Similarity matrices are symmetrical with typically ones along diagonal, Distance matrices are symmetrical with zeros along diagonal, and Directed graph matrices are not symmetrical.  Symmetrical matrices only store the upper triangle. The Adjacency class can also accommodate labels, but does not require them.

from nltools.data import Adjacency
from scipy.linalg import block_diag
import numpy as np

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)

#########################################################################
# 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.