bycycle-tools / bycycle

Cycle-by-cycle analysis of neural oscillations.
https://bycycle-tools.github.io/
Apache License 2.0
82 stars 21 forks source link

[ENH] Recompute consistency features at burst edges #90

Closed ryanhammonds closed 3 years ago

ryanhammonds commented 3 years ago

This is an improvement to #73 and recompute_edges. Period/amplitude consistency features may now be computed in one direction for non-bursting cycles that are immediately adjacent to bursts. Before the solution was to drop thresholds for edge cycles which works but was lazier.

import numpy as np
from neurodsp.sim import sim_combined

from bycycle.features import compute_features
from bycycle.burst.utils import recompute_edges
from bycycle.plts import plot_burst_detect_summary

# Simulate
np.random.seed(0)
n_seconds = 10
fs = 1000
f_range = (1, 51)

components = dict(sim_bursty_oscillation=dict(freq=15, cycle='gaussian', std=1),
                  sim_powerlaw=dict(exponent=-2))

sig = sim_combined(n_seconds, fs, components)

times = np.arange(0, len(sig)/fs, 1/fs)

thresholds = {'amp_fraction_threshold': 0.3,
              'amp_consistency_threshold': 0.55,
              'period_consistency_threshold': 0.55,
              'monotonicity_threshold': 0.75,
              'min_n_cycles': 3}

df_features = compute_features(sig, fs, f_range, threshold_kwargs=thresholds)

plot_burst_detect_summary(df_features, sig, fs, thresholds, plot_only_result=True, xlim=(5.8, 6.3))

indices = np.where((df_features['sample_peak'] >= (5.9* fs)) &
                   (df_features['sample_peak'] <= (6.2* fs)))[0]

print('Original:   ', df_features.iloc[indices]['period_consistency'].values)

df_features = recompute_edges(df_features, thresholds)

plot_burst_detect_summary(df_features, sig, fs, thresholds, plot_only_result=True, xlim=(5.8, 6.3))

print('Recomputed: ', df_features.iloc[indices]['period_consistency'].values)

consist

codecov-io commented 3 years ago

Codecov Report

Merging #90 (5ff856e) into main (fe7de44) will decrease coverage by 0.41%. The diff coverage is 92.30%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #90      +/-   ##
==========================================
- Coverage   95.80%   95.39%   -0.42%     
==========================================
  Files          22       22              
  Lines         739      760      +21     
==========================================
+ Hits          708      725      +17     
- Misses         31       35       +4     
Impacted Files Coverage Δ
bycycle/plts/cyclepoints.py 97.77% <75.00%> (-2.23%) :arrow_down:
bycycle/plts/burst.py 91.30% <80.00%> (-4.35%) :arrow_down:
bycycle/burst/utils.py 100.00% <100.00%> (ø)
bycycle/features/burst.py 97.59% <100.00%> (+0.33%) :arrow_up:
bycycle/utils/dataframes.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 fe7de44...5ff856e. Read the comment docs.

srcole commented 3 years ago

This is a really good change! Nice example in the first comment too.

ryanhammonds commented 3 years ago

Thanks for the review! I pushed related updates. The last thing to do is sort out type checking.