Skip to content

Commit 5bbd2a4

Browse files
authored
Merge pull request #2243 from djarecka/cmtk_errors
adding import error in cmtk.nbs and imprve KeyError when specific att…
2 parents 90d72a6 + a28eff5 commit 5bbd2a4

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

nipype/interfaces/cmtk/nbs.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def ntwks_to_matrices(in_files, edge_key):
3232
for idx, name in enumerate(in_files):
3333
graph = nx.read_gpickle(name)
3434
for u, v, d in graph.edges(data=True):
35-
graph[u][v]['weight'] = d[edge_key] # Setting the edge requested edge value as weight value
35+
try:
36+
graph[u][v]['weight'] = d[edge_key] # Setting the edge requested edge value as weight value
37+
except:
38+
raise KeyError("the graph edges do not have {} attribute".format(edge_key))
3639
matrix[:, :, idx] = nx.to_numpy_matrix(graph) # Retrieve the matrix
3740
return matrix
3841

@@ -77,6 +80,10 @@ class NetworkBasedStatistic(BaseInterface):
7780
output_spec = NetworkBasedStatisticOutputSpec
7881

7982
def _run_interface(self, runtime):
83+
84+
if not have_cv:
85+
raise ImportError("cviewer library is not available")
86+
8087
THRESH = self.inputs.threshold
8188
K = self.inputs.number_of_permutations
8289
TAIL = self.inputs.t_tail
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from __future__ import unicode_literals
2+
from ..nbs import NetworkBasedStatistic
3+
from ....utils.misc import package_check
4+
import numpy as np
5+
import networkx as nx
6+
import pytest
7+
8+
have_cv = True
9+
try:
10+
package_check('cviewer')
11+
except Exception as e:
12+
have_cv = False
13+
14+
@pytest.fixture()
15+
def creating_graphs(tmpdir):
16+
graphlist = []
17+
graphnames = ["name"+str(i) for i in range(6)]
18+
for idx, name in enumerate(graphnames):
19+
graph = np.random.rand(10,10)
20+
G = nx.from_numpy_matrix(graph)
21+
out_file = str(tmpdir) + graphnames[idx] + '.pck'
22+
# Save as pck file
23+
nx.write_gpickle(G, out_file)
24+
graphlist.append(out_file)
25+
return graphlist
26+
27+
28+
@pytest.mark.skipif(have_cv, reason="tests for import error, cviewer available")
29+
def test_importerror(creating_graphs):
30+
graphlist = creating_graphs
31+
group1 = graphlist[:3]
32+
group2 = graphlist[3:]
33+
34+
nbs = NetworkBasedStatistic()
35+
nbs.inputs.in_group1 = group1
36+
nbs.inputs.in_group2 = group2
37+
nbs.inputs.edge_key = "weight"
38+
39+
with pytest.raises(ImportError) as e:
40+
nbs.run()
41+
assert "cviewer library is not available" == str(e.value)
42+
43+
44+
@pytest.mark.skipif(not have_cv, reason="cviewer has to be available")
45+
def test_keyerror(creating_graphs):
46+
graphlist =creating_graphs
47+
48+
group1 = graphlist[:3]
49+
group2 = graphlist[3:]
50+
51+
nbs = NetworkBasedStatistic()
52+
nbs.inputs.in_group1 = group1
53+
nbs.inputs.in_group2 = group2
54+
nbs.inputs.edge_key = "Your_edge"
55+
56+
with pytest.raises(KeyError) as e:
57+
nbs.run()
58+
assert "the graph edges do not have Your_edge attribute" in str(e.value)

0 commit comments

Comments
 (0)