forked from fani-lab/SEERa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphToText.py
51 lines (48 loc) · 1.9 KB
/
GraphToText.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import networkx as nx
import glob
import numpy as np
import params
def G2T(graph_path, path2_save_gel=params.gel['path2saveGEL']):
graphs_path = glob.glob(f'{graph_path}/*.net')
for gp in graphs_path:
pathtemp = path2_save_gel+'/' + str(gp.split("\\")[-1].split(".")[0])
if os.path.exists(pathtemp + '.txt'):
os.remove(pathtemp + '.txt')
File_object = open(pathtemp + '.txt', 'a+')
g = nx.read_gpickle(gp)
File_object.write(str(len(g.nodes())))
File_object.write('\n')
for i in range(g.order()):
File_object.write(str(i))
File_object.write(',')
File_object.write(str(len(g.edges([i]))-1))
edges = g.edges([i])
for edge in edges:
if edge[0] != edge[1]:
File_object.write(':')
File_object.write(str(edge[1]))
File_object.write(',')
File_object.write(str(g.get_edge_data(i, edge[1])['weight'].__round__()))
File_object.write('\n')
File_object.close()
def T2A(path2read, path2save):
embeddedspath = glob.glob(f'{path2read}/Zmatrix*')
for embeddedpath in embeddedspath:
file1 = open(embeddedpath, 'r')
lines = file1.readlines()
print(lines[0])
print(len(lines))
array = np.zeros((int(lines[0].split('\n')[0]), params.gel['EmbeddingDim']))
for i in range(1,len(lines)):
line = lines[i]
parts = line.split(':')
for j in range(1, len(parts)):
array[i-1, int(parts[j].split(',')[0])] = float(parts[j].split(',')[1])
pathtemp = embeddedpath.split('\\')[-1]
np.save(f'{path2save}/{pathtemp}.npy', array)
npys = glob.glob(f'{path2save}/*.npy')
t = []
for tt in npys:
t.append(np.load(tt))
np.save(f'{path2save}/embeddeds.npy', np.asarray(t))