Example #1
0
    def test_find_peaks_window_size(self):
        """
        Verify that window_size is passed correctly to private function and
        affects the result.
        """
        sigmas = [2.0, 2.0]
        num_points = 1000
        test_data, act_locs = _gen_gaussians_even(sigmas, num_points)
        widths = np.arange(0.1, max(sigmas), 0.2)
        noise_amp = 0.05
        np.random.seed(18181911)
        test_data += (np.random.rand(num_points) - 0.5) * (2 * noise_amp)

        # Possibly contrived negative region to throw off peak finding
        # when window_size is too large
        test_data[250:320] -= 1

        found_locs = find_peaks_cwt(test_data,
                                    widths,
                                    gap_thresh=2,
                                    min_snr=3,
                                    min_length=None,
                                    window_size=None)
        with pytest.raises(AssertionError):
            assert found_locs.size == act_locs.size

        found_locs = find_peaks_cwt(test_data,
                                    widths,
                                    gap_thresh=2,
                                    min_snr=3,
                                    min_length=None,
                                    window_size=20)
        assert found_locs.size == act_locs.size
Example #2
0
    def test_find_peaks_withnoise(self):
        """
        Verify that peak locations are (approximately) found
        for a series of gaussians with added noise.
        """
        sigmas = [5.0, 3.0, 10.0, 20.0, 10.0, 50.0]
        num_points = 500
        test_data, act_locs = _gen_gaussians_even(sigmas, num_points)
        widths = np.arange(0.1, max(sigmas))
        noise_amp = 0.07
        np.random.seed(18181911)
        test_data += (np.random.rand(num_points) - 0.5) * (2 * noise_amp)
        found_locs = find_peaks_cwt(test_data,
                                    widths,
                                    min_length=15,
                                    gap_thresh=1,
                                    min_snr=noise_amp / 5)

        np.testing.assert_equal(
            len(found_locs), len(act_locs),
            'Different number' + 'of peaks found than expected')
        diffs = np.abs(found_locs - act_locs)
        max_diffs = np.array(sigmas) / 5
        np.testing.assert_array_less(
            diffs, max_diffs,
            'Maximum location differed' + 'by more than %s' % (max_diffs))
Example #3
0
    def test_find_peaks_with_one_width(self):
        """
        Verify that the `width` argument
        in `find_peaks_cwt` can be a float
        """
        xs = np.arange(0, np.pi, 0.05)
        test_data = np.sin(xs)
        widths = 1
        found_locs = find_peaks_cwt(test_data, widths)

        np.testing.assert_equal(found_locs, 32)
Example #4
0
 def test_find_peaks_nopeak(self):
     """
     Verify that no peak is found in
     data that's just noise.
     """
     noise_amp = 1.0
     num_points = 100
     np.random.seed(181819141)
     test_data = (np.random.rand(num_points) - 0.5)*(2*noise_amp)
     widths = np.arange(10, 50)
     found_locs = find_peaks_cwt(test_data, widths, min_snr=5, noise_perc=30)
     np.testing.assert_equal(len(found_locs), 0)
Example #5
0
 def test_find_peaks_exact(self):
     """
     Generate a series of gaussians and attempt to find the peak locations.
     """
     sigmas = [5.0, 3.0, 10.0, 20.0, 10.0, 50.0]
     num_points = 500
     test_data, act_locs = _gen_gaussians_even(sigmas, num_points)
     widths = np.arange(0.1, max(sigmas))
     found_locs = find_peaks_cwt(test_data, widths, gap_thresh=2, min_snr=0,
                                      min_length=None)
     np.testing.assert_array_equal(found_locs, act_locs,
                     "Found maximum locations did not equal those expected")
Example #6
0
 def test_find_peaks_nopeak(self):
     """
     Verify that no peak is found in
     data that's just noise.
     """
     noise_amp = 1.0
     num_points = 100
     np.random.seed(181819141)
     test_data = (np.random.rand(num_points) - 0.5)*(2*noise_amp)
     widths = np.arange(10, 50)
     found_locs = find_peaks_cwt(test_data, widths, min_snr=5, noise_perc=30)
     np.testing.assert_equal(len(found_locs), 0)
Example #7
0
 def test_find_peaks_exact(self):
     """
     Generate a series of gaussians and attempt to find the peak locations.
     """
     sigmas = [5.0, 3.0, 10.0, 20.0, 10.0, 50.0]
     num_points = 500
     test_data, act_locs = _gen_gaussians_even(sigmas, num_points)
     widths = np.arange(0.1, max(sigmas))
     found_locs = find_peaks_cwt(test_data, widths, gap_thresh=2, min_snr=0,
                                      min_length=None)
     np.testing.assert_array_equal(found_locs, act_locs,
                     "Found maximum locations did not equal those expected")
