Esempio n. 1
0
    def test_parse_graphml(self):
        G=networkx.parse_graphml(self.simple_data)
        assert_equals(sorted(G.nodes()),\
            [u'n0', u'n1', u'n10', u'n2', u'n3', u'n4', u'n5', u'n6', u'n7', u'n8', u'n9'])
        assert_equals( [sorted(e) for e in sorted(G.edges(keys=True))],\
            [[u'foo', u'n0', u'n2'], [0, u'n1', u'n2'], [0, u'n10', u'n8'], \
             [0, u'n2', u'n3'], [0, u'n3', u'n4'], [0, u'n3', u'n5'], \
             [0, u'n4', u'n6'], [0, u'n5', u'n7'], [0, u'n5', u'n6'], \
             [0, u'n6', u'n8'], [0, u'n10', u'n8'], [0, u'n7', u'n8'], \
             [0, u'n8', u'n9']] )

        G=networkx.parse_graphml(self.attribute_data.split('\n'))
        assert_equals(sorted(G.nodes()),[u'n0', u'n1', u'n2', u'n3', u'n4', u'n5'])
        assert_equals( sorted(G.edges(keys=True)),\
                [(u'n0', u'n1', u'e1'), (u'n0', u'n2', u'e0'), (u'n1', u'n3', u'e2'), \
                 (u'n2', u'n4', u'e4'), (u'n3', u'n2', u'e3'), (u'n3', u'n5', u'e5'), \
                 (u'n5', u'n3', u'e5'), (u'n5', u'n4', u'e6')] )
        assert_equals(sorted(G.nodes(data=True)),\
                [(u'n0', {u'color': u'green', u'id': u'n0'}),\
                (u'n1', {u'color': u'yellow', u'id': u'n1'}),\
                (u'n2', {u'color': u'blue', u'id': u'n2'}),\
                (u'n3', {u'color': u'yellow', u'id': u'n3', u'd3': u'red'}),\
                (u'n4', {u'color': u'yellow', u'id': u'n4'}),\
                (u'n5', {u'color': u'turquoise', u'id': u'n5'})] )
        assert_equals( sorted(G.edges(keys=True,data=True)),\
           [(u'n0', u'n1', u'e1', {u'source': u'n0', u'id': u'e1', u'weight': 1.0, u'target': u'n1'}),\
            (u'n0', u'n2', u'e0', {u'source': u'n0', u'id': u'e0', u'weight': 1.0, u'target': u'n2'}),\
            (u'n1', u'n3', u'e2', {u'source': u'n1', u'id': u'e2', u'weight': 2.0, u'target': u'n3'}),\
            (u'n2', u'n4', u'e4', {u'source': u'n2', u'id': u'e4', u'target': u'n4'}),\
            (u'n3', u'n2', u'e3', {u'source': u'n3', u'id': u'e3', u'target': u'n2'}),\
            (u'n3', u'n5', u'e5', {u'directed': u'false', u'source': u'n3', u'target': u'n5', u'id': u'e5'}),\
            (u'n5', u'n3', u'e5', {u'directed': u'false', u'source': u'n3', u'target': u'n5', u'id': u'e5'}),\
            (u'n5', u'n4', u'e6', {u'source': u'n5', u'id': u'e6', u'weight': 1.1, u'target': u'n4'})] )
