コード例 #1
0
    def test_linked_nodes(self):
        self.a.import_adj_file(self.SMALL_FILE)
        self.a.apply_threshold()
        for n in self.a.G.nodes(data=True):
            self.assertRaises(KeyError, lambda: n[1][ct.LINKED_NODES])

        self.a.find_linked_nodes()
        for n in self.a.G.nodes(data=True):
            n[1][ct.LINKED_NODES]  # it should not raise any exception

        self.assertTrue(
            sorted(self.a.G.nodes[0][ct.LINKED_NODES]) == [1, 2, 3])
        self.assertTrue(
            sorted(self.a.G.nodes[1][ct.LINKED_NODES]) == [0, 2, 3])

        # directed
        c = mbt.Brain(directed=True)
        c.import_adj_file(self.SMALL_FILE)
        c.apply_threshold()
        c.find_linked_nodes()
        for n in c.G.nodes(data=True):
            n[1][ct.LINKED_NODES]  # it should not raise any exception

        self.assertTrue(sorted(c.G.nodes[0][ct.LINKED_NODES]) == [1, 2, 3])
        self.assertTrue(sorted(c.G.nodes[1][ct.LINKED_NODES]) == [0, 2, 3])
コード例 #2
0
 def test_make_absolute(self):
     c = mbt.Brain(directed=True)
     c.import_adj_file(self.SMALL_NEG_FILE)
     c.apply_threshold()
     c.make_edges_absolute()
     self.assertTrue(all(e[2][ct.WEIGHT] >= 0
                         for e in c.G.edges(data=True)))
コード例 #3
0
 def setUp(self):
     self.a = mbt.Brain()
     self.SMALL_FILE = "test/data/3d_grid_adj.txt"
     self.SMALL_NEG_FILE = "test/data/3d_grid_adj_neg.txt"
     self.MODIF_FILE = "test/data/3d_grid_adj2.txt"
     self.COORD_FILE = "test/data/3d_grid_coords.txt"
     self.PROPS_FILE = "test/data/3d_grid_properties.txt"
コード例 #4
0
def _get_ordered_array_and_labels(matrix, dummy_adj_file, hemi_prop, lobes_prop, anat_prop,
                                  hemi_name, lobes_name, anat_name):
    dummy_brain = mbt.Brain()
    dummy_brain.import_adj_file(dummy_adj_file)
    dummy_brain.apply_threshold()
    dummy_brain.import_properties(anat_prop)
    dummy_brain.import_properties(lobes_prop)
    dummy_brain.import_properties(hemi_prop)

    # Sorting the nodes, first by hemisphere, then by lobe,
    # then by anatomical label, and then by node number
    nodes = copy.deepcopy(list(dummy_brain.G.nodes(data=True)))
    nodes.sort(key=lambda x: (x[1][hemi_name], x[1][lobes_name], x[1][anat_name], x[0]))

    # Getting the labels of the nodes and the numbers, now in ascending order
    labels = [x[1][anat_name] for x in nodes]
    permutation = [x[0] for x in nodes]

    # Copying adjMat, and ordering the rows and columns
    order = np.argsort(permutation)

    arr = np.copy(matrix)
    arr = arr[:, order]
    arr = arr[order, :]

    return arr, labels
コード例 #5
0
    def test_matrices(self):
        dummy_brain = mbt.Brain()
        dummy_brain.import_adj_file(rt.DUMMY_ADJ_FILE_500)
        dictionary = {0: dummy_brain, 1: dummy_brain}
        fig, _ = mpt.plot_avg_matrix(dictionary)
        plt.close(fig)

        fig, _ = mpt.plot_strength_matrix(dummy_brain)
        plt.close(fig)