Example #8
0
    def test_find_peaks_withnoise(self):
        """
        Verify that peak locations are (approximately) found
        for a series of gaussians with added noise.
        """
        sigmas = [5.0, 3.0, 10.0, 20.0, 10.0, 50.0]
        num_points = 500
        test_data, act_locs = _gen_gaussians_even(sigmas, num_points)
        widths = np.arange(0.1, max(sigmas))
        noise_amp = 0.07
        np.random.seed(18181911)
        test_data += (np.random.rand(num_points) - 0.5)*(2*noise_amp)
        found_locs = find_peaks_cwt(test_data, widths, min_length=15,
                                         gap_thresh=1, min_snr=noise_amp / 5)

        np.testing.assert_equal(len(found_locs), len(act_locs), 'Different number' +
                                'of peaks found than expected')
        diffs = np.abs(found_locs - act_locs)
        max_diffs = np.array(sigmas) / 5
        np.testing.assert_array_less(diffs, max_diffs, 'Maximum location differed' +
                                     'by more than %s' % (max_diffs))
def find_peaks_2(s, eta=None, arc=None):
    """ 
    """
    logger.info('using calibration line location algorithm 2')
    s         = s[:-20] # Crop out the back portion
    s        -= np.amin(s)
    coeffs    = np.polyfit(np.arange(len(s)), s, 10)
    s_fit     = np.polyval(coeffs, np.arange(len(s)))
    sp        = s - s_fit
    if eta is not None or arc is not None:
        sp[0:20] = 0. # Set the first few pixels to zero since we don't want false lines
    sp       -= np.amin(sp)
    sp       -= np.median(sp)
    #print(np.median(sp), 1.1*np.median(sp))
    if eta is not None or arc is not None:
        #peaks_i   = argrelextrema(sp, np.greater, order=2)
        #print(peaks_i)
        peaks_i   = find_peaks_cwt(sp, [4,5,6,7,8])#, min_length=20)#, min_snr=3)
        #print('PEAKS', peaks_i)
    else:
        peaks_i   = argrelextrema(sp, np.greater)
    peaks_y   = sp[peaks_i]
    #for p,z in zip(peaks_i[0], peaks_y):
    #    print(p, z, z/peaks_y.mean())
#     big_peaks = peaks_i[0][np.where(peaks_y > 1.7 * peaks_y.mean())]
    if eta is not None:
        #big_peaks = peaks_i[np.where(peaks_y > 1.1 * sp.mean())]
        #big_peaks = peaks_i[np.where(peaks_y > 1.1 * np.median(sp))]
        big_peaks = peaks_i[np.where(peaks_y >= 0.15)] # XXX We could change this to use percentiles to estimate the cutoff
        #print(big_peaks)
    elif arc is not None:
        s         = s - np.median(s)
        peaks_y   = s[peaks_i]
        big_peaks = peaks_i[np.where(peaks_y >= 0.005)] # XXX We could change this to use percentiles to estimate the cutoff
    else:
        big_peaks = peaks_i[0][np.where(peaks_y > 1.4 * peaks_y.mean())]
    
    ### TESTING
    '''
    import pylab as pl
    pl.figure(figsize=(15, 5))
    pl.cla()
    if eta is not None or arc is not None: offsetPlot = 0
    else: offsetPlot = 20
    pl.plot(s + offsetPlot, 'r-')
    pl.plot(sp, 'k-')
    pl.plot(s_fit + offsetPlot, 'm-', alpha=0.5)
    for peak in peaks_i:
        if peak == peaks_i[0]: pl.axvline(peak, color='r', ls=':', lw=1.5, label='peaks')
        else: pl.axvline(peak, color='r', ls=':', lw=1.5)
    #pl.scatter(big_peaks, sp[big_peaks], color='C0')
    for peak in big_peaks:
        if peak == big_peaks[0]: pl.axvline(peak, color='b', ls='--', lw=0.9, label='big peaks')
        else: pl.axvline(peak, color='b', ls='--', lw=0.9)
    pl.legend()
    pl.show()
    #sys.exit()
    '''
    ### TESTING
    
    
    return(big_peaks)
Example #10
0
    def test_find_peaks_with_non_default_wavelets(self):
        x = gaussian(200, 2)
        widths = np.array([1, 2, 3, 4])
        a = find_peaks_cwt(x, widths, wavelet=gaussian)

        np.testing.assert_equal(np.array([100]), a)