-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandaloneGUI_css.m
More file actions
218 lines (164 loc) · 8.44 KB
/
StandaloneGUI_css.m
File metadata and controls
218 lines (164 loc) · 8.44 KB
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
%
%main functions for unsupervised sorting. this function is called by
%either the UI or by a script.
%
%urut/may07
function StandaloneGUI_css(pathsOrig, filesToProcess, thres, normalizationChannels, paramsIn)
% which Nlx2MatCSC.m
starttime = clock;
timeSortingStats = []; %i time nrSpikesSorted
timeDetectionStats = [];%i time duration(in blocks of 512000 samples)
%set prefix of raw data files and result files
rawFilePrefix = copyFieldIfExists( paramsIn, 'rawFilePrefix', 'A' );
processedFilePrefix = copyFieldIfExists( paramsIn, 'processedFilePrefix', 'A' );
pathOutOrig=pathsOrig.pathOut;
for kkk=1:length(filesToProcess) % production
% for kkk=1:length(filesToProcess) % change to for for debugging
paths=pathsOrig;
i = filesToProcess(kkk);
stri = num2str(i);
currentThresInd=kkk;
handles=[];
handles.correctionFactorThreshold=0; %minimal threshold, >0 makes threshold bigger
handles.paramExtractionThreshold=thres(currentThresInd);
handles = copyStructFields( paramsIn, handles, {'minNrSpikes','blockNrRawFig','doGroundNormalization','rawFileVersion','detectionMethod','detectionParams','peakAlignMethod','displayFigures',...
'runningAverageLength','blocksize'});
%handles.blockNrRawFig = paramsIn.blockNrRawFig;
%handles.doGroundNormalization = paramsIn.doGroundNormalization;
%handles.rawFileVersion=paramsIn.rawFileVersion;
%handles.detectionMethod=paramsIn.detectionMethod;
%handles.detectionParams=paramsIn.detectionParams;
%handles.peakAlignMethod=paramsIn.peakAlignMethod;
%define file format dependent properties
[samplingFreq, limit, rawFilePostfix] = defineFileFormat(paramsIn.rawFileVersion, paramsIn.samplingFreq);
handles.samplingFreq = samplingFreq; %sampling freq of raw data
handles.limit = limit; %dynamic range
handles.pathRaw = paths.pathRaw;
%define include range
handles.includeFilename=[paths.timestampspath 'timestampsInclude.txt'];
includeRange=[];
if exist(handles.includeFilename,'file')==2
includeRange = dlmread(handles.includeFilename);
['include range is set from ' handles.includeFilename]
handles.includeRange=includeRange; % kaminskij information for ploting spike rate
else
warning(['include range is not set! file not found: ' handles.includeFilename]);
handles.includeRange=[]; % kaminskij information for ploting spike rate
end
%find the channels used for normalization of this electrode
if paramsIn.doGroundNormalization
if size(normalizationChannels,1)==1
handles.normalizationChannels = normalizationChannels;
else
electrodeInd = mapChannelToElectrode( i );
normalizeThisChannel = 0;
if ~isempty( electrodeInd )
% check if at least one normalization channel is given for this electrode
if ~isempty(find(normalizationChannels(2,:)==electrodeInd))
normalizeThisChannel=1;
end
end
if normalizeThisChannel
handles.normalizationChannels = setdiff( normalizationChannels(1, find( normalizationChannels(2,:) == electrodeInd ) ), paramsIn.groundChannels);
else
disp(['normalization channel not defined for this channel, dont normalize - ' stri]);
handles.normalizationChannels = [];
handles.doGroundNormalization=0;
end
end
else
handles.normalizationChannels = [];
end
paths.pathOut = [ pathOutOrig '/' num2str(handles.paramExtractionThreshold) '/'];
if exist(paths.pathOut,'dir')==0
['creating directory ' paths.pathOut]
mkdir(paths.pathOut);
end
handles.rawFilename=[paths.pathRaw rawFilePrefix stri rawFilePostfix];
handles.channID = filesToProcess(kkk);
handles.channRow = kkk;
handles.NWBfile = paramsIn.NWB2process;
if paramsIn.doDetection
% if exist(handles.NWBfile,'file')==0
% ['file does not exist, skip ' handles.NWBfile]
% continue;
% end
handles.rawFilePrefix = rawFilePrefix;
handles.rawFilePostfix = rawFilePostfix;
handles.nwbRawloc = paths.pathNWB;
starttimeDetection=clock;
handles = StandaloneInit_css( handles , paramsIn.tillBlocks, paramsIn.prewhiten, paramsIn.alignMethod(kkk), includeRange );
timeDetection = abs(etime(starttimeDetection,clock))
%timeDetectionStats(size(timeDetectionStats,1)+1,:) = [ i timeDetection handles.blocksProcessedForInit];
handles.filenamePrefix = [paths.pathOut processedFilePrefix stri];
storeSortResultFiles( [], handles, 2 , 2 );%2==no figures, 2=noGUI
end
starttimeSorting=clock;
if paramsIn.doSorting || paramsIn.doFigures || ~paramsIn.noProjectionTest
handles.basepath=paths.pathOut;
handles.prefix=processedFilePrefix;
handles.from=stri;
[handles,fileExists] = loadSortResultFiles([],handles, 2);
handles.filenamePrefix=[paths.pathOut handles.prefix stri];
if fileExists==0
['File does not exist: ' handles.filenamePrefix];
continue;
end
if paramsIn.doSorting
if size(handles.newSpikesNegative,1)>0
starttimeSorting=clock;
[handles] = sortMain( [], handles, 2, paramsIn.thresholdMethod ); %2=no GUI
timeSorting=abs(etime(starttimeSorting,clock))
%timeSortingStats(size(timeSortingStats,1)+1,:) = [i timeSorting length(handles.assignedClusterNegative)];
storeSortResultFiles( [], handles, 2, 2 );%2==no figures
else
'nothing to sort (0 spikes)'
end
end
handles.label=[ paths.patientID ' ' handles.prefix handles.from ' Th:' num2str(thres(currentThresInd))];
handles.label = strrep(handles.label,'_',' ');
disp(['producing figure for ' handles.label]);
%clusters and PCA figures
if paramsIn.doFigures
outputPathFigs=[paths.pathFigs num2str(thres(currentThresInd)) '/'];
produceFigures(handles, outputPathFigs, paramsIn.outputFormat, paramsIn.thresholdMethod);
produceOverviewFigure(handles, outputPathFigs, paramsIn.outputFormat, paramsIn.thresholdMethod)
end
%projection test
if ~paramsIn.noProjectionTest
produceProjectionFigures(handles,[paths.pathFigs num2str(thres(currentThresInd)) '/'], paramsIn.outputFormat, paramsIn.thresholdMethod);
end
end
% make raw plots at the end, so that sorting result is included in raw plots as colored spikes (cluster identity)
if paramsIn.doRawGraphs && exist(handles.rawFilename,'file')>0
handles.prefix = processedFilePrefix;
handles.rawFilePrefix = rawFilePrefix;
handles.rawFilePostfix = rawFilePostfix;
handles.from=stri;
handles.basepath=paths.pathOut;
[handles,fileExists] = loadSortResultFiles([],handles, 2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% JAT
produceRawTraceFig(handles, [paths.pathFigs num2str(thres(currentThresInd)) '/'], 'pdf');
end
% plot zoomed-in raw traces to examine waveforms
if isfield(paramsIn, 'doshortRawGraphs') && exist(handles.rawFilename,'file')>0
if paramsIn.doshortRawGraphs~=0
handles.prefix = processedFilePrefix;
handles.rawFilePrefix = rawFilePrefix;
handles.rawFilePostfix = rawFilePostfix;
handles.from=stri;
handles.basepath=paths.pathOut;
[handles,fileExists] = loadSortResultFiles([],handles, 2);
produceRawTraceFigSHORT(handles, [paths.pathFigs num2str(thres(currentThresInd)) '/'], paramsIn.outputFormat, paramsIn.doshortRawGraphs);
end
end
% plot cross correlations between units
if isfield(paramsIn, 'produceXcorrFigures')
if paramsIn.produceXcorrFigures~=0
XcorrlengthinMS=40;
binsize=1;
produceXcorrFigures(handles,[paths.pathFigs num2str(thres(currentThresInd)) '/'], paramsIn.outputFormat, paramsIn.thresholdMethod,XcorrlengthinMS,binsize);
end
end
end
etime(clock,starttime)