コード例 #6
0
    def test_percentages(self):
        self.a.import_adj_file(self.SMALL_FILE)
        self.a.apply_threshold(threshold_type="totalEdges", value=1)
        self.assertEqual(utils.threshold_to_percentage(self.a, 0.6),
                         3 / 6)  # from the file

        self.assertEqual(utils.percent_connected(self.a), 1 / 6)
        self.a.apply_threshold()
        self.assertEqual(utils.percent_connected(self.a), 1)

        # directed
        c = mbt.Brain(directed=True)
        c.import_adj_file(self.SMALL_FILE)
        self.assertEqual(utils.threshold_to_percentage(c, 0.6), 5 / 12)

        c.apply_threshold(threshold_type="totalEdges", value=1)
        self.assertEqual(utils.percent_connected(c), 1 / 12)
        c.apply_threshold()
        self.assertEqual(utils.percent_connected(c), 1)
        c.apply_threshold(threshold_type="totalEdges", value=0)
        self.assertEqual(utils.percent_connected(c), 0)
コード例 #7
0
    def test_import_adj_file(self):
        self.assertRaises(IOError, self.a.import_adj_file, "sdfasdf")
        self.a.import_adj_file(self.SMALL_FILE)
        self.assertEqual(self.a.adjMat.shape, (4, 4))
        self.assertEqual(self.a.adjMat[0][0], 0.802077230054)
        self.assertEqual(self.a.G.number_of_nodes(), 4)
        self.assertEqual(len(self.a.G.edges()), 0)

        b = mbt.Brain()
        b.import_adj_file(self.MODIF_FILE,
                          delimiter=",",
                          nodes_to_exclude=[2, 4])
        # Confirm general info
        self.assertEqual(b.adjMat.shape, (15, 15))
        self.assertEqual(b.G.number_of_nodes(), 13)
        self.assertEqual(b.G.number_of_edges(), 0)
        self.assertEqual(b.adjMat[0][0], 0)
        self.assertEqual(b.adjMat[1][0], 1.541495524150943430e-01)
        self.assertTrue(np.isnan(b.adjMat[6][0]))
        # Confirm excluded nodes
        self.assertTrue(all(np.isnan(x) for x in b.adjMat[:, 2]))
        self.assertTrue(all(np.isnan(x) for x in b.adjMat[:, 4]))
        self.assertTrue(all(np.isnan(x) for x in b.adjMat[2, :]))
        self.assertTrue(all(np.isnan(x) for x in b.adjMat[4, :]))
