-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbatchFetchDomainProps.m
477 lines (374 loc) · 16.4 KB
/
batchFetchDomainProps.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
function batchFetchDomainProps(filelist,region, datafilename, useStimuli, stimuliIndices)
%batchFetchDomainProps - A wrapper and output generator for getting information on active pixel fraction per location during the movie, after 'locationData' data structure has been returned and saved into 'region' from wholeBrain_activeFraction.m
%Examples:
% >> batchFetchDomainProps(filelist);
% >> batchFetchDomainProps({filename},region);
% >> batchFetchDomainProps({filename},region,'dDomainProps.txt');
%
%**USE**
%Must provide one input:
%
%(1) table with desired filenames (space delimited txt file, with full filenames in first column)
%files.txt should have matlab filenames in first column.
%can have an extra columns with descriptor/factor information for the file. This will be the rowinfo that is attached to each measure observation in the following script.
%filelist = readtext('files.txt',' '); %grab readtext.m file script from matlab central
%or
%(2) a single filename (filename of your region .mat file) as a cell array, i.e. {filename}
%
%Options:
%filelist={filename}; % cell array of strings, can pass just a single filename and a single already loaded region structure, if only getting values for a single file.
%region - datastructure, if you just want to do a single file loaded into workspace
%datafilename - string, append data to prexisting table with filename 'datafilename'
%useStimuli - string, 'true' | 'false'
%stimuliIndices - integer vector of stimulus indices or a cell array of strings of stimulus descriptions for selecting stimuli in your region.stimuli data structure
%
%Output:
%Right now this function will automatically write to a space-delimited txt file outputs, a 'region.location/active period type' based dataset 'dLocationProps.txt'
%And these outputs will be appended if the file already exists.
%
% See also wholeBrain_getActiveFractionPeriods.m, wholeBrain_activeFraction.m, batchFetchStimResponseProps, batchFetchCalciumEventProps.m
%
%James B. Ackman, 2013-04-11 12:17:14, updated 2013-11-13 10:08:46
%-----------------------------------------------------------------------------------------
%- Set up options and default parameters
%-----------------------------------------------------------------------------------------
if nargin< 5 || isempty(stimuliIndices); stimuliIndices = []; end
if nargin< 4 || isempty(useStimuli); useStimuli = 'false'; end
if nargin< 3 || isempty(datafilename),
datafilename = 'dDomainProps.txt';
matlabUserPath = userpath;
matlabUserPath = matlabUserPath(1:end-1);
datafilename = fullfile(matlabUserPath,datafilename);
else
[pathstr, name, ext] = fileparts(datafilename); %test whether a fullfile path was specified
if isempty(pathstr) %if one was not specified, save the output datafilename into the users matlab home startup directory
matlabUserPath = userpath;
matlabUserPath = matlabUserPath(1:end-1);
datafilename = fullfile(matlabUserPath,datafilename);
end
end
if nargin< 2 || isempty(region); region = []; end
%---**functionHandles.workers and functionHandles.main must be valid functions in this program or in matlabpath to provide an array of function_handles
functionHandles.workers = {@filename @matlab_filename @domainInd @region_name1 @region_name2 @volume_px @xwidth_px @ywidth_px @xcentr_px @ycentr_px @zcentr_px @MeanIntensity @MaxIntensity @onset_fr @offset_fr @Duration_s @theta_deg @rho_px};
functionHandles.main = @wholeBrain_getDomainStats;
%tableHeaders = {'filename' 'matlab.filename' 'region.name' 'roi.number' 'nrois' 'roi.height.px' 'roi.width.px' 'xloca.px' 'yloca.px' 'xloca.norm' 'yloca.norm' 'freq.hz' 'intvls.s' 'onsets.s' 'durs.s' 'ampl.df'};
%filename %roi no. %region.name %roi size %normalized xloca %normalized yloca %region.stimuli{numStim}.description %normalized responseFreq %absolutefiringFreq(dFreq) %meanLatency %meanAmpl %meanDur
tableHeaders = cellfun(@func2str, functionHandles.workers, 'UniformOutput', false);
%---Generic opening function---------------------
setupHeaders = exist(datafilename,'file');
if setupHeaders < 1
%write headers to file----
fid = fopen(datafilename,'a');
appendCellArray2file(datafilename,tableHeaders,fid)
else
fid = fopen(datafilename,'a');
end
%---Generic main function loop-------------------
%Provide valid function handle
mainfcnLoop(filelist, region, datafilename, functionHandles, [], fid, useStimuli, stimuliIndices)
fclose(fid);
function mainfcnLoop(filelist, region, datafilename, functionHandles, datasetSelector, fid, useStimuli, stimuliIndices)
%start loop through files-----------------------------------------------------------------
if nargin < 5 || isempty(datasetSelector), datasetSelector=[]; end
if nargin < 7 || isempty(useStimuli), useStimuli=[]; end
if nargin < 8 || isempty(stimuliIndices), stimuliIndices=[]; end
if nargin< 2 || isempty(region);
region = []; loadfile = 1;
else
loadfile = 0;
end
fnms = filelist(:,1);
if size(filelist,1) > 1 && size(filelist,2) > 1
fnms2 = filelist(:,2);
end
for j=1:numel(fnms)
if loadfile > 0
matfile=load(fnms{j});
region=matfile.region;
end
if ~isfield(region,'filename')
if size(filelist,2) > 1 && ~isfield(region,'filename')
[pathstr, name, ext] = fileparts(fnms2{j});
region.filename = [name ext]; %2012-02-07 jba
else
region.filename = ['.tif'];
end
end
[pathstr, name, ext] = fileparts(fnms{j});
region.matfilename = [name ext]; %2012-02-07 jba
% rowinfo = [name1 name2]; %cat cell array of strings
% rowinfo = filelist(j,:);
sprintf(fnms{j})
disp('--------------------------------------------------------------------')
%myEventProps(region,rowinfo);
functionHandles.main(region, functionHandles.workers, datafilename, datasetSelector, fid, useStimuli, stimuliIndices)
if ismac | ispc
h = waitbar(j/numel(fnms));
else
disp([num2str(j) '/' num2str(numel(fnms))])
end
end
%data=results;
if ismac | ispc
close(h)
end
%-----------------------------------------------------------------------------------------
%dataFunctionHandle
function output = wholeBrain_getDomainStats(region, functionHandles, datafilename, datasetSelector, fid, useStimuli, stimuliIndices)
%script to fetch the active and non-active pixel fraction period durations
%for all data and all locations
%2013-04-09 11:35:04 James B. Ackman
%Want this script to be flexible to fetch data for any number of location Markers as well as duration distributions for both non-active and active periods.
%Should get an extra location signal too-- for combined locations/hemisphere periods.
%2013-04-11 18:00:23 Added under the batchFetchLocation generalized wrapper table functions
varin.datafilename=datafilename;
varin.region=region;
if strcmp(useStimuli,'true') & isempty(stimuliIndices) & isfield(region,'stimuli');
stimuliIndices=1:numel(region.stimuli);
elseif strcmp(useStimuli,'true') & iscellstr(stimuliIndices) & isfield(region,'stimuli') %if the input is a cellarray of strings
ind = [];
for i = 1:length(region.stimuli)
for k = 1:length(stimuliIndices)
if strcmp(region.stimuli{i}.description,stimuliIndices{k})
ind = [ind i];
end
end
end
stimuliIndices = ind; %assign indices
elseif strcmp(useStimuli,'true') & isnumeric(stimuliIndices) & isfield(region,'stimuli')
return
elseif ~isfield(region,'stimuli') || strcmp(useStimuli,'false')
stimuliIndices = [];
else
error('Bad input to useStimuli, stimuliIndices, or region.stimuli missing')
end
if isfield(region,'domainData')
data = region.domainData;
if isfield(data.STATS,'descriptor')
% roiBoundingBox = [];
ObjectIndices = [];
% j = 0;
for i = 1:data.CC.NumObjects
if ~strcmp(data.STATS(i).descriptor, 'artifact')
% j = j + 1;
% roiBoundingBox(j,:) = STATS(i).BoundingBox;
ObjectIndices = [ObjectIndices i];
end
end
else
ObjectIndices = 1:data.CC.NumObjects;
end
for idx = ObjectIndices
varin.idx = idx;
printStats(functionHandles, varin, fid)
end
else
error('region.domainData not found')
end
function out = filename(varin)
%movie .tif filename descriptor string
out = varin.region.filename;
function out = matlab_filename(varin)
%analysed .mat file descriptor string
out = varin.region.matfilename;
function out = domainInd(varin)
%location name descriptor string
out = varin.idx;
function out = region_name1(varin)
%location name descriptor string
data = varin.region.domainData;
locationMarkers = {varin.region.locationData.data.name};
name = [];
se = strel('disk',1);
for locationIndex = 1:length(locationMarkers)
locationName = varin.region.locationData.data(locationIndex).name;
coords = varin.region.coords{strcmp(varin.region.name,locationName)};
centrRowCol = [round(data.STATS(varin.idx).Centroid(2)) round(data.STATS(varin.idx).Centroid(1))];
sz = data.CC.ImageSize(1:2);
ROImask = poly2mask(coords(:,1),coords(:,2),sz(1),sz(2));
ROImask = imdilate(ROImask,se);
if ROImask(centrRowCol(1),centrRowCol(2)) > 0
name = locationName;
break
end
end
out = name;
function out = region_name2(varin)
%location name descriptor string
data = varin.region.domainData;
locationMarkers = {varin.region.locationData.data.name};
name = [];
se = strel('disk',1);
for locationIndex = 1:length(locationMarkers)
locationName = varin.region.locationData.data(locationIndex).name;
coords = varin.region.coords{strcmp(varin.region.name,locationName)};
centrRowCol = [round(data.STATS(varin.idx).Centroid(2)) round(data.STATS(varin.idx).Centroid(1))];
sz = data.CC.ImageSize(1:2);
ROImask = poly2mask(coords(:,1),coords(:,2),sz(1),sz(2));
ROImask = imdilate(ROImask,se);
if ROImask(centrRowCol(1),centrRowCol(2)) > 0
name = locationName;
end
end
out = name;
function out = volume_px(varin)
%location name descriptor string
out = varin.region.domainData.STATS(varin.idx).Area;
function out = xwidth_px(varin)
%location name descriptor string
out = varin.region.domainData.STATS(varin.idx).BoundingBox(:,4);
function out = ywidth_px(varin)
%location name descriptor string
out = varin.region.domainData.STATS(varin.idx).BoundingBox(:,5);
function out = xcentr_px(varin)
%location name descriptor string
out = varin.region.domainData.STATS(varin.idx).Centroid(:,1);
function out = ycentr_px(varin)
%location name descriptor string
out = varin.region.domainData.STATS(varin.idx).Centroid(:,2);
function out = zcentr_px(varin)
%location name descriptor string
out = varin.region.domainData.STATS(varin.idx).Centroid(:,3);
function out = MeanIntensity(varin)
%location name descriptor string
% out = varin.region.domainData.STATS(varin.idx).MeanIntensity - abs(varin.region.Amin); %To correct for the min value originally used to Scale deltaF/F array so everything was positive valued in wholeBrain_segmentation.m
out = varin.region.domainData.STATS(varin.idx).MeanIntensity;
function out = MaxIntensity(varin)
%location name descriptor string
% out = varin.region.domainData.STATS(varin.idx).MaxIntensity - abs(varin.region.Amin); %To correct for the min value originally used to Scale deltaF/F array so everything was positive valued in wholeBrain_segmentation.m
out = varin.region.domainData.STATS(varin.idx).MaxIntensity;
function out = onset_fr(varin)
%location name descriptor string
out = ceil(varin.region.domainData.STATS(varin.idx).BoundingBox(:,3));
function out = offset_fr(varin)
%location name descriptor string
out = ceil(varin.region.domainData.STATS(varin.idx).BoundingBox(:,3)) + (ceil(varin.region.domainData.STATS(varin.idx).BoundingBox(:,6))-1);
function out = Duration_s(varin)
%location name descriptor string
out = round(varin.region.domainData.STATS(varin.idx).BoundingBox(:,6)) * varin.region.timeres;
function out = theta_deg(varin)
%location name descriptor string
if isfield(varin.region.domainData, 'Vsum')
out = varin.region.domainData.Vsum(varin.idx).theta .* 180/pi;
else
out = NaN;
end
function out = rho_px(varin)
%location name descriptor string
if isfield(varin.region.domainData, 'Vsum')
out = varin.region.domainData.Vsum(varin.idx).rho;
else
out = NaN;
end
%========domainFreq_hz====================================================================
function out = nDomains(varin)
if isfield(varin.region,'domainData')
data = varin.region.domainData;
if isfield(data.STATS,'descriptor')
roiBoundingBox = [];
ObjectIndices = [];
j = 0;
for i = 1:data.CC.NumObjects
if ~strcmp(data.STATS(i).descriptor, 'artifact')
j = j + 1;
% roiBoundingBox(j,:) = STATS(i).BoundingBox;
ObjectIndices = [ObjectIndices i];
end
end
else
ObjectIndices = 1:data.CC.NumObjects;
end
coords = varin.region.coords{strcmp(varin.region.name,varin.locationName)};
count = 0;
for i = ObjectIndices
centr = data.STATS(i).Centroid;
inp = inpolygon(centr(1),centr(2),coords(:,1),coords(:,2));
if inp, count = count + 1; end
end
out = count;
else
out = NaN;
end
function out = domainFreq_hz(varin)
out = nDomains(varin);
if ~isnan(out)
data = varin.region.domainData;
out = out / (data.CC.ImageSize(3) * varin.region.timeres);
else
out = NaN;
end
%=========================================================================================
%-------------unused functions-----------------------------------
function out = actvPeriodType(varin)
%active period type descriptor string
out = varin.periodType;
function out = actvFraction(varin)
%fraction of all pixels active by location for one recording
data = varin.region.locationData.data;
out = data(varin.locationIndex).activeFraction;
function out = stimulusDesc(varin)
data = varin.region.locationData.data;
out = varin.stimulusdesc;
function out = nstimuli(varin)
data = varin.region.locationData.data;
out = varin.nstimuli;
function out = stimOn(varin)
data = varin.region.locationData.data;
out = varin.on;
function out = stimOff(varin)
data = varin.region.locationData.data;
out = varin.off;
function out = nMaskPixels(varin)
data = varin.region.locationData.data;
out = data(varin.locationIndex).nPixels;
function out = nPixelsActiveTotal(varin)
data = varin.region.locationData.data;
out = sum(data(varin.locationIndex).nPixelsByFrame);
function out = nPixelsActive(varin)
data = varin.region.locationData.data;
out = sum(data(varin.locationIndex).nPixelsByFrame(varin.on:varin.off));
function out = nPixelsActivePerSec(varin)
data = varin.region.locationData.data;
out = sum(data(varin.locationIndex).nPixelsByFrame(varin.on:varin.off)) / (numel(varin.on:varin.off)*varin.region.timeres);
function out = maxFraction(varin)
%maximum fraction of all pixels active at one time (default is by frame, TODO: change/add in future by binned time?)
data = varin.region.locationData.data;
out = max(data(varin.locationIndex).activeFractionByFrame);
function out = minFraction(varin)
%minimum fraction of all pixels active at one time
data = varin.region.locationData.data;
out = min(data(varin.locationIndex).activeFractionByFrame);
function out = meanFraction(varin)
%mean active pixel fraction across *all* frames
data = varin.region.locationData.data;
out = mean(data(varin.locationIndex).activeFractionByFrame);
function out = sdFraction(varin)
%standard deviation active pixel fraction across *all* frames
data = varin.region.locationData.data;
out = std(data(varin.locationIndex).activeFractionByFrame);
function out = meanActvFraction(varin)
%mean pixel fraction across all the *active* frames
data = varin.region.locationData.data;
actvFramesIdx = find(data(varin.locationIndex).activeFractionByFrame);
out = mean(data(varin.locationIndex).activeFractionByFrame(actvFramesIdx));
function out = sdActvFraction(varin)
%standard deviation pixel fraction across all the *active* frames
data = varin.region.locationData.data;
actvFramesIdx = find(data(varin.locationIndex).activeFractionByFrame);
out = std(data(varin.locationIndex).activeFractionByFrame(actvFramesIdx));
function out = actvFrames(varin)
%number of active frames
data = varin.region.locationData.data;
out = numel(find(data(varin.locationIndex).activeFractionByFrame));
function out = actvTimeFraction(varin)
%fraction of total movie time the location is active
data = varin.region.locationData.data;
out = actvFrames(varin)/length(data(varin.locationIndex).activeFractionByFrame);
function out = nonActvFrames(varin)
%number of non-active frames
data = varin.region.locationData.data;
out = length(data(varin.locationIndex).activeFractionByFrame) - actvFrames(varin);
function out = nonActvTimeFraction(varin)
%fraction of total movie time the location is non-active
data = varin.region.locationData.data;
out = nonActvFrames(varin)/length(data(varin.locationIndex).activeFractionByFrame);