@@ -238,14 +238,10 @@ def _reset_internal_settings(self):
238238 # Bandwidth limits are given in 2-sided peak bandwidth
239239 # Convert to gaussian std parameter limits
240240 self ._gauss_std_limits = tuple ([bwl / 2 for bwl in self .peak_width_limits ])
241- # Bounds for aperiodic fitting. Drops bounds on knee parameter if not set to fit knee
242- self ._ap_bounds = self ._ap_bounds if self .aperiodic_mode == 'knee' \
243- else tuple (bound [0 ::2 ] for bound in self ._ap_bounds )
244241
245242 # Otherwise, assume settings are unknown (have been cleared) and set to None
246243 else :
247244 self ._gauss_std_limits = None
248- self ._ap_bounds = None
249245
250246
251247 def _reset_data_results (self , clear_freqs = False , clear_spectrum = False , clear_results = False ):
@@ -286,7 +282,7 @@ def _reset_data_results(self, clear_freqs=False, clear_spectrum=False, clear_res
286282 self ._peak_fit = None
287283
288284
289- def add_data (self , freqs , power_spectrum , freq_range = None ):
285+ def add_data (self , freqs , power_spectrum , freq_range = None , clear_results = True ):
290286 """Add data (frequencies, and power spectrum values) to the current object.
291287
292288 Parameters
@@ -298,17 +294,22 @@ def add_data(self, freqs, power_spectrum, freq_range=None):
298294 freq_range : list of [float, float], optional
299295 Frequency range to restrict power spectrum to.
300296 If not provided, keeps the entire range.
297+ clear_results : bool, optional, default: True
298+ Whether to clear prior results, if any are present in the object.
299+ This should only be set to False if data for the current results are being re-added.
301300
302301 Notes
303302 -----
304303 If called on an object with existing data and/or results
305304 they will be cleared by this method call.
306305 """
307306
308- # If any data is already present, then clear data & results
307+ # If any data is already present, then clear previous data
308+ # Also clear results, if present, unless indicated not to
309309 # This is to ensure object consistency of all data & results
310- if np .any (self .freqs ):
311- self ._reset_data_results (True , True , True )
310+ self ._reset_data_results (clear_freqs = self .has_data ,
311+ clear_spectrum = self .has_data ,
312+ clear_results = self .has_model and clear_results )
312313
313314 self .freqs , self .power_spectrum , self .freq_range , self .freq_res = \
314315 self ._prepare_data (freqs , power_spectrum , freq_range , 1 , self .verbose )
@@ -717,6 +718,10 @@ def _simple_ap_fit(self, freqs, power_spectrum):
717718 np .log10 (self .freqs [- 1 ]) - np .log10 (self .freqs [0 ]))
718719 if not self ._ap_guess [2 ] else self ._ap_guess [2 ]]
719720
721+ # Get bounds for aperiodic fitting, dropping knee bound if not set to fit knee
722+ ap_bounds = self ._ap_bounds if self .aperiodic_mode == 'knee' \
723+ else tuple (bound [0 ::2 ] for bound in self ._ap_bounds )
724+
720725 # Collect together guess parameters
721726 guess = np .array ([off_guess + kne_guess + exp_guess ])
722727
@@ -729,7 +734,7 @@ def _simple_ap_fit(self, freqs, power_spectrum):
729734 warnings .simplefilter ("ignore" )
730735 aperiodic_params , _ = curve_fit (get_ap_func (self .aperiodic_mode ),
731736 freqs , power_spectrum , p0 = guess ,
732- maxfev = self ._maxfev , bounds = self . _ap_bounds )
737+ maxfev = self ._maxfev , bounds = ap_bounds )
733738 except RuntimeError :
734739 raise FitError ("Model fitting failed due to not finding parameters in "
735740 "the simple aperiodic component fit." )
@@ -774,14 +779,18 @@ def _robust_ap_fit(self, freqs, power_spectrum):
774779 freqs_ignore = freqs [perc_mask ]
775780 spectrum_ignore = power_spectrum [perc_mask ]
776781
782+ # Get bounds for aperiodic fitting, dropping knee bound if not set to fit knee
783+ ap_bounds = self ._ap_bounds if self .aperiodic_mode == 'knee' \
784+ else tuple (bound [0 ::2 ] for bound in self ._ap_bounds )
785+
777786 # Second aperiodic fit - using results of first fit as guess parameters
778787 # See note in _simple_ap_fit about warnings
779788 try :
780789 with warnings .catch_warnings ():
781790 warnings .simplefilter ("ignore" )
782791 aperiodic_params , _ = curve_fit (get_ap_func (self .aperiodic_mode ),
783792 freqs_ignore , spectrum_ignore , p0 = popt ,
784- maxfev = self ._maxfev , bounds = self . _ap_bounds )
793+ maxfev = self ._maxfev , bounds = ap_bounds )
785794 except RuntimeError :
786795 raise FitError ("Model fitting failed due to not finding "
787796 "parameters in the robust aperiodic fit." )
@@ -851,7 +860,7 @@ def _fit_peaks(self, flat_iter):
851860 guess_std = compute_gauss_std (fwhm )
852861
853862 except ValueError :
854- # This procedure can fail (extremely rarely), if both le & ri ind's end up as None
863+ # This procedure can fail (very rarely), if both left & right inds end up as None
855864 # In this case, default the guess to the average of the peak width limits
856865 guess_std = np .mean (self .peak_width_limits )
857866
@@ -1027,21 +1036,21 @@ def _drop_peak_overlap(self, guess):
10271036 Notes
10281037 -----
10291038 For any gaussians with an overlap that crosses the threshold,
1030- the lowest height guess guassian is dropped.
1039+ the lowest height guess Gaussian is dropped.
10311040 """
10321041
1033- # Sort the peak guesses by increasing frequency, so adjacenent peaks can
1034- # be compared from right to left.
1042+ # Sort the peak guesses by increasing frequency
1043+ # This is so adjacent peaks can be compared from right to left
10351044 guess = sorted (guess , key = lambda x : float (x [0 ]))
10361045
10371046 # Calculate standard deviation bounds for checking amount of overlap
1038- # The bounds are the gaussian frequncy +/- gaussian standard deviation
1047+ # The bounds are the gaussian frequency +/- gaussian standard deviation
10391048 bounds = [[peak [0 ] - peak [2 ] * self ._gauss_overlap_thresh ,
10401049 peak [0 ] + peak [2 ] * self ._gauss_overlap_thresh ] for peak in guess ]
10411050
10421051 # Loop through peak bounds, comparing current bound to that of next peak
10431052 # If the left peak's upper bound extends pass the right peaks lower bound,
1044- # Then drop the guassian with the lower height.
1053+ # then drop the Gaussian with the lower height
10451054 drop_inds = []
10461055 for ind , b_0 in enumerate (bounds [:- 1 ]):
10471056 b_1 = bounds [ind + 1 ]
0 commit comments