forked from cortex-lab/Suite2P
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_ops3.m
191 lines (161 loc) · 6.98 KB
/
build_ops3.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
function ops = build_ops3(db, ops)
ops.nplanes = getOr(ops, 'nplanes', 1);
ops.nchannels = getOr(ops, 'nchannels', 1);
ops.readTiffHeader = getOr(ops,'readTiffHeader',1);
if isfield(db, 'expred')
if ~isempty(db.expred)
ops.nchannels_red = getOr(ops, 'nchannels_red', 2);
end
end
% ops = db;
if ~iscell(db.mouse_name)
% this is the usual case where we have a simple single session recording
ops = addfields(ops, db);
for k = 1:length(db.expts)
ops.SubDirs{k} = num2str(db.expts(k));
end
if isempty(db.expts)
ops.SubDirs{1} = [];
end
if ~isfield(ops, 'RootDir')
ops.RootDir = fullfile(ops.RootStorage, ops.mouse_name, ops.date);
end
% build file list
ops.fsroot = [];
for j = 1:length(ops.SubDirs)
ffile = dir(fullfile(ops.RootDir, ops.SubDirs{j}, '*.tif'));
fname = struct2cell(ffile);
fname = fname(1,:)';
[~,index] = sort_nat(fname);
ops.fsroot{j} = ffile(index);
ops.fsroot{j} = cat(1, ops.fsroot{j}, ...
dir(fullfile(ops.RootDir, ops.SubDirs{j}, '*.tiff')));
for k = 1:length(ops.fsroot{j})
ops.fsroot{j}(k).name = fullfile(ops.RootDir, ops.SubDirs{j}, ops.fsroot{j}(k).name);
end
end
if isfield(db, 'expred') && ~isempty(db.expred) && ...
(~isfield(db, 'nchannels_red') || isempty(db.nchannels_red))
ffile = dir(fullfile(ops.RootDir, num2str(db.expred), '*.tif'));
fname = struct2cell(ffile);
fname = fname(1,:)';
[~,index] = sort_nat(fname);
ops.fsred = ffile(index);
for k = 1:length(ops.fsroot{j})
ops.fsred(k).name = fullfile(ops.RootDir, num2str(db.expred), ops.fsred(k).name);
end
end
else
% here we might have multiple sessions, which we want to be analyzed
% together (exactly the same FOV)
nSessions = length(db.mouse_name);
% a backwards compatible version of db
dbCompat = db;
dbCompat.mouse_name = db.mouse_name{1};
dbCompat.date = db.date{1};
dbCompat.expts = cell2mat(db.expts(:)');
ops = addfields(ops, dbCompat);
ops.db_orig = db;
ops.fsroot = cell(0);
ops.SubDirs = cell(0);
for iSession = 1:nSessions
ops.RootDir = fullfile(ops.RootStorage, db.mouse_name{iSession}, db.date{iSession});
for iExp = 1:length(db.expts{iSession})
ops.SubDirs{end+1} = num2str(db.expts{iSession}(iExp));
ops.fsroot{end+1} = dir(fullfile(ops.RootDir, ops.SubDirs{end}, '*.tif'));
for iFile = 1:length(ops.fsroot{end})
ops.fsroot{end}(iFile).name = fullfile(ops.RootDir, ops.SubDirs{end}, ops.fsroot{end}(iFile).name);
end
end
end
% this line to be backward compatible (just in case)
ops.RootDir = fullfile(ops.RootStorage, ops.mouse_name, ops.date);
end
if ops.readTiffHeader
try
% MK code for automatically determining number of planes and channels
[~, header] = loadFramesBuff(ops.fsroot{1}(1).name, 1, 1, 1);
hh=header{1};
verStr = ['SI.VERSION_MAJOR = ',char(39),'2016b',char(39)];
if contains(hh, verStr) % For scanimage 2016b, SF
str = hh(strfind(hh,'channelSave = '):end);
ind = strfind(str, 'SI');
ch = str2num(str(15 : ind(1)-1));
ops.nchannels = length(ch);
fastZEnable = sscanf(hh(strfind(hh,'hFastZ.enable = '):end), 'hFastZ.enable = %s');
fastZEnable = strcmp(fastZEnable,'true');
fastZDiscardFlybackFrames = sscanf(hh(strfind(hh, 'hFastZ.discardFlybackFrames = '):end), 'hFastZ.discardFlybackFrames = %s');
fastZDiscardFlybackFrames = strcmp(fastZDiscardFlybackFrames,'true');
stackNumSlices = sscanf(hh(strfind(hh, 'hStackManager.numSlices = '):end), 'hStackManager.numSlices = %d');
ops.nplanes = 1;
if fastZEnable
ops.nplanes = stackNumSlices+fastZDiscardFlybackFrames;
end
str = hh(strfind(hh, 'scanZoomFactor = '):end);
ind = strfind(str, 'SI');
ops.zoomMicro = str2double(str(18 : ind(1)-1));
ops.imageRate = sscanf(hh(strfind(hh, 'scanFrameRate = '):end), 'scanFrameRate = %f');
if isfield(db, 'expred') && ~isempty(db.expred) && ...
(~isfield(db, 'nchannels_red') || isempty(db.nchannels_red))
[~, header] = loadFramesBuff(ops.fsred(1).name, 1, 1, 1);
hh=header{1};
str = hh(strfind(hh, 'channelSave = '):end);
ind = strfind(str, 'SI');
ch = str2num(str(15 : ind(1)-1));
ops.nchannels_red = length(ch);
end
end
% Old scanimage
str = hh(strfind(hh, 'channelsSave = '):end);
ind = strfind(str, 'scanimage');
ch = str2num(str(16 : ind(1)-1));
ops.nchannels = length(ch);
fastZEnable = sscanf(hh(strfind(hh, 'fastZEnable = '):end), 'fastZEnable = %d');
fastZDiscardFlybackFrames = sscanf(hh(strfind(hh, 'fastZDiscardFlybackFrames = '):end), 'fastZDiscardFlybackFrames = %d');
if isempty(fastZDiscardFlybackFrames)
fastZDiscardFlybackFrames = 0;
end
stackNumSlices = sscanf(hh(strfind(hh, 'stackNumSlices = '):end), 'stackNumSlices = %d');
ops.nplanes = 1;
if fastZEnable
ops.nplanes = stackNumSlices+fastZDiscardFlybackFrames;
end
str = hh(strfind(hh, 'scanZoomFactor = '):end);
ind = strfind(str, 'scanimage');
ops.zoomMicro = str2double(str(18 : ind(1)-1));
ops.imageRate = sscanf(hh(strfind(hh, 'scanFrameRate = '):end), 'scanFrameRate = %f');
% get number of channels of red experiment
if isfield(db, 'expred') && ~isempty(db.expred) && ...
(~isfield(db, 'nchannels_red') || isempty(db.nchannels_red))
[~, header] = loadFramesBuff(ops.fsred(1).name, 1, 1, 1);
hh=header{1};
str = hh(strfind(hh, 'channelsSave = '):end);
ind = strfind(str, 'scanimage');
ch = str2num(str(16 : ind(1)-1));
ops.nchannels_red = length(ch);
end
catch
end
end
if isfield(ops, 'zoomMicro')
ops.zoom = getOr(ops, 'zoom', ops.zoomMicro);
else
ops.zoom = getOr(ops, 'zoom', 1);
end
if ~(isfield(ops, 'planesToProcess') && ~isempty(ops.planesToProcess))
ops.planesToProcess = 1:ops.nplanes;
else
% planesToProcess is not working right now
ops.planesToProcess = 1:ops.nplanes;
end
CharSubDirs = '';
for i = 1:length(ops.SubDirs)
CharSubDirs = [CharSubDirs ops.SubDirs{i} '_'];
end
CharSubDirs = CharSubDirs(1:end-1);
ops.CharSubDirs = CharSubDirs;
ops.ResultsSavePath = sprintf('%s//%s//%s//%s//', ops.ResultsSavePath, ops.mouse_name, ops.date, ...
CharSubDirs);
if isempty(db.expts)
ops.expts = [];
end