Skip to content

Fast annotation from mask #10088

Closed
Closed

Description

@snwnde

Describe the new feature or enhancement

In mne/preprocessing/artifact_detection.py there is a function called _annotations_from_mask. As its name suggests it creates an annotation from a numpy array mask. A for loop has been used in its implementation, which could be very time-consuming when the number of components is large.

Describe your proposed implementation

This could be implemented without any for loop. I have the following code, working in my tests:

def _annotations_from_mask(times, mask, annot_name):
    """Construct annotations from boolean mask of the data."""
    from scipy.ndimage.morphology import distance_transform_edt
    from scipy.signal import find_peaks
    mask_tf = distance_transform_edt(mask)
    midpoint_index = find_peaks(mask_tf)[0]
    onsets_index = midpoint_index - mask_tf[midpoint_index].astype(int) + 1
    ends_index = midpoint_index + mask_tf[midpoint_index].astype(int) - 1
    
    # Ensure onsets_index >= 0, otherwise the duration starts from the beginning
    onsets_index[onsets_index < 0] = 0
    # Ensure ends_index < len(times), otherwise the duration is to the end of times
    ends_index[ends_index >= len(times)] = len(times) - 1

    onsets = times[onsets_index]
    ends = times[ends_index]
    durations = ends - onsets
    desc = [annot_name]*len(durations)
    return Annotations(onsets, durations, desc)

This implementation doesn't require any additional package.
With a mask of num_comps equal to 24389, %timeit gives for the original implementation and the mine respectively:

2min 1s ± 640 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
590 ms ± 16.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Describe possible alternatives

I do not have any other implementation in mind.

Please tell me how you think of this enhancement. If everything seems fine, I will be glad to create a PR.

Activity

  1. welcome commented on Dec 3, 2021

    @welcome

    Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴🏽‍♂️

  2. agramfort commented on Dec 4, 2021

    @agramfort
    SponsorMember
  3. snwnde commented on Dec 4, 2021

    @snwnde
    ContributorAuthor

    I tweaked the code to address the following problems:

    • find_peaks cannot find peaks at the start and the end of arrays,
    • find_peaks returns the first peak index, preventing the previous code to correctly give ends_index.
    def _annotations_from_mask(times, mask, annot_name):
        """Construct annotations from boolean mask of the data."""
        from scipy.ndimage.morphology import distance_transform_edt
        from scipy.signal import find_peaks
        mask_tf = distance_transform_edt(mask)
        # Overcome the shortcoming of find_peaks
        # in finding a marginal peak, by
        # inserting 0s at the front and the
        # rear, then subtracting in index
        ins_mask_tf = np.concatenate((np.zeros(1), mask_tf, np.zeros(1)))
        left_midpt_index = find_peaks(ins_mask_tf)[0] - 1
        right_midpt_index = np.flip(len(ins_mask_tf) - 1 - find_peaks(ins_mask_tf[::-1])[0]) - 1
        onsets_index = left_midpt_index - mask_tf[left_midpt_index].astype(int) + 1
        ends_index = right_midpt_index + mask_tf[right_midpt_index].astype(int)
        # Ensure onsets_index >= 0,
        # otherwise the duration starts from the beginning
        onsets_index[onsets_index < 0] = 0
        # Ensure ends_index < len(times),
        # otherwise the duration is to the end of times
        ends_index[ends_index >= len(times)] = len(times) - 1
        onsets = times[onsets_index]
        ends = times[ends_index]
        durations = ends - onsets
        desc = [annot_name] * len(durations)
        return Annotations(onsets, durations, desc)
  4. agramfort commented on Dec 4, 2021

    @agramfort
    SponsorMember
  5. snwnde commented on Dec 4, 2021

    @snwnde
    ContributorAuthor

    Can you send the pull request or shall we do it ?

    @agramfort I created the PR #10089. Some checks fail though. It seems a precision problem.

  6. added a commit that references this issue on Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions