Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,43 @@ on:

jobs:
test:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
path: neurocode # Checkout into a folder named 'neurocode'

- name: Checkout external repository
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
repository: ayalab1/CellExplorer
path: CellExplorer

- name: Set up MATLAB
uses: matlab-actions/setup-matlab@v1
uses: matlab-actions/setup-matlab@v2
with:
release: R2025b
products: >
Signal_Processing_Toolbox
DSP_System_Toolbox
Statistics_and_Machine_Learning_Toolbox
Parallel_Computing_Toolbox

- name: Compile MEX files
run: |
matlab -batch "cd neurocode/utilities/intervalsC+; mex CountInIntervals.c; mex FindInInterval.c; mex MatchUpIndices.c"
matlab -batch "cd neurocode/utilities/mapsC+; mex Contiguous.c; mex FindField.c"
# Public repositories get a MATLAB license only through the
# matlab-actions run steps, not by calling matlab from a shell.
- name: Compile interval MEX files
uses: matlab-actions/run-command@v2
with:
command: cd neurocode/utilities/intervalsC+; mex CountInIntervals.c; mex FindInInterval.c; mex MatchUpIndices.c

- name: Compile map MEX files
uses: matlab-actions/run-command@v2
with:
command: cd neurocode/utilities/mapsC+; mex Contiguous.c; mex FindField.c

- name: Run MATLAB tests
run: |
matlab -batch "addpath(genpath('neurocode')); addpath('CellExplorer/calc_CellMetrics'); results = runtests('neurocode/test'); assertSuccess(results);"
uses: matlab-actions/run-command@v2
with:
command: addpath(genpath('neurocode')); addpath('CellExplorer/calc_CellMetrics'); results = runtests('neurocode/test'); assertSuccess(results);
98 changes: 67 additions & 31 deletions lfp/getEMGFromLFP.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
% 'samplingFrequency' - desired sampling rate for EMG output. default:2
% 'noPrompts' (default: false) prevents prompts about saving/adding metadata
% 'fromDat' -uses the .dat file instead of .lfp (default:false)
% 'maxAutoChannels' - maximum number of spike groups sampled when
% channels are picked automatically (one channel
% per group). Recordings with more usable spike
% groups use this many groups spread evenly
% across the spike-group list, because the cost
% grows with the number of channel pairs.
% Recordings with at most this many usable
% groups are unaffected.
% specialChannels are always added on top.
% Use inf to disable the cap. default: 16
%
%
% OUTPUTS
Expand All @@ -45,6 +55,8 @@
% .channels - channel #'s used for analysis
% .detectorName - string name of function used
% .samplingFrequency - 1 / sampling rate of EMGCorr data
% .detectorinfo - how channels were selected (method,
% maxAutoChannels, spike groups used, ...)
%
% =========================================================================

Expand All @@ -68,6 +80,7 @@
addParameter(p, 'noPrompts', false, @islogical);
addParameter(p, 'fromDat', false, @islogical);
addParameter(p, 'chInfo', [], @isstruct);
addParameter(p, 'maxAutoChannels', 16, @(x) isnumeric(x) && isscalar(x) && x >= 2);
parse(p, varargin{:})

restrict = p.Results.restrict;
Expand All @@ -80,6 +93,7 @@
noPrompts = p.Results.noPrompts;
fromDat = p.Results.fromDat;
chInfo = p.Results.chInfo;
maxAutoChannels = p.Results.maxAutoChannels;

if ~isempty(p.Results.saveLocation)
matfilename = fullfile(p.Results.saveLocation, [recordingname, '.EMGFromLFP.LFP.mat']);
Expand Down Expand Up @@ -120,7 +134,6 @@

binScootS = 1 ./ samplingFrequency;
binScootSamps = round(Fs*binScootS); % must be integer, or error on line 190
corrChunkSz = 20; %for batch-processed correlations

%% Pick channels and to analyze
% get spike groups,
Expand All @@ -132,8 +145,11 @@
% xcorrs_chs is a list of channels that will be loaded
% spkgrpstouse is a list of spike groups to find channels from

nUsableSpikeGroups = NaN;
spkgrpsused = [];
if ~isempty(restrictChannels) % If restrict channel case:
xcorr_chs = restrictChannels;
selectionMethod = 'restrictChannels';
else
% get list of spike groups (aka shanks) that should be used
usablechannels = [];
Expand All @@ -149,6 +165,24 @@

% okay, lets limit usableshankchannels with spkgrpstouse. Thus, empty shanks will be excluded.
usableshankchannels = usableshankchannels(spkgrpstouse);
nUsableSpikeGroups = length(spkgrpstouse);

% The number of channel pairs, and with it the run time, grows
% quadratically with the number of spike groups. Keep at most
% maxAutoChannels groups, spread evenly over the spike-group list.
% Recordings with <= maxAutoChannels usable groups take the unchanged
% path below.
if nUsableSpikeGroups > maxAutoChannels
keepgroups = unique(round(linspace(1, nUsableSpikeGroups, floor(maxAutoChannels))));
usableshankchannels = usableshankchannels(keepgroups);
spkgrpstouse = spkgrpstouse(keepgroups);
selectionMethod = 'onePerSpikeGroupCapped';
elseif length(usableshankchannels) > 1
selectionMethod = 'onePerSpikeGroup';
else
selectionMethod = 'fiveFromSingleSpikeGroup';
end
spkgrpsused = spkgrpstouse;