Esempio n. 2
0
    def test_parse_graphml(self):
        G = networkx.parse_graphml(self.simple_data)
        assert_equals(sorted(G.nodes()),\
            [u'n0', u'n1', u'n10', u'n2', u'n3', u'n4', u'n5', u'n6', u'n7', u'n8', u'n9'])
        assert_equals( [sorted(e) for e in sorted(G.edges(keys=True))],\
            [[u'foo', u'n0', u'n2'], [0, u'n1', u'n2'], [0, u'n10', u'n8'], \
             [0, u'n2', u'n3'], [0, u'n3', u'n4'], [0, u'n3', u'n5'], \
             [0, u'n4', u'n6'], [0, u'n5', u'n7'], [0, u'n5', u'n6'], \
             [0, u'n6', u'n8'], [0, u'n10', u'n8'], [0, u'n7', u'n8'], \
             [0, u'n8', u'n9']] )

        G = networkx.parse_graphml(self.attribute_data.split('\n'))
        assert_equals(sorted(G.nodes()),
                      [u'n0', u'n1', u'n2', u'n3', u'n4', u'n5'])
        assert_equals( sorted(G.edges(keys=True)),\
                [(u'n0', u'n1', u'e1'), (u'n0', u'n2', u'e0'), (u'n1', u'n3', u'e2'), \
                 (u'n2', u'n4', u'e4'), (u'n3', u'n2', u'e3'), (u'n3', u'n5', u'e5'), \
                 (u'n5', u'n3', u'e5'), (u'n5', u'n4', u'e6')] )
        assert_equals(sorted(G.nodes(data=True)),\
                [(u'n0', {u'color': u'green', u'id': u'n0'}),\
                (u'n1', {u'color': u'yellow', u'id': u'n1'}),\
                (u'n2', {u'color': u'blue', u'id': u'n2'}),\
                (u'n3', {u'color': u'yellow', u'id': u'n3', u'd3': u'red'}),\
                (u'n4', {u'color': u'yellow', u'id': u'n4'}),\
                (u'n5', {u'color': u'turquoise', u'id': u'n5'})] )
        assert_equals( sorted(G.edges(keys=True,data=True)),\
           [(u'n0', u'n1', u'e1', {u'source': u'n0', u'id': u'e1', u'weight': 1.0, u'target': u'n1'}),\
            (u'n0', u'n2', u'e0', {u'source': u'n0', u'id': u'e0', u'weight': 1.0, u'target': u'n2'}),\
            (u'n1', u'n3', u'e2', {u'source': u'n1', u'id': u'e2', u'weight': 2.0, u'target': u'n3'}),\
            (u'n2', u'n4', u'e4', {u'source': u'n2', u'id': u'e4', u'target': u'n4'}),\
            (u'n3', u'n2', u'e3', {u'source': u'n3', u'id': u'e3', u'target': u'n2'}),\
            (u'n3', u'n5', u'e5', {u'directed': u'false', u'source': u'n3', u'target': u'n5', u'id': u'e5'}),\
            (u'n5', u'n3', u'e5', {u'directed': u'false', u'source': u'n3', u'target': u'n5', u'id': u'e5'}),\
            (u'n5', u'n4', u'e6', {u'source': u'n5', u'id': u'e6', u'weight': 1.1, u'target': u'n4'})] )
Esempio n. 3
0
def convert_traitgraph_to_dot(record_id, rec_num, format_string, directory):
    query = dict()
    query['_id'] = ObjectId(record_id)
    res = data.AxelrodStatsTreestructured.m.find(query)
    log.debug("converting traitgraphs from %s to dot", record_id)

    for result in res:
        graph_list = result['culture_graphml_repr']
        graphs = []
        #log.debug("num graphs in graphml list: %s", len(graph_list))
        for g in graph_list:
            g_c = g['content']
            graph = nx.parse_graphml(g_c)
            graphs.append(graph)

        for i in range(0, len(graphs)):
            #log.debug("writing file for graph: %s", i)
            name = str(record_id)
            name += "-"
            name += str(i)

            fname = directory
            fname += "/sample-"
            fname += format_string % rec_num
            fname += "-"
            fname += str(i)
            fname += "-"
            fname += str(record_id)
            fname += ".dot"
            write_ordered_dot(graphs[i], fname, name)
Esempio n. 4
0
def convert_traitgraph_to_dot(record_id, rec_num, format_string, directory):
    query = dict()
    query['_id'] = ObjectId(record_id)
    res = data.AxelrodStatsTreestructured.m.find(query)
    log.debug("converting traitgraphs from %s to dot", record_id)

    for result in res:
        graph_list = result['culture_graphml_repr']
        graphs = []
        #log.debug("num graphs in graphml list: %s", len(graph_list))
        for g in graph_list:
            g_c = g['content']
            graph = nx.parse_graphml(g_c)
            graphs.append(graph)

        for i in range(0, len(graphs)):
            #log.debug("writing file for graph: %s", i)
            name = str(record_id)
            name += "-"
            name += str(i)

            fname = directory
            fname += "/sample-"
            fname += format_string % rec_num
            fname += "-"
            fname += str(i)
            fname += "-"
            fname += str(record_id)
            fname += ".dot"
            write_ordered_dot(graphs[i], fname, name)
