/
netgen.py
executable file
·352 lines (274 loc) · 15.8 KB
/
netgen.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
NetGen is a tool for financial network analysis
Copyright (C) 2013 Tarik Roukny (troukny@ulb.ac.be)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
"""
# ==========================================
# Libraries and Packages
import sys
import os
import src.config as config
import src.netmint.netmint as Netmint
from src.analyzer import Analyzer
from datetime import datetime
# ==========================================
print ("\n\n---------------------------------------------------------------------------------------------------------------")
print (" NetGen v1.0 ")
print ("---------------------------------------------------------------------------------------------------------------\n")
# -------------------------------------------------------------
#
# stop_system (message)
#
# -------------------------------------------------------------
def stop_system (message):
sys.exit(message)
def get_entry_str (vector, i, j):
entry = ''
if isinstance (vector, str):
entry = vector
else :
if isinstance (vector [i], str):
entry = vector [i]
else :
# to add: verification
entry = vector[i][j]
return entry
def get_entry_dict (vector, i, j):
entry = ''
if isinstance (vector, dict):
entry = vector
else :
if isinstance (vector [i], dict):
entry = vector [i]
else :
# to add: verification
entry = vector[i][j]
return entry
def generate_config_netmint(in_file_directory, in_file_name,
out_file_directory, is_weighted, weight_id, is_directed):
file = open (os.getcwd() + "/src/netmint/src/config.py", 'w')
file.write("#!/usr/bin/pythonw\n# -*- coding: utf-8 -*-\n")
file.write("in_file_directory = \'{0}networks/\'\n".format(in_file_directory))
file.write("in_file_name = \'{0}\'\n".format(in_file_name))
file.write("out_file_directory = \'{0}\'\n".format(out_file_directory))
str = 'no'
if is_weighted == 'on':
str = 'yes'
file.write("is_weighted = \'{0}\'\n".format(str))
file.write("weight_id = 'weight'\n")
# \'{0}\'\n".format(weight_id))
str = 'no'
if is_directed == 'on':
str = 'yes'
file.write("is_directed = \'{0}\'\n".format(str))
file.write("node_fitness = \'no\'\n")
file.write("fitness_id = \'node\'\n")
file.write("full_analysis = \'yes\'\n")
file.write("aggregate_number = \'3\'\n")
file.close()
def launch_netmint(in_file_directory, in_file_name,
out_file_directory, is_directed, is_weighted, weight_id):
generate_config_netmint(in_file_directory, in_file_name, out_file_directory, is_weighted, weight_id, is_directed)
os.system(os.getcwd() + "/src/netmint/netmint.py")
#-------------------------------------------------------------------------
#
# MAIN
#
#-------------------------------------------------------------------------
if __name__ == '__main__':
#####################################
# Importing information from config #
#####################################
file_info = [ config.in_file_directory, config.in_file_name, config.in_file_extension,
config.in_file_delimiter, config.in_file_quote,
config.in_file_missing_value,
config.out_file_directory, config.out_file_name,
config.out_file_extension, config.out_file_delimiter,
config.out_file_missing_value,
config.out_file_single_file, config.out_file_separate_line]
# raw and clean data structure
raw_data_structure = config.raw_data_structure
clean_data_structure = config.clean_data_structure
# information related to the mapping-merging processes
mapping_info = [ config.mapping_file_directory, config.mapping_file_name,
config.mapping_file_extension, config.mapping_file_delimiter,
config.mapping_replace_ids,
config.mapping_kept_id_position, config.mapping_lost_id_position,
config.mapping_target_position,
config.mapping_drop_unreferenced_entries,
config.mapping_target_unreferenced_entries,
config.mapping_drop_ghosts,
config.mapping_remove_duplicates,
config.mapping_target_duplicates_set,
config.mapping_merge_entries,
config.mapping_target_merge_set,
config.mapping_commands ]
# information related to the network analysis
network_info = [ config.network_analysis, config.network_out_node,
config.network_in_node, config.network_is_directed,
config.network_is_weighted, config.network_edge_weight ]
n_in_files = 0
if isinstance(file_info[1], str):
n_in_files = 1
else:
if isinstance(file_info[1], list):
n_in_files = len(file_info[1])
print ('number of in files : ', n_in_files)
# string text used for the log
log_text = '++++++++++++++++++++++++++++++++ \n NETGEN LOG FILE:\n++++++++++++++++++++++++++++++++ \n'
##################
# Launch process #
##################
# for each file in the in_file_name list from config
for i in range(n_in_files) :
##############################
# Get the proper information #
##############################
# in file
in_file_directory = get_entry_str(file_info [0], i, 0)
in_file_name = get_entry_str(file_info [1], i, 0)
in_extension = get_entry_str(file_info [2], i, 0)
in_delimiter = get_entry_str(file_info [3], i, 0)
in_file_quote = get_entry_str(file_info [4], i, 0)
in_file_missing_value = get_entry_str(file_info [5], i, 0)
# out file
out_file_directory = get_entry_str(file_info [6], i, 0)
out_file_name = get_entry_str(file_info [7], i, 0)
out_extension = get_entry_str(file_info [8], i, 0)
out_delimiter = get_entry_str(file_info [9], i, 0)
out_file_missing_value = get_entry_str(file_info [10], i, 0)
out_file_single_file = get_entry_str(file_info [11], i, 0)
out_file_separate_line = get_entry_str(file_info [12], i, 0)
start = datetime.now()
print ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print (" Opening data file : {0}".format(in_file_directory + in_file_name))
print (" - Started: " + str(start))
print ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
# for log
log_text += "\n\n - DATA FILE : {0} at {1}".format(in_file_directory + in_file_name, str(start))+'\n'
########################
# Read The Config File #
########################
print ("Step 0. CONFIG VERIFICATION\n")
# initialization - generating an Analyzer with all info to generate file handlers
_Analyzer = Analyzer( in_file_directory, in_file_name, in_extension, in_delimiter,
in_file_missing_value, in_file_quote,
out_file_directory, out_file_name, out_extension, out_delimiter,
out_file_missing_value,
out_file_single_file, out_file_separate_line,
raw_data_structure, clean_data_structure )
# verification of admitted formats (in, out and data)
if not _Analyzer.check_valid():
stop_system(_Analyzer.error_message)
##########################
# Structure the raw data #
##########################
print ("Step 1. STRUCTURING THE RAW DATA\n")
_Analyzer.structure_raw_data()
################################
# Generate the clean data #
################################
print ("Step 2. GENERATING THE CLEAN DATA\n")
_Analyzer.generate_clean_data()
###################
# Potential - Map #
###################
n_maps = 0
if isinstance(mapping_info[1], str):
n_maps = 1
else:
if isinstance(mapping_info[1], list):
n_maps = len(mapping_info[1])
for j in range(n_maps) :
print ("Step 2.{0}. SMOOTHING DATA (MAPPING)\n".format(j+1))
mapping_file_directory = get_entry_str(mapping_info[0], j, i)
mapping_file_name = get_entry_str(mapping_info[1], j, i)
mapping_file_extension = get_entry_str(mapping_info[2], j, i)
mapping_file_delimiter = get_entry_str(mapping_info[3], j, i)
mapping_replace_ids = get_entry_str(mapping_info[4], j, i)
mapping_kept_id_position = get_entry_str(mapping_info[5], j, i)
mapping_lost_id_position = get_entry_str(mapping_info[6], j, i)
mapping_target_position = get_entry_str(mapping_info[7], j, i)
mapping_drop_unreferenced_entries = get_entry_str(mapping_info[8], j, i)
mapping_target_unreferenced_entries = get_entry_str(mapping_info[9], j, i)
mapping_drop_ghosts = get_entry_str(mapping_info[10], j, i)
mapping_remove_duplicates = get_entry_str(mapping_info[11], j, i)
mapping_target_duplicates_set = get_entry_str(mapping_info[12], j, i)
mapping_merge_entries = get_entry_str(mapping_info[13], j, i)
mapping_target_merge_set = get_entry_str(mapping_info[14], j, i)
mapping_commands = get_entry_str(mapping_info[15], j, i)
#==============================================================================
# #ADD A VERIFICATION STEP!!!
#==============================================================================
mapping_results = _Analyzer.map_data( mapping_file_directory, mapping_file_name,
mapping_file_extension, mapping_file_delimiter,
mapping_replace_ids,
mapping_kept_id_position, mapping_lost_id_position,
mapping_target_position,
mapping_drop_unreferenced_entries,
mapping_target_unreferenced_entries,
mapping_drop_ghosts,
mapping_remove_duplicates,
mapping_target_duplicates_set,
mapping_merge_entries,
mapping_target_merge_set, mapping_commands )
_Analyzer.clean_data_handler.data_final = mapping_results[0]
# for log
log_text += mapping_results[1] +'\n'
########################
# Save clean data #
########################
print ("Step 3. SAVING THE CLEAN DATA\n")
_Analyzer.save_clean_data()
##############################
# Check for network analysis #
##############################
network_analysis = get_entry_str(network_info [0], i, 0)
network_out_node = get_entry_str(network_info [1], i, 0)
network_in_node = get_entry_str(network_info [2], i, 0)
network_is_directed = get_entry_str(network_info [3], i, 0)
network_is_weighted = get_entry_str(network_info [4], i, 0)
network_edge_weight = get_entry_str(network_info [5], i, 0)
if network_analysis == 'on':
print ("Step 4. CREATING THE NETWORK\n")
network_directory = out_file_directory + '/networks/'
if not os.path.exists(network_directory):
os.makedirs(network_directory)
_Analyzer.create_network( network_directory,
network_out_node, network_in_node,
network_is_directed, network_is_weighted,
network_edge_weight)
#####################
# End #
#####################
finish = datetime.now()
print ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print (" - Finished: " + str(finish))
print (" >> Total time elapsed: "+str(finish-start))
print ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
# for log
log_text += 'Finished at: {0}\n'.format(str(finish))
########################################
# Running the overall network analysis #
########################################
if network_analysis == 'on':
print ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print (" Starting the Network Analysis " )
print ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
launch_netmint(file_info[6], file_info[7],
file_info[6], config.network_is_directed,
config.network_is_weighted, config.network_edge_weight)
print ("---------------------------------------------------------------------------------------------------------------")
print (" PROCESS TERMINATED")
print ("---------------------------------------------------------------------------------------------------------------\n")
file = open ('log/netgen_log.txt','w')
file.write(log_text)
file.close()