File tree Expand file tree Collapse file tree 1 file changed +8
-14
lines changed Expand file tree Collapse file tree 1 file changed +8
-14
lines changed Original file line number Diff line number Diff line change @@ -3874,23 +3874,17 @@ def contiguous_regions(mask):
3874
3874
"""
3875
3875
return a list of (ind0, ind1) such that mask[ind0:ind1].all() is
3876
3876
True and we cover all such regions
3877
-
3878
- TODO: this is a pure python implementation which probably has a much
3879
- faster numpy impl
3880
3877
"""
3878
+ mask = np .asarray (mask , dtype = bool )
3879
+
3880
+ if not mask .size :
3881
+ return []
3881
3882
3882
- in_region = None
3883
- boundaries = []
3884
- for i , val in enumerate (mask ):
3885
- if in_region is None and val :
3886
- in_region = i
3887
- elif in_region is not None and not val :
3888
- boundaries .append ((in_region , i ))
3889
- in_region = None
3883
+ idx , = np .nonzero (mask [:- 1 ] != mask [1 :])
3884
+ idx = np .concatenate ((([0 ],) if mask [0 ] else ()) + (idx + 1 ,) +
3885
+ (([len (mask )],) if mask [- 1 ] else ()))
3890
3886
3891
- if in_region is not None :
3892
- boundaries .append ((in_region , i + 1 ))
3893
- return boundaries
3887
+ return list (zip (idx [::2 ], idx [1 ::2 ]))
3894
3888
3895
3889
3896
3890
def cross_from_below (x , threshold ):
You can’t perform that action at this time.
0 commit comments