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
|
|
|
'ave': self.libs.mne.read_evokeds, |
29
|
|
|
'fwd': self.libs.mne.read_forward_solution, |
30
|
|
|
'trans': self.libs.mne.read_trans, |
31
|
|
|
'raw': partial(self.libs.mne.io.read_raw_fif, allow_maxshield=True), |
32
|
|
|
} |
33
|
|
|
oldLevel = logging.getLogger('mne').getEffectiveLevel() |
34
|
|
|
logging.getLogger('mne').setLevel(logging.ERROR) |
35
|
|
|
for ftype, readfif in ftypes.items(): |
36
|
|
|
try: |
37
|
|
|
img = readfif(self.path) |
38
|
|
|
if img == []: |
39
|
|
|
continue |
40
|
|
|
break |
41
|
|
|
except ValueError: |
42
|
|
|
continue |
43
|
|
|
else: |
44
|
|
|
ftype = 'other' |
45
|
|
|
logging.getLogger('mne').setLevel(oldLevel) |
46
|
|
|
|
47
|
|
|
if ftype == 'raw': |
48
|
|
|
sub = img.info['subject_info'] |
49
|
|
|
if sub is not None: |
50
|
|
|
provenance['subject'] = sub['first_name']+' '+sub['last_name'] |
51
|
|
|
provenance['project'] = img.info['proj_name'] |
52
|
|
|
acqTS = img.info['meas_date'][0] |
53
|
|
|
provenance['acquired'] = datetime.fromtimestamp(acqTS) |
54
|
|
|
T = img.last_samp - img.first_samp + 1 |
55
|
|
|
provenance['dimensions'] = [img.info['nchan'], T] |
56
|
|
|
provenance['sampling-frequency'] = img.info['sfreq'] |
57
|
|
|
provenance['duration'] = T/img.info['sfreq'] |
58
|
|
|
|
59
|
|
|
if ftype == 'epo': |
60
|
|
|
provenance['lowpass'] = img.info['lowpass'] |
61
|
|
|
provenance['highpass'] = img.info['highpass'] |
62
|
|
|
provenance['bad-channels'] = img.info['bads'] |
63
|
|
|
provenance['dimensions'] = [img.events.shape[0], img.times.shape[0]] |
64
|
|
|
|
65
|
|
|
if ftype == 'ave': |
66
|
|
|
nEvokeds = len(img) |
67
|
|
|
provenance['dimensions'] = [nEvokeds] + list(img[0].data.shape) |
68
|
|
|
|
69
|
|
|
if ftype == 'cov': |
70
|
|
|
provenance['dimensions'] = list(img.data.shape) |
71
|
|
|
|
72
|
|
|
provenance['fif-type'] = ftype |
73
|
|
|
provenance['modality'] = 'MEG' |
74
|
|
|
return provenance |
75
|
|
|
|
76
|
|
|
def attach(self, form='json'): |
77
|
|
|
""" |
78
|
|
|
Attach the current provenance to the file by appending it as a |
79
|
|
|
json-encoded string to the 'description' header field. |
80
|
|
|
|
81
|
|
|
This is only attempted if the file has been inspect()-ed and |
82
|
|
|
has been determined to be a raw fif file. |
83
|
|
|
|
84
|
|
|
Args: |
85
|
|
|
form (str): Data format in which to serialize provenance. Defaults |
86
|
|
|
to 'json'. |
87
|
|
|
""" |
88
|
|
|
if 'fif-type' in self.provenance: |
89
|
|
|
if self.provenance['fif-type'] == 'raw': |
90
|
|
|
info = self.libs.mne.io.read_info(self.path) |
91
|
|
|
provstr = self.getProvenance(form) |
92
|
|
|
info['description'] = info['description']+' NIPROV:'+provstr |
93
|
|
|
self.libs.mne.io.write_info(self.path, info) |
94
|
|
|
|
95
|
|
|
|