Skip to content

Commit c0f3180

Browse files
CopilotNabarb
andauthored
Extend absoluteToRelativeEvents with robust event filtering, trial inference, and flexible trial-start reference modes (#3)
* Initial plan * feat: extend absoluteToRelativeEvents with name-value modes and trial inference Co-authored-by: Nabarb <23075957+Nabarb@users.noreply.github.com> * chore: address code review feedback for absoluteToRelativeEvents Co-authored-by: Nabarb <23075957+Nabarb@users.noreply.github.com> * feat: make absoluteToRelativeEvents accept name-value args and robustly handle trial bounds Replace inputParser with a name-value arguments interface for absoluteToRelativeEvents, add an input validator for trialStartReference, and expose inferTrialFromBounds as a named option. Improve robustness when pairing trial start/end timestamps by trimming unmatched leading ends or trailing starts and only keeping properly ordered start/end pairs. Minor tidy-ups to variable formatting. * feat: standardize event structure and improve event handling logic Update the Events_ struct to use PascalCase fields (Name, Trial, Data) and refine the absoluteToRelativeEvents utility with trial skipping and improved trial inference. Ensure addEvents remaps trial IDs to a consecutive range and update PCA/GPFA to correctly calculate explained variance during projection. * feat: add cumulative arclength and update trial indexing in NeuralEmbedding Update the arclength utility to return cumulative segment distances. Refine trial selection logic in NeuralEmbedding to use direct indices for event lookup, ensure tMask is consistently stored as a column vector, and increase event marker visibility in visualizations. * feat: add plotEventDensity to visualize event distribution along arclength Adds a method to compute and plot the density of events relative to the normalized arclength of neural trajectories. The method uses weighted kernel density estimation to account for non-uniform arclength velocity and provides multi-event visualization using the gramm library. * feat: optimize plotEventDensity performance and robustness Refactors the density calculation to process trials in a single pass and replaces expensive distance calculations with vectorized interpolation. Improves robustness by adding checks for finite arclengths and minimum data points for kernel density estimation. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Nabarb <23075957+Nabarb@users.noreply.github.com> Co-authored-by: fbarban <federico.barban@unige.it> Co-authored-by: Fede <fedbarban+github@gmail.com>
1 parent 557bfc6 commit c0f3180

5 files changed

Lines changed: 419 additions & 44 deletions

File tree

+embedding/GPFA.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [E,ProjMatrix,VarExplained]= GPFA(D,pars,W, VarExplained)
1+
function [E,ProjMatrix,VarExplained]= GPFA(D,pars,W)
22
if nargin < 3
33
[W, VarExplained] = deal([]);
44
end

+embedding/PCA.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [E,ProjMatrix,ProjMatrixInv,VarExplained] = PCA(D,pars,W, VarExplained)
1+
function [E,ProjMatrix,ProjMatrixInv,VarExplained] = PCA(D,pars,W)
22

33
if nargin < 3
44
[W, VarExplained] = deal([]);
@@ -12,8 +12,13 @@
1212
end
1313
if pars.projectOnly
1414
E = embedding.PCA.project(D,W);
15+
Winv = W{1}';
16+
data = [D{:}];
17+
VarExplained = NeuralEmbedding.explainedVar(Winv * [E{:}], data);
18+
VarExplained = {VarExplained(2,1)};
19+
1520
ProjMatrix = W;
16-
ProjMatrixInv = {W{1}'};
21+
ProjMatrixInv = {Winv};
1722
else
1823
[E,ProjMatrix,VarExplained] = embedding.PCA.reduce(D_,pars.numPC);
1924
E = cellfun(@(e)e(1:pars.numPC,:),E,'UniformOutput',false);

+metrics/+compute/arclength.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [arclen,seglen] = arclength(px,py,varargin)
1+
function [arclen,seglen,cumarclen] = arclength(px,py,varargin)
22
% arclength: compute arc length of a space curve, or any curve represented as a sequence of points
33
% usage: [arclen,seglen] = arclength(px,py) % a 2-d curve
44
% usage: [arclen,seglen] = arclength(px,py,pz) % a 3-d space curve
@@ -156,6 +156,7 @@
156156
% compute the chordal linear arclengths
157157
seglen = sqrt(sum(diff(data,[],1).^2,2));
158158
arclen = sum(seglen);
159+
cumarclen = cumsum(seglen);
159160

160161
% we can quit if the method was 'linear'.
161162
if strcmpi(method,'linear')
@@ -209,6 +210,7 @@
209210

210211
% and sum the segments
211212
arclen = sum(seglen);
213+
cumarclen = cumsum(seglen);
212214

213215
% ==========================
214216
% end main function

0 commit comments

Comments
 (0)