def generate_view(time, values, num_bins, bin_width, t_min, t_max, normalize=True): """Generates a view of a phase-folded and binned light curve. Args: time: 1D NumPy array of time values, sorted in ascending order. values: N-dimensional NumPy array with the same length as time. 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: NumPy array of length num_bins containing the aggregated values in uniformly spaced bins on the phase-folded time axis. """ view, bin_counts = binning.bin_and_aggregate(time, values, num_bins, bin_width, t_min, t_max) # Empty bins fall back to the global median. view = np.where(bin_counts > 0, view, np.median(values)) if normalize: view -= np.median(view, axis=0) view /= np.abs(np.min(view, axis=0)) return view
def test2DMax(self): x = np.array([-4, -2, -2, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 3]) y = np.array([ [0, 123], [-1, 0], [1, 2], [4, -4], [5, 4], [6, 6], [1, 50], [3, 100], [3, 100], [9, 110], [1, 0], [1, 0], [1, 0], [1, 0], [-2, 5], ]) result, bin_counts = bin_and_aggregate(x, y, num_bins=5, bin_width=2, x_min=-5, x_max=5, aggr_fn=np.max) np.testing.assert_array_almost_equal( [[0, 123], [1, 2], [6, 6], [9, 110], [1, 5]], result) np.testing.assert_array_equal([1, 2, 3, 4, 5], bin_counts)
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, bin_counts = bin_and_aggregate(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) np.testing.assert_array_equal([2, 2, 2, 2, 2], bin_counts)
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, bin_counts = bin_and_aggregate(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) np.testing.assert_array_equal([1, 1, 1, 1, 1], bin_counts)
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, bin_counts = bin_and_aggregate(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) np.testing.assert_array_equal([5, 6, 6, 6, 6], bin_counts)
def testMedianAggr(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, 1, 3, 3, 9, 1, 1, 1, 1, -2]) result, bin_counts = bin_and_aggregate(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) np.testing.assert_array_equal([1, 2, 3, 4, 5], bin_counts)
def _make_views(tce, time, values, global_view_nbins, global_view_bin_width_factor, local_view_nbins, local_view_bin_width_factor, local_view_num_durations, aggr_fn): """Creates global and local views with embeddings or flux.""" # Extract event attributes. period = tce["tce_period"] t0 = tce["tce_time0bk"] duration = tce["tce_duration"] t_min = -period / 2 t_max = period / 2 time, values = preprocess.phase_fold_and_sort_light_curve( time, values, period, t0) aggr_fn = getattr(np, aggr_fn) global_view, global_view_counts = binning.bin_and_aggregate( time, values, num_bins=global_view_nbins, bin_width=period * global_view_bin_width_factor, x_min=t_min, x_max=t_max, aggr_fn=aggr_fn) local_view, local_view_counts = binning.bin_and_aggregate( time, values, num_bins=local_view_nbins, bin_width=duration * local_view_bin_width_factor, x_min=max(t_min, -duration * local_view_num_durations), x_max=min(t_max, duration * local_view_num_durations), aggr_fn=aggr_fn) return { "global_view": (global_view, global_view_counts), "local_view": (local_view, local_view_counts), }
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, bin_counts = bin_and_aggregate(x, y, num_bins=7, bin_width=1, x_min=0, x_max=7) np.testing.assert_array_equal([0, 0, 0, 5, 3, 1, 5.5], result) np.testing.assert_array_equal([0, 1, 2, 3, 4, 5, 10], bin_counts)
def testErrors(self): # x size less than 2. x = [1] y = [2] with self.assertRaises(ValueError): bin_and_aggregate(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): bin_and_aggregate(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): bin_and_aggregate(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): bin_and_aggregate(x, y, num_bins=2, bin_width=0.25, x_min=3.5, x_max=4) # bin_width nonpositive. with self.assertRaises(ValueError): bin_and_aggregate(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): bin_and_aggregate(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): bin_and_aggregate(x, y, num_bins=1, bin_width=1, x_min=0, x_max=2)
def testDefaultXMinMax(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, bin_counts = bin_and_aggregate(x, y, num_bins=5) np.testing.assert_array_equal([7, 1, 5, 0, 3], result) np.testing.assert_array_equal([1, 2, 3, 0, 4], bin_counts)