-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoloAcqnvsPrairietest.m
140 lines (112 loc) · 4.48 KB
/
HoloAcqnvsPrairietest.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
function HoloAcqnvsPrairietest(folder, animal, day, mask)
%{
Function to acquire the holostim in a prairie scope
animal -> animal for the experiment
day -> day for the experiment
neuronMask -> matrix for spatial filters with px*py*unit
%}
%%
%**********************************************************
%**************** PARAMETERS ****************************
%**********************************************************
expectedLengthExperiment = 3*7000%ceil(3*60*frameRate); % in frames
savePath = fullfile(folder, animal, day);
if ~exist(savePath, 'dir')
mkdir(savePath);
end
%prairie view parameters
chanIdx = 2; % green channel
% envPath = fullfile(folder,"utils", "Tseries_VivekNuria_15.env") ;%TODO set environment
%%
%*********************************************************************
%****************** INITIALIZE ***********************************
%*********************************************************************
global pl holoActivity imageBuffer
%% Cleaning
finishup = onCleanup(@() cleanMeUp(savePath)); %in case of ctrl-c it will lunch cleanmeup
%% Prepare the nidaq
% s = daq.createSession('ni');
% addDigitalChannel(s,'dev5','Port0/Line0:0','OutputOnly');
%% Prepare for Prairie
% connection to Prairie
pl = actxserver('PrairieLink.Application');
pl.Connect()
% pause needed for prairie to respond
pause(2)
% Prairie variables
px = pl.PixelsPerLine();
py = pl.LinesPerFrame();
% Prairie commands
pl.SendScriptCommands("-srd True 0");
pl.SendScriptCommands("-lbs True 0");
%define where to save the file
savePathPrairie = fullfile(savePath, "im");
if ~exist(savePathPrairie, 'dir')
mkdir(savePathPrairie);
end
savePrairieFiles(savePath, pl, "holostim")
lastFrame = zeros(px, py); % to compare with new incoming frames
% set the environment for the Time Series in PrairieView
% tslCommand = "-tsl " + envPath;
% pl.SendScriptCommands(tslCommand);
%% Load variables
numberNeurons = max(max(mask));
% create smaller versions of the spatial filter
strcMask = obtainStrcMaskfromMask(mask);
%% Prepare the nidaq
s = daq.createSession('ni');
addDigitalChannel(s,'dev5','Port0/Line0:2','OutputOnly');
ni_out = [0 0 0];
outputSingleScan(s,ni_out);%set
ni_getimage = [1 0 0];
%% Create the file where to store the holostim
holoActivity = zeros(numberNeurons, expectedLengthExperiment) + nan;
fileName = fullfile(savePath, 'holoActivity.dat');
% creates a file with the correct shape
fileID = fopen(fileName,'w');
if ~exist(fileName, 'file')
disp('file does not exist. Memmap will not be saved')
end
fwrite(fileID, holoActivity,'double');
fclose(fileID);
% maps the file into memory
m = memmapfile(fileName, 'Format',{'double',size(holoActivity),'holoAct'}, 'repeat', 1);
m.Writable = true;
%%
%************************************************************************
%*************************** RUN ********************************
%************************************************************************
frame = 1; % initialize frames
%start the time_series scan
pl.SendScriptCommands("-ts");
pause(0.05); %empirically discovered time for the prairie to start gears
imageBuffer = zeros(512,512,expectedLengthExperiment);
disp('Starting holo acquisition')
counterSame = 0;
while counterSame < 1000
Im = pl.GetImage_2(chanIdx, px, py);
if ~isequal(Im,lastFrame)
lastFrame = Im; % comparison and assignment takes ~4ms
imageBuffer(:,:,frame) = Im;
outputSingleScan(s,ni_getimage); pause(0.001); outputSingleScan(s,[0 0 0]);
unitVals = obtainRoi(Im, strcMask); % function to obtain Rois values
holoActivity(:,frame) = unitVals;
m.Data.holoAct(:,frame) = unitVals; % 1 ms
frame = frame + 1;
counterSame = 0;
else
counterSame = counterSame + 1;
end
end
% pl.Disconnect();
end
function cleanMeUp(savePath)
global pl holoActivity imageBuffer
disp('cleaning')
% evalin('base','save baseVars.mat'); %do we want to save workspace?
% saving the global variables
save(fullfile(savePath, 'holoOnline.mat'), 'holoActivity', 'imageBuffer')
if pl.Connected()
pl.Disconnect();
end
end