forked from cortex-lab/mmmGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouseMovieManager.m
276 lines (223 loc) · 9.78 KB
/
MouseMovieManager.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
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
classdef MouseMovieManager < handle
% MouseMovieManager
% keeps track of a mouse movie object, allows starting, stopping,
% transferring to server, etc.
%
% TODO:
% - check that files don't exist already, both locally and on server
properties
mouseName = 'noName';
expNum = 1;
expDate = now;
vidObj = [];
videoID = 'none';
expServerName = [];
expServerObj = [];
copyToServer = true;
filePaths = {};
writerType = 'none';
statusCallback = [];
guiHandle = [];
connectTimer = [];
alyxInstance = [];
end
methods (Static)
function mmmObj = MouseMovieManager(videoID)
% constructor given config specification
if nargin>0
mmmObj = MouseMovieManager;
%mmmObj.mouseName = mouseName;
mmmObj.videoID = videoID;
mmmObj.vidObj = mmm.openVideoObject(videoID);
myUD = get(mmmObj.vidObj, 'UserData');
myUD.parentObj = mmmObj;
set(mmmObj.vidObj, 'UserData', myUD);
mmmObj.startPreview()
mmmObj.statusCallback = @mmm.defaultStatusCallback;
localP = mmmObj.localPath();
if disk_free(localP(1:find(localP==':',1)))<20e9
mmmObj.statusCallback(mmmObj, sprintf('WARNING! Disk space is low! Please clear space!'));
end
end
end
end
methods
function startPreview(mmmObj)
preview(mmmObj.vidObj);
end
function setROI(mmmObj)
v = mmmObj.vidObj;
stoppreview(v)
v.ROIPosition = [0 0 v.VideoResolution];
hPreview = preview(v);
h = imrect(get(hPreview, 'Parent'));
pos = wait(h);
stoppreview(v)
v.ROIPosition = pos;
set(h, 'visible', 'off');
preview(v);
mmmObj.vidObj = v;
end
function setLineROI(mmmObj)
setROI(mmmObj);
v = mmmObj.vidObj;
stoppreview(v);
ROI = v.ROIPosition;
ROI(3) = 1;
v.ROIPosition = ROI;
preview(v);
end
function start(mmmObj)
% use this manually, from the command line
mmmObj.expDate = now;
mmmObj.startDirect();
end
function startDirect(mmmObj)
mmm.openVideoWriter(mmmObj);
start(mmmObj.vidObj);
mmmObj.statusCallback(mmmObj, sprintf('now recording'));
end
function p = localPath(mmmObj)
% p = fullfile(getLocalDir(), mmmObj.mouseName, ...
% datestr(mmmObj.expDate, 'yyyy-mm-dd'), ...
% num2str(mmmObj.expNum));
p = dat.expPath(mmmObj.mouseName, mmmObj.expDate, mmmObj.expNum, 'main', 'local');
end
function fn = localFileName(mmmObj)
p = mmmObj.localPath();
fn = fullfile(p, mmmObj.videoID);
end
function p = serverPath(mmmObj)
% p = fullfile(getServerDir(), mmmObj.mouseName, ...
% datestr(mmmObj.expDate, 'yyyy-mm-dd'), ...
% num2str(mmmObj.expNum));
p = dat.expPath(mmmObj.mouseName, mmmObj.expDate, mmmObj.expNum, 'main', 'master');
end
function fn = serverFileName(mmmObj)
p = mmmObj.serverPath();
fn = fullfile(p, mmmObj.videoID);
end
function stop(mmmObj)
stopVid(mmmObj)
stopCleanup(mmmObj)
stopCopy(mmmObj)
if ~isempty(mmmObj.guiHandle)
handles = guidata(mmmObj.guiHandle);
set(handles.edtMouseName, 'String', '[idle]');
set(handles.edtExpNum, 'String', '[idle]');
set(handles.edtExpDate, 'String', '[idle]');
end
end
function stopVid(mmmObj)
% helper for stop
stop(mmmObj.vidObj)
mmmObj.statusCallback(mmmObj, sprintf('no longer recording'));
end
function stopCleanup(mmmObj)
% write the ROI position
ROI = mmmObj.vidObj.ROIPosition;
save([mmmObj.localFileName() '_ROI.mat'], 'ROI');
mmmObj.filePaths{end+1} = [mmmObj.localFileName() '_ROI.mat'];
if strcmp(mmmObj.writerType, 'myWriteMovieData')
mmmObj.statusCallback(mmmObj, sprintf('waiting for movie to finish collecting'));
pause(mmmObj.vidObj.TimerPeriod*1.1);
while mmmObj.vidObj.framesAvailable>0
mmmObj.statusCallback(mmmObj, sprintf('getting %d more frames...', mmmObj.vidObj.framesAvailable));
q = mmmObj.vidObj.TimerFcn;
fid = q{2};
mmm.myWriteMovieData(mmmObj.vidObj, [], fid);
end
framesAcquired = mmmObj.vidObj.framesAcquired;
eventLog = get(mmmObj.vidObj, 'EventLog');
save([mmmObj.localFileName() '_metadata.mat'], 'framesAcquired', 'eventLog');
mmmObj.filePaths{end+1} = [mmmObj.localFileName() '_metadata.mat'];
fclose(fid);
end
end
function stopCopy(mmmObj)
if mmmObj.copyToServer
copyNow(mmmObj);
end
end
function copyNow(mmmObj)
p = mmmObj.serverPath();
mmmObj.statusCallback(mmmObj, sprintf('copying data to %s', p));
mkdir(p);
if ~isempty(mmmObj.filePaths)
for f = 1:length(mmmObj.filePaths)
mmmObj.statusCallback(mmmObj, sprintf(' copying %s', mmmObj.filePaths{f}));
copyfile(mmmObj.filePaths{f}, p);
subject = mmmObj.mouseName;
if ~isempty(mmmObj.alyxInstance) && ~strcmp(subject,'default')
try
[~,fn,ext] = fileparts(mmmObj.filePaths{f});
fullpath = fullfile(p,[fn ext]);
if isfield(mmmObj.alyxInstance, 'subsessionURL')
subsessionURL = mmmObj.alyxInstance.subsessionURL;
else
subsessionURL = [];
end
if strcmp(ext, '.mj2')
dsetType = [mmmObj.videoID 'Movie'];
% alyx.registerFile(subject,subsessionURL,dsetType,...
% fullpath,'zserver',mmmObj.alyxInstance);
alyx.registerFile(fullpath, 'mj2', ...
subsessionURL, dsetType, ...
[], mmmObj.alyxInstance);
end
catch ex
warning('couldnt register files to alyx');
disp(ex)
end
end
end
end
mmmObj.filePaths = {};
mmmObj.statusCallback(mmmObj, 'done copying data.');
end
function listenForStart(mmmObj)
events = {'ExpStarting', 'ExpStarted', 'ExpStopped', 'Connected', 'Disconnected', 'AlyxSend'};
anonListen = @(srcObj, eventObj) mmm.movieListener(srcObj, eventObj, mmmObj);
for e = 1:length(events)
s{e} = mmmComm.create(mmm.getExpServerName());
% s{e}.connect(true);
addlistener(s{e}, events{e}, anonListen);
end
mmmObj.expServerObj = s;
mmmObj.connectTimer = timer('Period', 10, 'TimerFcn', @(s,e)mmm.tryConnect(s,e,mmmObj), 'ExecutionMode', 'fixedRate');
mmmObj.statusCallback(mmmObj, sprintf('trying to connect to... %s', s{1}.Name));
start(mmmObj.connectTimer);
end
function reset(mmmObj)
mmmObj.vidObj.ROIPosition = [0 0 mmmObj.vidObj.VideoResolution];
mmmObj.filePaths = {};
if strcmp(mmmObj.writerType, 'VideoWriter')
close(mmmObj.vidObj.DiskLogger);
end
localP = mmmObj.localPath();
if disk_free(localP(1:find(localP==':',1)))<20e9
mmmObj.statusCallback(mmmObj, sprintf('WARNING! Disk space is low! Please clear space!'));
end
end
function close(mmmObj)
mmmObj.delete;
end
function delete(mmmObj)
mmmObj.statusCallback(mmmObj, sprintf('deleting movie object'));
if ~isempty(mmmObj.expServerObj)
for s = 1:length(mmmObj.expServerObj)
fprintf(1, 'disconnecting expServerObj %d\n', s);
mmmObj.expServerObj{s}.disconnect();
end
end
stoppreview(mmmObj.vidObj);
if strcmp(mmmObj.writerType, 'VideoWriter')
close(mmmObj.vidObj.DiskLogger);
else
% file should have been closed already on stop
end
delete(mmmObj.vidObj);
mmmObj.statusCallback(mmmObj, sprintf('deleted movie object'));
end
end
end