-
Notifications
You must be signed in to change notification settings - Fork 20
/
dataSourceMoBI.m
245 lines (233 loc) · 10.3 KB
/
dataSourceMoBI.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
% Definition of the class dataSourceMoBI. This class reads into MoBILAB the
% content of a folder containing MoBILAB files.
%
% Author: Alejandro Ojeda, SCCN, INC, UCSD, 05-Apr-2011
%%
classdef dataSourceMoBI < dataSource
methods
%%
function obj = dataSourceMoBI(mobiDataDirectory)
% Creates a dataSourceMoBI object.
%
% Input arguments:
% folder: folder containing MoBILAB files
%
% Output arguments:
% obj: dataSource object (handle)
%
% Usage:
% % folder: folder containing MoBILAB files
% obj = dataSourceMoBI( folder );
if nargin < 1, error('No enough input arguments.');end
mobiDataDirectory = strtrim(mobiDataDirectory);
folder = dir(mobiDataDirectory);
files = {folder.name};
I = strfind(files,'.hdr');
I = cellfun(@isempty,I);
files(I) = [];
if isempty(files), error('The folder is empty.');end
loc = find(files{1} == '-');
if length(loc) < 8
try
warning off %#ok
load([mobiDataDirectory filesep files{1}],'-mat','sessionUUID');
warning on; %#ok
if ~exist('sessionUUID','var'), error('old format');end
catch %#ok
metadata = load([mobiDataDirectory filesep files{1}],'-mat');
if isfield(metadata,'sessionUUID')
sessionUUID = metadata.sessionUUID;
else
uuid = generateUUID;
sessionUUID = uuid;
end
end
for it=1:length(files)
oldHeader = fullfile(mobiDataDirectory,[files{it}(1:end-4) '.hdr']);
oldBinFile = fullfile(mobiDataDirectory,[files{it}(1:end-4) '.bin']);
newHeader = fullfile(mobiDataDirectory,[files{it}(1:end-4) '_' sessionUUID '.hdr']);
newBinFile = fullfile(mobiDataDirectory,[files{it}(1:end-4) '_' sessionUUID '.bin']);
java.io.File(oldHeader ).renameTo(java.io.File(newHeader));
java.io.File(oldBinFile).renameTo(java.io.File(newBinFile));
end
else loc = find(files{1} == '_');
sessionUUID = files{1}(loc(end)+1:end-4);
end
if exist([mobiDataDirectory filesep 'descriptor.MoBI'],'file')
load([mobiDataDirectory filesep 'descriptor.MoBI'],'-mat');
try txt = char(datasource.notes); %#ok
save([mobiDataDirectory filesep 'notes_' sessionUUID '.txt'],'-ascii','txt');
end
end
%% constructing the dataSource object
obj@dataSource(mobiDataDirectory,sessionUUID);
obj.listenerHandle.Enabled = false;
headers = pickfiles(mobiDataDirectory,{sessionUUID '.hdr'});
N = size(headers,1);
obj.container.initStatusbar(1,N,'Loading files...');
for it=1:N
header = deblank(headers(it,:));
if obj.checkHeader(header), obj.addItem(header);end
obj.container.statusbar(it);
end
dob = zeros(1,length(obj.item));
for it=1:length(obj.item), dob(it) = obj.item{it}.dob;end
[~,loc] = sort(dob);
obj.item = obj.item(loc);
obj.connect;
obj.updateLogicalStructure;
[~,obj.gObj] = viewLogicalStructure(obj,'',false);
if isempty(obj.item{1}.sessionUUID), obj.linkData;end
obj.listenerHandle.Enabled = true;
end
%%
function val = checkHeader(obj,header)
if isempty(header), error('This folder is corrupted. Cannot find the headers.');end
val = true;
version = coreStreamObject.version;
try
warning off
load(header,'-mat','hdrVersion');
warning on
catch hdrVersion = -inf; %#ok
end
if ~exist('hdrVersion','var'), hdrVersion = -inf;end
if version == hdrVersion, return;end
hdrVersion = version; %#ok
save(header,'-mat','-append','hdrVersion');
metadata = load(header,'-mat');
if isfield(metadata,'metadata'), metadata = metadata.metadata;end
if ~exist('metadata','var')
val = false;
binFile = [header(1:end-3) 'bin'];
if exist(binFile,'file')
toZip = {header,binFile};
else
toZip = header;
end
zipfile = [obj.mobiDataDirectory filesep 'lost+found.zip'];
fprintf(['File ' header ' may be corrupt.\nIt will be added to ' zipfile '\n']);
if exist(zipfile,'file'), zipfile = [obj.mobiDataDirectory filesep 'lost+found2.zip'];end
zip(zipfile,toZip,obj.mobiDataDirectory);
java.io.File(zipfile).renameTo(java.io.File([obj.mobiDataDirectory filesep 'lost+found.zip']));
delete(header);
if exist(binfile,'file'), delete(binFile);end
return;
end
saveThis = false;
if isfield(metadata,'eventInStruct')
metadata.event = metadata.eventInStruct;
metadata = rmfield(metadata,'eventInStruct');
saveThis = true;
end
if ~isfield(metadata.event,'hedTag')
metadata.event.hedTag = metadata.event.label;
saveThis = true;
end
if ~isfield(metadata,'class')
if ~isempty(strfind(metadata.name,'biosemi')) || ~isempty(strfind(metadata.name,'eeg'))
metadata.class = 'eeg';
elseif ~isempty(strfind(metadata.name,'phasespace')) || ~isempty(strfind(metadata.name,'mocap'))
metadata.class = 'mocap';
else metadata.class = 'dataStream';
end
saveThis = true;
end
if strcmp(metadata.class,'dataStream') && (~isempty(strfind(metadata.name,'biosemi')) || ~isempty(strfind(metadata.name,'eeg')))
metadata.class = 'eeg';
saveThis = true;
end
if isfield(metadata,'hardwareMetaDataObj')
metadata.hardwareMetaData = metadata.hardwareMetaDataObj;
metadata = rmfield(metadata,'hardwareMetaDataObj');
saveThis = true;
end
if ~isfield(metadata,'parentCommand')
metadata.parentCommand = [];
saveThis = true;
end
if ~isempty(metadata.parentCommand) && isfield(metadata.parentCommand,'uuid') && ~ischar(metadata.parentCommand.uuid)
metadata.parentCommand.uuid = char(metadata.parentCommand.uuid);
saveThis = true;
end
if strcmp(metadata.class,'vectorMeasure') || strcmp(metadata.class,'segmentedMocap')
metadata.class = 'mocap';
saveThis = true;
end
if ~isfield(metadata,'binFile')
if ~isfield(metadata,'mmfName'), metadata.mmfName = [header(1:end-4) '.bin'];end
metadata.binFile = metadata.mmfName;
metadata = rmfield(metadata,'mmfName');
saveThis = true;
end
if ~strcmp(metadata.binFile(1:end-4),header(1:end-4))
metadata.binFile = [header(1:end-4) '.bin'];
metadata.header = header;
saveThis = true;
end
if ~isfield(metadata,'sessionUUID')
if ~isempty(obj.item)
metadata.sessionUUID = obj.item{1}.sessionUUID;
else
uuid = generateUUID;
metadata.sessionUUID = uuid;
end
saveThis = true;
end
if ~ischar(metadata.uuid), metadata.uuid = char(metadata.uuid);saveThis = true;end
if ~ischar(metadata.sessionUUID), metadata.sessionUUID = char(metadata.sessionUUID);saveThis = true;end
if isfield(metadata,'segmentUUID') && ~ischar(metadata.segmentUUID), metadata.segmentUUID = char(metadata.segmentUUID);saveThis = true;end
if isfield(metadata,'originalStreamObj') && ~ischar(metadata.originalStreamObj), metadata.originalStreamObj = char(metadata.originalStreamObj);saveThis = true;end
binFile = pickfiles(obj.mobiDataDirectory,{metadata.uuid obj.sessionUUID '.bin'});
if ~strcmp(binFile,metadata.binFile)
metadata.binFile = binFile;
metadata.header = header;
saveThis = true;
end
if isfield(metadata,'segmentObj')
id = obj.segmentList.getSegmentID(metadata.segmentObj);
if isempty(id), metadata.segmentUUID = obj.segmentList.addSegment(metadata.segmentObj);
else metadata.segmentUUID = id;
end
metadata = rmfield(metadata,'segmentObj');
saveThis = true;
end
if saveThis
disp(['Updating: ' header]);
metadata2headerFile(metadata);
end
clear metadata
end
end
end
%%---------------------------------------
function outFile = look4it(file,name)
outFile = [];
[path,~,ext] = fileparts(file);
tmpFiles = pickfiles(path,{name ext});
if iscell(tmpFiles)
for it=1:length(tmpFiles)
[~,filename] = fileparts(tmpFiles{it});
indTmp = strfind(filename,name);
if indTmp==1
outFile = tmpFiles{it};
break
end
end
elseif ischar(tmpFiles) && size(tmpFiles,1)==1
[~,filename] = fileparts(tmpFiles);
indTmp = strfind(filename,name);
if indTmp==1
outFile = tmpFiles;
end
else
for it=1:size(tmpFiles,1)
[~,filename] = fileparts(deblank(tmpFiles(it,:)));
indTmp = strfind(filename,name);
if indTmp==1
outFile = deblank(tmpFiles(it,:));
break
end
end
end
end