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

FifFile.inspect()   C

Complexity

Conditions 7

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
dl 0
loc 49
rs 5.5
1
from __future__ import division
2
import logging
3
from datetime import datetime
4
from functools import partial
5
from niprov.basefile import BaseFile
6
from niprov.libraries import Libraries
7
8
9
class FifFile(BaseFile):
10
11
    def __init__(self, location, **kwargs):
12
        super(FifFile, self).__init__(location, **kwargs)
13
        self.libs = self.dependencies.getLibraries()
14
15
    def inspect(self):
16
        provenance = super(FifFile, self).inspect()
17
        """ try:
18
                img = self.libs.mne.io.Raw(self.path, allow_maxshield=True)
19
                except ValueError:
20
                    pass
21
                else:
22
                    inspect file
23
                    Return
24
        """
25
        ftypes = {
26
            'cov': self.libs.mne.read_cov,
27
            'epo': self.libs.mne.read_epochs,
28
            'evo': self.libs.mne.read_evokeds,
29
            'raw': partial(self.libs.mne.io.read_raw_fif, allow_maxshield=True),
30
        }
31
        oldLevel = logging.getLogger('mne').getEffectiveLevel()
32
        logging.getLogger('mne').setLevel(logging.ERROR)
33
        for ftype, readfif in ftypes.items():
34
            try:
35
                img = readfif(self.path)
36
                break
37
            except ValueError:
38
                continue
39
        else:
40
            ftype = 'other'
41
        logging.getLogger('mne').setLevel(oldLevel)
42
43
        if ftype == 'raw':
44
            sub = img.info['subject_info']
45
            if sub is not None:
46
                provenance['subject'] = sub['first_name']+' '+sub['last_name']
47
            provenance['project'] = img.info['proj_name']
48
            acqTS = img.info['meas_date'][0]
49
            provenance['acquired'] = datetime.fromtimestamp(acqTS)
50
            T = img.last_samp - img.first_samp + 1
51
            provenance['dimensions'] = [img.info['nchan'], T]
52
            provenance['sampling-frequency'] = img.info['sfreq']
53
            provenance['duration'] = T/img.info['sfreq']
54
55
        if ftype == 'epo':
56
            provenance['lowpass'] = img.info['lowpass']
57
            provenance['highpass'] = img.info['highpass']
58
            provenance['bad-channels'] = img.info['bads']
59
            provenance['dimensions'] = [img.events.shape[0], img.times.shape[0]]
60
61
        provenance['fif-type'] = ftype
62
        provenance['modality'] = 'MEG'
63
        return provenance
64
65
    def attach(self, form='json'):
66
        """
67
        Attach the current provenance to the file by appending it as a
68
        json-encoded string to the 'description' header field.
69
70
        Args:
71
            form (str): Data format in which to serialize provenance. Defaults 
72
                to 'json'.
73
        """
74
        info = self.libs.mne.io.read_info(self.path)
75
        provstr = self.getProvenance(form)
76
        info['description'] = info['description']+' NIPROV:'+provstr
77
        self.libs.mne.io.write_info(self.path, info)
78
79