24
24
iflogger = logging .getLogger ("nipype.interface" )
25
25
26
26
27
+ def _read_pickle (fname ):
28
+ with open (fname , 'rb' ) as f :
29
+ return pickle .load (f )
30
+
31
+
27
32
def read_unknown_ntwk (ntwk ):
28
33
if not isinstance (ntwk , nx .classes .graph .Graph ):
29
34
_ , _ , ext = split_filename (ntwk )
30
35
if ext == ".pck" :
31
- ntwk = nx . read_gpickle (ntwk )
36
+ ntwk = _read_pickle (ntwk )
32
37
elif ext == ".graphml" :
33
38
ntwk = nx .read_graphml (ntwk )
34
39
return ntwk
@@ -121,7 +126,7 @@ def average_networks(in_files, ntwk_res_file, group_id):
121
126
counting_ntwk = ntwk .copy ()
122
127
# Sums all the relevant variables
123
128
for index , subject in enumerate (in_files ):
124
- tmp = nx . read_gpickle (subject )
129
+ tmp = _read_pickle (subject )
125
130
iflogger .info ("File %s has %i edges" , subject , tmp .number_of_edges ())
126
131
edges = list (tmp .edges ())
127
132
for edge in edges :
@@ -200,7 +205,8 @@ def average_networks(in_files, ntwk_res_file, group_id):
200
205
201
206
# Writes the networks and returns the name
202
207
network_name = group_id + "_average.pck"
203
- nx .write_gpickle (avg_ntwk , op .abspath (network_name ))
208
+ with open (op .abspath (network_name ), 'wb' ) as f :
209
+ pickle .dump (avg_ntwk , f , pickle .HIGHEST_PROTOCOL )
204
210
iflogger .info ("Saving average network as %s" , op .abspath (network_name ))
205
211
avg_ntwk = fix_keys_for_gexf (avg_ntwk )
206
212
network_name = group_id + "_average.gexf"
@@ -460,7 +466,7 @@ def _run_interface(self, runtime):
460
466
edgentwks = list ()
461
467
kntwks = list ()
462
468
matlab = list ()
463
- ntwk = nx . read_gpickle (self .inputs .in_file )
469
+ ntwk = _read_pickle (self .inputs .in_file )
464
470
465
471
# Each block computes, writes, and saves a measure
466
472
# The names are then added to the output .pck file list
@@ -483,7 +489,8 @@ def _run_interface(self, runtime):
483
489
for key in list (node_measures .keys ()):
484
490
newntwk = add_node_data (node_measures [key ], ntwk )
485
491
out_file = op .abspath (self ._gen_outfilename (key , "pck" ))
486
- nx .write_gpickle (newntwk , out_file )
492
+ with open (out_file , 'wb' ) as f :
493
+ pickle .dump (newntwk , f , pickle .HIGHEST_PROTOCOL )
487
494
nodentwks .append (out_file )
488
495
if isdefined (self .inputs .out_node_metrics_matlab ):
489
496
node_out_file = op .abspath (self .inputs .out_node_metrics_matlab )
@@ -497,7 +504,8 @@ def _run_interface(self, runtime):
497
504
for key in list (edge_measures .keys ()):
498
505
newntwk = add_edge_data (edge_measures [key ], ntwk )
499
506
out_file = op .abspath (self ._gen_outfilename (key , "pck" ))
500
- nx .write_gpickle (newntwk , out_file )
507
+ with open (out_file , 'wb' ) as f :
508
+ pickle .dump (newntwk , f , pickle .HIGHEST_PROTOCOL )
501
509
edgentwks .append (out_file )
502
510
if isdefined (self .inputs .out_edge_metrics_matlab ):
503
511
edge_out_file = op .abspath (self .inputs .out_edge_metrics_matlab )
@@ -521,7 +529,8 @@ def _run_interface(self, runtime):
521
529
out_file = op .abspath (
522
530
self ._gen_outfilename (self .inputs .out_k_crust , "pck" )
523
531
)
524
- nx .write_gpickle (ntwk_measures [key ], out_file )
532
+ with open (out_file , 'wb' ) as f :
533
+ pickle .dump (ntwk_measures [key ], f , pickle .HIGHEST_PROTOCOL )
525
534
kntwks .append (out_file )
526
535
gpickled .extend (kntwks )
527
536
0 commit comments