コード例 #8
0
    def test_apply_threshold(self):
        self.a.import_adj_file(self.MODIF_FILE,
                               delimiter=",",
                               nodes_to_exclude=[2, 4])
        self.a.import_spatial_info(
            self.COORD_FILE)  # making sure this doesn't influence the rest
        self.a.apply_threshold()
        all_edges = self.a.G.number_of_edges()
        # Although there are 3 NAs in the file, just the upper half of the matrix is considered
        degrees = self.a.G.degree()
        self.assertEqual(degrees[0], 11)
        self.assertRaises(KeyError, lambda: degrees[2])
        self.assertRaises(KeyError, lambda: degrees[4])
        self.assertEqual(degrees[5], 12)

        accepted_edges = ((0, 1), (0, 5), (1, 5), (3, 5))
        not_accepted = ((0, 0), (1, 1), (2, 1), (0, 2), (0, 3), (0, 4), (1, 2),
                        (1, 3), (1, 4))
        self.assertTrue(all(e in self.a.G.edges() for e in accepted_edges))
        self.assertTrue(all(e not in self.a.G.edges() for e in not_accepted))
        self.assertEqual(self.a.G.number_of_edges(),
                         76)  # (15*15 - 15) /2 - 2 - 27

        # edgePC
        b = mbt.Brain()
        b.import_adj_file(self.MODIF_FILE,
                          delimiter=",",
                          nodes_to_exclude=[2, 4])
        for val in [True, False]:
            b.apply_threshold(threshold_type="edgePC",
                              value=10.5,
                              use_absolute=val)
            self.assertEqual(b.G.number_of_edges(), int(0.105 * all_edges))
            self.assertTrue((1, 12) in b.G.edges())
            b.apply_threshold(threshold_type="edgePC",
                              value=0,
                              use_absolute=val)
            self.assertEqual(b.G.number_of_edges(), 0)

        # totalEdges
        for val in [True, False]:
            b.apply_threshold(threshold_type="totalEdges",
                              value=3,
                              use_absolute=val)
            self.assertEqual(b.G.number_of_edges(), 3)
            self.assertTrue((1, 12) in b.G.edges())
            b.apply_threshold(threshold_type="totalEdges",
                              value=1000,
                              use_absolute=val)
            self.assertEqual(b.G.number_of_edges(), 76)
            b.apply_threshold(threshold_type="totalEdges",
                              value=0,
                              use_absolute=val)
            self.assertEqual(b.G.number_of_edges(), 0)

        # tVal
        b.apply_threshold(threshold_type="tVal", value=3)
        self.assertTrue(all(e[2][ct.WEIGHT] >= 3
                            for e in b.G.edges(data=True)))
        self.assertEqual(b.G.number_of_edges(), 0)
        b.apply_threshold(threshold_type="tVal",
                          value=6.955292039622642530e-01)
        self.assertTrue(
            all(e[2][ct.WEIGHT] >= 6.955292039622642530e-01
                for e in b.G.edges(data=True)))
        self.assertEqual(b.G.number_of_edges(), 1)
        b.apply_threshold(threshold_type="tVal", value=0.5)
        self.assertTrue(
            all(e[2][ct.WEIGHT] >= 0.5 for e in b.G.edges(data=True)))

        # directed
        c = mbt.Brain(directed=True)
        c.import_adj_file(self.MODIF_FILE, delimiter=",")
        for val in [True, False]:
            c.apply_threshold(use_absolute=val)
            all_edges = c.G.number_of_edges()
            self.assertEqual(c.G.number_of_edges(), 207)  # 15*15 - 15 -3NAs
            c.apply_threshold(threshold_type="edgePC",
                              value=10.5,
                              use_absolute=val)
            self.assertEqual(c.G.number_of_edges(), int(0.105 * all_edges))
            c.apply_threshold(threshold_type="totalEdges",
                              value=76,
                              use_absolute=val)
            self.assertEqual(c.G.number_of_edges(), 76)
            c.apply_threshold(threshold_type="totalEdges",
                              value=10000,
                              use_absolute=val)
            self.assertEqual(c.G.number_of_edges(), 207)
        c.apply_threshold(threshold_type="tVal", value=0.5)
        self.assertTrue(
            all(e[2][ct.WEIGHT] >= 0.5 for e in c.G.edges(data=True)))

        # Absolute thresholding
        c.import_adj_file(self.SMALL_NEG_FILE)
        c.apply_threshold(threshold_type="totalEdges",
                          value=2,
                          use_absolute=True)
        self.assertTrue(c.G.edges[1, 2][ct.WEIGHT] == -0.843798947781)
        self.assertTrue(c.G.edges[2, 0][ct.WEIGHT] == 0.858463902674)
        c.apply_threshold(threshold_type="tVal", value=0.5, use_absolute=True)
        self.assertTrue(
            all(e[2][ct.WEIGHT] >= 0.5 or e[2][ct.WEIGHT] <= -0.5
                for e in c.G.edges(data=True)))
        b.import_adj_file(self.SMALL_NEG_FILE)
        b.apply_threshold(threshold_type="totalEdges",
                          value=1,
                          use_absolute=True)
        self.assertTrue(b.G.edges[1, 2][ct.WEIGHT] == -0.843798947781)
        b.apply_threshold(threshold_type="edgePC", value=20, use_absolute=True)
        self.assertTrue(b.G.edges[1, 2][ct.WEIGHT] == -0.843798947781)
        self.assertEqual(b.G.number_of_edges(), 1)