-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_parse.m
170 lines (141 loc) · 4.75 KB
/
common_parse.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
% COMMON_PARSE.M
%
% this file contains basic "parsing logic" common
% to each of the bmi3 task behavior files.
%
% it is a script, not a function, so be very careful not to
% stomp on existing variables
%
% $Id: common_parse.m 769 2010-03-04 03:16:00Z joey $
% file is loaded here
%% RECENTLY COMMENTED!!
% picking trial states from trial start to the next trial start
% this avoids alignment issues if trials are aborted inbetween sessions
% also when merging two or more sessions
warning('OFF','MATLAB:load:variableNotFound');
warning('OFF','MATLAB:load:variablePatternNotFound');
WORKSPACE_BOUNDS = [-25 25 ; -25 25]; % very generous workspace boundaries
MY_EPS = 1e-4; % a small number, but not thaaat small
if (~exist('cursor_x','var'))
cursor_x = js_x;
cursor_y = js_y;
end
% this can happen if the task is only in the x-axis (i.e. never update y)
if (~exist('cursor_y','var'))
cursor_y = [0 0];
end
% isnt this bad?
if any(cursor_x(:,1) < WORKSPACE_BOUNDS(1,1))
cursor_x(cursor_x(:,1) < WORKSPACE_BOUNDS(1,1),:) = [];
end
if any(cursor_x(:,1) > WORKSPACE_BOUNDS(1,2))
cursor_x(cursor_x(:,1) > WORKSPACE_BOUNDS(1,2),:) = [];
end
if any(cursor_y(:,1) < WORKSPACE_BOUNDS(2,1))
cursor_y(cursor_y(:,1) < WORKSPACE_BOUNDS(2,1),:) = [];
end
if any(cursor_y(:,1) > WORKSPACE_BOUNDS(2,2))
cursor_y(cursor_y(:,1) > WORKSPACE_BOUNDS(2,2),:) = [];
end
if ~exist('ST_DEFAULT','var')
if exist('ST_READY','var')
ST_DEFAULT = ST_READY;
else
error('no ST_DEFAULT or ST_READY');
end
end
if ~exist('ST_OVERRIDE','var')
ST_OVERRIDE = -1;
end
% the first stab at trial start and end encodes
trial_start = find(state(:,1) == ST_DEFAULT);
trial_end = find( (state(:,1) == ST_INTERTRIAL) );
% look for sequential juice override button presses
% Turn: 109 105 109 105 109 105 101
% Into: 109 109 109 105 101
tmp = find(state(:,1) == ST_OVERRIDE);
try
% find an intertrial b/t two overrides
tmp2 = state(tmp+1,1) == ST_INTERTRIAL & state(tmp+2,1) == ST_OVERRIDE;
catch
% override at end of session
tmp2 = state(tmp(1:end-1)+1,1) == ST_INTERTRIAL & state(tmp(1:end-1)+2,1) == ST_OVERRIDE;
end
if (any(tmp2))
state((tmp(tmp2)+1),:) = []; % clear excess intertrials
trial_start = find(state(:,1) == ST_DEFAULT);
trial_end = find( (state(:,1) == ST_INTERTRIAL) );
end
% we also have to handle the case where a juice override is nestled
% between two intertrials through bad luck
% Turn: 105 109 105 101
% Into: 105 109 101
tmp = find(state(:,1) == ST_INTERTRIAL);
if (~isempty(tmp))
% ignore if intertrial comes very close to the end
if ( length(state) - tmp(end) <= 2 )
tmp(end) = [];
end
% find an override b/t two intertrials
tmp2 = state(tmp+1,1) == ST_OVERRIDE & state(tmp+2,1) == ST_INTERTRIAL;
if (any(tmp2))
% clear the second intertrial in each tripplet
state((tmp(tmp2))+2,:) = [];
trial_start = find(state(:,1) == ST_DEFAULT);
trial_end = find( (state(:,1) == ST_INTERTRIAL) );
end
clear tmp2;
end
clear tmp;
%% RECENTLY COMMENTED!!
% picking trial states from trial start to the next trial start
% this avoids alignment issues if trials are aborted inbetween sessions
% also when merging two or more sessions
% ignore trial(s) that are started right before the task is ended
% tmp = length(trial_start)-length(trial_end);
% if (tmp>0)
% trial_start = trial_start(1:end-tmp);
% end
% clear tmp;
ntrials = length(trial_start);
% picking up completed trials; omitting the last unfinised one.
if length(trial_start) > length(trial_end)
tmp = length(trial_start) - length(trial_end);
ntrials = length(trial_end);
trial_start = trial_start(1:end-tmp);
if tmp>1
disp('warning: more than one unfinished trials');
end
clear tmp
end
if (ntrials == 0)
warning('common_parse:no_trials','No completed trials in session!');
return
end
% preallocate trial structure
trial(ntrials) = struct( ...
'outcome', [], ...
'outcome_t', [], ...
'start_t', [], ... % ST_DEFAULT
'end_t', [], ... % ST_INTERTRIAL
'len', [], ...
'states', [], ... % the states for this trial
'states_t', [] ... % time of these states
);
outcome = state(trial_end-1,1); % whatever comes before ST_INTERTRIAL
outcome_time = state(trial_end-1,2); % time of outcome state
start_time = state(trial_start,2);
end_time = state(trial_end,2);
trial_len = end_time - start_time;
% fill the trial structure
for i=1:ntrials
trial(i).outcome = outcome(i);
trial(i).outcome_t = outcome_time(i);
trial(i).start_t = start_time(i);
trial(i).end_t = end_time(i);
trial(i).len = trial_len(i);
trial(i).states = state(trial_start(i):trial_end(i),1);
trial(i).states_t = state(trial_start(i):trial_end(i),2);
end
clear i;
clear outcome outcome_time start_time end_time trial_len;