-
Notifications
You must be signed in to change notification settings - Fork 2
/
LoadSpikeData_byElectrode.m
250 lines (232 loc) · 12.1 KB
/
LoadSpikeData_byElectrode.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
function Spikes=LoadSpikeData_byElectrode(argFname,traces,electrodes,samplingRate,bitResolution)
if logical(regexp(argFname,'Ch\d+.')) %from Spike2
load(argFname)
Spikes.Units{electrodes,1}=nw_401.codes(:,1);
Spikes.SpikeTimes{electrodes,1}=uint32(nw_401.times*samplingRate);
Spikes.Waveforms{electrodes,1}=nw_401.values;
Spikes.samplingRate(electrodes,1)=samplingRate;
elseif contains(argFname,'.hdf5') % Spyking Circus
fName=regexp(argFname,'\S+?(?=\.\w+\.\w+$)','match','once');
postFix='';
if isempty(fName)
fName=regexp(argFname,'\S+?(?=\.\w+\-\w+\.\w+$)','match','once'); %in case loading merged files
if ~isempty(fName)
postFix='-merged';
end
end
templateToEl=h5read([fName '.clusters' postFix '.hdf5'],'/electrodes'); % this are the *preferred* electrodes for all K templates
for elNum=1:electrodes
try
%Clusters data (including non-clustered spikes)
% Spikes.Units{elNum,1}=h5read([fName '.clusters.hdf5'],['/clusters_' num2str(elNum-1)]);
% Spikes.SpikeTimes{elNum,1}=h5read([fName '.clusters.hdf5'],['/times_' num2str(elNum-1)]);
%Results, after fitting templates
thisElTemplates=find(templateToEl==elNum-1)-1;
[spktimes,units]=deal(cell(size(thisElTemplates,1)+1,1));
for templt=1:size(thisElTemplates,1)
spktimes{templt}=h5read([fName '.result' postFix '.hdf5'],['/spiketimes/temp_' num2str(thisElTemplates(templt))]);
units{templt}=ones(size(spktimes{templt},1),1)*templt;
end
% collect non-fitted ("garbage") spikes, with unit ID 0
try
spktimes{templt+1}=h5read([fName '.result' postFix '.hdf5'],['/gspikes/elec_' num2str(elNum-1)]);
units{templt+1}=zeros(size(spktimes{templt+1},1),1);
catch
% no "garbage" spikes
end
% concatenate values
Spikes.Units{elNum,1}=vertcat(units{:});
Spikes.SpikeTimes{elNum,1}=vertcat(spktimes{:});
% sort times, and adjust unit orders
[Spikes.SpikeTimes{elNum,1},timeIdx]=sort(Spikes.SpikeTimes{elNum,1});
Spikes.Units{elNum,1}=Spikes.Units{elNum,1}(timeIdx);
% extract spike waveforms
if isa(traces,'memmapfile') % reading electrode data from .dat file
Spikes.Waveforms{elNum,1}=ExtractChunks(traces.Data(elNum:electrodes:max(size(traces.Data))),...
Spikes.SpikeTimes{elNum,1},50,'tshifted'); %'tzero' 'tmiddle' 'tshifted'
else
Spikes.Waveforms{elNum,1}=ExtractChunks(traces(elNum,:),...
Spikes.SpikeTimes{elNum,1},50,'tshifted'); %'tzero' 'tmiddle' 'tshifted'
end
% scale to resolution
Spikes.Waveforms{elNum,1}=Spikes.Waveforms{elNum,1}.*bitResolution;
Spikes.samplingRate(elNum,1)=samplingRate;
% plots
% foo=traces.Data(elNum:electrodes:max(size(traces.Data)));
% figure; hold on
% plot(foo(round(size(foo,1)/2)-samplingRate:round(size(foo,1)/2)+samplingRate));
% axis('tight');box off;
% text(100,100,num2str(round(size(foo,1)/2)))
% text(100,50,'PrV 77 115 El 11');
% allunits= Spikes.Units{elNum,1};
% allspktimes=Spikes.SpikeTimes{elNum,1};
% spkTimes=allspktimes(allspktimes>=round(size(foo,1)/2)-samplingRate &...
% allspktimes<round(size(foo,1)/2)+samplingRate & allunits==1);
% rasterHeight=ones(1,size(spkTimes,2))*(min(get(gca,'ylim'))/4*3);
% plot(spkTimes-(round(size(foo,1)/2)-samplingRate),...
% rasterHeight,'Color','r',...
% 'linestyle','none','Marker','^');
catch
end
end
elseif contains(argFname,'rez.mat') || contains(argFname,'_KS') %Kilosort
load(argFname);
spikeTimes = uint64(rez.st3(:,1));
spikeTemplates = uint32(rez.st3(:,2));
templates=abs(rez.Wraw);
templateToEl=zeros(max(unique(spikeTemplates)),1);
for templNum=1:max(unique(spikeTemplates))
thatTemplate=squeeze(templates(:,:,templNum));
[elecRow,~] = ind2sub(size(thatTemplate),find(thatTemplate==max(max(thatTemplate))));
if size(elecRow,1)>1
if length(unique(elecRow))>1 %weird
% then look for next biggest value?
return
else
elecRow=unique(elecRow);
end
end
templateToEl(templNum)=elecRow;
end
for elNum=1:electrodes
try
%Results, after fitting templates
thisElTemplates=find(templateToEl==elNum);
units=false(size(spikeTemplates,1),1);
for templt=1:size(thisElTemplates,1)
units=units | spikeTemplates==thisElTemplates(templt);
end
Spikes.Units{elNum,1}=spikeTemplates(units);
Spikes.SpikeTimes{elNum,1}=spikeTimes(units);
% extract spike waveforms traces = memmapfile('example.dat','Format','int16');
if isa(traces,'memmapfile') % reading electrode data from .dat file
Spikes.Waveforms{elNum,1}=ExtractChunks(traces.Data(elNum:electrodes:max(size(traces.Data))),...
Spikes.SpikeTimes{elNum,1},50,'tshifted'); %'tzero' 'tmiddle' 'tshifted'
else
Spikes.Waveforms{elNum,1}=ExtractChunks(traces(elNum,:),...
Spikes.SpikeTimes{elNum,1},50,'tshifted'); %'tzero' 'tmiddle' 'tshifted'
end
% scale to resolution
Spikes.Waveforms{elNum,1}=Spikes.Waveforms{elNum,1}.*bitResolution;
Spikes.samplingRate(elNum,1)=samplingRate;
catch
end
end
elseif contains(argFname,'.csv') || contains(argFname,'_jrc.mat') %from JRClust
%% locate the _jrc file
dirListing=dir;
S0struct=dirListing(~cellfun('isempty',cellfun(@(x) strfind(x,'_jrc.mat'),...
{dirListing.name},'UniformOutput',false))).name;
% dimm_spk Dimensions for spike waveforms (stored in_spkwav.bin file)
% viTime_spk Spike timing in ADC sample unit
% cviSpk_site Cell of spike index (for _spk prefix) per site
% miClu_log
% P Parameter struct used for automated clustering
% S_clu Cluster-specific information
load(S0struct, 'dimm_spk','viTime_spk','cviSpk_site','miClu_log','P','S_clu')
%% import info from cvs file export
% clusterInfo = ImportJRClusSortInfo(fName);
%% if we want to attribute each cluster to a specific electrode:
% allClusters=unique(clusterInfo.clusterNum);
% for clusNum=1:length(allClusters)
% bestSite=mode(clusterInfo.bestSite(clusterInfo.clusterNum==allClusters(clusNum)));
% clusterInfo.bestSite(clusterInfo.clusterNum==allClusters(clusNum))=bestSite;
% end
% Spikes.Units=clusterInfo.clusterNum;
% Spikes.SpikeTimes=clusterInfo.bestSite;
%% get filtered waveforms
vcFile=dirListing(~cellfun('isempty',cellfun(@(x) strfind(x,'wav'),...
{dirListing.name},'UniformOutput',false))).name;
vcDataType = 'int16';
fid=fopen(vcFile, 'r');
% mnWav = fread_workingresize(fid, dimm, vcDataType);
mnWav = fread(fid, prod(dimm_spk), ['*', vcDataType]);
if numel(mnWav) == prod(dimm_spk)
mnWav = reshape(mnWav, dimm_spk);
else
dimm2 = floor(numel(mnWav) / dimm_spk(1));
if dimm2 >= 1
mnWav = reshape(mnWav, dimm_spk(1), dimm2);
else
mnWav = [];
end
end
if ~isempty(vcFile), fclose(fid); end
%% degenerate. keeping largest waveforms
% keepSite=squeeze(prod(abs(mnWav)));[keepSite,~]=find(keepSite==max(keepSite));
% waveForms=nan(size(mnWav,1),size(mnWav,3));
% for spktTimeIdx=1:size(mnWav,3)
% waveForms(:,spktTimeIdx)=squeeze(mnWav(:,keepSite(spktTimeIdx),spktTimeIdx));
% end
for elNum=1:electrodes
try
units=cviSpk_site{elNum}; % if data from csv file: clusterInfo.bestSite==elNum;
units=units(miClu_log(units,1)>=0);
Spikes.Units{elNum,1}=miClu_log(units,1); % clusterInfo.clusterNum(units);
Spikes.SpikeTimes{elNum,1}=viTime_spk(units) ; % clusterInfo.timeStamps(units)*samplingRate;
Spikes.Waveforms{elNum,1}=squeeze(mnWav(:,1,units));
%% proof that the first trace in mnWav's 2nd dimension is always from the center site:
% miSites_clu = P.miSites(:, S_clu.viSite_clu); % which sites correspond to mnWav's 2nd dimension
% rndTimeStamp=922;
% figure; hold on;
% for wfNum=1:9
% plot(mnWav(:,wfNum,rndTimeStamp));
% end
% plot(mnWav(:,miSites_clu(:,miClu_log(rndTimeStamp,1))==S_clu.viSite_clu(miClu_log(rndTimeStamp,1)),rndTimeStamp),'ko')
%% some more exploration
% mode(clusterInfo.clusterNum(units))
% foo=mnWav(:,:,units);
% figure; plot(mean(squeeze(foo(:,1,:)),2))
%
% foo=mnWav(:,:,clusterInfo.clusterNum==1);
% subsampleIdx=round(linspace(1,24000,20));
% figure; hold on;
% for timestamps=1:20
% plot(foo(:,1,subsampleIdx(timestamps)));
% end
% plot(mean(squeeze(mnWav(:,1,:)),2),'k','linewidth',1.5);
%
% figure; hold on;
% for avwf=1:9
% plot(squeeze(mnWav(:,avwf,2)));
% end
% plot(squeeze(mnWav(:,1,2)),'ko');
%
% faa=Spikes.Waveforms{elNum,1};
% figure; hold on;
% for timestamps=1:20
% plot(faa(timestamps,:)');
% end
% plot(mean(squeeze(mnWav(:,1,:)),2),'k','linewidth',1.5);
%% alternative spike extraction
% extract spike waveforms traces = memmapfile('example.dat','Format','int16');
% if isa(traces,'memmapfile') % reading electrode data from .dat file
% Spikes.Waveforms{elNum,1}=ExtractChunks(traces.Data(elNum:electrodes:max(size(traces.Data))),...
% Spikes.SpikeTimes{elNum,1},50,'tshifted'); %'tzero' 'tmiddle' 'tshifted'
% else
% Spikes.Waveforms{elNum,1}=ExtractChunks(traces(elNum,:),...
% Spikes.SpikeTimes{elNum,1},50,'tshifted'); %'tzero' 'tmiddle' 'tshifted'
% end
%% scale to resolution
Spikes.Waveforms{elNum,1}=Spikes.Waveforms{elNum,1}.*bitResolution;
Spikes.samplingRate(elNum,1)=samplingRate;
catch
[Spikes.Units{elNum,1},Spikes.SpikeTimes{elNum,1}]=deal([]);
end
end
elseif contains(argFname,'.mat') % just Matlab processing
%Matlab export - all units unsorted by default
for elNum=1:numel(electrodes)
try
Spikes.Units{elNum,1}=zeros(1,numel(find(Spikes.data{electrodes(elNum)})));
Spikes.SpikeTimes{elNum,1}=find(Spikes.data{electrodes(elNum)});
Spikes.Waveforms{elNum,1}=ExtractChunks(traces(elNum,:),...
Spikes.SpikeTimes{elNum,1},40,'tshifted'); %'tzero' 'tmiddle' 'tshifted'
% 0.25 bit per uV, so divide by 4 - adjust according to
% recording system
Spikes.Waveforms{elNum,1}=Spikes.Waveforms{elNum,1}./4;
catch
end
end
end
end