def testSplit(self): # Single segment. all_time = np.concatenate([np.arange(0, 1, 0.1), np.arange(1.5, 2, 0.1)]) all_flux = np.ones(15) # Gap width 0.5. split_time, split_flux = util.split(all_time, all_flux, gap_width=0.5) self.assertLen(split_time, 2) self.assertLen(split_flux, 2) self.assertSequenceAlmostEqual(np.arange(0, 1, 0.1), split_time[0]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[0]) self.assertSequenceAlmostEqual(np.arange(1.5, 2, 0.1), split_time[1]) self.assertSequenceAlmostEqual(np.ones(5), split_flux[1]) # Multi segment. all_time = [ np.concatenate([ np.arange(0, 1, 0.1), np.arange(1.5, 2, 0.1), np.arange(3, 4, 0.1) ]), np.arange(4, 5, 0.1) ] all_flux = [np.ones(25), np.ones(10)] self.assertEqual(len(all_time), 2) self.assertEqual(len(all_time[0]), 25) self.assertEqual(len(all_time[1]), 10) self.assertEqual(len(all_flux), 2) self.assertEqual(len(all_flux[0]), 25) self.assertEqual(len(all_flux[1]), 10) # Gap width 0.5. split_time, split_flux = util.split(all_time, all_flux, gap_width=0.5) self.assertLen(split_time, 4) self.assertLen(split_flux, 4) self.assertSequenceAlmostEqual(np.arange(0, 1, 0.1), split_time[0]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[0]) self.assertSequenceAlmostEqual(np.arange(1.5, 2, 0.1), split_time[1]) self.assertSequenceAlmostEqual(np.ones(5), split_flux[1]) self.assertSequenceAlmostEqual(np.arange(3, 4, 0.1), split_time[2]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[2]) self.assertSequenceAlmostEqual(np.arange(4, 5, 0.1), split_time[3]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[3]) # Gap width 1.0. split_time, split_flux = util.split(all_time, all_flux, gap_width=1) self.assertLen(split_time, 3) self.assertLen(split_flux, 3) self.assertSequenceAlmostEqual([ 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.5, 1.6, 1.7, 1.8, 1.9 ], split_time[0]) self.assertSequenceAlmostEqual(np.ones(15), split_flux[0]) self.assertSequenceAlmostEqual(np.arange(3, 4, 0.1), split_time[1]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[1]) self.assertSequenceAlmostEqual(np.arange(4, 5, 0.1), split_time[2]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[2])
def read_and_process_light_curve(kepid, kepler_data_dir, max_gap_width=0.75): """Reads a light curve, fits a B-spline and divides the curve by the spline. Args: kepid: Kepler id of the target star. kepler_data_dir: Base directory containing Kepler data. See kepler_io.kepler_filenames(). max_gap_width: Gap size (in days) above which the light curve is split for the fitting of B-splines. Returns: time: 1D NumPy array; the time values of the light curve. flux: 1D NumPy array; the normalized flux values of the light curve. Raises: IOError: If the light curve files for this Kepler ID cannot be found. ValueError: If the spline could not be fit. """ # Read the Kepler light curve. file_names = kepler_io.kepler_filenames(kepler_data_dir, kepid) if not file_names: raise IOError("Failed to find .fits files in %s for Kepler ID %s" % (kepler_data_dir, kepid)) all_time, all_flux = kepler_io.read_kepler_light_curve(file_names) # Split on gaps. all_time, all_flux = util.split(all_time, all_flux, gap_width=max_gap_width) # Logarithmically sample candidate break point spacings between 0.5 and 20 # days. bkspaces = np.logspace(np.log10(0.5), np.log10(20), num=20) # Generate spline. spline = kepler_spline.choose_kepler_spline( all_time, all_flux, bkspaces, penalty_coeff=1.0, verbose=False)[0] if spline is None: raise ValueError("Failed to fit spline with Kepler ID %s", kepid) # Concatenate the piecewise light curve and spline. time = np.concatenate(all_time) flux = np.concatenate(all_flux) spline = np.concatenate(spline) # In rare cases the piecewise spline contains NaNs in places the spline could # not be fit. We can't normalize those points if the spline isn't defined # there. Instead we just remove them. finite_i = np.isfinite(spline) if not np.all(finite_i): tf.logging.warn("Incomplete spline with Kepler ID %s", kepid) time = time[finite_i] flux = flux[finite_i] spline = spline[finite_i] # "Flatten" the light curve (remove low-frequency variability) by dividing by # the spline. flux /= spline return time, flux
def testSplit(self): all_time = [ np.concatenate([ np.arange(0, 1, 0.1), np.arange(1.5, 2, 0.1), np.arange(3, 4, 0.1) ]) ] all_flux = [np.array([1] * 25)] # Gap width 0.5. split_time, split_flux = util.split(all_time, all_flux, gap_width=0.5) self.assertLen(split_time, 3) self.assertLen(split_flux, 3) self.assertSequenceAlmostEqual( [0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9], split_time[0]) self.assertSequenceAlmostEqual([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], split_flux[0]) self.assertSequenceAlmostEqual([1.5, 1.6, 1.7, 1.8, 1.9], split_time[1]) self.assertSequenceAlmostEqual([1, 1, 1, 1, 1], split_flux[1]) self.assertSequenceAlmostEqual( [3., 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9], split_time[2]) self.assertSequenceAlmostEqual([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], split_flux[2]) # Gap width 1.0. split_time, split_flux = util.split(all_time, all_flux, gap_width=1) self.assertLen(split_time, 2) self.assertLen(split_flux, 2) self.assertSequenceAlmostEqual([ 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.5, 1.6, 1.7, 1.8, 1.9 ], split_time[0]) self.assertSequenceAlmostEqual( [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], split_flux[0]) self.assertSequenceAlmostEqual( [3., 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9], split_time[1]) self.assertSequenceAlmostEqual([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], split_flux[1])
def process_light_curve(all_time, all_flux): """Removes low-frequency variability from a light curve. Args: all_time: A list of numpy arrays; the time values of the raw light curve. all_flux: A list of numpy arrays corresponding to the time arrays in all_time. Returns: time: 1D NumPy array; the time values of the light curve. flux: 1D NumPy array; the normalized flux values of the light curve. """ # Split on gaps. all_time, all_flux = util.split(all_time, all_flux, gap_width=0.75) # Fit a piecewise-cubic spline with default arguments. spline = kepler_spline.fit_kepler_spline(all_time, all_flux, verbose=False)[0] # Concatenate the piecewise light curve and spline. time = np.concatenate(all_time) flux = np.concatenate(all_flux) spline = np.concatenate(spline) # In rare cases the piecewise spline contains NaNs in places the spline could # not be fit. We can't normalize those points if the spline isn't defined # there. Instead we just remove them. finite_i = np.isfinite(spline) if not np.all(finite_i): time = time[finite_i] flux = flux[finite_i] spline = spline[finite_i] # "Flatten" the light curve (remove low-frequency variability) by dividing by # the spline. flux /= spline return time, flux
def read_and_process_light_curve(kepid, kepler_data_dir, campaign, max_gap_width=0.75): """Reads a light curve, fits a B-spline and divides the curve by the spline. Args: kepid: Kepler id of the target star. kepler_data_dir: Base directory containing Kepler data. See kepler_io.kepler_filenames(). campaign: K2 campaign where data was taken. max_gap_width: Gap size (in days) above which the light curve is split for the fitting of B-splines. Returns: time: 1D NumPy array; the time values of the light curve. flux: 1D NumPy array; the normalized flux values of the light curve. Raises: IOError: If the light curve files for this Kepler ID cannot be found. ValueError: If the spline could not be fit. """ # Read the Kepler light curve. file_names = kepler_io.kepler_filenames(kepler_data_dir, kepid, campaign) if not file_names: print(campaign) raise IOError("Failed to find .idl file in %s for EPIC ID %s" % (kepler_data_dir, kepid)) all_time, all_flux = kepler_io.read_kepler_light_curve(file_names) # Split on gaps. all_time, all_flux = util.split(all_time, all_flux, gap_width=max_gap_width) # Logarithmically sample candidate break point spacings between 0.5 and 20 # days. bkspaces = np.logspace(np.log10(0.5), np.log10(20), num=20) # Generate spline. spline = kepler_spline.choose_kepler_spline( all_time, all_flux, bkspaces, penalty_coeff=1.0, verbose=False)[0] if spline is None: raise ValueError("Failed to fit spline with Kepler ID %s", kepid) # Concatenate the piecewise light curve and spline. time = np.concatenate(all_time) flux = np.concatenate(all_flux) spline = np.concatenate(spline) # In rare cases the piecewise spline contains NaNs in places the spline could # not be fit. We can't normalize those points if the spline isn't defined # there. Instead we just remove them. finite_i = np.isfinite(spline) if not np.all(finite_i): tf.logging.warn("Incomplete spline with Kepler ID %s", kepid) time = time[finite_i] flux = flux[finite_i] spline = spline[finite_i] # "Flatten" the light curve (remove low-frequency variability) by dividing by # the spline. flux /= spline #Remove points where the thrusters are on #using s.data.moving #Remove points where the xcenter is off #using.s.data.xc #Remove points where the background flux is off #using s.data.medians #Let's remove upward outliers? deviation = flux - np.median(flux) is_upward_outlier = np.logical_not(robust_mean.robust_mean(deviation, cut=3)[2]) np.logical_and(is_upward_outlier, deviation > 0, out=is_upward_outlier) flux = flux[~is_upward_outlier] time = time[~is_upward_outlier] return time, flux
def testSplit(self): # Single segment. all_time = np.concatenate( [np.arange(0, 1, 0.1), np.arange(1.5, 2, 0.1)]) all_flux = np.ones(15) # Gap width 0.5. split_time, split_flux = util.split(all_time, all_flux, gap_width=0.5) self.assertLen(split_time, 2) self.assertLen(split_flux, 2) self.assertSequenceAlmostEqual(np.arange(0, 1, 0.1), split_time[0]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[0]) self.assertSequenceAlmostEqual(np.arange(1.5, 2, 0.1), split_time[1]) self.assertSequenceAlmostEqual(np.ones(5), split_flux[1]) # Multi segment. all_time = [ np.concatenate([ np.arange(0, 1, 0.1), np.arange(1.5, 2, 0.1), np.arange(3, 4, 0.1) ]), np.arange(4, 5, 0.1) ] all_flux = [np.ones(25), np.ones(10)] self.assertEqual(len(all_time), 2) self.assertEqual(len(all_time[0]), 25) self.assertEqual(len(all_time[1]), 10) self.assertEqual(len(all_flux), 2) self.assertEqual(len(all_flux[0]), 25) self.assertEqual(len(all_flux[1]), 10) # Gap width 0.5. split_time, split_flux = util.split(all_time, all_flux, gap_width=0.5) self.assertLen(split_time, 4) self.assertLen(split_flux, 4) self.assertSequenceAlmostEqual(np.arange(0, 1, 0.1), split_time[0]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[0]) self.assertSequenceAlmostEqual(np.arange(1.5, 2, 0.1), split_time[1]) self.assertSequenceAlmostEqual(np.ones(5), split_flux[1]) self.assertSequenceAlmostEqual(np.arange(3, 4, 0.1), split_time[2]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[2]) self.assertSequenceAlmostEqual(np.arange(4, 5, 0.1), split_time[3]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[3]) # Gap width 1.0. split_time, split_flux = util.split(all_time, all_flux, gap_width=1) self.assertLen(split_time, 3) self.assertLen(split_flux, 3) self.assertSequenceAlmostEqual([ 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.5, 1.6, 1.7, 1.8, 1.9 ], split_time[0]) self.assertSequenceAlmostEqual(np.ones(15), split_flux[0]) self.assertSequenceAlmostEqual(np.arange(3, 4, 0.1), split_time[1]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[1]) self.assertSequenceAlmostEqual(np.arange(4, 5, 0.1), split_time[2]) self.assertSequenceAlmostEqual(np.ones(10), split_flux[2])
def read_and_process_light_curve(kepid, kepler_data_dir, max_gap_width=0.75): """Reads a light curve, fits a B-spline and divides the curve by the spline. Args: kepid: Kepler id of the target star. kepler_data_dir: Base directory containing Kepler data. See kepler_io.kepler_filenames(). max_gap_width: Gap size (in days) above which the light curve is split for the fitting of B-splines. Returns: time: 1D NumPy array; the time values of the light curve. flux: 1D NumPy array; the normalized flux values of the light curve. Raises: IOError: If the light curve files for this Kepler ID cannot be found. ValueError: If the spline could not be fit. """ # Read the Kepler light curve. file_names = kepler_io.kepler_filenames(kepler_data_dir, kepid) if not file_names: raise IOError("Failed to find .fits files in %s for Kepler ID %s" % (kepler_data_dir, kepid)) all_time, all_flux = kepler_io.read_kepler_light_curve(file_names) # Split on gaps. all_time, all_flux = util.split(all_time, all_flux, gap_width=max_gap_width) # Logarithmically sample candidate break point spacings between 0.5 and 20 # days. bkspaces = np.logspace(np.log10(0.5), np.log10(20), num=20) # Generate spline. spline = kepler_spline.choose_kepler_spline(all_time, all_flux, bkspaces, penalty_coeff=1.0, verbose=False)[0] if spline is None: raise ValueError("Failed to fit spline with Kepler ID %s", kepid) # Concatenate the piecewise light curve and spline. time = np.concatenate(all_time) flux = np.concatenate(all_flux) spline = np.concatenate(spline) # In rare cases the piecewise spline contains NaNs in places the spline could # not be fit. We can't normalize those points if the spline isn't defined # there. Instead we just remove them. finite_i = np.isfinite(spline) if not np.all(finite_i): tf.logging.warn("Incomplete spline with Kepler ID %s", kepid) time = time[finite_i] flux = flux[finite_i] spline = spline[finite_i] # "Flatten" the light curve (remove low-frequency variability) by dividing by # the spline. flux /= spline return time, flux