Skip to content

Commit

Permalink
giving the KS main file a more rational name
Browse files Browse the repository at this point in the history
  • Loading branch information
jenniferColonell committed Feb 17, 2021
1 parent d50e2ec commit dd36b2e
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 5 deletions.
6 changes: 3 additions & 3 deletions ecephys_spike_sorting/modules/kilosort_helper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def run_kilosort(args):

# shutil.copyfile(master_fullpath,
# os.path.join(args['kilosort_helper_params']['matlab_home_directory'],args['kilosort_helper_params']['master_file_name']))
shutil.copyfile(os.path.join(args['directories']['ecephys_directory'],'modules','kilosort_helper','main_KS2_KS25_KS3.m'),
os.path.join(args['kilosort_helper_params']['matlab_home_directory'],'main_KS2_KS25_KS3.m'))
shutil.copyfile(os.path.join(args['directories']['ecephys_directory'],'modules','kilosort_helper','main_kilosort_multiversion.m'),
os.path.join(args['kilosort_helper_params']['matlab_home_directory'],'main_kilosort_multiversion.m'))

if args['kilosort_helper_params']['kilosort_version'] == 1:

Expand Down Expand Up @@ -122,7 +122,7 @@ def run_kilosort(args):
eng.addpath(eng.genpath(KS_dir))
eng.addpath(eng.genpath(NPY_dir))
eng.addpath(home_dir)
eng.main_KS2_KS25_KS3(args['kilosort_helper_params']['kilosort2_params']['KSver'], \
eng.main_kilosort_multiversion(args['kilosort_helper_params']['kilosort2_params']['KSver'], \
args['kilosort_helper_params']['kilosort2_params']['remDup'], \
args['kilosort_helper_params']['kilosort2_params']['finalSplits'], \
args['kilosort_helper_params']['kilosort2_params']['labelGood'], \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
function main_KS2_KS25( KSver, remDup, finalSplits, labelGood, saveRez )
% paths will get set by the pipeline
% config file created by pipeline and saved in directory with master
% channel map created by pipeline, path specified in config

rP.KSver = KSver;
rP.remDup = remDup;
rP.finalSplits = finalSplits;
rP.labelGood = labelGood;
rP.saveRez = saveRez;

fprintf( 'main_KS2_KS25_KS3 params: \n');
disp(rP)

if ~(strcmp(rP.KSver, '2.5') | strcmp(rP.KSver, '2.0') | strcmp(rP.KSver, '3.0') )
fprintf('unsupported kilosort version\n');
return;
end

run('kilosort2_config_file.m')



if strcmp(rP.KSver, '2.5')
% main parameter changes from Kilosort2 to v2.5
ops.sig = 20; % spatial smoothness constant for registration
ops.fshigh = 300; % high-pass more aggresively
ops.nblocks = 5; % blocks for registration. 0 turns it off, 1 does rigid registration. Replaces "datashift" option.;
% random number generator is used in datashift and to set order of batches
% set seed and initialize here.
iseed = 1;
rng(iseed);
elseif strcmp(rP.KSver, '3.0')
% main parameter changes from Kilosort2 to v2.5
ops.sig = 20; % spatial smoothness constant for registration
ops.fshigh = 300; % high-pass more aggresively
ops.nblocks = 5; % blocks for registration. 0 turns it off, 1 does rigid registration. Replaces "datashift" option.

% main parameter changes from Kilosort2.5 to v3.0
% if using KS3, set ops.Th appropriately from the calling program
% ops.Th = [9, 9];

end

% find the binary file
rootZ = ops.rootZ;
ops.fbinary = fullfile(ops.datafile);

% print out ops
ops

% preprocess data to create temp_wh.dat
rez = preprocessDataSub(ops);

if ~strcmp(rP.KSver, '3.0')
% That is for KS2.0 and KS 2.5
if strcmp(rP.KSver, '2.5')
% data registration step
rez = datashift2(rez, 1); % last input is for shifting data
% main tracking and template matching algorithm
rez = learnAndSolve8b(rez, iseed);
elseif strcmp(KSver, '2.0')
% time-reordering as a function of drift
rez = clusterSingleBatches(rez);
rez = learnAndSolve8b(rez);
end

% OPTIONAL: remove double-counted spikes - solves issue in which individual spikes are assigned to multiple templates.
% See issue 29: https://github.com/MouseLand/Kilosort/issues/29
if rP.remDup
rez = remove_ks2_duplicate_spikes(rez);
end

% final merges
rez = find_merges(rez, 1);

% final splits by SVD
if rP.finalSplits
rez = splitAllClusters(rez, 1);
end

% decide on cutoff
rez = set_cutoff(rez);

% eliminate widely spread waveforms (likely noise); only implemented in KS2.5 release
if ( rP.labelGood & strcmp(rP.KSver, '2.5'))
rez.good = get_good_units(rez);
end

fprintf('found %d good units \n', sum(rez.good>0))

else
% For KS 3.0
rez = datashift2(rez, 1);

[rez, st3, tF] = extract_spikes(rez);

rez = template_learning(rez, tF, st3);

[rez, st3, tF] = trackAndSort(rez);

rez = final_clustering(rez, tF, st3);

rez = find_merges(rez, 1);

rezToPhy2(rez, rootZ);
end

% write to Phy
fprintf('Saving results to Phy \n')
rezToPhy(rez, rootZ);

if rP.saveRez
%% if you want to save the results to a Matlab file...

% discard features in final rez file (too slow to save)
rez.cProj = [];
rez.cProjPC = [];

% final time sorting of spikes, for apps that use st3 directly
[~, isort] = sortrows(rez.st3);
rez.st3 = rez.st3(isort, :);

% Ensure all GPU arrays are transferred to CPU side before saving to .mat
rez_fields = fieldnames(rez);
for i = 1:numel(rez_fields)
field_name = rez_fields{i};
if(isa(rez.(field_name), 'gpuArray'))
rez.(field_name) = gather(rez.(field_name));
end
end

% save final results as rez2
fprintf('Saving final results in rez2 \n')
fname = fullfile(rootZ, 'rez2.mat');
save(fname, 'rez', '-v7.3');
end

end
4 changes: 2 additions & 2 deletions ecephys_spike_sorting/scripts/sglx_filelist_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
# for each recording, specfiy a full path the the binary and a brain region

recording_specs = [
[r'D:\ecephys_fork\test_data\test_no_preprocess\SC011_022319_g0_tcat.imec3.ap.bin', 'default' ],
[r'D:\ecephys_fork\test_data\test_no_preprocess\SC024_092319_NP1.0_Midbrain_g0_tcat.imec0.ap.bin', 'default' ]
[r'D:\ecephys_fork\test_data\file_list_test\SC011_022319_g0_tcat.imec3.ap.bin', 'default' ],
[r'D:\ecephys_fork\test_data\file_list_test\SC024_092319_NP1.0_Midbrain_g0_tcat.imec0.ap.bin', 'default' ]
]


Expand Down

0 comments on commit dd36b2e

Please sign in to comment.