Completed
Pull Request — master (#170)
by Jasper
01:32
created

FifTests.setupEpochsFile()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 10
rs 9.4285
1
import unittest, numpy
2
from mock import Mock
3
from datetime import datetime
4
from tests.test_basefile import BaseFileTests
5
6
7
class FifTests(BaseFileTests):
8
9
    def setUp(self):
10
        super(FifTests, self).setUp()
11
        self.libs = Mock()
12
        self.libs.hasDependency.return_value = True
13
        self.libs.mne.io.read_raw_fif.side_effect = ValueError
14
        self.libs.mne.read_cov.side_effect = ValueError
15
        self.libs.mne.read_epochs.side_effect = ValueError
16
        self.libs.mne.read_evokeds.side_effect = ValueError
17
        self.libs.mne.read_forward_solution.side_effect = ValueError
18
        self.libs.mne.read_trans.side_effect = ValueError
19
        self.dependencies.getLibraries.return_value = self.libs
20
        from niprov.fif import FifFile
21
        self.constructor = FifFile
22
        self.file = FifFile(self.path, dependencies=self.dependencies)
23
24
    def test_Gets_basic_info_from_mne_and_returns_it(self):
25
        self.setupRawFile()
26
        out = self.file.inspect()
27
        self.assertEqual(out['subject'], 'John Doeish')
28
        self.assertEqual(out['project'], 'worlddom')
29
        self.assertEqual(out['acquired'], self.acquired)
30
31
    def test_Gets_dimensions(self):
32
        self.setupRawFile()
33
        out = self.file.inspect()
34
        self.assertEqual(out['dimensions'], [123, 91])
35
        self.assertEqual(out['sampling-frequency'], 200)
36
        self.assertEqual(out['duration'], 91/200.)
37
38
    def test_Attach_method(self):
39
        self.setupRawFile()
40
        self.file.getProvenance = Mock()
41
        self.file.getProvenance.return_value = 'serial prov'
42
        self.file.attach('json')
43
        self.file.getProvenance.assert_called_with('json')
44
        self.assertIn('existing bla bla ', #original string+added space 
45
            self.img.info['description'])
46
        self.assertIn('NIPROV:'+'serial prov', 
47
            self.img.info['description'])
48
        self.libs.mne.io.write_info.assert_called_with(self.file.path, 
49
            self.img.info)
50
51
    def test_Determines_modality(self):
52
        out = self.file.inspect()
53
        self.assertEqual(out['modality'], 'MEG')
54
55
    def test_Preserves_modality_if_inherited(self):
56
        pass # Doesn't have to preserve
57
58
    def test_Tries_different_mne_access_functions_or_fails_silently(self):
59
        out = self.file.inspect()
60
        self.libs.mne.io.read_raw_fif.assert_called_with(self.path, 
61
                                                         allow_maxshield=True)
62
        self.libs.mne.read_cov.assert_called_with(self.path)
63
        self.libs.mne.read_epochs.assert_called_with(self.path)
64
        self.libs.mne.read_evokeds.assert_called_with(self.path)
65
        self.libs.mne.read_forward_solution.assert_called_with(self.path)
66
        self.libs.mne.read_trans.assert_called_with(self.path)
67
        self.assertEqual(out['fif-type'], 'other')
68
69
    def test_epochs(self):
70
        self.setupEpochsFile()
71
        out = self.file.inspect()
72
        self.assertEqual(out['fif-type'], 'epo')
73
        self.assertEqual(out['lowpass'],40.0)
74
        self.assertEqual(out['highpass'],0.10000000149) 
75
        self.assertEqual(out['bad-channels'],['MEG666', 'ECG999'])
76
        self.assertEqual(out['dimensions'], [7, 455])
77
78
    def test_evokeds(self):
79
        self.setupEvokedsFile()
80
        out = self.file.inspect()
81
        self.assertEqual(out['fif-type'], 'ave')
82
        self.assertEqual(out['dimensions'], [3, 306, 455])
83
84
    def test_covariance(self):
85
        self.setupCovarianceFile()
86
        out = self.file.inspect()
87
        self.assertEqual(out['fif-type'], 'cov')
88
        self.assertEqual(out['dimensions'], [365, 365])
89
90
    def test_forward(self):
91
        self.setupForwardFile()
92
        out = self.file.inspect()
93
        self.assertEqual(out['fif-type'], 'fwd')
94
95
    def test_trans(self):
96
        self.setupTransFile()
97
        out = self.file.inspect()
98
        self.assertEqual(out['fif-type'], 'trans')
99
100
    def setupRawFile(self):
101
        TS = 1422522595.76096
102
        self.acquired = datetime.fromtimestamp(TS)
103
        self.img = Mock()
104
        self.img.info = {
105
            'meas_date':(TS,),
106
            'proj_name':'worlddom',
107
            'subject_info':{'first_name':'John','last_name':'Doeish'},
108
            'nchan':123,
109
            'sfreq':200,
110
            'description':'existing bla bla'}
111
        self.img.first_samp = 10
112
        self.img.last_samp = 100
113
        self.libs.mne.io.read_raw_fif.side_effect = None
114
        self.libs.mne.io.read_raw_fif.return_value = self.img
115
        self.libs.mne.io.read_info.return_value = self.img.info
116
117
    def setupEpochsFile(self):
118
        self.img = Mock()
119
        self.img.info = {
120
            'lowpass': 40.0,
121
            'highpass': 0.10000000149, 
122
            'bads': ['MEG666', 'ECG999']}
123
        self.img.events = numpy.zeros((7,3))
124
        self.img.times = numpy.zeros((455,1))
125
        self.libs.mne.read_epochs.side_effect = None
126
        self.libs.mne.read_epochs.return_value = self.img
127
128
    def setupEvokedsFile(self):
129
        self.img = Mock()
130
        self.img.info = {}
131
        self.img.data = numpy.zeros((306,455))
132
        self.libs.mne.read_evokeds.side_effect = None
133
        self.libs.mne.read_evokeds.return_value = [self.img, self.img, self.img]
134
135
    def setupCovarianceFile(self):
136
        self.img = Mock()
137
        self.img.data = numpy.zeros((365,365))
138
        self.libs.mne.read_cov.side_effect = None
139
        self.libs.mne.read_cov.return_value = self.img
140
141
    def setupForwardFile(self):
142
        self.img = Mock()
143
        self.libs.mne.read_forward_solution.side_effect = None
144
        self.libs.mne.read_forward_solution.return_value = self.img
145
146
    def setupTransFile(self):
147
        self.img = Mock()
148
        self.libs.mne.read_trans.side_effect = None
149
        self.libs.mne.read_trans.return_value = self.img
150
151