1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA IO helpers"""
10
11 import re
12 import os
13 import unittest
14 from tempfile import mkstemp
15 import numpy as N
16
17 from nose.tools import ok_, assert_equal
18
19 from mvpa import pymvpa_dataroot
20 from mvpa.misc.io import *
21 from mvpa.misc.fsl import *
22 from mvpa.misc.bv import BrainVoyagerRTC
23
24
26
28 ex1 = """eins zwei drei
29 0 1 2
30 3 4 5
31 """
32 file, fpath = mkstemp('mvpa', 'test')
33 file = open(fpath, 'w')
34 file.write(ex1)
35 file.close()
36
37
38 d = ColumnData(fpath, header=True)
39
40
41 self.failUnless(sorted(d.keys()) == ['drei','eins','zwei'])
42
43 self.failUnless(d['eins'] == [0, 3])
44 self.failUnless(d['zwei'] == [1, 4])
45 self.failUnless(d['drei'] == [2, 5])
46
47
48 d2 = ColumnData(d)
49
50
51 self.failUnless(sorted(d2.keys()) == ['drei','eins','zwei'])
52 self.failUnless(d2['eins'] == [0, 3])
53 self.failUnless(d2['zwei'] == [1, 4])
54 self.failUnless(d2['drei'] == [2, 5])
55
56
57 d += d2
58
59
60 self.failUnless(sorted(d.keys()) == ['drei','eins','zwei'])
61
62
63 self.failUnlessEqual(d['eins'], [0, 3, 0, 3])
64 self.failUnlessEqual(d['zwei'], [1, 4, 1, 4])
65 self.failUnlessEqual(d['drei'], [2, 5, 2, 5])
66
67
68
69 header_order = ['drei', 'zwei', 'eins']
70 d.tofile(fpath, header_order=header_order)
71
72
73 dsel = d.selectSamples([0, 2])
74 self.failUnlessEqual(dsel['eins'], [0, 0])
75 self.failUnlessEqual(dsel['zwei'], [1, 1])
76 self.failUnlessEqual(dsel['drei'], [2, 2])
77
78
79 d3 = ColumnData(fpath)
80 self.failUnlessEqual(d3._header_order, header_order)
81
82
83
84 d3['four'] = [0.1] * len(d3['eins'])
85 d3.tofile(fpath)
86
87 d4 = ColumnData(fpath)
88 self.failUnlessEqual(d4._header_order, header_order + ['four'])
89
90
91 try:
92 os.remove(fpath)
93 except WindowsError:
94 pass
95
96
98 sa = SampleAttributes(os.path.join(pymvpa_dataroot,
99 'attributes_literal.txt'),
100 literallabels=True)
101
102 ok_(sa.nrows == 1452, msg='There should be 1452 samples')
103
104
105 ev = sa.toEvents(funky='yeah')
106 ok_(len(ev) == 17 * (max(sa.chunks) + 1),
107 msg='Not all events got detected.')
108
109 ok_(len([e for e in ev if e.has_key('funky')]) == len(ev),
110 msg='All events need to have to custom arg "funky".')
111
112 ok_(ev[0]['label'] == ev[-1]['label'] == 'rest',
113 msg='First and last event are rest condition.')
114
115 ok_(ev[-1]['onset'] + ev[-1]['duration'] == sa.nrows,
116 msg='Something is wrong with the timiing of the events')
117
118
120 ex1 = """0.0 2.0 1
121 13.89 2 1
122 16 2.0 0.5
123 """
124 file, fpath = mkstemp('mvpa', 'test')
125 file = open(fpath, 'w')
126 file.write(ex1)
127 file.close()
128
129
130 d = FslEV3(fpath)
131
132
133 self.failUnless(sorted(d.keys()) == \
134 ['durations','intensities','onsets'])
135
136 self.failUnless(d['onsets'] == [0.0, 13.89, 16.0])
137 self.failUnless(d['durations'] == [2.0, 2.0, 2.0])
138 self.failUnless(d['intensities'] == [1.0, 1.0, 0.5])
139
140 self.failUnless(d.getNEVs() == 3)
141 self.failUnless(d.getEV(1) == (13.89, 2.0, 1.0))
142
143 try:
144 os.remove(fpath)
145 except WindowsError:
146 pass
147
148 d = FslEV3(os.path.join(pymvpa_dataroot, 'fslev3.txt'))
149 ev = d.toEvents()
150 self.failUnless(len(ev) == 3)
151 self.failUnless([e['duration'] for e in ev] == [9] * 3)
152 self.failUnless([e['onset'] for e in ev] == [6, 21, 35])
153 self.failUnless([e['features'] for e in ev] == [[1],[1],[1]])
154
155 ev = d.toEvents(label='face', chunk=0, crap=True)
156 ev[0]['label'] = 'house'
157 self.failUnless(len(ev) == 3)
158 self.failUnless([e['duration'] for e in ev] == [9] * 3)
159 self.failUnless([e['onset'] for e in ev] == [6, 21, 35])
160 self.failUnless([e['features'] for e in ev] == [[1],[1],[1]])
161 self.failUnless([e['label'] for e in ev] == ['house', 'face', 'face'])
162 self.failUnless([e['chunk'] for e in ev] == [0]*3)
163 self.failUnless([e['crap'] for e in ev] == [True]*3)
164
165
174
176 """Simple testing of reading RTC files from BrainVoyager"""
177
178 attr = BrainVoyagerRTC(os.path.join(pymvpa_dataroot, 'bv', 'smpl_model.rtc'))
179 self.failUnlessEqual(attr.ncolumns, 4, "We must have 4 colums")
180 self.failUnlessEqual(attr.nrows, 147, "We must have 147 rows")
181
182 self.failUnlessEqual(attr._header_order,
183 ['l_60 B', 'r_60 B', 'l_80 B', 'r_80 B'],
184 "We must got column names correctly")
185 self.failUnless(len(attr.r_60_B) == attr.nrows,
186 "We must have got access to column by property")
187 self.failUnless(attr.toarray() != None,
188 "We must have got access to column by property")
189
191 """Simple testing of helper Design2Labels"""
192
193 attr = BrainVoyagerRTC(os.path.join(pymvpa_dataroot, 'bv', 'smpl_model.rtc'))
194 labels0 = design2labels(attr, baseline_label='silence')
195 labels = design2labels(attr, baseline_label='silence',
196 func=lambda x:x>0.5)
197 Nsilence = lambda x:len(N.where(N.array(x) == 'silence')[0])
198
199 nsilence0 = Nsilence(labels0)
200 nsilence = Nsilence(labels)
201 self.failUnless(nsilence0 < nsilence,
202 "We must have more silence if thr is higher")
203 self.failUnlessEqual(len(labels), attr.nrows,
204 "We must have the same number of labels as rows")
205 self.failUnlessRaises(ValueError, design2labels, attr,
206 baseline_label='silence', func=lambda x:x>-1.0)
207
208
220
221
227
228
234
236 fname = os.path.join(pymvpa_dataroot,
237 'sample_design.fsf')
238
239 design = read_fsl_design(fname)
240
241 set_lines = [x for x in open(fname).readlines()
242 if x.startswith('set ')]
243 assert_equal(len(set_lines), len(design))
244
245
246 """TODO: would require the same special treatment for _files fields
247 re_set = re.compile("set ([^)]*\)).*")
248 for line in set_lines:
249 key = re_set.search(line).groups()[0]
250 if not key in design:
251 raise AssertionError(
252 "Key %s was not found in read FSL design" % key)
253 key_list = [' '.join(l.split(None,2)[1:2]) for l in set_lines]
254 for k in set(key_list):
255 if len([key for key in key_list if key == k]) == 2:
256 raise AssertionError(
257 "Got the non-unique beast %s" % k)
258 """
259
262
263
264 if __name__ == '__main__':
265 import runner
266