Esempio n. 5
0
def main():
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('task', help='GNPS FBMN Task ID')
    parser.add_argument('metadata_column', help='GNPS FBMN metadata_column')
    parser.add_argument('time_series',
                        help='GNPS FBMN time_series, comma separated')
    parser.add_argument('output_folder', help='output folder')
    parser.add_argument('--filter_metadata',
                        default="None",
                        help='filter_metadata_column')
    parser.add_argument('--filter_metadata_term',
                        default="None",
                        help='filter_metadata_term')
    parser.add_argument('--min_area_threshold',
                        type=int,
                        default=0,
                        help='min_area_threshold')

    args = parser.parse_args()

    # Acquiring Data
    task = args.task
    metadata_column = args.metadata_column
    time_series = args.time_series.split(",")
    filter_metadata_column = args.filter_metadata
    filter_metadata_term = args.filter_metadata_term
    MIN_AREA_THRESHOLD = args.min_area_threshold

    long_data_df = pd.read_csv(
        "http://gnps.ucsd.edu/ProteoSAFe/DownloadResultFile?task={}&block=main&file=feature_statistics/data_long.csv"
        .format(task))
    pairs_df = pd.DataFrame(
        requests.get(
            "https://gnps.ucsd.edu/ProteoSAFe/result_json.jsp?task={}&view=network_pairs_specnets_allcomponents"
            .format(task)).json()["blockData"])

    pairs_chemdir_df = calculate_chemdir(
        long_data_df,
        pairs_df,
        metadata_column,
        time_series,
        filter_metadata=filter_metadata_column,
        filter_metadata_term=filter_metadata_term,
        min_area_threshold=MIN_AREA_THRESHOLD)

    output_pairs_filename = os.path.join(args.output_folder,
                                         "chemdir_pairs.tsv")
    pairs_chemdir_df.to_csv(output_pairs_filename, sep="\t", index=False)

    # Formatting for GraphML
    r = requests.get(
        "http://gnps.ucsd.edu/ProteoSAFe/DownloadResultFile?task={}&block=main&file=gnps_molecular_network_graphml/"
        .format(task))
    G = nx.parse_graphml(r.text)
    merged_G = add_edges_network(G, pairs_chemdir_df)
    nx.write_graphml(merged_G,
                     os.path.join(args.output_folder,
                                  "chemdir_network.graphml"),
                     prettyprint=True)
Esempio n. 6
0
    def test_read_simple_undirected_graphml(self):
        G = self.simple_undirected_graph
        H = nx.read_graphml(self.simple_undirected_fh)
        assert_equal(sorted(G.nodes()), sorted(H.nodes()))
        assert_equal(sorted(sorted(e) for e in G.edges()), sorted(sorted(e) for e in H.edges()))
        self.simple_undirected_fh.seek(0)

        I = nx.parse_graphml(self.simple_undirected_data)
        assert_equal(sorted(G.nodes()), sorted(I.nodes()))
        assert_equal(sorted(sorted(e) for e in G.edges()), sorted(sorted(e) for e in I.edges()))
Esempio n. 7
0
    def test_read_simple_undirected_graphml(self):
        G = self.simple_undirected_graph
        H = nx.read_graphml(self.simple_undirected_fh)
        assert_nodes_equal(G.nodes(), H.nodes())
        assert_edges_equal(G.edges(), H.edges())
        self.simple_undirected_fh.seek(0)

        I = nx.parse_graphml(self.simple_undirected_data)
        assert_nodes_equal(G.nodes(), I.nodes())
        assert_edges_equal(G.edges(), I.edges())
Esempio n. 8
0
    def test_read_simple_undirected_graphml(self):
        G = self.simple_undirected_graph
        H = nx.read_graphml(self.simple_undirected_fh)
        assert_nodes_equal(G.nodes(), H.nodes())
        assert_edges_equal(G.edges(), H.edges())
        self.simple_undirected_fh.seek(0)

        I = nx.parse_graphml(self.simple_undirected_data)
        assert_nodes_equal(G.nodes(), I.nodes())
        assert_edges_equal(G.edges(), I.edges())
Esempio n. 9
0
    def test_read_simple_directed_graphml(self):
        G = self.simple_directed_graph
        H = nx.read_graphml(self.simple_directed_fh)
        assert_equal(sorted(G.nodes()), sorted(H.nodes()))
        assert_equal(sorted(G.edges()), sorted(H.edges()))
        assert_equal(sorted(G.edges(data=True)), sorted(H.edges(data=True)))
        self.simple_directed_fh.seek(0)

        I = nx.parse_graphml(self.simple_directed_data)
        assert_equal(sorted(G.nodes()), sorted(I.nodes()))
        assert_equal(sorted(G.edges()), sorted(I.edges()))
        assert_equal(sorted(G.edges(data=True)), sorted(I.edges(data=True)))
