def generate_view(time, flux, num_bins, bin_width, t_min, t_max, normalize=True): """Generates a view of a phase-folded light curve using a median filter. Args: time: 1D array of time values, sorted in ascending order. flux: 1D array of flux values. num_bins: The number of intervals to divide the time axis into. bin_width: The width of each bin on the time axis. t_min: The inclusive leftmost value to consider on the time axis. t_max: The exclusive rightmost value to consider on the time axis. normalize: Whether to center the median at 0 and minimum value at -1. Returns: 1D NumPy array of size num_bins containing the median flux values of uniformly spaced bins on the phase-folded time axis. """ view = median_filter.median_filter(time, flux, num_bins, bin_width, t_min, t_max) if normalize: view -= np.median(view) view /= np.abs(np.min(view)) return view
def testBucketBoundaries(self): x = np.array([-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]) y = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]) result = median_filter.median_filter(x, y, num_bins=5, bin_width=2, x_min=-5, x_max=5) np.testing.assert_array_equal([2.5, 4.5, 6.5, 8.5, 10.5], result)
def testEmptyBins(self): x = np.array([-1, 0, 1]) y = np.array([1, 2, 3]) result = median_filter.median_filter(x, y, num_bins=5, bin_width=2, x_min=-5, x_max=5) np.testing.assert_array_equal([2, 2, 1.5, 3, 2], result)
def testNarrowBins(self): x = np.array([-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]) y = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]) result = median_filter.median_filter(x, y, num_bins=5, bin_width=1, x_min=-4.5, x_max=4.5) np.testing.assert_array_equal([3, 5, 7, 9, 11], result)
def testWideBins(self): x = np.array([-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]) y = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]) result = median_filter.median_filter(x, y, num_bins=5, bin_width=6, x_min=-7, x_max=7) np.testing.assert_array_equal([3, 4.5, 6.5, 8.5, 10.5], result)
def testMedian(self): x = np.array([-4, -2, -2, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 3]) y = np.array([0, -1, 1, 4, 5, 6, 2, 2, 4, 4, 1, 1, 1, 1, -1]) result = median_filter.median_filter(x, y, num_bins=5, bin_width=2, x_min=-5, x_max=5) np.testing.assert_array_equal([0, 0, 5, 3, 1], result)
def testMultiSizeBins(self): # Construct bins with size 0, 1, 2, 3, 4, 5, 10, respectively. x = np.array([ 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 ]) y = np.array([ 0, -1, 1, 4, 5, 6, 2, 2, 4, 4, 1, 1, 1, 1, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]) result = median_filter.median_filter( x, y, num_bins=7, bin_width=1, x_min=0, x_max=7) np.testing.assert_array_equal([3, 0, 0, 5, 3, 1, 5.5], result)
def testErrors(self): # x size less than 2. x = [1] y = [2] with self.assertRaises(ValueError): median_filter.median_filter( x, y, num_bins=2, bin_width=1, x_min=0, x_max=2) # x and y not the same size. x = [1, 2] y = [4, 5, 6] with self.assertRaises(ValueError): median_filter.median_filter( x, y, num_bins=2, bin_width=1, x_min=0, x_max=2) # x_min not less than x_max. x = [1, 2, 3] with self.assertRaises(ValueError): median_filter.median_filter( x, y, num_bins=2, bin_width=1, x_min=-1, x_max=-1) # x_min greater than the last element of x. with self.assertRaises(ValueError): median_filter.median_filter( x, y, num_bins=2, bin_width=0.25, x_min=3.5, x_max=4) # bin_width nonpositive. with self.assertRaises(ValueError): median_filter.median_filter( x, y, num_bins=2, bin_width=0, x_min=1, x_max=3) # bin_width greater than or equal to x_max - x_min. with self.assertRaises(ValueError): median_filter.median_filter( x, y, num_bins=2, bin_width=1, x_min=1.5, x_max=2.5) # num_bins less than 2. x = [1, 2, 3] with self.assertRaises(ValueError): median_filter.median_filter( x, y, num_bins=1, bin_width=1, x_min=0, x_max=2)
def testDefaultArgs(self): x = np.array([-4, -2, -2, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 3]) y = np.array([7, -1, 3, 4, 5, 6, 2, 2, 4, 4, 1, 1, 1, 1, -1]) result = median_filter.median_filter(x, y, num_bins=5) np.testing.assert_array_equal([7, 1, 5, 2, 3], result)