Esempio n. 1
0
    def test_graph_undirectional_to_directional(self):
        """
        Test conversion of a undirectional to directional graph
        Conversion essentially breaks all linked edge pairs by removing the
        data reference pointer.
        """

        directional = graph_undirectional_to_directional(self.graph)

        # directed attribute changed to True
        self.assertTrue(directional.directed)
        self.assertNotEqual(id(directional), id(self.graph))

        # directional graph still contains same nodes and edges
        self.assertEqual(directional, self.graph)

        # data reference pointers determine directionality
        self.assertEqual(graph_directionality(directional), 'directional')
        self.assertEqual(
            graph_directionality(directional, has_data_reference=False),
            'undirectional')

        # directional edge pair no longer point to same value
        directional_checked = []
        for edge in directional.edges:
            directional_checked.append(
                id(directional.edges[edge]) != id(directional.edges[(
                    edge[1], edge[0])]))
        self.assertTrue(all(directional_checked))
Esempio n. 2
0
    def test_format_import(self):
        """
        Test import of format
        """

        graph = read_web(self.web_file, auto_parse_format=False)

        # Default graph attributes set
        self.assertEqual(len(graph), 694)
        self.assertEqual(len(graph.edges), 1386)
        self.assertEqual(graph.directed, False)
        self.assertEqual(graph_directionality(graph), 'undirectional')
        self.assertEqual(graph.root, 1)
        self.assertTrue(isinstance(graph, GraphAxis))

        # No ORM or format auto detect set, all values should be strings
        self.assertTrue(
            isinstance(graph.query_nodes({
                'key': 'ntrials'
            }).value, PY_STRING))
        self.assertTrue(
            isinstance(
                graph.query_nodes({
                    'key': 'rotate180_0'
                }).value, PY_STRING))

        for node in graph.query_nodes({'key': 'activereslist'}):
            self.assertTrue(isinstance(node.value, list))

        for node in graph.query_nodes({'type': 'FloatArray'}):
            self.assertTrue(all([isinstance(n, str) for n in node.get()]))
Esempio n. 3
0
    def test_format_import(self):
        """
        Test import of format.
        """

        graph = read_pydata(self.test_dict, level=0)

        # Default graph attributes set
        self.assertEqual(len(graph), 15)
        self.assertEqual(len(graph.edges), 28)
        self.assertEqual(graph.directed, False)
        self.assertEqual(graph_directionality(graph), 'undirectional')
        self.assertEqual(graph.root, 1)
        self.assertTrue(isinstance(graph, GraphAxis))

        # Because default auto_nid is True, string based node IDs not supported
        self.assertTrue('three' not in graph.nodes)
        self.assertTrue(8 in graph.nodes)

        # Test hierarchy
        self.assertItemsEqual(graph.children().keys(desc=False),
                              [4, u'one', u'three', u'two'])
        self.assertItemsEqual(graph.leaves().values(),
                              [2.22, 4.67, 5, u'four', 1, False, 3, True, 2])
        self.assertEqual(graph.getnodes(7).value, 5)

        query = graph.query_nodes({'key': 'five'})
        self.assertEqual(query.children().items(),
                         [('extra', graph.getnodes(4)), ('value', 5)])
Esempio n. 4
0
    def test_graph_is_undirected(self):

        self.assertFalse(self.graph.directed)
        self.assertEqual(graph_directionality(self.graph), 'undirectional')
        self.assertTrue(
            all([not edge.is_directed for edge in self.graph.iteredges()]))
        self.assertTrue(len(self.graph.edges) == 14)  # 2 * 7
Esempio n. 5
0
    def test_pickled_format_import(self):
        """
        Test import of pickled format.
        """

        # Plain text serialized
        pgf_file = os.path.join(FILEPATH, 'graph_pickled.pgf')
        graph = read_pgf(pgf_file, pickle_graph=True)

        # Default graph attributes set
        self.assertEqual(len(graph), 37)
        self.assertEqual(len(graph.edges), 72)
        self.assertEqual(graph.directed, False)
        self.assertEqual(graph_directionality(graph), 'directional')
        self.assertEqual(graph_directionality(graph, has_data_reference=False),
                         'undirectional')
        self.assertTrue(isinstance(graph, Graph))
Esempio n. 6
0
    def test_graph_directional_to_undirectional(self):
        """
        Test conversion of a directional to undirectional graph
        """

        undirectional = graph_directional_to_undirectional(self.graph)

        # directed attribute changed to True
        self.assertFalse(undirectional.directed)
        self.assertNotEqual(undirectional, self.graph)

        # data reference pointers determine directionality
        self.assertEqual(graph_directionality(undirectional), 'undirectional')
        self.assertEqual(
            graph_directionality(undirectional, has_data_reference=False),
            'undirectional')

        # directional edge pair point to same value
        undirectional_checked = []
        for edge in undirectional.edges:
            undirectional_checked.append(
                id(undirectional.edges[edge]) == id(undirectional.edges[(
                    edge[1], edge[0])]))
        self.assertTrue(all(undirectional_checked))

        # edge argument equality
        self.assertDictEqual(undirectional.edges[(3, 6)], {
            'arg1': 2.44,
            'arg2': False
        })
        self.assertDictEqual(undirectional.edges[(6, 3)], {
            'arg1': 2.44,
            'arg2': False
        })
        self.assertDictEqual(undirectional.edges[(5, 4)], {
            'arg1': 1.22,
            'arg2': False,
            'arg3': 'test'
        })
        self.assertDictEqual(undirectional.edges[(4, 5)], {
            'arg1': 1.22,
            'arg2': False,
            'arg3': 'test'
        })
