Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erroneous detection of burst on- and offsets in burst.utils.compute_burst_stats() #219

Closed
JanCBrammer opened this issue Aug 27, 2020 · 2 comments

Comments

@JanCBrammer
Copy link
Contributor

I believe that the detection of burst on- and offsets fails if the boolean burst vector starts and/or ends with bursts.

Here's an illustration:

import numpy as np


def find_starts_ends_A(bursting):    # current implementation

    starts = np.array([])
    ends = np.array([])

    for ii, index in enumerate(np.where(np.diff(bursting) != 0)[0]):

        if (ii % 2) == 0:
            starts = np.append(starts, index)
        else:
            ends = np.append(ends, index)

    return starts, ends


def find_starts_ends_B(bursting):    # suggested alternative implementation

    # Find the indices of changes.
    change = np.diff(bursting)
    idcs, = change.nonzero()

    idcs += 1    # Get indices following the change.

    if bursting[0]:
        # If the first sample fulfills the condition, prepend a zero.
        idcs = np.r_[0, idcs]

    if bursting[-1]:
        # If the last sample fulfills the condition, append an index
        # corresponding to the length of signal
        idcs = np.r_[idcs, bursting.size]

    starts = idcs[0::2]
    ends = idcs[1::2]

    return starts, ends


bursts = np.array([True, True, False, False, True, True, True, False, True])

startsA, endsA = find_starts_ends_A(bursts)
startsB, endsB = find_starts_ends_B(bursts)

print(f"starts A: {startsA}, ends A: {endsA}")
print(f"starts B: {startsB}, ends B: {endsB}")
starts A: [1. 6.], ends A: [3. 7.]
starts B: [0 4 8], ends B: [2 7 9]

Happy to make a PR. Thanks for the amazing package btw.

@ryanhammonds
Copy link
Member

Hi @JanCBrammer, thanks for reporting the bug! I tested your solution and it looks great. Feel free to open a PR if you have time. Otherwise, I can implement the change soon.

@JanCBrammer
Copy link
Contributor Author

Feel free to open a PR if you have time

Great, I should have some time next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants