/
NetworkProcessing.py
120 lines (107 loc) · 3.73 KB
/
NetworkProcessing.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import pandas as pd
import numpy as np
from pandas import DataFrame
import fileinput
import networkx as nx
import os
import datetime
# ========================================================
# INIT
# ========================================================
mToday = datetime.date.today()
# ========================================================
# ========================================================
def readLGF_Nodes_DF( source ):
mInFile = open( source ,mode='r')
fileString = mInFile.read()
mInFile.close()
if '@arcs' in fileString:
endPos = '@arcs'
elif '@edges' in fileString:
endPos = '@edges'
else:
print('No keyword \'@arcs\' or \'@edges\' found\n Wrong file format')
return
print fileString[:fileString.find('@nodes')]
strDat = fileString[fileString.find('@nodes')+7:fileString.find(endPos)]
mOutFile = open('tmp.txt',mode='w')
mOutFile.write(strDat)
mOutFile.close()
mFrame = pd.read_table('tmp.txt')
os.remove('tmp.txt')
mFrame = mFrame.dropna( axis=1, how='all')
return mFrame
# ========================================================
# ========================================================
def readLGF_EdgeArc_DF( source ):
mInFile = open( source ,mode='r')
fileString = mInFile.read()
mInFile.close()
if '@arcs' in fileString:
initKey = '@arcs'
initPos = 6
elif '@edges' in fileString:
initKey = '@edges'
initPos = 7
else:
print('No keyword \'@arcs\' or \'@edges\' found\n Wrong file format')
return
strDat = fileString[fileString.find(initKey)+initPos:]
mOutFile = open('tmp.txt',mode='w')
mOutFile.write(strDat)
mOutFile.close()
mFrame = pd.read_table('tmp.txt')
os.remove('tmp.txt')
mFrame = mFrame.dropna( axis=1, how='all')
mFrame.columns = np.append(['from', 'to'], mFrame.columns.values[2:])
return mFrame
# ========================================================
# ========================================================
def readLGF_Network( source ):
mInFile = open( source ,mode='r')
fileString = mInFile.read()
mInFile.close()
if '@arcs' in fileString:
initKey = '@arcs'
initPos = 6
elif '@edges' in fileString:
initKey = '@edges'
initPos = 7
else:
print('No keyword \'@arcs\' or \'@edges\' found\n Wrong file format')
return
strDat = fileString[fileString.find(initKey)+initPos:]
mOutFile = open('tmp.txt',mode='w')
mOutFile.write(strDat)
mOutFile.close()
mOutFile = open('tmp2.txt',mode='w')
for line in fileinput.input('tmp.txt'):
if not fileinput.isfirstline():
mOutFile.write(line)
mOutFile.close()
os.remove('tmp.txt')
g = nx.read_edgelist('tmp2.txt', nodetype=int, edgetype=int, data=False )
os.remove('tmp2.txt')
return g
# ========================================================
# ========================================================
def DF_merge_NXfunc( df, func, colName ):
mDF = pd.DataFrame.from_dict( func, orient='index')
mDF.columns = [str(colName)]
mDF = pd.merge( df , mDF, right_index=True, left_index=True)
return mDF
# ========================================================
# ========================================================
def writeDF_LGF( fileName, NodesDF, ArcsDF, info='' ):
file = open( fileName,'w')
file.write( mToday.strftime('%d. %m. %Y') + '\n' + info + '\n')
file.write('@nodes\n')
NodesDF.to_csv( file, index=False, sep="\t")
file.write('@arcs\n\t\t')
for x in ArcsDF.columns[2:]:
file.write(x)
file.write('\t')
file.write('\n')
ArcsDF.to_csv( file, index=False, sep="\t", header=False)
file.close
return "done"