test_melodicanalysis.py 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
#
# test_melodicanalysis.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#

import              os
import os.path   as op
import itertools as it

import numpy     as np
import pytest

import tests
import fsl.utils.path           as fslpath
import fsl.data.melodicanalysis as mela


def test_isMelodicImage():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix',
24
25
26
27
28
29
30
31
32
33

             'analysis.blica/melodic_IC.nii.gz',

             'analysis2.ica/melodic_oIC.nii.gz',
             'analysis2.ica/melodic_mix',
             'analysis2.ica/melodic_FTmix',
             'analysis3.ica/melodic_IC.nii.gz',
             'analysis3.ica/melodic_oIC.nii.gz',
             'analysis3.ica/melodic_mix',
             'analysis3.ica/melodic_FTmix']
34
35
36

    with tests.testdir(paths) as testdir:
        for p in paths:
37
38
39
40
            expected = any([p == 'analysis.ica/melodic_IC.nii.gz',
                            p == 'analysis2.ica/melodic_oIC.nii.gz',
                            p == 'analysis3.ica/melodic_IC.nii.gz',
                            p == 'analysis3.ica/melodic_oIC.nii.gz'])
41
42
43
44
45
46
47
48
49
50
51
52
            assert mela.isMelodicImage(op.join(testdir, p)) == expected


def test_isMelodicDir():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        assert mela.isMelodicDir(meldir)

53
54
55
56
57
    # melodic_oIC is ok
    with tests.testdir([p.replace('ic_IC', 'ic_oIC') for p in paths]) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        assert mela.isMelodicDir(meldir)

Paul McCarthy's avatar
Paul McCarthy committed
58
    # non-.ica prefix is ok
59
60
    with tests.testdir([p.replace('.ica', '.blob') for p in paths]) as testdir:
        meldir = op.join(testdir, 'analysis.blob')
Paul McCarthy's avatar
Paul McCarthy committed
61
        assert mela.isMelodicDir(meldir)
62
63
64
65
66
67
68
69
70
71
72
73

    # Directory must exist!
    assert not mela.isMelodicDir('non-existent.ica')

    # Directory must contain all of the above files
    perms = it.chain(it.combinations(paths, 1),
                     it.combinations(paths, 2))
    for p in perms:
        with tests.testdir(p) as testdir:
            meldir = op.join(testdir, 'analysis.ica')
            assert not mela.isMelodicDir(meldir)

74
75
76
77
78
79
    # same test for melodic_oIC
    for p in perms:
        with tests.testdir([pp.replace('ic_IC', 'ic_oIC') for pp in p]) as testdir:
            meldir = op.join(testdir, 'analysis.ica')
            assert not mela.isMelodicDir(meldir)

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

def test_getAnalysisDir():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

    testpaths = ['analysis.ica/melodic_IC.nii.gz',
                 'analysis.ica/log.txt',
                 'analysis.ica/stats/thresh_zstat1.nii.gz',
                 'analysis.ica/report/00index.html']

    with tests.testdir(paths) as testdir:
        expected = op.join(testdir, 'analysis.ica')
        for tp in testpaths:
            assert mela.getAnalysisDir(op.join(testdir, tp)) == expected