Esempio n. 10
0
 def test_read_graphml(self):
     import os,tempfile
     (fd,fname)=tempfile.mkstemp()
     fh=open(fname,'w')
     fh.write(self.simple_data)
     fh.close()
     Gin=networkx.read_graphml(fname)
     G=networkx.parse_graphml(self.simple_data)
     assert_equals( sorted(G.nodes(data=True)), sorted(Gin.nodes(data=True)))
     assert_equals( sorted(G.edges(data=True)), sorted(Gin.edges(data=True)))
     os.close(fd)
     os.unlink(fname)
Esempio n. 11
0
 def test_read_graphml(self):
     import os, tempfile
     (fd, fname) = tempfile.mkstemp()
     fh = open(fname, 'w')
     fh.write(self.simple_data)
     fh.close()
     Gin = networkx.read_graphml(fname)
     G = networkx.parse_graphml(self.simple_data)
     assert_equals(sorted(G.nodes(data=True)), sorted(Gin.nodes(data=True)))
     assert_equals(sorted(G.edges(data=True)), sorted(Gin.edges(data=True)))
     os.close(fd)
     os.unlink(fname)
Esempio n. 12
0
    def test_graphml_header_line(self):
        good = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
         http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="d0" for="node" attr.name="test" attr.type="boolean">
    <default>false</default>
  </key>
  <graph id="G">
    <node id="n0">
      <data key="d0">true</data>
    </node>
  </graph>
</graphml>
"""
        bad = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<graphml>
  <key id="d0" for="node" attr.name="test" attr.type="boolean">
    <default>false</default>
  </key>
  <graph id="G">
    <node id="n0">
      <data key="d0">true</data>
    </node>
  </graph>
</graphml>
"""
        ugly = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<graphml xmlns="https://ghghgh">
  <key id="d0" for="node" attr.name="test" attr.type="boolean">
    <default>false</default>
  </key>
  <graph id="G">
    <node id="n0">
      <data key="d0">true</data>
    </node>
  </graph>
</graphml>
"""
        for s in (good, bad):
            fh = io.BytesIO(s.encode('UTF-8'))
            G = nx.read_graphml(fh)
            H = nx.parse_graphml(s)
            for graph in [G, H]:
                assert_equal(graph.nodes['n0']['test'], True)

        fh = io.BytesIO(ugly.encode('UTF-8'))
        assert_raises(nx.NetworkXError, nx.read_graphml, fh)
        assert_raises(nx.NetworkXError, nx.parse_graphml, ugly)
Esempio n. 13
0
    def test_graphml_header_line(self):
        good = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
         http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="d0" for="node" attr.name="test" attr.type="boolean">
    <default>false</default>
  </key>
  <graph id="G">
    <node id="n0">
      <data key="d0">true</data>
    </node>
  </graph>
</graphml>
"""
        bad = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<graphml>
  <key id="d0" for="node" attr.name="test" attr.type="boolean">
    <default>false</default>
  </key>
  <graph id="G">
    <node id="n0">
      <data key="d0">true</data>
    </node>
  </graph>
</graphml>
"""
        ugly = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<graphml xmlns="https://ghghgh">
  <key id="d0" for="node" attr.name="test" attr.type="boolean">
    <default>false</default>
  </key>
  <graph id="G">
    <node id="n0">
      <data key="d0">true</data>
    </node>
  </graph>
</graphml>
"""
        for s in (good, bad):
            fh = io.BytesIO(s.encode('UTF-8'))
            G = nx.read_graphml(fh)
            H = nx.parse_graphml(s)
            for graph in [G, H]:
                assert_equal(graph.nodes['n0']['test'], True)

        fh = io.BytesIO(ugly.encode('UTF-8'))
        assert_raises(nx.NetworkXError, nx.read_graphml, fh)
        assert_raises(nx.NetworkXError, nx.parse_graphml, ugly)
