@@ -2359,7 +2359,7 @@ def __init__(self, mne, weakmain, annot, ch_name):
2359
2359
2360
2360
self .mne .plt .addItem (self , ignoreBounds = True )
2361
2361
2362
- self .annot .removeRequested .connect (self .remove )
2362
+ self .annot .removeSingleChannelAnnots .connect (self .remove )
2363
2363
self .annot .sigRegionChangeFinished .connect (self .update_plot_curves )
2364
2364
self .annot .sigRegionChanged .connect (self .update_plot_curves )
2365
2365
self .annot .sigToggleVisibility .connect (self .update_visible )
@@ -2399,6 +2399,7 @@ class AnnotRegion(LinearRegionItem):
2399
2399
regionChangeFinished = Signal (object )
2400
2400
gotSelected = Signal (object )
2401
2401
removeRequested = Signal (object )
2402
+ removeSingleChannelAnnots = Signal (object )
2402
2403
sigToggleVisibility = Signal (bool )
2403
2404
sigUpdateColor = Signal (str )
2404
2405
@@ -2435,29 +2436,54 @@ def __init__(self, mne, description, values, weakmain, ch_names=None):
2435
2436
self .mne .plt .addItem (self .label_item , ignoreBounds = True )
2436
2437
2437
2438
def _region_changed (self ):
2438
- self .regionChangeFinished .emit (self )
2439
- self .old_onset = self .getRegion ()[0 ]
2440
- # remove merged regions
2439
+ # Check for overlapping regions
2440
+ overlap_has_sca = []
2441
2441
overlapping_regions = list ()
2442
2442
for region in self .mne .regions :
2443
2443
if region .description != self .description or id (self ) == id (region ):
2444
2444
continue
2445
2445
values = region .getRegion ()
2446
- if any (self .getRegion ()[0 ] <= val <= self .getRegion ()[1 ] for val in values ):
2446
+ if (
2447
+ any (self .getRegion ()[0 ] <= val <= self .getRegion ()[1 ] for val in values )
2448
+ or (values [0 ] <= self .getRegion ()[0 ] <= values [1 ])
2449
+ and (values [0 ] <= self .getRegion ()[1 ] <= values [1 ])
2450
+ ):
2447
2451
overlapping_regions .append (region )
2452
+ overlap_has_sca .append (len (region .single_channel_annots ) > 0 )
2453
+
2454
+ # If this region or an overlapping region have
2455
+ # channel specific annotations then terminate
2456
+ if (len (self .single_channel_annots ) > 0 or any (overlap_has_sca )) and len (
2457
+ overlapping_regions
2458
+ ) > 0 :
2459
+ dur = self .getRegion ()[1 ] - self .getRegion ()[0 ]
2460
+ self .setRegion ((self .old_onset , self .old_onset + dur ))
2461
+ warn (
2462
+ "Can not combine channel-based annotations with "
2463
+ "any other annotation."
2464
+ )
2465
+ return
2466
+
2448
2467
# figure out new boundaries
2449
2468
regions_ = np .array (
2450
2469
[region .getRegion () for region in overlapping_regions ] + [self .getRegion ()]
2451
2470
)
2471
+
2472
+ self .regionChangeFinished .emit (self )
2473
+
2452
2474
onset = np .min (regions_ [:, 0 ])
2453
2475
offset = np .max (regions_ [:, 1 ])
2476
+
2477
+ self .old_onset = onset
2478
+
2454
2479
logger .debug (f"New { self .description } region: { onset :.2f} - { offset :.2f} " )
2455
2480
# remove overlapping regions
2456
2481
for region in overlapping_regions :
2457
2482
self .weakmain ()._remove_region (region , from_annot = False )
2458
2483
# re-set while blocking the signal to avoid re-running this function
2459
2484
with SignalBlocker (self ):
2460
2485
self .setRegion ((onset , offset ))
2486
+
2461
2487
self .update_label_pos ()
2462
2488
2463
2489
def _add_single_channel_annot (self , ch_name ):
@@ -2469,7 +2495,7 @@ def _remove_single_channel_annot(self, ch_name):
2469
2495
self .single_channel_annots [ch_name ].remove ()
2470
2496
self .single_channel_annots .pop (ch_name )
2471
2497
2472
- def _toggle_single_channel_annot (self , ch_name ):
2498
+ def _toggle_single_channel_annot (self , ch_name , update_color = True ):
2473
2499
"""Add or remove single channel annotations."""
2474
2500
# Exit if mne-python not updated to support shift-click
2475
2501
if not hasattr (self .weakmain (), "_toggle_single_channel_annotation" ):
@@ -2486,7 +2512,10 @@ def _toggle_single_channel_annot(self, ch_name):
2486
2512
else :
2487
2513
self ._remove_single_channel_annot (ch_name )
2488
2514
2489
- self .update_color (all_channels = (not list (self .single_channel_annots .keys ())))
2515
+ if update_color :
2516
+ self .update_color (
2517
+ all_channels = (not list (self .single_channel_annots .keys ()))
2518
+ )
2490
2519
2491
2520
def update_color (self , all_channels = True ):
2492
2521
"""Update color of annotation-region.
@@ -2539,6 +2568,7 @@ def update_visible(self, visible):
2539
2568
2540
2569
def remove (self ):
2541
2570
"""Remove annotation-region."""
2571
+ self .removeSingleChannelAnnots .emit (self )
2542
2572
self .removeRequested .emit (self )
2543
2573
vb = self .mne .viewbox
2544
2574
if vb and self .label_item in vb .addedItems :
0 commit comments