Skip to content

Tentative =>networkx-3.0 support #3535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions nipype/interfaces/cmtk/cmtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ def cmat(
# Add node information from specified parcellation scheme
path, name, ext = split_filename(resolution_network_file)
if ext == ".pck":
gp = nx.read_gpickle(resolution_network_file)
with open(resolution_network_file, 'rb') as f:
gp = pickle.load(f)
elif ext == ".graphml":
gp = nx.read_graphml(resolution_network_file)
else:
Expand Down Expand Up @@ -263,7 +264,7 @@ def cmat(
)
intersection_matrix = np.matrix(intersection_matrix)
I = G.copy()
H = nx.from_numpy_matrix(np.matrix(intersection_matrix))
H = nx.from_numpy_array(np.matrix(intersection_matrix))
H = nx.relabel_nodes(H, lambda x: x + 1) # relabel nodes so they start at 1
I.add_weighted_edges_from(
((u, v, d["weight"]) for u, v, d in H.edges(data=True))
Expand Down Expand Up @@ -379,7 +380,8 @@ def cmat(
fibdev.add_edge(u, v, weight=di["fiber_length_std"])

iflogger.info("Writing network as %s", matrix_name)
nx.write_gpickle(G, op.abspath(matrix_name))
with open(op.abspath(matrix_name), 'wb') as f:
pickle.dump(G, f, pickle.HIGHEST_PROTOCOL)

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

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


Expand Down
11 changes: 7 additions & 4 deletions nipype/interfaces/cmtk/nbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
import networkx as nx
import pickle

from ... import logging
from ..base import (
Expand Down Expand Up @@ -149,8 +150,8 @@ def _run_interface(self, runtime):
pADJ[x, y] = PVAL[idx]

# Create networkx graphs from the adjacency matrix
nbsgraph = nx.from_numpy_matrix(ADJ)
nbs_pval_graph = nx.from_numpy_matrix(pADJ)
nbsgraph = nx.from_numpy_array(ADJ)
nbs_pval_graph = nx.from_numpy_array(pADJ)

# Relabel nodes because they should not start at zero for our convention
nbsgraph = nx.relabel_nodes(nbsgraph, lambda x: x + 1)
Expand All @@ -172,12 +173,14 @@ def _run_interface(self, runtime):

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

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

Expand Down
12 changes: 8 additions & 4 deletions nipype/interfaces/cmtk/nx.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ def average_networks(in_files, ntwk_res_file, group_id):

# Writes the networks and returns the name
network_name = group_id + "_average.pck"
nx.write_gpickle(avg_ntwk, op.abspath(network_name))
with open(op.abspath(network_name), 'wb') as f:
pickle.dump(avg_ntwk, f, pickle.HIGHEST_PROTOCOL)
iflogger.info("Saving average network as %s", op.abspath(network_name))
avg_ntwk = fix_keys_for_gexf(avg_ntwk)
network_name = group_id + "_average.gexf"
Expand Down Expand Up @@ -483,7 +484,8 @@ def _run_interface(self, runtime):
for key in list(node_measures.keys()):
newntwk = add_node_data(node_measures[key], ntwk)
out_file = op.abspath(self._gen_outfilename(key, "pck"))
nx.write_gpickle(newntwk, out_file)
with open(out_file, 'wb') as f:
pickle.dump(newntwk, f, pickle.HIGHEST_PROTOCOL)
nodentwks.append(out_file)
if isdefined(self.inputs.out_node_metrics_matlab):
node_out_file = op.abspath(self.inputs.out_node_metrics_matlab)
Expand All @@ -497,7 +499,8 @@ def _run_interface(self, runtime):
for key in list(edge_measures.keys()):
newntwk = add_edge_data(edge_measures[key], ntwk)
out_file = op.abspath(self._gen_outfilename(key, "pck"))
nx.write_gpickle(newntwk, out_file)
with open(out_file, 'wb') as f:
pickle.dump(newntwk, f, pickle.HIGHEST_PROTOCOL)
edgentwks.append(out_file)
if isdefined(self.inputs.out_edge_metrics_matlab):
edge_out_file = op.abspath(self.inputs.out_edge_metrics_matlab)
Expand All @@ -521,7 +524,8 @@ def _run_interface(self, runtime):
out_file = op.abspath(
self._gen_outfilename(self.inputs.out_k_crust, "pck")
)
nx.write_gpickle(ntwk_measures[key], out_file)
with open(out_file, 'wb') as f:
pickle.dump(ntwk_measures[key], f, pickle.HIGHEST_PROTOCOL)
kntwks.append(out_file)
gpickled.extend(kntwks)

Expand Down
6 changes: 4 additions & 2 deletions nipype/interfaces/cmtk/tests/test_nbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ....utils.misc import package_check
import numpy as np
import networkx as nx
import pickle
import pytest

have_cv = True
Expand All @@ -17,10 +18,11 @@ def creating_graphs(tmpdir):
graphnames = ["name" + str(i) for i in range(6)]
for idx, name in enumerate(graphnames):
graph = np.random.rand(10, 10)
G = nx.from_numpy_matrix(graph)
G = nx.from_numpy_array(graph)
out_file = tmpdir.strpath + graphnames[idx] + ".pck"
# Save as pck file
nx.write_gpickle(G, out_file)
with open(out_file, 'wb') as f:
pickle.dump(G, f, pickle.HIGHEST_PROTOCOL)
graphlist.append(out_file)
return graphlist

Expand Down
2 changes: 1 addition & 1 deletion nipype/pipeline/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def _generate_dependency_list(self, graph):
import networkx as nx

self.procs, _ = topological_sort(graph)
self.depidx = nx.to_scipy_sparse_matrix(
self.depidx = nx.to_scipy_sparse_array(
graph, nodelist=self.procs, format="lil"
)
self.refidx = self.depidx.astype(int)
Expand Down