Esempio n. 14
0
 def test_command_graph(self):
     matches = [
         ((0, 0), (3, 3)),
         ((1, 11), (4, 14)),
         ((8, 12), (11, 15)),
     ]
     regions = utils.serialize_json(matches)
     result = self.runner.invoke(
         cli.graph,
         [regions, self.image_grid]
     )
     out = nx.parse_graphml(result.output.strip())
     graph = nx.read_gml(fixtures_path('graph.gml'))
     assert nodeset(out) == nodeset(graph)
     assert edgeset(out) == edgeset(graph)
Esempio n. 15
0
    def test_read_attribute_graphml(self):
        G = self.attribute_graph
        H = nx.read_graphml(self.attribute_fh)
        assert_nodes_equal(G.nodes(True), sorted(H.nodes(data=True)))
        ge = sorted(G.edges(data=True))
        he = sorted(H.edges(data=True))
        for a, b in zip(ge, he):
            assert_equal(a, b)
        self.attribute_fh.seek(0)

        I = nx.parse_graphml(self.attribute_data)
        assert_equal(sorted(G.nodes(True)), sorted(I.nodes(data=True)))
        ge = sorted(G.edges(data=True))
        he = sorted(I.edges(data=True))
        for a, b in zip(ge, he):
            assert_equal(a, b)
Esempio n. 16
0
    def test_read_attribute_graphml(self):
        G = self.attribute_graph
        H = nx.read_graphml(self.attribute_fh)
        assert_equal(sorted(G.nodes(True)), sorted(H.nodes(data=True)))
        ge = sorted(G.edges(data=True))
        he = sorted(H.edges(data=True))
        for a, b in zip(ge, he):
            assert_equal(a, b)
        self.attribute_fh.seek(0)

        I = nx.parse_graphml(self.attribute_data)
        assert_equal(sorted(G.nodes(True)), sorted(I.nodes(data=True)))
        ge = sorted(G.edges(data=True))
        he = sorted(I.edges(data=True))
        for a, b in zip(ge, he):
            assert_equal(a, b)
Esempio n. 17
0
    def test_bool(self):
        s = """<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
         http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="d0" for="node" attr.name="test" attr.type="boolean">
    <default>false</default>
  </key>
  <graph id="G" edgedefault="directed">
    <node id="n0">
      <data key="d0">true</data>
    </node>
    <node id="n1"/>
    <node id="n2">
      <data key="d0">false</data>
    </node>
    <node id="n3">
      <data key="d0">FaLsE</data>
    </node>
    <node id="n4">
      <data key="d0">True</data>
    </node>
    <node id="n5">
      <data key="d0">0</data>
    </node>
    <node id="n6">
      <data key="d0">1</data>
    </node>
  </graph>
</graphml>
"""
        fh = io.BytesIO(s.encode('UTF-8'))
        G = nx.read_graphml(fh)
        H = nx.parse_graphml(s)
        for graph in [G, H]:
            assert_equal(graph.nodes['n0']['test'], True)
            assert_equal(graph.nodes['n2']['test'], False)
            assert_equal(graph.nodes['n3']['test'], False)
            assert_equal(graph.nodes['n4']['test'], True)
            assert_equal(graph.nodes['n5']['test'], False)
            assert_equal(graph.nodes['n6']['test'], True)
Esempio n. 18
0
    def test_bool(self):
        s = """<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
        http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="d0" for="node" attr.name="test" attr.type="boolean">
    <default>false</default>
  </key>
  <graph id="G" edgedefault="directed">
    <node id="n0">
      <data key="d0">true</data>
    </node>
    <node id="n1"/>
    <node id="n2">
      <data key="d0">false</data>
    </node>
    <node id="n3">
      <data key="d0">FaLsE</data>
    </node>
    <node id="n4">
      <data key="d0">True</data>
    </node>
    <node id="n5">
      <data key="d0">0</data>
    </node>
    <node id="n6">
      <data key="d0">1</data>
    </node>
  </graph>
</graphml>
"""
        fh = io.BytesIO(s.encode('UTF-8'))
        G = nx.read_graphml(fh)
        H = nx.parse_graphml(s)
        for graph in [G, H]:
            assert_equal(graph.node['n0']['test'], True)
            assert_equal(graph.node['n2']['test'], False)
            assert_equal(graph.node['n3']['test'], False)
            assert_equal(graph.node['n4']['test'], True)
            assert_equal(graph.node['n5']['test'], False)
            assert_equal(graph.node['n6']['test'], True)
