-
Notifications
You must be signed in to change notification settings - Fork 84
/
loadKSdir.m
96 lines (73 loc) · 2.55 KB
/
loadKSdir.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
function spikeStruct = loadKSdir(ksDir, varargin)
if ~isempty(varargin)
params = varargin{1};
else
params = [];
end
if ~isfield(params, 'excludeNoise')
params.excludeNoise = true;
end
if ~isfield(params, 'loadPCs')
params.loadPCs = false;
end
% load spike data
spikeStruct = loadParamsPy(fullfile(ksDir, 'params.py'));
ss = readNPY(fullfile(ksDir, 'spike_times.npy'));
st = double(ss)/spikeStruct.sample_rate;
spikeTemplates = readNPY(fullfile(ksDir, 'spike_templates.npy')); % note: zero-indexed
if exist(fullfile(ksDir, 'spike_clusters.npy'))
clu = readNPY(fullfile(ksDir, 'spike_clusters.npy'));
else
clu = spikeTemplates;
end
tempScalingAmps = readNPY(fullfile(ksDir, 'amplitudes.npy'));
if params.loadPCs
pcFeat = readNPY(fullfile(ksDir,'pc_features.npy')); % nSpikes x nFeatures x nLocalChannels
pcFeatInd = readNPY(fullfile(ksDir,'pc_feature_ind.npy')); % nTemplates x nLocalChannels
else
pcFeat = [];
pcFeatInd = [];
end
cgsFile = '';
if exist(fullfile(ksDir, 'cluster_groups.csv'))
cgsFile = fullfile(ksDir, 'cluster_groups.csv');
end
if exist(fullfile(ksDir, 'cluster_group.tsv'))
cgsFile = fullfile(ksDir, 'cluster_group.tsv');
end
if ~isempty(cgsFile)
[cids, cgs] = readClusterGroupsCSV(cgsFile);
if params.excludeNoise
noiseClusters = cids(cgs==0);
st = st(~ismember(clu, noiseClusters));
spikeTemplates = spikeTemplates(~ismember(clu, noiseClusters));
tempScalingAmps = tempScalingAmps(~ismember(clu, noiseClusters));
if params.loadPCs
pcFeat = pcFeat(~ismember(clu, noiseClusters), :,:);
%pcFeatInd = pcFeatInd(~ismember(cids, noiseClusters),:);
end
clu = clu(~ismember(clu, noiseClusters));
cgs = cgs(~ismember(cids, noiseClusters));
cids = cids(~ismember(cids, noiseClusters));
end
else
clu = spikeTemplates;
cids = unique(spikeTemplates);
cgs = 3*ones(size(cids));
end
coords = readNPY(fullfile(ksDir, 'channel_positions.npy'));
ycoords = coords(:,2); xcoords = coords(:,1);
temps = readNPY(fullfile(ksDir, 'templates.npy'));
winv = readNPY(fullfile(ksDir, 'whitening_mat_inv.npy'));
spikeStruct.st = st;
spikeStruct.spikeTemplates = spikeTemplates;
spikeStruct.clu = clu;
spikeStruct.tempScalingAmps = tempScalingAmps;
spikeStruct.cgs = cgs;
spikeStruct.cids = cids;
spikeStruct.xcoords = xcoords;
spikeStruct.ycoords = ycoords;
spikeStruct.temps = temps;
spikeStruct.winv = winv;
spikeStruct.pcFeat = pcFeat;
spikeStruct.pcFeatInd = pcFeatInd;