-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheegplugin_eyesort.m
More file actions
322 lines (276 loc) · 12.5 KB
/
Copy patheegplugin_eyesort.m
File metadata and controls
322 lines (276 loc) · 12.5 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
% SPDX-License-Identifier: GPL-3.0-or-later
% EyeSort - Region-aware eye-tracking event labeling for EEGLAB
% Copyright (C) 2025 Eye Movements & Cognition Lab (USF)
% Copyright (C) 2025 Brandon Snyder, Sara Milligan, Elizabeth Schotter
% Author: Brandon Snyder
function currvers = eegplugin_eyesort(fig, ~, ~)
% EEGPLUGIN_EYESORT - Register the EyeSort EEGLAB plugin menu and callbacks.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% EyeSort Plugin for EEGLAB: %
% Main setup function %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Call the default values script to intialize global variables
eyesort_default_values;
% Clear any lingering batch processing variables from previous sessions
try
evalin('base', 'clear eyesort_batch_file_paths eyesort_batch_filenames eyesort_batch_output_dir eyesort_batch_mode');
catch
% Variables might not exist, which is fine
end
% Return plugin name and version in EEGLAB's standard parsable format.
currvers = ['EyeSort' eyesortver];
% Ensure minimum arguments are met
if nargin < 3
error('eegplugin_eyesort requires 3 arguments');
end
% Add the to the MATLAB path
p = which('eegplugin_eyesort', '-all');
% Ensures no duplicates
if length(p) > 1
warning('EyeSort:MultiplePaths', ...
'Multiple EyeSort folders found. Using the first one: %s', p{1});
end
p = p{1};
idx = strfind(p, 'eegplugin_eyesort.m');
if ~isempty(idx)
p = p(1:idx - 1); % Extract the folder path
addpath(p);
addpath(fullfile(p, 'pop_functions'));
addpath(fullfile(p, 'functions'));
else
error('Failed to locate the EyeSort plugin path.');
end
% Check if the EyeSort menu already exists
menuEEGLAB = findobj(fig, 'tag', 'EEGLAB'); % Find EEGLAB main menu
existingMenu = findobj(menuEEGLAB, 'tag', 'EyeSort'); % Check for existing EyeSort menu
%% Initializes EyeSort to the EEGLAB menu
if isempty(existingMenu)
% Create main menu with try-catch for better error handling
try
submenu = uimenu(menuEEGLAB, 'Label', 'EyeSort', 'tag', 'EyeSort', ...
'separator', 'on', 'Callback', @(~,~) update_eyesort_menu_state(), ...
'userdata', 'study:on;erpset:on');
% Store version number in a more accessible way
setappdata(submenu, 'EyeSortVersion', eyesortver);
% Add version as first item in the dropdown menu
uimenu(submenu, 'label', [' EyeSort v' eyesortver], ...
'separator', 'on');
% Store main submenu handle for menu state updates
setappdata(0, 'EyeSort_MainMenu', submenu);
% Add error checking for callbacks
uimenu(submenu, 'label', '1. Load EEG Dataset(s)', 'separator', 'on', ...
'userdata', 'study:on;erpset:on', ...
'callback', @(src,event) try_callback(@pop_load_datasets, src, event));
% Improve menu structure with error handling
loadInterestAreasMenu = uimenu(submenu, 'Label', '2. Setup Interest Areas', ...
'separator', 'off', 'Tag', 'EyeSort_LoadIA', 'enable', 'off', ...
'userdata', 'startup:off;study:on;erpset:on');
uimenu(loadInterestAreasMenu, 'Label', 'Text-Based Sentence Contents and Interest Areas', ...
'Tag', 'EyeSort_TextIA', 'enable', 'off', ...
'userdata', 'startup:off;study:on;erpset:on', ...
'callback', @(src,event) try_callback(@pop_load_text_ia, src, event));
uimenu(loadInterestAreasMenu, 'Label', 'Inspect Parsed Regions', ...
'Tag', 'EyeSort_InspectRegions', 'enable', 'off', ...
'userdata', 'startup:off;study:on;erpset:on', ...
'callback', @(src,event) try_callback(@pop_inspect_regions, src, event));
% Import columns from IA text file into EEG events
uimenu(submenu, 'label', '3. Import IA Columns to Events', 'separator', 'off', ...
'userdata', 'startup:off;study:on;erpset:on', ...
'callback', @(src,event) try_callback(@pop_import_ia_columns, src, event));
% Eye-tracking event labeling
uimenu(submenu, 'label', '4. Eye-Tracking Event Labeling', 'separator', 'off', ...
'userdata', 'startup:off;study:on;erpset:on', ...
'callback', @(src,event) try_callback(@pop_label_datasets, src, event));
% Add the new BDF generator menu item
uimenu(submenu, 'label', 'Generate BINLISTER BDF File', 'separator', 'on', ...
'userdata', 'study:on;erpset:on', ...
'callback', @(src,event) try_callback(@pop_generate_bdf, src, event));
uimenu(submenu, 'label', 'Modify Event Code Format', 'separator', 'off', ...
'userdata', 'startup:off;study:on;erpset:on', ...
'callback', @(src,event) try_callback(@pop_convert_event_codes, src, event));
historyScriptsMenu = uimenu(submenu, 'Label', 'History Scripts', ...
'separator', 'on', 'Tag', 'EyeSort_HistoryScripts', ...
'userdata', 'study:on;erpset:on');
uimenu(historyScriptsMenu, 'Label', 'Save processing history script', ...
'Tag', 'EyeSort_SaveSessionHistory', ...
'userdata', 'study:on;erpset:on', ...
'callback', @(src,event) try_callback(@save_eyesort_processing_script, src, event));
uimenu(submenu, 'label', 'Help', 'separator', 'on', ...
'userdata', 'study:on;erpset:on', ...
'callback', @(src,event) try_callback(@help_button, src, event));
catch ME
error('EyeSort:MenuCreation', 'Failed to create EyeSort menu: %s', ME.message);
end
else
warning('EyeSort:ExistingMenu', 'EyeSort menu already exists. Skipping creation.');
end
end
% No-output wrapper so try_callback does not treat the script path as history.
function save_eyesort_processing_script()
save_eyesort_session_script();
end
% Helper function for safer callback execution
function try_callback(callback_fn, ~, ~)
try
com = '';
EEG = [];
outputCount = nargout(callback_fn);
if outputCount >= 2
[EEG, com] = callback_fn();
elseif outputCount == 1
output = callback_fn();
if ischar(output) || isstring(output)
com = output;
else
EEG = output;
end
else
callback_fn();
end
EEG = get_current_eyesort_eeg(EEG);
if ~isempty(com)
EEG = record_eyesort_history(com, EEG);
end
refresh_eyesort_eeglab_state(EEG);
% Update menu state after successful operations (especially dataset loading)
update_eyesort_menu_state();
catch ME
errordlg(sprintf('Error in EyeSort operation: %s', ME.message), 'EyeSort Error');
rethrow(ME);
end
end
function EEG = get_current_eyesort_eeg(EEG)
try
baseEEG = evalin('base', 'EEG');
if ~isempty(baseEEG) && isstruct(baseEEG)
EEG = baseEEG;
end
catch
% Use the callback output if the base workspace is not available.
end
end
function EEG = refresh_eyesort_eeglab_state(EEG)
try
EEG = get_current_eyesort_eeg(EEG);
if ~isempty(EEG) && isstruct(EEG) && isscalar(EEG)
try
ALLEEG = evalin('base', 'ALLEEG');
CURRENTSET = evalin('base', 'CURRENTSET');
catch
ALLEEG = [];
CURRENTSET = 0;
end
if isempty(ALLEEG) || isempty(CURRENTSET) || ...
CURRENTSET < 1 || CURRENTSET > numel(ALLEEG)
[ALLEEG, EEG, CURRENTSET] = eeg_store(ALLEEG, EEG, 0);
else
[ALLEEG, EEG, CURRENTSET] = eeg_store(ALLEEG, EEG, CURRENTSET);
end
assignin('base', 'ALLEEG', ALLEEG);
assignin('base', 'EEG', EEG);
assignin('base', 'CURRENTSET', CURRENTSET);
elseif ~isempty(EEG) && isstruct(EEG)
assignin('base', 'EEG', EEG);
end
try
eeglab('redraw');
catch
% Redraw is best-effort; command-line workflows may not have a GUI.
end
catch ME
warning('EyeSort:RefreshState', 'Failed to refresh EEGLAB state: %s', ME.message);
end
end
% Function to update EyeSort menu state based on loaded datasets
function update_eyesort_menu_state()
try
% Get the main menu handle
mainMenu = getappdata(0, 'EyeSort_MainMenu');
if isempty(mainMenu) || ~ishandle(mainMenu)
return;
end
% Find the Load Interest Areas menu
loadIAMenu = findobj(mainMenu, 'Tag', 'EyeSort_LoadIA');
if isempty(loadIAMenu)
return;
end
% Check if datasets are loaded (either in ALLEEG or batch mode)
hasDatasets = false;
% First check for batch mode
try
batchMode = evalin('base', 'eyesort_batch_mode');
batchFilePaths = evalin('base', 'eyesort_batch_file_paths');
if ~isempty(batchMode) && batchMode && ~isempty(batchFilePaths) && length(batchFilePaths) >= 1
hasDatasets = true;
end
catch
% Batch mode variables don't exist, continue to check ALLEEG
end
% If not in batch mode, check ALLEEG for individual datasets
if ~hasDatasets
try
ALLEEG = evalin('base', 'ALLEEG');
if ~isempty(ALLEEG) && length(ALLEEG) >= 1
% Check if at least one dataset has actual data AND filename
for i = 1:length(ALLEEG)
if ~isempty(ALLEEG(i)) && isstruct(ALLEEG(i)) && ...
isfield(ALLEEG(i), 'data') && ~isempty(ALLEEG(i).data) && ...
isfield(ALLEEG(i), 'nbchan') && ALLEEG(i).nbchan > 0 && ...
isfield(ALLEEG(i), 'filename') && ~isempty(ALLEEG(i).filename) && ...
~strcmp(ALLEEG(i).filename, '')
hasDatasets = true;
break;
end
end
end
catch
% ALLEEG doesn't exist or can't be accessed
hasDatasets = false;
end
end
% Enable/disable Load Interest Areas menu based on dataset state
if hasDatasets
set(loadIAMenu, 'enable', 'on');
% Enable text IA submenu
textIAMenu = findobj(mainMenu, 'Tag', 'EyeSort_TextIA');
if ~isempty(textIAMenu), set(textIAMenu, 'enable', 'on'); end
else
set(loadIAMenu, 'enable', 'off');
% Disable text IA submenu
textIAMenu = findobj(mainMenu, 'Tag', 'EyeSort_TextIA');
if ~isempty(textIAMenu), set(textIAMenu, 'enable', 'off'); end
end
% Enable Inspect Parsed Regions only after Step 2 has been run
inspectMenu = findobj(mainMenu, 'Tag', 'EyeSort_InspectRegions');
if ~isempty(inspectMenu)
hasProcessed = false;
try
EEG = evalin('base', 'EEG');
if isfield(EEG, 'eyesort_processed') && EEG.eyesort_processed
hasProcessed = true;
end
catch
end
if ~hasProcessed
try
ALLEEG = evalin('base', 'ALLEEG');
for i = 1:length(ALLEEG)
if isfield(ALLEEG(i), 'eyesort_processed') && ALLEEG(i).eyesort_processed
hasProcessed = true;
break;
end
end
catch
end
end
if hasProcessed
set(inspectMenu, 'enable', 'on');
else
set(inspectMenu, 'enable', 'off');
end
end
catch ME
% Silently handle errors to avoid disrupting the plugin
warning('EyeSort:MenuUpdate', 'Failed to update menu state: %s', ME.message);
end
end