From f87652763b0d7ee3c3ceecaf55143048fca3ed71 Mon Sep 17 00:00:00 2001 From: Julie-Fabre Date: Wed, 5 Jun 2024 18:31:27 +0100 Subject: [PATCH 1/2] spatial decay slope update function --- bc_qualityMetrics_pipeline.m | 2 + qualityMetrics/bc_updateSpatialDecaySlope.m | 22 ++++++++ qualityMetrics/helpers/bc_getSpatialDecay.asv | 53 ------------------- 3 files changed, 24 insertions(+), 53 deletions(-) create mode 100644 qualityMetrics/bc_updateSpatialDecaySlope.m delete mode 100644 qualityMetrics/helpers/bc_getSpatialDecay.asv diff --git a/bc_qualityMetrics_pipeline.m b/bc_qualityMetrics_pipeline.m index ca09af2b..8527fd5b 100644 --- a/bc_qualityMetrics_pipeline.m +++ b/bc_qualityMetrics_pipeline.m @@ -24,6 +24,8 @@ % empty(e.g. ephysMetaDir = '') kilosortVersion = 4;% if using kilosort4, you need to change this value. Otherwise it does not matter. +bc_updateSpatialDecaySlope(ephysKilosortPath, savePath) + %% check MATLAB version if exist('isMATLABReleaseOlderThan', 'file') == 0 % function introduced in MATLAB 2020b. oldMATLAB = true; diff --git a/qualityMetrics/bc_updateSpatialDecaySlope.m b/qualityMetrics/bc_updateSpatialDecaySlope.m new file mode 100644 index 00000000..3c3451d0 --- /dev/null +++ b/qualityMetrics/bc_updateSpatialDecaySlope.m @@ -0,0 +1,22 @@ +function bc_updateSpatialDecaySlope(kilosortSavePath, qMetricSavePath) + +% load in quality metrics +[~, qMetric] = bc_loadSavedMetrics(qMetricSavePath); + +% load in relevant kilosort files +[~, spikeTemplates, templateWaveforms, ~, ~, ... + ~, ~] = bc_loadEphysData(kilosortSavePath); + +uniqueTemplates = unique(spikeTemplates); + +% update spatial decay value +for iUnit = 1:size(uniqueTemplates,1) + + thisUnit = uniqueTemplates(iUnit); + + qMetric.spatialDecaySlope(iUnit) = qMetric.spatialDecaySlope(iUnit)./max(max(templateWaveforms(thisUnit, :,:))); +end + +% save new metrics +parquetwrite([fullfile(qMetricSavePath, 'templates._bc_qMetrics.parquet')], qMetric) +end \ No newline at end of file diff --git a/qualityMetrics/helpers/bc_getSpatialDecay.asv b/qualityMetrics/helpers/bc_getSpatialDecay.asv deleted file mode 100644 index b6d5981f..00000000 --- a/qualityMetrics/helpers/bc_getSpatialDecay.asv +++ /dev/null @@ -1,53 +0,0 @@ -function [spatialDecaySlope, spatialDecayFit, spatialDecayPoints, spatialDecayPoints_loc, estimatedUnitXY] = ... - bc_getSpatialDecay(templateWaveforms, thisUnit, maxChannel, channelPositions, linearFit) - - if linearFit % linear of fit of first 6 channels (at same X position). - % In real, good units, these points decrease linearly sharply (and, in further away channels they then decrease exponentially). - % In noise artefacts they are mostly flat. - channels_withSameX = find(channelPositions(:, 1) <= channelPositions(maxChannel, 1)+33 & ... - channelPositions(:, 1) >= channelPositions(maxChannel, 1)-33); % for 4 shank probes - if numel(channels_withSameX) >= 5 - if find(channels_withSameX == maxChannel) > 5 - channels_forSpatialDecayFit = channels_withSameX( ... - find(channels_withSameX == maxChannel):-1:find(channels_withSameX == maxChannel)-5); - else - channels_forSpatialDecayFit = channels_withSameX( ... - find(channels_withSameX == maxChannel):1:min(find(channels_withSameX == maxChannel)+5, size(channels_withSameX, 1))); - end - - % get maximum value %QQ could we do value at detected trough is peak - % waveform? - spatialDecayPoints = max(abs(squeeze(templateWaveforms(thisUnit, :, channels_forSpatialDecayFit)))); - - estimatedUnitXY = channelPositions(maxChannel, :); - relativePositionsXY = channelPositions(channels_forSpatialDecayFit, :) - estimatedUnitXY; - channelPositions_relative = sqrt(nansum(relativePositionsXY.^2, 2)); - - [~, sortexChanPosIdx] = sort(channelPositions_relative); - spatialDecayPoints_norm = spatialDecayPoints(sortexChanPosIdx); - spatialDecayPoints_loc = channelPositions_relative(sortexChanPosIdx); - - % normalize spatial decay points - spatialDecayPoints_norm = spatialDecayPoints_norm ./ max(spatialDecayPoints_norm); - % linear fit - spatialDecayFit = polyfit(spatialDecayPoints_loc, spatialDecayPoints_norm', 1); % fit first order polynomial to data. first output is slope of polynomial, second is a constant - spatialDecaySlope = spatialDecayFit(1); - if length(spatialDecayPoints) < 6 - if length(spatialDecayPoints) > 1 - spatialDecayPoints = [spatialDecayPoints_norm, nan(21-length(spatialDecayPoints_norm),1)]; - else - spatialDecayPoints = [spatialDecayPoints_norm; nan(21-length(spatialDecayPoints_norm),1)]; - end - end - else - warning('No other good channels with same x location') - spatialDecayFit = NaN; - spatialDecaySlope = NaN; - spatialDecayPoints = nan(1, 6); - - end - else % not yet implemented. exponential fit? - - - end -end \ No newline at end of file From 85ffcce176874aa12e469f0a240ac234c66e370c Mon Sep 17 00:00:00 2001 From: Julie-Fabre Date: Wed, 5 Jun 2024 18:32:33 +0100 Subject: [PATCH 2/2] spatial decay slope update function --- bc_qualityMetrics_pipeline.m | 2 -- 1 file changed, 2 deletions(-) diff --git a/bc_qualityMetrics_pipeline.m b/bc_qualityMetrics_pipeline.m index 8527fd5b..ca09af2b 100644 --- a/bc_qualityMetrics_pipeline.m +++ b/bc_qualityMetrics_pipeline.m @@ -24,8 +24,6 @@ % empty(e.g. ephysMetaDir = '') kilosortVersion = 4;% if using kilosort4, you need to change this value. Otherwise it does not matter. -bc_updateSpatialDecaySlope(ephysKilosortPath, savePath) - %% check MATLAB version if exist('isMATLABReleaseOlderThan', 'file') == 0 % function introduced in MATLAB 2020b. oldMATLAB = true;