Skip to content

Commit

Permalink
Update P2N-NetworksBis.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Patent2net committed Jul 23, 2021
1 parent f8b1160 commit 792161d
Showing 1 changed file with 5 additions and 178 deletions.
183 changes: 5 additions & 178 deletions Patent2Net/P2N-NetworksBis.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ def Cleaning(texte): # this is for graphviz. Maybe an ascii converter would be o
os.remove(ResultGephiPath+'/'+outputFile)
except:
pass
nx.write_gexf(G1, ResultGephiPath+'/'+outputFile, version='1.2draft')

# nx.set_edge_attributes( G1, WeightDyn, "weight")
AtribDyn=OrderedDict()
Atrib = OrderedDict()
Expand All @@ -601,187 +601,14 @@ def Cleaning(texte): # this is for graphviz. Maybe an ascii converter would be o

# setting nodes attributes (id, start, end in time format)
nx.set_node_attributes(G1, AtribDyn)

nx.write_gexf(G1, ResultGephiPath + '/' + outputFile, version='1.2draft')
# nx.write_gpickle(G1, temporPath+'/'+network+prefix)
#
#
nx.write_gexf(G1, ResultGephiPath+'/temp.gexf', version='1.2draft')

#from there its a hack (sometimes ugly) as networkx2.2 doesn't deal correctly with dynamics nets
with open(ResultGephiPath+'/temp.gexf', 'r', encoding='utf8') as fic:
data = fic.readlines()
enco = data[0:2]
temp=data[1:1]
temp.append(""" <graph defaultedgetype="directed" mode="dynamic" name="" timeformat="date">\n""")
enco.append(""" <graph defaultedgetype="directed" mode="dynamic" name="" timeformat="date">\n""")
# temp.append(""" <attributes class="edge" mode="static">\n""")
# temp.append(""" <attribute id="5" title="name" type="string" />\n""")
# temp.append(""" </attributes>\n""")
temp.append(""" <attributes class="edge" mode="dynamic" timeformat="date">\n""" )
temp.append(""" <attribute id="5" title="weight" type="double" />\n""")
temp.append(""" </attributes>\n""")
temp.append(""" <attributes class="node" mode="static" timeformat="date">\n""")
temp.append(""" <attribute id="0" title="category" type="string" />\n""")
temp.append(""" <attribute id="1" title="degree_in" type="long" />\n""")
temp.append(""" <attribute id="2" title="degree_out" type="long" /> \n""")
temp.append(""" <attribute id="3" title="degree" type="long" />\n""")
temp.append(""" <attribute id="4" title="url" type="string" />\n""")
temp.append(""" </attributes>\n""")

temp.append(""" <meta>\n""")
temp.append(""" <creator>Patent2Net V3</creator>\n""")
temp += data[16:]
data= "".join(temp)
with open(ResultGephiPath+'/temp2.gexf', "w", encoding='utf8') as fic:
fic.write(enco[1] + data)

with open(ResultGephiPath+'/temp2.gexf', encoding='utf8') as fic:
# don = fic.readlines()
# data = ''.join(don[1:])
data=fic.read()
don = data.splitlines()
root = etree.fromstring(data.strip())
nodes = [truc for truc in root.iterdescendants()]
bords = [truc for truc in don if truc.count('edges')]
if ResultGephiPath+'/HackTest.gexf' in os.listdir(ResultGephiPath+'/'):
os.remove(ResultGephiPath+'/HackTest.gexf')
with open(ResultGephiPath+'/HackTest.gexf', 'w', encoding='utf8') as ficRes:
ficRes.write("".join(enco))
# ficRes.write(don[1])
# ficRes.write(don[2])
for el in nodes:
if el.tag in ["{http://www.gexf.net/1.2draft}attributes",
"{http://www.gexf.net/1.2draft}meta",
]:
ficRes.write(str(etree.tostring(el, pretty_print=True, encoding='utf8', method='xml')))

elif el.tag in ["{http://www.gexf.net/1.2draft}nodes"]:
ficRes.write(str(etree.tostring(el, pretty_print=True, encoding='utf8', method='xml')))
ficRes.write(bords[0])
elif el.tag == "{http://www.gexf.net/1.2draft}edge":
rac = copy.copy(el)
rac.clear()
rac.attrib['source']= el.attrib['source']
rac.attrib['target'] = el.attrib['target']
for attr in el.iterchildren():
compt = 0
NewAttr=etree.Element(attr.tag)
for attrs in attr.iterchildren():
# NewAttrs =etree.Element( attrs.tag)

if attrs.attrib['for'] == '5':

SRC = int(el.attrib['source'])
TGT = int(el.attrib['target'])
if (SRC, TGT) in WeightDyn.keys():
for poids in WeightDyn[(SRC, TGT)]:

attrs.attrib['value'] = str(poids['value'])
attrs.attrib['start'] = poids['start']
attrs.attrib['end'] = poids['end']
#NewAttrs =etree.Element(attrs)
NewAttr.append(attrs)
else:
NewAttr.append(attrs)
rac.append(NewAttr)
ficRes.write(str(etree.tostring( rac, pretty_print=True, encoding='utf8', method='xml')))