Esempio n. 7
0
    def test_graph_is_mixed(self):

        self.assertTrue(self.graph.directed)
        self.assertEqual(graph_directionality(self.graph), 'mixed')
        self.assertEqual(
            sum([1 for edge in self.graph.iteredges() if edge.is_directed]), 4)
        self.assertEqual(
            sum([1 for edge in self.graph.iteredges()
                 if not edge.is_directed]), 6)
        self.assertTrue(len(self.graph.edges) == 10)
Esempio n. 8
0
    def test_graph_directional_to_undirectional(self):
        """
        Test conversion of a directional to undirectional graph
        """

        undirectional = graph_directional_to_undirectional(self.graph)

        # directed attribute changed to True
        self.assertFalse(undirectional.directed)
        self.assertNotEqual(undirectional, self.graph)

        # data reference pointers determine directionality
        self.assertEqual(graph_directionality(undirectional), 'undirectional')
        self.assertEqual(
            graph_directionality(undirectional, has_data_reference=False),
            'undirectional')

        # all edges exist in pairs resulting duplicated values
        values = undirectional.edges(data='nd').values()
        self.assertEqual(len(values), len(set(values)) * 2)
Esempio n. 9
0
    def test_graph_edge_removal_directed(self):
        """
        Directed removal in a undirected graph is supported
        """

        self.graph.remove_edge(2, 3, directed=True)

        self.assertFalse((2, 3) in self.graph.edges)
        self.assertTrue((3, 2) in self.graph.edges)
        self.assertEqual(len(self.graph.edges), 13)

        # globally still marked as undirected but of mixed type
        self.assertFalse(self.graph.directed)
        self.assertEqual(graph_directionality(self.graph), 'mixed')
Esempio n. 10
0
    def test_format_import(self):
        """
        Test import of format. The graph defines a root and thus will be
        imported as a GraphAxis object.
        """

        lgf_file = os.path.join(FILEPATH, 'graph.lgf')
        graph = read_lgf(lgf_file)

        # Default graph attributes set
        self.assertEqual(len(graph), 6)
        self.assertEqual(len(graph.edges), 12)
        self.assertEqual(graph.directed, False)
        self.assertEqual(graph_directionality(graph), 'undirectional')
        self.assertTrue(isinstance(graph, Graph))

        # Because default auto_nid is True, string based node IDs not supported
        self.assertTrue('five' not in graph.nodes)
        self.assertTrue(5 in graph.nodes)
Esempio n. 11
0
    def test_format_import(self):
        """
        Test import of format
        """

        tgf_file = os.path.join(FILEPATH, 'graph.tgf')
        graph = read_tgf(tgf_file)

        # Default graph attributes set
        self.assertEqual(len(graph), 11)
        self.assertEqual(len(graph.edges), 11)
        self.assertEqual(graph.directed, False)
        self.assertEqual(graph_directionality(graph), 'directional')
        self.assertEqual(graph.root, None)
        self.assertTrue(isinstance(graph, Graph))

        # auto_nid is False, arbitrary node ID's supported
        self.assertTrue('eleven' in graph.nodes)
        self.assertTrue(10 in graph.nodes)
Esempio n. 12
0
    def test_format_custom_import(self):
        """
        Test TGF import with custom Graph instance
        """

        # Graph axis class with custom nid ID's
        graph = GraphAxis()
        graph.data.auto_nid = False
        graph.directed = True

        tgf_file = os.path.join(FILEPATH, 'graph.tgf')
        graph = read_tgf(tgf_file, graph=graph)

        # Custom graph attributes set and string based node IDs supported
        self.assertEqual(len(graph), 11)
        self.assertEqual(len(graph.edges), 11)
        self.assertEqual(graph.directed, True)
        self.assertEqual(graph_directionality(graph), 'directional')
        self.assertTrue(isinstance(graph, GraphAxis))
        self.assertTrue('eleven' in graph.nodes)
Esempio n. 13
0
    def test_format_import(self):
        """
        Test import of format. The graph defines a root and thus will be
        imported as a GraphAxis object.
        """

        jgf_file = os.path.join(FILEPATH, 'graph_axis.jgf')
        graph = read_jgf(jgf_file)

        # Default graph attributes set
        self.assertEqual(len(graph), 35)
        self.assertEqual(len(graph.edges), 72)
        self.assertEqual(graph.directed, False)
        self.assertEqual(graph_directionality(graph, has_data_reference=False),
                         'undirectional')
        self.assertEqual(graph.root, 1)
        self.assertTrue(isinstance(graph, GraphAxis))

        # Because default auto_nid is True, string based node IDs not supported
        self.assertTrue('eleven' not in graph.nodes)
        self.assertTrue(11 in graph.nodes)