|
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
|
|
|
|