Skip to content

Commit

Permalink
fix bug burst ring buffer concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
timonmerk committed Apr 12, 2024
1 parent 251bc23 commit a61e255
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,5 @@ plot_0_first_demo.py

requirements.lock
requirements-dev.lock

plot_1_example_BIDS.py
32 changes: 23 additions & 9 deletions py_neuromodulation/nm_bursts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def __init__(
self.ch_names = ch_names
self.threshold = self.s["burst_settings"]["threshold"]
self.time_duration_s = self.s["burst_settings"]["time_duration_s"]
self.samples_overlap = int(
self.sfreq
* (self.s["segment_length_features_ms"] / 1000)
/ self.s["sampling_rate_features_hz"]
)

self.fband_names = self.s["burst_settings"]["frequency_bands"]
self.f_ranges = [
Expand Down Expand Up @@ -105,7 +110,11 @@ def calc_feature(self, data: np.array, features_compute: dict) -> dict:
self.data_buffer[ch_name][fband_name] = new_dat
else:
self.data_buffer[ch_name][fband_name] = np.concatenate(
(self.data_buffer[ch_name][fband_name], new_dat), axis=0
(
self.data_buffer[ch_name][fband_name],
new_dat[-self.samples_overlap :],
),
axis=0,
)[-self.num_max_samples_ring_buffer :]

# calc features
Expand Down Expand Up @@ -152,9 +161,9 @@ def calc_feature(self, data: np.array, features_compute: dict) -> dict:
if self.data_buffer[ch_name][fband_name][-1] > burst_thr:
in_burst = True

features_compute[
f"{ch_name}_bursts_{fband_name}_in_burst"
] = in_burst
features_compute[f"{ch_name}_bursts_{fband_name}_in_burst"] = (
in_burst
)
return features_compute

@staticmethod
Expand All @@ -170,15 +179,20 @@ def get_burst_amplitude_length(
burst_length = []
burst_amplitude = []

burst_time_points = np.where(deriv==True)[0]
burst_time_points = np.where(deriv == True)[0]

for i in range(burst_time_points.size//2):
burst_length.append(burst_time_points[2 * i + 1] - burst_time_points[2 * i])
burst_amplitude.append(beta_averp_norm[burst_time_points[2 * i] : burst_time_points[2 * i + 1]])
for i in range(burst_time_points.size // 2):
burst_length.append(
burst_time_points[2 * i + 1] - burst_time_points[2 * i]
)
burst_amplitude.append(
beta_averp_norm[
burst_time_points[2 * i] : burst_time_points[2 * i + 1]
]
)

# the last burst length (in case isburst == True) is omitted,
# since the true burst length cannot be estimated
burst_length = np.array(burst_length) / sfreq

return burst_amplitude, burst_length

0 comments on commit a61e255

Please sign in to comment.