96
97
98
99
100
101
102
103
    paths = ['analysis.ica/melodic_blob.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

    with tests.testdir(paths) as testdir:
        for tp in testpaths:
            assert mela.getAnalysisDir(op.join(testdir, tp)) is None

104
105
106

def test_getTopLevelAnalysisDir():
    testpaths = [
107
108
109
110
111
112
        ('REST.ica/filtered_func_data.ica/melodic_IC.nii.gz',   'REST.ica'),
        ('REST.ica/filtered_func_data.ica/melodic_mix',         'REST.ica'),
        ('analysis.gica/groupmelodic.ica/melodic_IC.nii.gz',    'analysis.gica'),
        ('analysis.feat/filtered_func_data.ica/melodic_mix',    'analysis.feat'),
        ('not/any/analysis/directories/here/melodic_IC.nii.gz', None),
    ]
113
114
115
116
117
118

    for tp, expected in testpaths:
        assert mela.getTopLevelAnalysisDir(tp) == expected


def test_getDataFile():
119

120
121
122
123
124
125
126
127
128
129
130
    testcases = [(['analysis.ica/filtfunc.ica/melodic_IC.nii.gz',
                   'analysis.ica/filtfunc.ica/melodic_mix',
                   'analysis.ica/filtfunc.ica/melodic_FTmix',
                   'analysis.ica/filtered_func_data.nii.gz'],
                  'analysis.ica/filtfunc.ica',
                  'analysis.ica/filtered_func_data.nii.gz'),
                 (['analysis.feat/filtfunc.ica/melodic_IC.nii.gz',
                   'analysis.feat/filtfunc.ica/melodic_mix',
                   'analysis.feat/filtfunc.ica/melodic_FTmix',
                   'analysis.feat/filtered_func_data.nii.gz'],
                  'analysis.feat/filtfunc.ica',
131
                  'analysis.feat/filtered_func_data.nii.gz'),
132
133
134
135
136
137
                 (['analysis.feat/filtfunc.ica/melodic_IC.nii.gz',
                   'analysis.feat/filtfunc.ica/melodic_mix',
                   'analysis.feat/filtfunc.ica/melodic_FTmix',
                   'analysis.feat/filtered_func_data_clean.nii.gz'],
                  'analysis.feat/filtfunc.ica',
                  'analysis.feat/filtered_func_data_clean.nii.gz'),
138
139
140
141
142
143
144
145
146
                 (['no/analysis/dirs/here/melodic_IC.nii.gz'],
                   'no/analysis/dirs/here/',
                   None),
                 (['analysis.feat/analysis.ica/melodic_IC.nii.gz',
                   'analysis.feat/analysis.ica/melodic_mix',
                   'analysis.feat/analysis.ica/melodic_FTmix'],
                   'analysis.feat/analysis.ica',
                   None),
    ]
147
148

    for paths, meldir, expected in testcases:
Paul McCarthy's avatar
Paul McCarthy committed
149
150
151
        paths = [op.join(*p.split('/')) for p in paths]
        if expected is not None:
            expected = op.join(*expected.split('/'))
152
153
154
        with tests.testdir(paths) as testdir:
            assert mela.getDataFile(meldir) == expected

155

156
157
158
159
160
161
def test_getMeanFile():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix',
             'analysis.ica/mean.nii.gz']

Paul McCarthy's avatar
Paul McCarthy committed
162
163
    paths = [op.join(*p.split('/')) for p in paths]

164
165
    with tests.testdir(paths) as testdir:
        meldir   = op.join(testdir, 'analysis.ica')
Paul McCarthy's avatar
Paul McCarthy committed
166
        expected = op.join(testdir, 'analysis.ica', 'mean.nii.gz')
167

168
        assert mela.getMeanFile(meldir) == expected
169

170
171
172
173
174
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix',
             'analysis.ica/mean.txt']

Paul McCarthy's avatar
Paul McCarthy committed
175
176
    paths = [op.join(*p.split('/')) for p in paths]

177
178
179
180
181
182
183
184
185
186
187
    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        with pytest.raises(fslpath.PathError):
            mela.getMeanFile(meldir)


def test_getICFile():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

Paul McCarthy's avatar
Paul McCarthy committed
188
189
    paths = [op.join(*p.split('/')) for p in paths]

190
191
    with tests.testdir(paths) as testdir:
        meldir   = op.join(testdir, 'analysis.ica')
Paul McCarthy's avatar
Paul McCarthy committed
192
        expected = op.join(testdir, 'analysis.ica', 'melodic_IC.nii.gz')
193
        assert mela.getICFile(meldir) == expected
194