ficRes.write(don[len(don)-3])
ficRes.write(don[len(don)-2].strip())
ficRes.write(don[len(don)-1])
if outputFile in os.listdir(ResultGephiPath+'/'):
os.remove(ResultGephiPath+'/'+outputFile)
#Doing same hacking system for networkJS exports
# this could be factorised!!!!
with open(ResultGephiPath+'/temp.gexf', encoding='utf8') as fic:
data = fic.readlines()
enco = data[0:1]
temp=data[1:2]
temp.append(""" <graph defaultedgetype="directed" mode="static" name="" >\n""")
enco.append(""" <graph defaultedgetype="directed" mode="static" name="">\n""")
# temp.append(""" <attributes class="edge" mode="static">\n""")
# temp.append(""" <attribute id="5" title="name" type="string" />\n""")
# temp.append(""" </attributes>\n""")
temp.append(""" <attributes class="edge" mode="static">\n""" )
temp.append(""" <attribute id="5" title="weight" type="double" />\n""")
temp.append(""" </attributes>\n""")
temp.append(""" <attributes class="node" mode="static" timeformat="date">\n""")
temp.append(""" <attribute id="0" title="category" type="string" />\n""")
temp.append(""" <attribute id="1" title="degree_in" type="long" />\n""")
temp.append(""" <attribute id="2" title="degree_out" type="long" /> \n""")
temp.append(""" <attribute id="3" title="degree" type="long" />\n""")
temp.append(""" <attribute id="4" title="url" type="string" />\n""")
temp.append(""" </attributes>\n""")

temp.append(""" <meta>\n""")
temp.append(""" <creator>Patent2Net V3</creator>\n""")
temp += data[14:]
data= "".join(temp)

with open(ResultGephiPath+'/temp2.gexf', "w", encoding='utf8') as fic:
fic.write( data)

with open(ResultGephiPath+'/temp2.gexf', encoding='utf8') as fic:
# don = fic.readlines()
# data = ''.join(don[1:])
data=fic.read()
don = data.splitlines()
root = etree.fromstring(data.strip())
nodes = [truc for truc in root.iterdescendants()]
bords = [truc for truc in don if truc.count('edges')]
if ResultGephiPath+'/HackTestJS.gexf' in os.listdir(ResultGephiPath+'/'):
os.remove(ResultGephiPath+'/HackTestJS.gexf')
with open(ResultGephiPath+'/HackTestJS.gexf', 'w') as ficRes:
ficRes.write("".join(enco))
# ficRes.write(don[1])
# ficRes.write(don[2])
for el in nodes:
if el.tag in ["{http://www.gexf.net/1.2draft}attributes",
"{http://www.gexf.net/1.2draft}meta",
]:
ficRes.write(str(etree.tostring(el, pretty_print=True, encoding='utf8', method='xml')))

elif el.tag in ["{http://www.gexf.net/1.2draft}edges"]:
ficRes.write(str(etree.tostring(el, pretty_print=True, encoding='utf8', method='xml')))
ficRes.write(bords[0])
#parsing this time node attributes to avoid dynamic attributes (LOL)
elif el.tag == "{http://www.gexf.net/1.2draft}node":
rac = copy.copy(el)
rac.clear()
rac.attrib['id']= el.attrib['id']
rac.attrib['label'] = el.attrib['label']
for attr in el.iterchildren():

rac.append(attr)
compt = 0
NewAttr=etree.Element(attr.tag)
for attrs in attr.iterchildren():
#NewAttrs =etree.Element( attrs.tag)
NewAttr.append(attrs)
# if attrs.attrib['for'] == '5':
#
# SRC = int(el.attrib['source'])
# TGT = int(el.attrib['target'])
# if (SRC, TGT) in WeightDyn.keys():
# for poids in WeightDyn[(SRC, TGT)]:
#
# attrs.attrib['value'] = str(poids['value'])
# attrs.attrib['start'] = poids['start']
# attrs.attrib['end'] = poids['end']
# #NewAttrs =etree.Element(attrs)
# NewAttr.append(attrs)
# else:
#
rac.append(NewAttr)
ficRes.write(str(etree.tostring( rac, pretty_print=True, encoding='utf8', method='xml')))

ficRes.write(don[len(don)-3])
ficRes.write(don[len(don)-2].strip())
ficRes.write(don[len(don)-1])

# nx.write_gexf(G1, ResultGephiPath+'/temp.gexf', version='1.2draft')

#making the html from model
RenderTemplate(
RenderTemplate(
"Graphe.html",
ResultGephiPath + '/'+outputFile.replace('.gexf','JS.html'),
TitleNet=network[1:]+' Network for ' + requete,
Expand All @@ -792,7 +619,7 @@ def Cleaning(texte): # this is for graphviz. Maybe an ascii converter would be o

# making the js from model
# maybe we could adjust node size and other parameters here
RenderTemplate(
RenderTemplate(
"gephiConfig.js",
ResultGephiPath + '/'+outputFile.replace('.gexf','') +'Conf.js',
FicRezo=outputFile,
Expand Down

0 comments on commit 792161d

Please sign in to comment.