-
Notifications
You must be signed in to change notification settings - Fork 16
/
mff_exportevents.m
149 lines (135 loc) · 6.48 KB
/
mff_exportevents.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
% mff_exportevents - export MFF EEG event from EEGLAB structure to
% 'Events_exported_from_EEGLAB.xml' file
% Usage:
% mff_exportevents(EEG, mffFile);
%
% Inputs:
% EEG - EEGLAB structure
% mffFile - filename/foldername for the MFF file (MFF file/folder must
% already exist)
% This file is part of mffmatlabio.
%
% mffmatlabio is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% mffmatlabio is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with mffmatlabio. If not, see <https://www.gnu.org/licenses/>.
function mff_exportevents(EEG, mffFile)
mff_path;
if isfield(EEG.etc, 'recordingtime')
begTime = EEG.etc.recordingtime;
else
begTime = now;
end
if isfield(EEG.etc, 'timezone')
timeZone = EEG.etc.timezone;
else
timeZone = '00:00';
end
srate = EEG.srate;
% create a factory
mfffactorydelegate = javaObject('com.egi.services.mff.api.LocalMFFFactoryDelegate');
mfffactory = javaObject('com.egi.services.mff.api.MFFFactory', mfffactorydelegate);
% determine the number of files
events = EEG.event;
if isfield(EEG.event, 'name')
allNames = { events.name };
allNames = cellfun(@num2str, allNames, 'UniformOutput', false);
uniqueNames = unique( allNames );
else
uniqueNames = { '' };
end
tracktype = '';
for iFile = 1:length(uniqueNames)
% Create Signal object and read in event track file.
fileName = 'Events_exported_from_EEGLAB.xml';
if length(uniqueNames) > 1
eventtrackfilename = fullfile(mffFile, [ fileName(1:end-4) '_' num2str(iFile) '.xml' ]);
indEvents = strmatch( uniqueNames{iFile}, allNames, 'exact');
if isfield(events, 'tracktype')
tracktype = events(indEvents(1)).tracktype;
end
else
eventtrackfilename = fullfile(mffFile, fileName);
if isfield(events, 'tracktype')
tracktype = events(1).tracktype;
end
indEvents = 1:length(events);
end
if ~isempty(events) && ~all(cellfun(@(x)isequal(x, 'boundary'), {events(indEvents).type}))
eventtracktype = javaObject('com.egi.services.mff.api.MFFResourceType', javaMethod('valueOf', 'com.egi.services.mff.api.MFFResourceType$MFFResourceTypes', 'kMFF_RT_EventTrack'));
if mfffactory.createResourceAtURI(eventtrackfilename, eventtracktype)
disp('Success at creating the event file');
else
disp('Could not create event file');
end
fprintf('Exporting %d events...\n', length(events));
eventtrackObj = mfffactory.openResourceAtURI(eventtrackfilename, eventtracktype);
if isfield(events, 'name') && ~isempty(uniqueNames{iFile})
eventtrackObj.setName(events(1).name);
end
if isfield(events, 'tracktype') && ~isempty(tracktype)
eventtrackObj.setTrackType(tracktype);
end
jList = javaObject('java.util.ArrayList');
addLatency = 0;
multiplier = 24*60*60*srate;
keyWarning = true;
for iEvent = 1:length(events)
if strcmpi(events(iEvent).type, 'boundary') % do not export boundary events
addLatency = addLatency + events(iEvent).duration;
else
if ~isfield(events, 'name') || strcmpi(events(iEvent).name, uniqueNames{iFile})
eventObj = javaObject('com.egi.services.mff.api.Event');
% Get keys for event and display key codes
if isfield(events(iEvent), 'code')
eventObj.setCode( events(iEvent).code );
else
eventObj.setCode(num2str(events(iEvent).type));
end
if isfield(events(iEvent), 'description'), eventObj.setDescription( events(iEvent).description ); end
if isfield(events(iEvent), 'duration' ), eventObj.setDuration( events(iEvent).duration ); end
if isfield(events(iEvent), 'label'), eventObj.setLabel( events(iEvent).label ); end
if isfield(events(iEvent), 'sourcedevice'), eventObj.setSourceDevice(events(iEvent).sourcedevice ); end
if isfield(events, 'mffkeysbackup') && ~isempty(events(iEvent).mffkeysbackup)
jListKeys = javaObject('java.util.ArrayList');
tmpmffkeysbackup = events(iEvent).mffkeysbackup;
tmpmffkeysbackup(tmpmffkeysbackup == 10) = ' '; % remove carriage return
tmpKeys = eval(tmpmffkeysbackup);
for iKey = 1:length(tmpKeys);
keyObj = javaObject('com.egi.services.mff.api.Key');
keyObj.setCode(tmpKeys(iKey).code);
keyObj.setData(tmpKeys(iKey).data);
keyObj.setDataType(tmpKeys(iKey).datatype);
keyObj.setDescription(tmpKeys(iKey).description);
jListKeys.add(keyObj);
end
eventObj.setKeys(jListKeys);
elseif keyWarning
disp('Warning: no MFF backup key structure found - MFF keys not exported');
disp(' import keys when loading the data to be able to export them');
keyWarning = false;
end
realLatency = (events(iEvent).latency + addLatency)/multiplier+begTime;
tmp = mff_encodetime(realLatency, timeZone);
if isfield(events(iEvent), 'begintime')
if ~isequal(events(iEvent).begintime, tmp) && ~isempty(events(iEvent).begintime)
fprintf('Note: exported event time %d differ from original one %s vs %s\n', iEvent, events(iEvent).begintime, tmp);
end
end
eventObj.setBeginTime(mff_encodetime(realLatency, timeZone));
jList.add(eventObj);
end
end
end
eventtrackObj.setEvents(jList);
eventtrackObj.saveResource();
end
end