Esempio n. 19
0
    def test_multigraph_keys(self):
        # Test that reading multigraphs uses edge id attributes as keys
        s = """<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
         http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <graph id="G" edgedefault="directed">
    <node id="n0"/>
    <node id="n1"/>
    <edge id="e0" source="n0" target="n1"/>
    <edge id="e1" source="n0" target="n1"/>
  </graph>
</graphml>
"""
        fh = io.BytesIO(s.encode('UTF-8'))
        G = nx.read_graphml(fh)
        expected = [("n0", "n1", "e0"), ("n0", "n1", "e1")]
        assert_equal(sorted(G.edges(keys=True)), expected)
        fh.seek(0)
        H = nx.parse_graphml(s)
        assert_equal(sorted(H.edges(keys=True)), expected)
Esempio n. 20
0
    def test_multigraph_keys(self):
        # Test that reading multigraphs uses edge id attributes as keys
        s = """<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
         http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <graph id="G" edgedefault="directed">
    <node id="n0"/>
    <node id="n1"/>
    <edge id="e0" source="n0" target="n1"/>
    <edge id="e1" source="n0" target="n1"/>
  </graph>
</graphml>
"""
        fh = io.BytesIO(s.encode('UTF-8'))
        G = nx.read_graphml(fh)
        expected = [("n0", "n1", "e0"), ("n0", "n1", "e1")]
        assert_equal(sorted(G.edges(keys=True)), expected)
        fh.seek(0)
        H = nx.parse_graphml(s)
        assert_equal(sorted(H.edges(keys=True)), expected)
            continue
        url = query_result['url']
        try:
            if query_result['name'][-3:].lower() == 'zip':
                base_dir_graphml_tagged = base_dir_harvard + 'graph_ml_tagged/'
                zipped_fp = utils.get_zip_fp(query_result['url'])
                for contents in zipped_fp.infolist():
                    if not contents.is_dir():
                        name = contents.filename
                        base_name = contents.filename.split('.')[-2].replace(
                            '/', '-')
                        edge_path = base_dir_graphml_tagged + 'edge_lists/' + base_name + '.csv'

                        if exists(edge_path):
                            continue
                        G = nx.parse_graphml(
                            zipped_fp.read(contents.filename).decode('utf-8'))
            else:
                base_dir_graphml_tagged = base_dir_harvard + 'graph_ml_tagged_non_zipped/'
                base_name = query_result['name'].split('.')[-2].replace(
                    '/', '-')
                edge_path = base_dir_graphml_tagged + 'edge_lists/' + base_name + '.csv'
                if exists(edge_path):
                    continue
                name = base_name
                with urllib.request.urlopen(url) as graph_ml_fp:
                    graph_ml_lines = graph_ml_fp.read().decode('utf-8')
                G = nx.parse_graphml(graph_ml_lines)
        except:
            print("Couldn't parse: " + name)
            continue
        edge_path = base_dir_graphml_tagged + 'edge_lists/' + base_name + '.csv'
Esempio n. 22
0
    def test_yfiles_extension(self):
        data = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:y="http://www.yworks.com/xml/graphml"
         xmlns:yed="http://www.yworks.com/xml/yed/3"
         xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
         http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <!--Created by yFiles for Java 2.7-->
  <key for="graphml" id="d0" yfiles.type="resources"/>
  <key attr.name="url" attr.type="string" for="node" id="d1"/>
  <key attr.name="description" attr.type="string" for="node" id="d2"/>
  <key for="node" id="d3" yfiles.type="nodegraphics"/>
  <key attr.name="Description" attr.type="string" for="graph" id="d4">
    <default/>
  </key>
  <key attr.name="url" attr.type="string" for="edge" id="d5"/>
  <key attr.name="description" attr.type="string" for="edge" id="d6"/>
  <key for="edge" id="d7" yfiles.type="edgegraphics"/>
  <graph edgedefault="directed" id="G">
    <node id="n0">
      <data key="d3">
        <y:ShapeNode>
          <y:Geometry height="30.0" width="30.0" x="125.0" y="100.0"/>
          <y:Fill color="#FFCC00" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content"
           borderDistance="0.0" fontFamily="Dialog" fontSize="13"
           fontStyle="plain" hasBackgroundColor="false" hasLineColor="false"
           height="19.1328125" modelName="internal" modelPosition="c"
           textColor="#000000" visible="true" width="12.27099609375"
           x="8.864501953125" y="5.43359375">1</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <node id="n1">
      <data key="d3">
        <y:ShapeNode>
          <y:Geometry height="30.0" width="30.0" x="183.0" y="205.0"/>
          <y:Fill color="#FFCC00" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content"
          borderDistance="0.0" fontFamily="Dialog" fontSize="13"
          fontStyle="plain" hasBackgroundColor="false" hasLineColor="false"
          height="19.1328125" modelName="internal" modelPosition="c"
          textColor="#000000" visible="true" width="12.27099609375"
          x="8.864501953125" y="5.43359375">2</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <edge id="e0" source="n0" target="n1">
      <data key="d7">
        <y:PolyLineEdge>
          <y:Path sx="0.0" sy="0.0" tx="0.0" ty="0.0"/>
          <y:LineStyle color="#000000" type="line" width="1.0"/>
          <y:Arrows source="none" target="standard"/>
          <y:BendStyle smoothed="false"/>
        </y:PolyLineEdge>
      </data>
    </edge>
  </graph>
  <data key="d0">
    <y:Resources/>
  </data>
