-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmegaSort.m
More file actions
128 lines (104 loc) · 4.12 KB
/
megaSort.m
File metadata and controls
128 lines (104 loc) · 4.12 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
function megaSort()
baseName = '~/Desktop/Data/140811/';
wildcard = 'RL140811_023_0[1,2,3,4]*.mat';
nClusters = 3;
negPeaks = true; % Negative going spikes
posPeaks = false;
peakLimit = 1.8; % SD (abs. value) 1.2 for ab1,
spikeWidth = .005; % Seconds
LPF = 1000; % Hz
HPF = 100; % Hz
plotOn = true;
fileList = jdir([baseName,wildcard]);
dataMatrix = [];
timeCodes = [];
for fileN = 1:length(fileList)
fileName = fileList(fileN).name
load([baseName,fileName]);
% Filter data, store
data.LPF.freq = LPF;
data.LPF.h = fdesign.lowpass('N,F3dB',4,data.LPF.freq/(data.sampleRate/2));
data.LPF.d = design(data.LPF.h,'butter');
data.HPF.freq = HPF;
data.HPF.h = fdesign.highpass('N,F3dB',4,data.HPF.freq/(data.sampleRate/2));
data.HPF.d = design(data.HPF.h,'butter');
lpV = filtfilt(data.LPF.d.sosMatrix,data.LPF.d.ScaleValues,data.V);
data.fV = filtfilt(data.HPF.d.sosMatrix,data.HPF.d.ScaleValues,lpV);
data.dVdT = [diff(data.fV);0];
data.spikeWidth = spikeWidth;
% Find peaks, only take biggest ones.
spikeHalfWidth = round(spikeWidth/2*data.sampleRate);
dataMean = mean(data.dVdT);
dataStd = std(data.dVdT);
peakListN = []; peakListP = []; peakHeightsN = []; peakHeightsP = [];
ixN = []; ixP = [];
if negPeaks
[peakListN,peakHeightsN] = peakFind(data.dVdT,[0,1]);
ixN = find((peakHeightsN - dataMean)./dataStd < -peakLimit);
end
if posPeaks
[peakListP,peakHeightsP] = peakFind(data.dVdT,[1,0]);
ixP = find((peakHeightsP - dataMean)./dataStd > peakLimit);
end
peakList = [peakListN(ixN);peakListP(ixP)];
peakHeights = [peakHeightsN(ixN);peakHeightsP(ixP)];
% Remove peaks too close to start or end
remIX = find((peakList <= 2*spikeHalfWidth) | (peakList >= (length(data.dVdT) - 2*spikeHalfWidth)));
peakList(remIX) = [];
peakHeights(remIX) = [];
% Assemble trace snippets into a matrix for dimensionality reduction
for peakN = 1:length(peakList)
saveSnip = data.dVdT((peakList(peakN) - spikeHalfWidth):(peakList(peakN) + spikeHalfWidth));
dataMatrix(end+1,:) = saveSnip';
timeCodes(end+1) = fileN*length(data.dVdT) + peakList(peakN);
end
% size(dataMatrix)
end
% timeCodes = timeCodes.*10^-7;
% dataMatrix = cat(2,dataMatrix,timeCodes');
% size(dataMatrix)
% Reduce dimensionality
PCAdim = 50;
perplexity = getPerplexity(size(dataMatrix,1));
theta = .3;
mappedX = fast_tsne(dataMatrix, PCAdim, perplexity, theta);
% [COEFFS, SCORES] = princomp(dataMatrix);
% mappedX = SCORES(:,1:2);
% Cluster
Z = linkage(mappedX,'ward','euclidean');
IDX = cluster(Z,'maxclust',nClusters);
% IDX = kmeans(mappedX,nClusters);
% Sort clusters by peak values
for clustN=1:nClusters
ix = find(IDX == clustN);
meanWaveform = mean(dataMatrix(ix,:),1);
maxWave(clustN) = abs(meanWaveform(spikeHalfWidth+1));
end
[B,IX] = sort(maxWave,'descend');
for clustN=1:nClusters
findClust = IX(clustN);
ix = find(IDX == findClust);
newIDX(ix) = clustN;
end
IDX = newIDX;
% % Save cluster info to the data structure
data.spikeSamples = peakList;
data.spikeClusters = IDX;
data.spikeEmbedding = mappedX;
%
% % Make well-separated spike avg. waveforms.
% data = makeSpikeAvg(data);
% Plot if required
if plotOn
figure;
subplot(2,2,1);
plotEmbeddings(data);
for clustN=1:nClusters
ix = find(IDX == clustN);
meanWaveform = mean(dataMatrix(ix,:),1);
subplot(2,2,2);
plot(dataMatrix(ix,:)','Color',pretty(clustN)); hold on;
subplot(2,2,3);
plot(meanWaveform,'Color',pretty(clustN)); hold on;
end
end