2424iflogger = logging .getLogger ("nipype.interface" )
2525
2626
27+ def _read_pickle (fname ):
28+ with open (fname , 'rb' ) as f :
29+ return pickle .load (f )
30+
31+
2732def read_unknown_ntwk (ntwk ):
2833 if not isinstance (ntwk , nx .classes .graph .Graph ):
2934 _ , _ , ext = split_filename (ntwk )
3035 if ext == ".pck" :
31- ntwk = nx . read_gpickle (ntwk )
36+ ntwk = _read_pickle (ntwk )
3237 elif ext == ".graphml" :
3338 ntwk = nx .read_graphml (ntwk )
3439 return ntwk
@@ -121,7 +126,7 @@ def average_networks(in_files, ntwk_res_file, group_id):
121126 counting_ntwk = ntwk .copy ()
122127 # Sums all the relevant variables
123128 for index , subject in enumerate (in_files ):
124- tmp = nx . read_gpickle (subject )
129+ tmp = _read_pickle (subject )
125130 iflogger .info ("File %s has %i edges" , subject , tmp .number_of_edges ())
126131 edges = list (tmp .edges ())
127132 for edge in edges :
@@ -200,7 +205,8 @@ def average_networks(in_files, ntwk_res_file, group_id):
200205
201206 # Writes the networks and returns the name
202207 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 )
204210 iflogger .info ("Saving average network as %s" , op .abspath (network_name ))
205211 avg_ntwk = fix_keys_for_gexf (avg_ntwk )
206212 network_name = group_id + "_average.gexf"
@@ -460,7 +466,7 @@ def _run_interface(self, runtime):
460466 edgentwks = list ()
461467 kntwks = list ()
462468 matlab = list ()
463- ntwk = nx . read_gpickle (self .inputs .in_file )
469+ ntwk = _read_pickle (self .inputs .in_file )
464470
465471 # Each block computes, writes, and saves a measure
466472 # The names are then added to the output .pck file list
@@ -483,7 +489,8 @@ def _run_interface(self, runtime):
483489 for key in list (node_measures .keys ()):
484490 newntwk = add_node_data (node_measures [key ], ntwk )
485491 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 )
487494 nodentwks .append (out_file )
488495 if isdefined (self .inputs .out_node_metrics_matlab ):
489496 node_out_file = op .abspath (self .inputs .out_node_metrics_matlab )
@@ -497,7 +504,8 @@ def _run_interface(self, runtime):
497504 for key in list (edge_measures .keys ()):
498505 newntwk = add_edge_data (edge_measures [key ], ntwk )
499506 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 )
501509 edgentwks .append (out_file )
502510 if isdefined (self .inputs .out_edge_metrics_matlab ):
503511 edge_out_file = op .abspath (self .inputs .out_edge_metrics_matlab )
@@ -521,7 +529,8 @@ def _run_interface(self, runtime):
521529 out_file = op .abspath (
522530 self ._gen_outfilename (self .inputs .out_k_crust , "pck" )
523531 )
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 )
525534 kntwks .append (out_file )
526535 gpickled .extend (kntwks )
527536
0 commit comments