</graphml>
"""
        fh = io.BytesIO(data.encode('UTF-8'))
        G = nx.read_graphml(fh)
        assert_equal(list(G.edges()), [('n0', 'n1')])
        assert_equal(G['n0']['n1']['id'], 'e0')
        assert_equal(G.nodes['n0']['label'], '1')
        assert_equal(G.nodes['n1']['label'], '2')

        H = nx.parse_graphml(data)
        assert_equal(list(H.edges()), [('n0', 'n1')])
        assert_equal(H['n0']['n1']['id'], 'e0')
        assert_equal(H.nodes['n0']['label'], '1')
        assert_equal(H.nodes['n1']['label'], '2')
Esempio n. 23
0
def show_graph(graph_str: str):
    G = nx.parse_graphml(graph_str)
    nx.draw(G, pos=(nx.planar_layout(G)), with_labels=True)
    plt.show()
Esempio n. 24
0
def convert_topology(xml_root, out_file, set_labels):
    graphml_ns = 'http://graphml.graphdrawing.org/xmlns'
    graphml_ns_prefix = '{' + graphml_ns + '}'

    ET.register_namespace('', graphml_ns)

    # map of functions to apply to text/values for elements with a given id
    id_type_conversions = {}

    # remap any of the attribute names and types, and build `id_type_conversions`
    for x in xml_root.findall('{}key'.format(graphml_ns_prefix)):
        if x.attrib['attr.name'] in ATTR_CONVERSIONS:
            (attr_name, attr_type,
             attr_map_fn) = ATTR_CONVERSIONS[x.attrib['attr.name']]
            x.attrib['attr.name'] = attr_name
            if attr_type != None:
                x.attrib['attr.type'] = attr_type
            if attr_map_fn != None:
                id_type_conversions[x.attrib['id']] = attr_map_fn

    # transform the text/values
    for x in xml_root.findall('{}graph'.format(graphml_ns_prefix)):
        for y in x.findall('{}data'.format(graphml_ns_prefix)):
            if y.attrib['key'] in id_type_conversions:
                y.text = id_type_conversions[y.attrib['key']](y.text)
        nodes = x.findall('{}node'.format(graphml_ns_prefix))
        edges = x.findall('{}edge'.format(graphml_ns_prefix))
        for y in nodes + edges:
            for z in y.findall('{}data'.format(graphml_ns_prefix)):
                if z.attrib['key'] in id_type_conversions:
                    z.text = id_type_conversions[z.attrib['key']](z.text)

    removed_graph_keys = {}
    removed_graph_data = {}

    # collect and remove the keys for any unsupported graph attributes
    for x in xml_root.findall('{}key'.format(graphml_ns_prefix)):
        # if x.attrib['attr.name'] in UNSUPPORTED_ATTRS:
        if x.attrib['for'] == 'graph':
            removed_graph_keys[x.attrib['id']] = x.attrib['attr.name']
            xml_root.remove(x)

    # store and remove the text/values for any unsupported graph attributes
    for x in xml_root.findall('{}graph'.format(graphml_ns_prefix)):
        for y in x.findall('{}data'.format(graphml_ns_prefix)):
            if y.attrib['key'] in removed_graph_keys:
                removed_graph_data[removed_graph_keys[
                    y.attrib['key']]] = y.text
                x.remove(y)

    # build the graph from the xml
    xml = ET.tostring(xml_root, encoding='utf-8', method='xml').decode('utf-8')
    graph = nx.parse_graphml(xml)

    # shadow doesn't use any attributes that would go in 'node_default' or
    # 'edge_default', so we don't expect there to be any
    if 'node_default' in graph.graph:
        assert len(graph.graph['node_default']) == 0
        del graph.graph['node_default']

    if 'edge_default' in graph.graph:
        assert len(graph.graph['edge_default']) == 0
        del graph.graph['edge_default']

    # Only change node and edge labels if the label option was given.
    # Our custom labels help avoid cross-ref from an edge to a node via the node's
    # numeric id, which can be a pain especially on large graphs.
    if set_labels:
        graph = nx.relabel_nodes(
            graph, lambda x: f"node at {graph.nodes[x]['ip_address']}")
        for (source, target) in graph.edges:
            src_ip = graph.nodes[source]['ip_address']
            tgt_ip = graph.nodes[target]['ip_address']
            graph[source][target]['label'] = f"path from {src_ip} to {tgt_ip}"

    # generate gml from the graph
    try:
        nx.write_gml(graph, out_file)
    except nx.exception.NetworkXError:
        # we require keys with underscores which isn't technically allowed by the
        # spec, but both igraph and recent versions of networkx allow them
        print("Unable to write GML. Do you have networkx version >= 2.5?",
              file=sys.stderr)
        raise

    return removed_graph_data
Esempio n. 25
0
    def test_yfiles_extension(self):
        data = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xmlns:yed="http://www.yworks.com/xml/yed/3" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd">
  <!--Created by yFiles for Java 2.7-->
  <key for="graphml" id="d0" yfiles.type="resources"/>
  <key attr.name="url" attr.type="string" for="node" id="d1"/>
  <key attr.name="description" attr.type="string" for="node" id="d2"/>
  <key for="node" id="d3" yfiles.type="nodegraphics"/>
  <key attr.name="Description" attr.type="string" for="graph" id="d4">
    <default/>
  </key>
  <key attr.name="url" attr.type="string" for="edge" id="d5"/>
  <key attr.name="description" attr.type="string" for="edge" id="d6"/>
  <key for="edge" id="d7" yfiles.type="edgegraphics"/>
  <graph edgedefault="directed" id="G">
    <node id="n0">
      <data key="d3">
        <y:ShapeNode>
          <y:Geometry height="30.0" width="30.0" x="125.0" y="100.0"/>
          <y:Fill color="#FFCC00" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" borderDistance="0.0" fontFamily="Dialog" fontSize="13" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="19.1328125" modelName="internal" modelPosition="c" textColor="#000000" visible="true" width="12.27099609375" x="8.864501953125" y="5.43359375">1</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <node id="n1">
      <data key="d3">
        <y:ShapeNode>
          <y:Geometry height="30.0" width="30.0" x="183.0" y="205.0"/>
          <y:Fill color="#FFCC00" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" borderDistance="0.0" fontFamily="Dialog" fontSize="13" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="19.1328125" modelName="internal" modelPosition="c" textColor="#000000" visible="true" width="12.27099609375" x="8.864501953125" y="5.43359375">2</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <edge id="e0" source="n0" target="n1">
      <data key="d7">
        <y:PolyLineEdge>
          <y:Path sx="0.0" sy="0.0" tx="0.0" ty="0.0"/>
          <y:LineStyle color="#000000" type="line" width="1.0"/>
          <y:Arrows source="none" target="standard"/>
          <y:BendStyle smoothed="false"/>
        </y:PolyLineEdge>
      </data>
    </edge>
  </graph>
  <data key="d0">
    <y:Resources/>
  </data>
</graphml>
"""
        fh = io.BytesIO(data.encode("UTF-8"))
        G = nx.read_graphml(fh)
        assert_equal(G.edges(), [("n0", "n1")])
        assert_equal(G["n0"]["n1"]["id"], "e0")
        assert_equal(G.node["n0"]["label"], "1")
        assert_equal(G.node["n1"]["label"], "2")

        H = nx.parse_graphml(data)
        assert_equal(H.edges(), [("n0", "n1")])
        assert_equal(H["n0"]["n1"]["id"], "e0")
        assert_equal(H.node["n0"]["label"], "1")
        assert_equal(H.node["n1"]["label"], "2")