-
Notifications
You must be signed in to change notification settings - Fork 1
/
endOfExperiment.m
112 lines (84 loc) · 2.9 KB
/
endOfExperiment.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
%=========================================================================%
% individual states are
%sessionStart
%prepareNextTrial
%run
%timeOut
%trialEnd
%enOfExperiment
%=========================================================================%
function fhandle = endOfExperiment
global daqSession
global BallUDPPort
global ScanImageUDP;
global EyeCameraUDP;
global TimelineUDP;
global OptiStimUDP;
global EXPREF;
global OFFLINE;
global SESSION;
global EXP;
% cleans up and exits state system
fprintf('<stateSystem> endOfExperiment\n'); % debug
Priority(0);
Screen('CloseAll');
SESSION.Log;
if ~isequal(EXP.stimType, 'REPLAY_SCRAMBLED')
nSmallRewards = sum(ismember({SESSION.Log(2:end).Event}, {'INTERMEDIATE', 'USER'}));
nLargeRewards = sum(ismember({SESSION.Log(2:end).Event}, {'CORRECT'}));
fprintf('\nnTrials = %d, nSmallRewards = %d, nLargeRewards = %d\n\n', ...
length(SESSION.allTrials), nSmallRewards, nLargeRewards);
waterAmount = nSmallRewards*EXP.smallRewardAmount + ...
nLargeRewards*EXP.largeRewardAmount;
fprintf('Water received = %05.3f ml\n', waterAmount);
end
if ~OFFLINE
stopAllDaq;
delete(daqSession);
[animalID, iseries, iexp] = dat.expRefToMpep(EXPREF);
msgString = sprintf('ExpEnd %s %d %d', animalID, iseries, iexp);
pnet(ScanImageUDP, 'write', msgString);
pnet(ScanImageUDP, 'writePacket');
pnet(EyeCameraUDP, 'write', msgString);
pnet(EyeCameraUDP, 'writePacket');
pnet(TimelineUDP, 'write', msgString);
pnet(TimelineUDP, 'writePacket');
if EXP.optiStim
msgStruct = struct('instruction', 'ExpEnd', 'ExpRef', EXPREF);
msgJson = savejson('msg', msgStruct);
pnet(OptiStimUDP, 'write', msgJson);
pnet(OptiStimUDP, 'writePacket');
end
end
heapTotalMemory = java.lang.Runtime.getRuntime.totalMemory;
heapFreeMemory = java.lang.Runtime.getRuntime.freeMemory;
%closing all the UDP ports
pnet(BallUDPPort, 'close');
pnet(ScanImageUDP, 'close');
pnet(EyeCameraUDP, 'close');
pnet(TimelineUDP, 'close');
if EXP.optiStim
pnet(OptiStimUDP, 'close');
end
clear mex;
if(heapFreeMemory < (heapTotalMemory*0.1))
java.lang.Runtime.getRuntime.gc;
fprintf('\n garbage collection \n');
end
fhandle = []; % exit state system
% return; %skip the Alyx thing for now
if strfind(upper(animalID), 'FAKE')
% do nothing
else
fprintf('Saving to Alyx..\n')
% if exist('C:\Users\Experiment\Documents\MATLAB\Alyx', 'dir')
% addpath(genpath('C:\Users\Experiment\Documents\MATLAB\Alyx'));
% else
% addpath(genpath('C:\Users\Experiment\Documents\GitHub\alyx-matlab'));
% end
% onLoad;
% alyxData.user = 'julie';
myAlyx = Alyx;
newWater = myAlyx.postWater(animalID, waterAmount, now, 'Water 10% Sucrose');
fprintf('%05.3f ml water administered\n%s\n', waterAmount, newWater.url)
end