6
6
7
7
from nipype .testing import assert_equal , assert_true
8
8
from nipype .interfaces import utility
9
+ import nipype .pipeline .engine as pe
10
+
9
11
10
12
def test_rename ():
11
13
tempdir = os .path .realpath (mkdtemp ())
12
14
origdir = os .getcwd ()
13
15
os .chdir (tempdir )
14
16
15
17
# Test very simple rename
16
- _ = open ("file.txt" ,"w" ).close ()
18
+ _ = open ("file.txt" , "w" ).close ()
17
19
rn = utility .Rename (in_file = "file.txt" , format_string = "test_file1.txt" )
18
20
res = rn .run ()
19
21
outfile = os .path .join (tempdir , "test_file1.txt" )
20
22
yield assert_equal , res .outputs .out_file , outfile
21
23
yield assert_true , os .path .exists (outfile )
22
24
23
25
# Now a string-formatting version
24
- rn = utility .Rename (in_file = "file.txt" , format_string = "%(field1)s_file%(field2)d" ,keep_ext = True )
26
+ rn = utility .Rename (in_file = "file.txt" , format_string = "%(field1)s_file%(field2)d" , keep_ext = True )
25
27
# Test .input field creation
26
28
yield assert_true , hasattr (rn .inputs , "field1" )
27
29
yield assert_true , hasattr (rn .inputs , "field2" )
@@ -36,3 +38,31 @@ def test_rename():
36
38
# Clean up
37
39
os .chdir (origdir )
38
40
shutil .rmtree (tempdir )
41
+
42
+
43
+ def test_function ():
44
+ tempdir = os .path .realpath (mkdtemp ())
45
+ origdir = os .getcwd ()
46
+ os .chdir (tempdir )
47
+
48
+ def gen_random_array (size ):
49
+ import numpy as np
50
+ return np .random .rand (size , size )
51
+
52
+ f1 = pe .MapNode (utility .Function (input_names = ['size' ], output_names = ['random_array' ], function = gen_random_array ), name = 'random_array' , iterfield = ['size' ])
53
+ f1 .inputs .size = [2 , 3 , 5 ]
54
+
55
+ wf = pe .Workflow (name = "test_workflow" )
56
+
57
+ def increment_array (in_array ):
58
+ return in_array + 1
59
+
60
+ f2 = pe .MapNode (utility .Function (input_names = ['in_array' ], output_names = ['out_array' ], function = increment_array ), name = 'increment_array' , iterfield = ['in_array' ])
61
+
62
+ wf .connect (f1 , 'random_array' , f2 , 'in_array' )
63
+
64
+ wf .run ()
65
+
66
+ # Clean up
67
+ os .chdir (origdir )
68
+ shutil .rmtree (tempdir )
0 commit comments