neurodsp-tools / neurodsp

Digital signal processing for neural time series.
https://neurodsp-tools.github.io/
Apache License 2.0
281 stars 61 forks source link

[WIP] Phase consistency measures #263

Open ryanhammonds opened 3 years ago

ryanhammonds commented 3 years ago

I was exploring the phase relationship between spikes and lfp and came across a few methods that may be useful additions. Currently, this adds pairwise phase consistency (Vinck et al. 2010). This PR or future PRs could also include phase-locking value.

TomDonoghue commented 3 years ago

I think adding these phase measures makes sense!

In terms of scope, mostly we've stuck to measures applied to single signals (univariate), but I think adding some relatively simple bivariate phase measure things that one might want to use, is totally reasonable!

I have a quick implementation of PLV that I use sometimes that I think should be fairly solid, if you want to use / add that:

def phase_locking_value(theta1, theta2):
    """Compute the phase locking value between two signals.
    From: https://dsp.stackexchange.com/questions/25165/phase-locking-value-phase-synchronization
    """

    complex_phase_diff = np.exp(np.complex(0, 1) * (theta1 - theta2))
    plv = np.abs(np.sum(complex_phase_diff)) / len(theta1)

    return plv

theta1 & theta2 should be 1d arrays of instantaneous phase, I think.

ryanhammonds commented 3 years ago

Sorry for the delayed response, I was out last week. Glad you think this was a reasonable addition. I was fascinated by the paper, wrote the code to do it and figured it could be useful to others.

Thanks for the PLV snippet. I'll add that sometime soon. This and #264 can go in after the 2.2.0 release. The new release is ready on my end, but I can always re-review or ask someone else in the lab to help with reviews as I know you're busy, no worries either way!

codecov-commenter commented 3 years ago

Codecov Report

Merging #263 (310a4e4) into main (ea27979) will decrease coverage by 0.03%. The diff coverage is 96.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #263      +/-   ##
==========================================
- Coverage   97.58%   97.54%   -0.04%     
==========================================
  Files          94       96       +2     
  Lines        2482     2567      +85     
==========================================
+ Hits         2422     2504      +82     
- Misses         60       63       +3     
Impacted Files Coverage Δ
neurodsp/tests/rhythm/test_phase.py 94.87% <94.87%> (ø)
neurodsp/rhythm/phase.py 97.77% <97.77%> (ø)
neurodsp/rhythm/__init__.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update ea27979...310a4e4. Read the comment docs.