195
196
197
198
    paths = ['analysis.ica/melodic_IC.txt',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

Paul McCarthy's avatar
Paul McCarthy committed
199
200
    paths = [op.join(*p.split('/')) for p in paths]

201
202
203
    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        with pytest.raises(fslpath.PathError):
204
205
            mela.getICFile(meldir)

206
207
208
209
210
211

def test_getMixFile():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

Paul McCarthy's avatar
Paul McCarthy committed
212
213
    paths = [op.join(*p.split('/')) for p in paths]

214
215
    with tests.testdir(paths) as testdir:
        meldir   = op.join(testdir, 'analysis.ica')
Paul McCarthy's avatar
Paul McCarthy committed
216
        expected = op.join(testdir, 'analysis.ica', 'melodic_mix')
217
        assert mela.getMixFile(meldir) == expected
218

219
220
    paths = ['analysis.ica/melodic_IC.ni.gz',
             'analysis.ica/melodic_FTmix']
Paul McCarthy's avatar
Paul McCarthy committed
221
222
    paths = [op.join(*p.split('/')) for p in paths]

223
224
225
226
227
228
229
230
    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        assert mela.getMixFile(meldir) is None

def test_getFTMixFile():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']
Paul McCarthy's avatar
Paul McCarthy committed
231
    paths = [op.join(*p.split('/')) for p in paths]
232
233
234

    with tests.testdir(paths) as testdir:
        meldir   = op.join(testdir, 'analysis.ica')
Paul McCarthy's avatar
Paul McCarthy committed
235
        expected = op.join(testdir, 'analysis.ica', 'melodic_FTmix')
236
        assert mela.getFTMixFile(meldir) == expected
237

238
239
    paths = ['analysis.ica/melodic_IC.ni.gz',
             'analysis.ica/melodic_mix']
Paul McCarthy's avatar
Paul McCarthy committed
240
241
    paths = [op.join(*p.split('/')) for p in paths]

242
243
    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
244
        assert mela.getFTMixFile(meldir) is None
245
246
247
248
249
250

def test_getReportFile():
    paths = ['analysis.ica/filtfunc.ica/melodic_IC.nii.gz',
             'analysis.ica/filtfunc.ica/melodic_mix',
             'analysis.ica/filtfunc.ica/melodic_FTmix',
             'analysis.ica/report.html']
Paul McCarthy's avatar
Paul McCarthy committed
251
    paths = [op.join(*p.split('/')) for p in paths]
252
253

    with tests.testdir(paths) as testdir:
Paul McCarthy's avatar
Paul McCarthy committed
254
255
        meldir   = op.join(testdir, 'analysis.ica', 'filtfunc.ica')
        expected = op.join(testdir, 'analysis.ica', 'report.html')
256
        assert op.abspath(mela.getReportFile(meldir)) == expected
257

258
259
260
    paths = ['analysis.ica/filtfunc.ica/melodic_IC.ni.gz',
             'analysis.ica/filtfunc.ica/melodic_mix',
             'analysis.ica/filtfunc.ica/melodic_FTmix']
Paul McCarthy's avatar
Paul McCarthy committed
261
262
    paths = [op.join(*p.split('/')) for p in paths]

263
264
    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
265
        assert mela.getReportFile(meldir) is None
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328


def test_getNumComponents():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        icfile = op.join(meldir,  'melodic_IC.nii.gz')

        tests.make_random_image(icfile, (10, 10, 10, 17))

        assert mela.getNumComponents(meldir) == 17

    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        with pytest.raises(Exception):
            mela.getNumComponents(meldir)


def test_getComponentTimeSeries():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

    with tests.testdir(paths) as testdir:
        meldir  = op.join(testdir, 'analysis.ica')
        mixfile = op.join(meldir,  'melodic_mix')

        data = np.zeros((40, 20))
        for i in range(20):
            data[:, i] = np.arange(i, i + 40)

        np.savetxt(mixfile, data)
        assert np.all(mela.getComponentTimeSeries(meldir) == data)

    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        with pytest.raises(Exception):
            mela.getComponentTimeSeries(meldir)


def test_getComponentPowerSpectra():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

    with tests.testdir(paths) as testdir:
        meldir    = op.join(testdir, 'analysis.ica')
        ftmixfile = op.join(meldir,  'melodic_FTmix')

        data = np.zeros((40, 20))
        for i in range(20):
            data[:, i] = np.arange(i, i + 40)

        np.savetxt(ftmixfile, data)
        assert np.all(mela.getComponentPowerSpectra(meldir) == data)

    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        with pytest.raises(Exception):
            mela.getComponentPowerSpectra(meldir)