Commit e5af6123 authored by Benoit GAUZERE's avatar Benoit GAUZERE

Implementation of attributes genericity for both gxl and ct functions

parent 19c8c91c
......@@ -2,7 +2,6 @@
"""
def loadCT(filename):
import networkx as nx
"""load data from .ct file.
Notes
......@@ -16,26 +15,29 @@ def loadCT(filename):
1 3 1 1 <- each line describes an edge : to, from,?, label
2 3 1 1
"""
import networkx as nx
from os.path import basename
content = open(filename).read().splitlines()
g = nx.Graph(name=str(content[0]), filename=basename(filename)) # set name of the graph
tmp = content[1].split(" ")
if tmp[0] == '':
nb_nodes = int(tmp[1]) # number of the nodes
nb_edges = int(tmp[2]) # number of the edges
else:
nb_nodes = int(tmp[0])
nb_edges = int(tmp[1])
for i in range(0, nb_nodes):
tmp = content[i + 2].split(" ")
tmp = [x for x in tmp if x != '']
g.add_node(i, label=tmp[3])
for i in range(0, nb_edges):
tmp = content[i + g.number_of_nodes() + 2]
tmp = [tmp[i:i+3] for i in range(0, len(tmp), 3)]
g.add_edge(int(tmp[0]) - 1, int(tmp[1]) - 1, label=int(tmp[3]))
g = nx.Graph()
with open(filename) as f:
content = f.read().splitlines()
g = nx.Graph(name=str(content[0]), filename=basename(filename)) # set name of the graph
tmp = content[1].split(" ")
if tmp[0] == '':
nb_nodes = int(tmp[1]) # number of the nodes
nb_edges = int(tmp[2]) # number of the edges
else:
nb_nodes = int(tmp[0])
nb_edges = int(tmp[1])
for i in range(0, nb_nodes):
tmp = content[i + 2].split(" ")
tmp = [x for x in tmp if x != '']
g.add_node(i, atom=tmp[3])
for i in range(0, nb_edges):
tmp = content[i + g.number_of_nodes() + 2]
tmp = [tmp[i:i+3] for i in range(0, len(tmp), 3)]
g.add_edge(int(tmp[0]) - 1, int(tmp[1]) - 1, bond_type=tmp[3].strip())
return g
......@@ -48,16 +50,21 @@ def loadGXL(filename):
root = tree.getroot()
index = 0
g = nx.Graph(filename=basename(filename), name=root[0].attrib['id'])
dic = {}
dic = {} #used to retrieve incident nodes of edges
for node in root.iter('node'):
label = node.find('attr')[0].text # Take only one attribute
dic[node.attrib['id']] = index
g.add_node(index, id=node.attrib['id'], label=label)
labels = {}
for attr in node.iter('attr'):
labels[attr.attrib['name']] = attr[0].text
g.add_node(index, **labels)
index += 1
for edge in root.iter('edge'):
label = edge.find('attr')[0].text
g.add_edge(dic[edge.attrib['from']], dic[edge.attrib['to']], label=label)
labels = {}
for attr in edge.iter('attr'):
labels[attr.attrib['name']] = attr[0].text
g.add_edge(dic[edge.attrib['from']], dic[edge.attrib['to']], **labels)
return g
def saveGXL(graph, filename):
......@@ -72,10 +79,7 @@ def saveGXL(graph, filename):
for v in graph:
current_node = ET.SubElement(graph_node, 'node', attrib={'id' : str(v)})
for attr in graph.nodes[v].keys():
#hard fix : force to chem before taking into account genericity
# must be attr instead of label
label = 'chem'
cur_attr = ET.SubElement(current_node, 'attr', attrib={'name' : label})
cur_attr = ET.SubElement(current_node, 'attr', attrib={'name' : attr})
cur_value = ET.SubElement(cur_attr,graph.nodes[v][attr].__class__.__name__)
cur_value.text = graph.nodes[v][attr]
......@@ -85,10 +89,7 @@ def saveGXL(graph, filename):
cur_edge = ET.SubElement(graph_node, 'edge', attrib={'from' : str(v1),
'to' : str(v2)})
for attr in graph[v1][v2].keys():
#hard fix : force to chem before taking into account genericity
# must be attr instead of label
label = 'valence'
cur_attr = ET.SubElement(cur_edge, 'attr', attrib={'name' : label})
cur_attr = ET.SubElement(cur_edge, 'attr', attrib={'name' : attr})
cur_value = ET.SubElement(cur_attr, graph[v1][v2][attr].__class__.__name__)
cur_value.text = str(graph[v1][v2][attr])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment