-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForGephi.py
254 lines (223 loc) · 9.79 KB
/
ForGephi.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# FORGEPHI Tools which writes input files names.csv and links.csv to Gephi inputs from FTDNA FF match lists
#
# (c) Ilpo Kantonen 2020
import csv
from csv import reader
#-----------------------------------------------------------------------------------------------------------
#----------------------------------------- ForGephi --------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------
class ForGephi:
indN = 0
indL = 0
firstname = ''
foundkitlist = []
max_e = 0
fnameN = 'names.csv'
fnameL = 'links.csv'
#-------------------------------------------------------------------------------------------------------
#----------------------------------------- Constructor -------------------------------------------------
#-------------------------------------------------------------------------------------------------------
def __init__():
indN = 0
indL = 0
max_e = 200
#-------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------
#----------------------------------------- setMax ------------------------------------------------------
#-------------------------------------------------------------------------------------------------------
@staticmethod
def setMax(count):
max_e = count
#-------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------
#----------------------------------------- addNames1 ---------------------------------------------------
#-------------------------------------------------------------------------------------------------------
# fname - Match file
@staticmethod
def addNames1(fname):
fnameN = 'names.csv'
nf = open(fnameN, "w")
nf.write("Id,Label,timeset\n")
dingeli = '0,Ilpo Kantonen,\n'
nf.write(dingeli)
count_writed = 0
max_e = 200
with open(fname, 'r') as read_obj2:
i=0
csv_reader = reader(read_obj2)
for row2 in csv_reader:
# Skip header line of match list
if i > 0:
if max_e > 0:
if count_writed < max_e-1:
count_writed += 1
nf.write('%d,%s,\n' %(count_writed,row2[0]))
i += 1
nf.close()
return(count_writed+1)
#-------------------------------------------------------------------------------------------------------
#----------------------------------------- addNames2 ---------------------------------------------------
#-------------------------------------------------------------------------------------------------------
# fname - Match file
# offset - Count of links in previous(es) file(s)
@staticmethod
def addNames2(fname,offset):
rows = []
# Remember content of names file before it changes
fnameN = 'names.csv'
max_e = 200
with open(fnameN, 'r') as read_obj4:
i=0
csv4_reader = reader(read_obj4)
for row4 in csv4_reader:
# Skip header line of match list
if i > 0:
rows.append(row4)
i += 1
nf = open(fnameN, "a")
u = 0
count_writed = 0
with open(fname, 'r') as read_obj2:
csv2_reader = reader(read_obj2)
i=0
sama = 0
j=0
for row2 in csv2_reader:
if j > 0:
sama=0
xyz=0
numero_str = ''
numero = 0
for xyzz in rows:
if row2[0] in rows[xyz]:
sama = 1
break
xyz += 1
if sama == 0:
if max_e > 0:
if count_writed < max_e-1:
count_writed += 1
nf.write('%d,%s,\n' %(offset,row2[0]))
offset += 1
j += 1
j += 1
ind=u
nf.close()
return(offset+i)
#-------------------------------------------------------------------------------------------------------
#----------------------------------------- addLinks1 ---------------------------------------------------
#-------------------------------------------------------------------------------------------------------
# fname - Match file
# max_e - max_eimum amount of people which is counted in
@staticmethod
def addLinks1(fname):
spn=0
count_writed = 0
max_e = 200
# Open output file and write header and first person from to it
fnameL = 'links.csv'
nf = open(fnameL, "w")
nf.write("Source,Target,Type,Id,Label,timeset,Weight\n")
nf.write('%d,%d,Undirected,%d,,,%d\n' %(0,0,0,0))
writtenlinks = 0
nf = open(fnameL, "a")
with open(fname, 'r') as read_obj2:
i=0
csv_reader = reader(read_obj2)
for row2 in csv_reader:
# Skip header line of match list
if i > 1:
if max_e > 0:
if i < max_e:
count_writed += 1
nf.write('%d,%d,Undirected,%d,,,%d\n' %(0,i-1,i-2,int(float(row2[7]))))
writtenlinks += 1
i += 1
nf.close()
return(writtenlinks)
#-------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------
#----------------------------------------- findName ----------------------------------------------------
#-------------------------------------------------------------------------------------------------------
# name - Who is searched
# return - Index on list where found. -1 if not found
def findName(name):
with open(filen, 'r') as read_obj:
csv_reader = reader(read_obj)
i = 0
for a in csv_reader:
i += 1
if a[1] == name:
break
return(i-1)
#-------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------
#----------------------------------------- addLinks2 ---------------------------------------------------
#-------------------------------------------------------------------------------------------------------
# fname - Match file
# offset - Count of links in previous(es) file(s)
# person - from who the links are
@staticmethod
def addLinks2(fname,offset,sourcepersonname):
# person = who links they are
links = []
names = []
u=0
count_writed = 0
fnameL = 'links.csv'
# Remember content of links file before it changes
with open(fnameL, 'r') as read_obj4:
i=0
csv4_reader = reader(read_obj4)
for row4 in csv4_reader:
# Skip header line of match list
if i > 0:
links.append(row4)
i += 1
# Find source persons index, so you can write his or hers links to links.csv
fnameN = 'names.csv'
names = []
inteksi = 0
fnamesN = 'names.csv'
with open(fnamesN, 'r') as read_obj7:
csv7_reader = reader(read_obj7)
for n7 in csv7_reader:
if inteksi > 0:
names.append(n7)
inteksi += 1
i_tmp=0
for etsihanta in names:
if etsihanta[1] == sourcepersonname:
break
else:
i_tmp += 1
sourceindex = i_tmp
max_e = 200
writtenlinks=0
fnameL = 'links.csv'
nf = open(fnameL, "a")
with open(fname, 'r') as read_obj12:
i=0
csv_reader = reader(read_obj12)
for row12 in csv_reader:
# Skip header line of match list
if i > 1:
if max_e > 0:
if i < max_e:
count_writed += 1
i_tmp=0
for etsihanta in names:
sintso = ''
sintso = row12[1]
sintso += row12[2]
if etsihanta[1] == row12[0]:
break
else:
i_tmp += 1
targetindex = i_tmp
nf.write('%d,%d,Undirected,%d,,,%d\n' %(sourceindex,targetindex,offset+i-2,int(float(row12[7]))))
writtenlinks += 1
i += 1
nf.close()
return(offset+writtenlinks)
#-------------------------------------------------------------------------------------------------------