-
Notifications
You must be signed in to change notification settings - Fork 91
/
NoRMCorreSetParms.m
235 lines (220 loc) · 11.1 KB
/
NoRMCorreSetParms.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
function options = NoRMCorreSetParms(varargin)
% Struct for setting the NoRMCorre algorithm parameters. Any parameter that is
% not set gets a default value
% Author: Eftychios A. Pnevmatikakis
% Simons Foundation, 2016
Names = [
% dataset info
'd1 ' % number of rows
'd2 ' % number of cols
'd3 ' % number of planes (for 3d imaging, default: 1)
% patches
'grid_size ' % size of non-overlapping regions (default: [d1,d2,d3])
'overlap_pre ' % size of overlapping region (default: [32,32,16])
'min_patch_size ' % minimum size of patch (default: [32,32,16])
'min_diff ' % minimum difference between patches (default: [16,16,5])
'us_fac ' % upsampling factor for subpixel registration (default: 20)
'mot_uf ' % degree of patches upsampling (default: [4,4,1])
'max_dev ' % maximum deviation of patch shift from rigid shift (default: [3,3,1])
'overlap_post ' % size of overlapping region after upsampling (default: [32,32,16])
'max_shift ' % maximum rigid shift in each direction (default: [15,15,5])
'phase_flag ' % flag for using phase correlation (default: false)
'shifts_method ' % method to apply shifts ('FFT','cubic','linear')
% template updating
'upd_template ' % flag for online template updating (default: true)
'init_batch ' % length of initial batch (default: 100)
'bin_width ' % width of each bin (default: 200)
'buffer_width ' % number of local means to keep in memory (default: 50)
'method ' % method for averaging the template (default: {'median';'mean})
'iter ' % number of data passes (default: 1)
'boundary ' % method of boundary treatment 'NaN','copy','zero','template' (default: 'copy')
% misc
'add_value ' % add dc value to data (default: 0)
'use_parallel ' % for each frame, update patches in parallel (default: false)
'memmap ' % flag for saving memory mapped motion corrected file (default: false)
'mem_filename ' % name for memory mapped file (default: 'motion_corrected.mat')
'mem_batch_size ' % batch size during memory mapping for speed (default: 5000)
'print_msg ' % flag for printing progress to command line (default: true)
% plotting
'plot_flag ' % flag for plotting results in real time (default: false)
'make_avi ' % flag for making movie (default: false)
'name ' % name for movie (default: 'motion_corrected.avi')
'fr ' % frame rate for movie (default: 30)
% output type
'output_type ' % 'mat' (load in memory), 'memmap', 'tiff', 'hdf5', 'bin' (default:mat)
'h5_groupname ' % name for hdf5 dataset (default: 'mov')
'h5_filename ' % name for hdf5 saved file (default: 'motion_corrected.h5')
'tiff_filename ' % name for saved tiff stack (default: 'motion_corrected.tif')
'output_filename ' % name for saved file will be used if `h5_,tiff_filename` are not specified
% use windowing
'use_windowing ' % flag for windowing data before fft (default: false)
'window_length ' % length of window on each side of the signal as a fraction of signal length
% total length = length(signal)(1 + 2*window_length). (default: 0.5)
% bitsize for reading .raw files
'bitsize ' % (default: 2 (uint16). other choices 1 (uint8), 4 (single), 8 (double))
% offset from bidirectional sampling
'correct_bidir ' % check for offset due to bidirectional scanning (default: true)
'nFrames ' % number of frames to average (default: 50)
'bidir_us ' % upsampling factor for bidirectional sampling (default: 10)
'col_shift ' % known bi-directional offset provided by the user (default: [])
];
[m,n] = size(Names);
names = lower(Names);
% Combine all leading options structures o1, o2, ... in l1Set(o1,o2,...).
options = [];
for j = 1:m
eval(['options.' Names(j,:) '= [];']);
end
i = 1;
while i <= nargin
arg = varargin{i};
if ischar(arg), break; end
if ~isempty(arg) % [] is a valid options argument
if ~isa(arg,'struct')
error(sprintf(['Expected argument %d to be a string parameter name ' ...
'or an options structure\ncreated with OPTIMSET.'], i));
end
for j = 1:m
if any(strcmp(fieldnames(arg),deblank(Names(j,:))))
eval(['val = arg.' Names(j,:) ';']);
else
val = [];
end
if ~isempty(val)
eval(['options.' Names(j,:) '= val;']);
end
end
end
i = i + 1;
end
% A finite state machine to parse name-value pairs.
if rem(nargin-i+1,2) ~= 0
error('Arguments must occur in name-value pairs.');
end
expectval = 0; % start expecting a name, not a value
while i <= nargin
arg = varargin{i};
if ~expectval
if ~ischar(arg)
error(sprintf('Expected argument %d to be a string parameter name.', i));
end
lowArg = lower(arg);
j = strmatch(lowArg,names);
if isempty(j) % if no matches
error(sprintf('Unrecognized parameter name ''%s''.', arg));
elseif length(j) > 1 % if more than one match
% Check for any exact matches (in case any names are subsets of others)
k = strmatch(lowArg,names,'exact');
if length(k) == 1
j = k;
else
msg = sprintf('Ambiguous parameter name ''%s'' ', arg);
msg = [msg '(' deblank(Names(j(1),:))];
for k = j(2:length(j))'
msg = [msg ', ' deblank(Names(k,:))];
end
msg = sprintf('%s).', msg);
error(msg);
end
end
expectval = 1; % we expect a value next
else
eval(['options.' Names(j,:) '= arg;']);
expectval = 0;
end
i = i + 1;
end
if expectval
error(sprintf('Expected value for parameter ''%s''.', arg));
end
Values = [
% dataset info
{[]}
{[]}
{1}
% patches
{[]} % size of non-overlapping regions (default: [d1,d2,d3])
{[32,32,16]} % size of overlapping region (default: [32,32,16])
{[32,32,16]} % minimum size of patch (default: [32,32,16])
{[16,16,5]} % minimum difference between patches (default: [16,16,5])
{50} % upsampling factor for subpixel registration (default: 50)
{[4,4,1]} % degree of patches upsampling (default: [4,4,1])
{[3,3,1]} % maximum deviation of patch shift from rigid shift (default: [3,3,1])
{[32,32,16]} % size of overlapping region after upsampling (default: [32,32,16])
{[15,15,5]} % maximum rigid shift in each direction
{false} % use phase correlation (good for high SNR)
{'FFT'} % method for applying shifts ('FFT', 'linear', 'cubic')
% template updating
{true} % flag for online template updating (default: true)
{100} % length of initial batch (default: 100)
{200} % width of each bin (default: 200)
{50} % number of local means to keep in memory (default: 50)
{{'median';'mean'}} % method for averaging the template (default: {'median';'mean'}
{1} % number of data passes (default: 1)
{'copy'} % method of boundary treatment (default: 'copy')
% misc
{0} % add dc value to data (default: 0)
{false} % for each frame, update patches in parallel (default: false)
{false} % flag for saving memory mapped motion corrected file (default: false)
{'motion_corrected.mat'} % name for memory mapped file (default: 'motion_corrected.mat')
{1000} % batch size used during memory mapping for faster mapping
{1} % flag for printing progress to command line (default: true)
% plotting
{false} % flag for plotting results in real time (default: false)
{false} % flag for making movie (default: false)
{'motion_corrected.avi'} % name for movie (default: 'motion_corrected.avi')
{30} % frame rate for movie (default: 30)
% output_type
{'mat'}
{'mov'}
{'motion_corrected.h5'}
{'motion_corrected.tif'}
{''}
% use_windowing
{false}
{0.5}
% bitsize for reading .raw files
{2}
% offset from bidirectional sampling
{true}
{50}
{10}
{[]}
];
for j = 1:m
if eval(['isempty(options.' Names(j,:) ')'])
eval(['options.' Names(j,:) '= Values{j};']);
end
end
if ~isempty(options.output_filename)
out_type = options.output_type;
[filepath,name,~] = fileparts(options.output_filename);
output_filename = fullfile(filepath,name);
if strcmpi(options.h5_filename,'motion_corrected.h5') && (strcmpi(out_type,'h5') || strcmpi(out_type,'hdf5'))
options.h5_filename = [output_filename,'.h5'];
end
if strcmpi(options.tiff_filename,'motion_corrected.tif') && (strcmpi(out_type,'tif') || strcmpi(out_type,'tiff'))
options.tiff_filename = [output_filename,'.tif'];
end
end
if isempty(options.d1); options.d1 = input('What is the total number of rows? \n'); end
if isempty(options.d2); options.d2 = input('What is the total number of columns? \n'); end
%if options.d3 == 1; nd = 2; else nd = 3; end
if isempty(options.grid_size); options.grid_size = [options.d1,options.d2,options.d3]; end
if length(options.grid_size) == 1; options.grid_size = options.grid_size*ones(1,3); end
if length(options.grid_size) == 2; options.grid_size(3) = 1; end
if length(options.min_patch_size) == 1; options.min_patch_size = options.min_patch_size*ones(1,3); end
if length(options.min_patch_size) == 2; options.min_patch_size(3) = 1; end
if length(options.min_diff) == 1; options.min_diff = options.min_diff*ones(1,3); end
if length(options.min_diff) == 2; options.min_diff(3) = 1; end
if length(options.overlap_pre) == 1; options.overlap_pre = options.overlap_pre*ones(1,3); end
if length(options.overlap_pre) == 2; options.overlap_pre(3) = 1; end
if length(options.overlap_post) == 1; options.overlap_post = options.overlap_post*ones(1,3); end
if length(options.overlap_post) == 2; options.overlap_post(3) = 1; end
if length(options.max_shift) == 1; options.max_shift = options.max_shift*ones(1,3); end
if length(options.max_shift) == 2; options.max_shift(3) = 1; end
if length(options.max_dev) == 1; options.max_dev = options.max_dev*ones(1,3); end
if length(options.max_dev) == 2; options.max_dev(3) = 1; end
if length(options.mot_uf) == 1; options.mot_uf = options.mot_uf*ones(1,3); end
if length(options.mot_uf) == 2; options.mot_uf(3) = 1; end
options.mot_uf(options.grid_size >= [options.d1,options.d2,options.d3]) = 1;