def test_find_peaks_raises_typeerror_if_mask_is_wrong_type(): x = np.arange(0, 3200) stddev = 8 x0 = x.min() + 0.5 * x.ptp() g = models.Gaussian1D(mean=x0, stddev=stddev) y = g(x) mask = np.zeros_like(y) with pytest.raises(TypeError): peaks_detected, _ = tracing.find_peaks(y, np.ones_like(y) * stddev, mask=mask)
def test_find_peaks(): x = np.arange(0, 3200) y = np.zeros_like(x, dtype=float) n_peaks = 20 stddev = 8. peaks = np.linspace( x.min() + 0.05 * x.ptp(), x.max() - 0.05 * x.ptp(), n_peaks) for x0 in peaks: g = models.Gaussian1D(mean=x0, stddev=stddev) y += g(x) peaks_detected, _ = tracing.find_peaks(y, np.ones_like(y) * stddev) np.testing.assert_allclose(peaks_detected, peaks, atol=0.5)
def recalc_apertures(self): """ Recalculate the apertures based on the current set of fitter inputs. This will redo the aperture detection. Then it calls to each registered listener function and calls it with a list of N locations and N limits. """ max_apertures = self.max_apertures if not isinstance(max_apertures, int): max_apertures = int(max_apertures) # TODO: find_peaks might not be best considering we have no # idea whether sources will be extended or not widths = np.arange(3, 20) peaks_and_snrs = tracing.find_peaks(self.profile, widths, mask=self.prof_mask & DQ.not_signal, variance=1.0, reject_bad=False, min_snr=3, min_frac=0.2) if peaks_and_snrs.size == 0: self.locations = [] self.all_limits = [] else: # Reverse-sort by SNR and return only the locations self.locations = np.array( sorted(peaks_and_snrs.T, key=lambda x: x[1], reverse=True)[:max_apertures]).T[0] self.all_limits = tracing.get_limits(np.nan_to_num(self.profile), self.prof_mask, peaks=self.locations, threshold=self.threshold, method=self.sizing_method) for listener in self.recalc_listeners: listener(self.locations, self.all_limits) for l in self.listeners: for i, loclim in enumerate(zip(self.locations, self.all_limits)): loc = loclim[0] lim = loclim[1] l.handle_aperture(i + 1, loc, lim[0], lim[1])
def test_find_peaks(noise): x = np.arange(0, 3200) y = np.zeros_like(x, dtype=float) n_peaks = 20 stddev = 4. peaks = np.linspace(x.min() + 0.05 * x.ptp(), x.max() - 0.05 * x.ptp(), n_peaks) for x0 in peaks: g = models.Gaussian1D(mean=x0, stddev=stddev, amplitude=100) y += g(x) np.random.seed(0) y += (np.random.random(x.size) - 0.5) * noise peaks_detected, _ = tracing.find_peaks(y, np.ones_like(y) * stddev) np.testing.assert_allclose(peaks_detected, peaks, atol=1)