@@ -35,7 +35,10 @@ def __init__(self, data):
35
35
self .fig .canvas .mpl_connect ('scroll_event' , self .on_wheel )
36
36
self .fig .canvas .mpl_connect ('key_press_event' , self .on_key )
37
37
38
- # three selectors for rejection (left mouse), addition (central mouse), and deletion (right mouse)
38
+ # three selectors for:
39
+ # 1. rejection (left mouse),
40
+ # 2. addition (central mouse), and
41
+ # 3. deletion (right mouse)
39
42
reject = functools .partial (self .on_edit , reject = True )
40
43
delete = functools .partial (self .on_edit , reject = False )
41
44
include = functools .partial (self .on_edit , reject = False , insert = True )
@@ -98,19 +101,21 @@ def on_edit(self, xmin, xmax, *, reject=False, insert=False):
98
101
"""
99
102
100
103
if reject and insert :
101
- raise Exception ( 'Selected to both reject and insert! Program goes kaput. ' )
104
+ raise ValueError ( 'Cannot select both reject and insert!' )
102
105
103
106
tmin , tmax = np .searchsorted (self .time , (xmin , xmax ))
104
107
pmin , pmax = np .searchsorted (self .data .peaks , (tmin , tmax ))
105
108
106
109
if insert :
107
- temp_peak = np .argmax (self .data .data [tmin :tmax ])
108
- if temp_peak == 0 :
110
+ tmp = np .argmax (self .data .data [tmin :tmax ]) if tmin != tmax else 0
111
+ newpeak = tmin + tmp
112
+ if newpeak == tmin :
113
+ self .plot_signals ()
109
114
return
110
- newpeak = [tmin + temp_peak ]
111
115
else :
112
116
bad = np .arange (pmin , pmax , dtype = int )
113
117
if len (bad ) == 0 :
118
+ self .plot_signals ()
114
119
return
115
120
116
121
if reject :
@@ -121,7 +126,7 @@ def on_edit(self, xmin, xmax, *, reject=False, insert=False):
121
126
# store edits in local history & call function
122
127
if insert :
123
128
self .included .add (newpeak )
124
- self .data = operations .add_peaks (self .data , newpeak , pmin )
129
+ self .data = operations .add_peaks (self .data , newpeak )
125
130
else :
126
131
rej .update (self .data .peaks [bad ].tolist ())
127
132
self .data = fcn (self .data , self .data .peaks [bad ])
@@ -131,7 +136,8 @@ def on_edit(self, xmin, xmax, *, reject=False, insert=False):
131
136
def undo (self ):
132
137
""" Resets last span select peak removal """
133
138
# check if last history entry was a manual reject / delete
134
- if self .data ._history [- 1 ][0 ] not in ['reject_peaks' , 'delete_peaks' ]:
139
+ relevant = ['reject_peaks' , 'delete_peaks' , 'add_peaks' ]
140
+ if self .data ._history [- 1 ][0 ] not in relevant :
135
141
return
136
142
137
143
# pop off last edit and delete
@@ -149,7 +155,12 @@ def undo(self):
149
155
peaks ['remove' ]
150
156
)
151
157
self .deleted .difference_update (peaks ['remove' ])
152
-
158
+ elif func == 'add_peaks' :
159
+ self .data ._metadata ['peaks' ] = np .delete (
160
+ self .data ._metadata ['peaks' ],
161
+ np .searchsorted (self .data ._metadata ['peaks' ], peaks ['add' ]),
162
+ )
163
+ self .included .remove (peaks ['add' ])
153
164
self .data ._metadata ['troughs' ] = utils .check_troughs (self .data ,
154
165
self .data .peaks )
155
166
self .plot_signals ()
0 commit comments