Skip to content

Commit 24c55a6

Browse files
TheChymeraeffigies
authored andcommitted
Writing pickles directly as networkx no longer ships write_gpickle
1 parent 83c8cf8 commit 24c55a6

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

nipype/interfaces/cmtk/cmtk.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ def cmat(
226226
# Add node information from specified parcellation scheme
227227
path, name, ext = split_filename(resolution_network_file)
228228
if ext == ".pck":
229-
gp = nx.read_gpickle(resolution_network_file)
229+
with open(resolution_network_file, 'rb') as f:
230+
gp = pickle.load(f)
230231
elif ext == ".graphml":
231232
gp = nx.read_graphml(resolution_network_file)
232233
else:
@@ -379,7 +380,8 @@ def cmat(
379380
fibdev.add_edge(u, v, weight=di["fiber_length_std"])
380381

381382
iflogger.info("Writing network as %s", matrix_name)
382-
nx.write_gpickle(G, op.abspath(matrix_name))
383+
with open(op.abspath(matrix_name), 'wb') as f:
384+
pickle.dump(G, f, pickle.HIGHEST_PROTOCOL)
383385

384386
numfib_mlab = nx.to_numpy_matrix(numfib, dtype=int)
385387
numfib_dict = {"number_of_fibers": numfib_mlab}
@@ -394,7 +396,8 @@ def cmat(
394396
path, name, ext = split_filename(matrix_name)
395397
intersection_matrix_name = op.abspath(name + "_intersections") + ext
396398
iflogger.info("Writing intersection network as %s", intersection_matrix_name)
397-
nx.write_gpickle(I, intersection_matrix_name)
399+
with open(intersection_matrix_name, 'wb') as f:
400+
pickle.dump(I, f, pickle.HIGHEST_PROTOCOL)
398401

399402
path, name, ext = split_filename(matrix_mat_name)
400403
if not ext == ".mat":
@@ -1070,7 +1073,8 @@ def create_nodes(roi_file, resolution_network_file, out_filename):
10701073
)
10711074
)
10721075
G.nodes[int(u)]["dn_position"] = tuple([xyz[0], xyz[2], -xyz[1]])
1073-
nx.write_gpickle(G, out_filename)
1076+
with open(out_filename, 'wb') as f:
1077+
pickle.dump(G, f, pickle.HIGHEST_PROTOCOL)
10741078
return out_filename
10751079

10761080

nipype/interfaces/cmtk/nbs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import numpy as np
88
import networkx as nx
9+
import pickle
910

1011
from ... import logging
1112
from ..base import (
@@ -172,12 +173,14 @@ def _run_interface(self, runtime):
172173

173174
path = op.abspath("NBS_Result_" + details)
174175
iflogger.info(path)
175-
nx.write_gpickle(nbsgraph, path)
176+
with open(path, 'wb') as f:
177+
pickle.dump(nbsgraph, f, pickle.HIGHEST_PROTOCOL)
176178
iflogger.info("Saving output NBS edge network as %s", path)
177179

178180
pval_path = op.abspath("NBS_P_vals_" + details)
179181
iflogger.info(pval_path)
180-
nx.write_gpickle(nbs_pval_graph, pval_path)
182+
with open(pval_path, 'wb') as f:
183+
pickle.dump(nbs_pval_graph, f, pickle.HIGHEST_PROTOCOL)
181184
iflogger.info("Saving output p-value network as %s", pval_path)
182185
return runtime
183186

nipype/interfaces/cmtk/nx.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ def average_networks(in_files, ntwk_res_file, group_id):
200200

201201
# Writes the networks and returns the name
202202
network_name = group_id + "_average.pck"
203-
nx.write_gpickle(avg_ntwk, op.abspath(network_name))
203+
with open(op.abspath(network_name), 'wb') as f:
204+
pickle.dump(avg_ntwk, f, pickle.HIGHEST_PROTOCOL)
204205
iflogger.info("Saving average network as %s", op.abspath(network_name))
205206
avg_ntwk = fix_keys_for_gexf(avg_ntwk)
206207
network_name = group_id + "_average.gexf"
@@ -483,7 +484,8 @@ def _run_interface(self, runtime):
483484
for key in list(node_measures.keys()):
484485
newntwk = add_node_data(node_measures[key], ntwk)
485486
out_file = op.abspath(self._gen_outfilename(key, "pck"))
486-
nx.write_gpickle(newntwk, out_file)
487+
with open(out_file, 'wb') as f:
488+
pickle.dump(newntwk, f, pickle.HIGHEST_PROTOCOL)
487489
nodentwks.append(out_file)
488490
if isdefined(self.inputs.out_node_metrics_matlab):
489491
node_out_file = op.abspath(self.inputs.out_node_metrics_matlab)
@@ -497,7 +499,8 @@ def _run_interface(self, runtime):
497499
for key in list(edge_measures.keys()):
498500
newntwk = add_edge_data(edge_measures[key], ntwk)
499501
out_file = op.abspath(self._gen_outfilename(key, "pck"))
500-
nx.write_gpickle(newntwk, out_file)
502+
with open(out_file, 'wb') as f:
503+
pickle.dump(newntwk, f, pickle.HIGHEST_PROTOCOL)
501504
edgentwks.append(out_file)
502505
if isdefined(self.inputs.out_edge_metrics_matlab):
503506
edge_out_file = op.abspath(self.inputs.out_edge_metrics_matlab)
@@ -521,7 +524,8 @@ def _run_interface(self, runtime):
521524
out_file = op.abspath(
522525
self._gen_outfilename(self.inputs.out_k_crust, "pck")
523526
)
524-
nx.write_gpickle(ntwk_measures[key], out_file)
527+
with open(out_file, 'wb') as f:
528+
pickle.dump(ntwk_measures[key], f, pickle.HIGHEST_PROTOCOL)
525529
kntwks.append(out_file)
526530
gpickled.extend(kntwks)
527531

nipype/interfaces/cmtk/tests/test_nbs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from ....utils.misc import package_check
33
import numpy as np
44
import networkx as nx
5+
import pickle
56
import pytest
67

78
have_cv = True
@@ -20,7 +21,8 @@ def creating_graphs(tmpdir):
2021
G = nx.from_numpy_matrix(graph)
2122
out_file = tmpdir.strpath + graphnames[idx] + ".pck"
2223
# Save as pck file
23-
nx.write_gpickle(G, out_file)
24+
with open(out_file, 'wb') as f:
25+
pickle.dump(G, f, pickle.HIGHEST_PROTOCOL)
2426
graphlist.append(out_file)
2527
return graphlist
2628

0 commit comments

Comments
 (0)