question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fast annotation from mask

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
agramfortcommented, Dec 4, 2021

looks smart !

can you send us a PR?

thx a lot

0reactions
snwndecommented, Dec 4, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Annotation of image and creating our own Mask-RCNN Model.
It allows us to interact from image annotation to neural networks training 10x faster. Benefits of using supervisely : Organize image annotation /...
Read more >
Converting annotations to object segmentation mask images
This notebook demonstrates tools to convert annotations into contours or masks that can be used with algorithms like Mask-RCNN. There are two approaches...
Read more >
Generating Image Segmentation Masks — The Easy Way
How-To · 1. Open via. · 2. Start Annotating: Click on the border of an object and draw a polygon around the object....
Read more >
Image mask annotation from time-lapse of images using QuPath
When all the annotations are made, I would like to export a Z-stack of binary masks of the same dimensions as the original...
Read more >
Data annotation for mask rcnn - Stack Overflow
If you have a look COCO dataset, you can see it has 2 types of annotation format - bounding box and mask(polygon). Therefore,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found