Completed
Push — master ( 6d6a7d...e4617d )
by Jasper
8s
created

FifFile   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 44
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
B inspect() 0 24 2
A attach() 0 13 1
1
from __future__ import division
2
from datetime import datetime, timedelta
3
from niprov.basefile import BaseFile
4
from niprov.libraries import Libraries
5
6
7
class FifFile(BaseFile):
8
9
    def __init__(self, location, **kwargs):
10
        super(FifFile, self).__init__(location, **kwargs)
11
        self.libs = self.dependencies.getLibraries()
12
13
    def inspect(self):
14
        provenance = super(FifFile, self).inspect()
15
        #TODO try statement loop to check & inspect different fif filetypes
16
        """ try:
17
                img = self.libs.mne.io.Raw(self.path, allow_maxshield=True)
18
                except ValueError:
19
                    pass
20
                else:
21
                    inspect file
22
                    Return
23
        """
24
        img = self.libs.mne.io.Raw(self.path, allow_maxshield=True)
25
        sub = img.info['subject_info']
26
        if sub is not None:
27
            provenance['subject'] = sub['first_name']+' '+sub['last_name']
28
        provenance['project'] = img.info['proj_name']
29
        acqTS = img.info['meas_date'][0]
30
        provenance['acquired'] = datetime.fromtimestamp(acqTS)
31
        T = img.last_samp - img.first_samp + 1
32
        provenance['dimensions'] = [img.info['nchan'], T]
33
        provenance['sampling-frequency'] = img.info['sfreq']
34
        provenance['duration'] = timedelta(seconds=T/img.info['sfreq'])
35
        provenance['modality'] = 'MEG'
36
        return provenance
37
38
    def attach(self, form='json'):
39
        """
40
        Attach the current provenance to the file by appending it as a
41
        json-encoded string to the 'description' header field.
42
43
        Args:
44
            form (str): Data format in which to serialize provenance. Defaults 
45
                to 'json'.
46
        """
47
        info = self.libs.mne.io.read_info(self.path)
48
        provstr = self.getProvenance(form)
49
        info['description'] = info['description']+' NIPROV:'+provstr
50
        self.libs.mne.io.write_info(self.path, info)
51
52