% get number of channels per shank. If one valid shank, choose 5 channels
if length(usableshankchannels) > 1
Expand Down Expand Up @@ -185,15 +219,15 @@

switch fromDat
case false
lfp = LoadBinary(lfpFile, 'nChannels', nChannels, 'channels', xcorr_chs, ...
lfp = loadBinary(lfpFile, 'nChannels', nChannels, 'channels', xcorr_chs, ...
'start', restrict(1), 'duration', diff(restrict), 'frequency', Fs); %read and convert to mV

if isempty(lfp)
error('LFP file empty. Must have valid binary file to compute EMG.')
end

case true
lfp = LoadBinary(datFile, 'nChannels', nChannels, 'channels', xcorr_chs, ...
lfp = loadBinary(datFile, 'nChannels', nChannels, 'channels', xcorr_chs, ...
'start', restrict(1), 'duration', diff(restrict), 'frequency', datFs, ...
'downsample', datFs./Fs); %read and convert to mV
if isempty(lfp)
Expand All @@ -214,40 +248,35 @@
xcorr_window_samps = round(binScootS*Fs);
xcorr_window_inds = -xcorr_window_samps:xcorr_window_samps; %+- that number of ms in samples

% new version... batches of correlation calculated at once
% Pearson correlation of every channel pair in each window, summed over
% pairs. Windows are processed in blocks of bins: each channel's windows are
% centered and scaled once per block, so a pair costs one elementwise
% product instead of a per-bin corr() call. Same formula as corr (NaN for a
% flat window), results agree with the former per-bin loop to rounding error.
timestamps = (1 + xcorr_window_inds(end)):binScootSamps:(size(lfp, 1) - xcorr_window_inds(end));
numbins = length(timestamps);
EMGCorr = zeros(numbins, 1);
% tic
counter = 1;
for j = 1:(length(xcorr_chs))
for k = (j + 1):length(xcorr_chs)
%disp([num2str(counter*2 ./ (length(xcorr_chs)*length(xcorr_chs)*length(timestamps)))])
bz_Counter(counter, (length(xcorr_chs) * (length(xcorr_chs) - 1))./2, 'Channel Pair')
c1 = [];
c2 = [];
binind = 0;
binindstart = 1;
for i = timestamps
binind = binind + 1;
s1 = lfp(i + xcorr_window_inds, j);
s2 = lfp(i + xcorr_window_inds, k);
c1 = cat(2, c1, s1);
c2 = cat(2, c2, s2);
if size(c1, 2) == corrChunkSz || i == timestamps(end)
binindend = binind;
tmp = corr(c1, c2);
tmp = diag(tmp);
EMGCorr(binindstart:binindend) = EMGCorr(binindstart:binindend) + tmp;
c1 = [];
c2 = [];
binindstart = binind + 1;
end
nCh = length(xcorr_chs);
nPairs = nCh * (nCh - 1) / 2;
nWin = length(xcorr_window_inds);
% bins per block, keeping the block of windowed data near 256 MB
binsPerBlock = max(1, floor(256e6/(8 * nWin * max(nCh, 1))));
for binindstart = 1:binsPerBlock:numbins
binindend = min(numbins, binindstart+binsPerBlock-1);
nb = binindend - binindstart + 1;
sampleinds = xcorr_window_inds(:) + timestamps(binindstart:binindend);
z = reshape(double(lfp(sampleinds(:), :)), nWin, nb, nCh);
z = z - sum(z, 1) / nWin;
z = z ./ sqrt(sum(z.^2, 1));
for j = 1:nCh
for k = (j + 1):nCh
r = sum(z(:, :, j).*z(:, :, k), 1);
r(r > 1) = 1; % guard against rounding, as corr does
r(r < -1) = -1;
EMGCorr(binindstart:binindend) = EMGCorr(binindstart:binindend) + r(:);
end
counter = counter + 1;
end
end
% toc

EMGCorr = EMGCorr / (length(xcorr_chs) * (length(xcorr_chs) - 1) / 2); % normalize

Expand All @@ -256,6 +285,13 @@
EMGFromLFP.channels = xcorr_chs;
EMGFromLFP.detectorName = 'getEMGFromLFP';
EMGFromLFP.samplingFrequency = samplingFrequency;
EMGFromLFP.detectorinfo.channelSelection = selectionMethod;
EMGFromLFP.detectorinfo.maxAutoChannels = maxAutoChannels;
EMGFromLFP.detectorinfo.nUsableSpikeGroups = nUsableSpikeGroups;
EMGFromLFP.detectorinfo.spikeGroupsUsed = spkgrpsused;
EMGFromLFP.detectorinfo.specialChannels = specialChannels;
EMGFromLFP.detectorinfo.rejectChannels = rejectChannels;
EMGFromLFP.detectorinfo.nChannelPairs = nPairs;

if ~any(~isnan(EMGFromLFP.data))
keyboard
Expand Down
Loading
Loading