forked from DeNardoLab/BehaviorDEPOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateEvents.m
155 lines (135 loc) · 6.17 KB
/
calculateEvents.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
function [out, Params, P] = calculateEvents(Behavior, Params, P)
tmpbeh = struct2cell(Behavior);
numframes = length(tmpbeh{1}.Vector);
%% load in cue frames from experiment file
% 5/19/21 ZZ
% section added to automate importing of multiple cue frames per file
if isempty(P.loadcueframes)
% ask for loading of cue frames
answer = questdlg('Load cue frames from cueframes var in experiment file?', '', "Yes", "No", "No");
if answer == "Yes"
P.loadcueframes = 1;
else
P.loadcueframes = 0;
end
end
if P.loadcueframes
cd(Params.basedir);
exp_file = dir('*-*-*_*-*-*.mat'); % load mat file
load([exp_file.folder, '\', exp_file.name], 'cueframes'); % load cueframes
cd(P.script_dir);
cueframes2 = struct2cell(cueframes);
cuenames = fieldnames(cueframes);
for i_cue = 1:length(cueframes2) % iterate over all cues
eventmat = cueframes2{i_cue};
eventname = cuenames{i_cue};
event_vec = zeros(numframes,1);
for i = 1:size(eventmat,1)
event_vec(eventmat(i,1):eventmat(i,2),1) = 1;
end
% loop through behaviors
beh_name = fieldnames(Behavior);
beh_cell = struct2cell(Behavior);
eventstats = [eventname '_PerBehDuringCue'];
for i = 1:length(beh_cell) % loop through behaviors
i_beh_name = string(beh_name(i)); % load behvavior name
i_beh_vec = beh_cell{i}.Vector; % load behavior vector
in_event_beh = find(event_vec == 1 & i_beh_vec == 1);
in_event_beh_vec = zeros(size(i_beh_vec));
in_event_beh_vec(in_event_beh) = 1;
per_event_beh = sum(in_event_beh_vec) / sum(i_beh_vec);
out.(eventname).EventVector = event_vec;
out.(eventname).EventBouts = eventmat;
out.(eventname).(i_beh_name).BehInEventVector = in_event_beh_vec;
out.(eventname).(i_beh_name).Cue_BehInEventVector = [];
for cue = 1:size(eventmat,1)
this_cue = in_event_beh_vec(eventmat(cue,1):eventmat(cue,2))';
out.(eventname).(i_beh_name).Cue_BehInEventVector = [out.(eventname).(i_beh_name).Cue_BehInEventVector; this_cue];
end
out.(eventname).(i_beh_name).PerBehInEvent = per_event_beh;
for j = 1:size(eventmat,1)
j_beh_vec = i_beh_vec(eventmat(j,1):eventmat(j,2));
stats{j} = sum(j_beh_vec) / length(j_beh_vec);
end
out.(eventname).(i_beh_name).PerBehDuringCue = stats;
end
end
else
%% prompt for number of timestamp files
tf = 0;
if isempty(Params.cueFile)
while ~tf
[numfiles,tf] = listdlg('PromptString','Number of timestamp files','ListString',{'1','2','3'}, 'SelectionMode','single');
end
else
numfiles = 1;
end
Params.num_events = numfiles;
% get cue file name if not previously saved
for f = 1:numfiles
if isempty(P.reuse_cue_name) || P.reuse_cue_name == 0
if numfiles >= 1
prompt = {'Assign name to time cues'};
dlgtitle = 'Input';
dims = [1 40];
definput = {'Event'};
eventname = inputdlg(prompt,dlgtitle,dims,definput);
eventname = cleanText(eventname);
eventname = char(eventname);
end
elseif P.reuse_cue_name == 1
eventname = P.eventname;
end
if Params.batch && isempty(Params.reuse_cue_name)
resp = questdlg('Re-use time cue name for other batched process files?', '', 'Yes', 'No', 'Yes');
if isequal(resp,'Yes')
Params.reuse_cue_name = 1;
P.reuse_cue_name = 1;
P.eventname = eventname;
elseif isequal(resp,'No')
Params.reuse_cue_name = 0;
P.reuse_cue_name = 0;
end
end
% load event times
if isempty(Params.cueFile)
disp(['Select file containing timestamps for ' eventname])
[f p] = uigetfile('*.*','Select file containing timestamps for events');
eventmat = readmatrix([p f]);
else
eventmat = readmatrix(Params.cueFile);
end
% convert event times to vector
event_vec = zeros(numframes,1);
for i = 1:size(eventmat,1)
event_vec(eventmat(i,1):eventmat(i,2),1) = 1;
end
% loop through behaviors
beh_name = fieldnames(Behavior);
beh_cell = struct2cell(Behavior);
eventstats = [eventname '_PerBehDuringCue'];
for i = 1:length(beh_cell) % loop through behaviors
i_beh_name = string(beh_name(i)); % load behvavior name
i_beh_vec = beh_cell{i}.Vector; % load behavior vector
in_event_beh = find(event_vec == 1 & i_beh_vec == 1);
in_event_beh_vec = zeros(size(i_beh_vec));
in_event_beh_vec(in_event_beh) = 1;
per_event_beh = sum(in_event_beh_vec) / sum(i_beh_vec);
out.(eventname).EventVector = event_vec;
out.(eventname).EventBouts = eventmat;
out.(eventname).(i_beh_name).BehInEventVector = in_event_beh_vec;
out.(eventname).(i_beh_name).Cue_BehInEventVector = [];
for cue = 1:size(eventmat,1)
this_cue = in_event_beh_vec(eventmat(cue,1):eventmat(cue,2))';
out.(eventname).(i_beh_name).Cue_BehInEventVector = [out.(eventname).(i_beh_name).Cue_BehInEventVector; this_cue];
end
out.(eventname).(i_beh_name).PerBehInEvent = per_event_beh;
for j = 1:size(eventmat,1)
j_beh_vec = i_beh_vec(eventmat(j,1):eventmat(j,2));
stats{j} = sum(j_beh_vec) / length(j_beh_vec);
end
out.(eventname).(i_beh_name).PerBehDuringCue = stats;
end
end
end
end