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 read_light_curve(kepid, kepler_data_dir): """Reads a Kepler light curve. Args: kepid: Kepler id of the target star. kepler_data_dir: Base directory containing Kepler data. See kepler_io.kepler_filenames(). Returns: 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. Raises: IOError: If the light curve files for this Kepler ID cannot be found. """ # 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)) return kepler_io.read_kepler_light_curve(file_names)
def read_light_curve(kepid, kepler_data_dir): """Reads a Kepler light curve. Args: kepid: Kepler id of the target star. kepler_data_dir: Base directory containing Kepler data. See kepler_io.kepler_filenames(). Returns: 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. Raises: IOError: If the light curve files for this Kepler ID cannot be found. """ # 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 {} for Kepler ID {}".format( kepler_data_dir, kepid)) return kepler_io.read_kepler_light_curve(file_names)
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 testKeplerFilenames(self): # All quarters. filenames = kepler_io.kepler_filenames( "/my/dir/", 1234567, check_existence=False) self.assertItemsEqual([ "/my/dir/0012/001234567/kplr001234567-2009131105131_llc.fits", "/my/dir/0012/001234567/kplr001234567-2009166043257_llc.fits", "/my/dir/0012/001234567/kplr001234567-2009259160929_llc.fits", "/my/dir/0012/001234567/kplr001234567-2009350155506_llc.fits", "/my/dir/0012/001234567/kplr001234567-2010078095331_llc.fits", "/my/dir/0012/001234567/kplr001234567-2010009091648_llc.fits", "/my/dir/0012/001234567/kplr001234567-2010174085026_llc.fits", "/my/dir/0012/001234567/kplr001234567-2010265121752_llc.fits", "/my/dir/0012/001234567/kplr001234567-2010355172524_llc.fits", "/my/dir/0012/001234567/kplr001234567-2011073133259_llc.fits", "/my/dir/0012/001234567/kplr001234567-2011177032512_llc.fits", "/my/dir/0012/001234567/kplr001234567-2011271113734_llc.fits", "/my/dir/0012/001234567/kplr001234567-2012004120508_llc.fits", "/my/dir/0012/001234567/kplr001234567-2012088054726_llc.fits", "/my/dir/0012/001234567/kplr001234567-2012179063303_llc.fits", "/my/dir/0012/001234567/kplr001234567-2012277125453_llc.fits", "/my/dir/0012/001234567/kplr001234567-2013011073258_llc.fits", "/my/dir/0012/001234567/kplr001234567-2013098041711_llc.fits", "/my/dir/0012/001234567/kplr001234567-2013131215648_llc.fits" ], filenames) # Subset of quarters. filenames = kepler_io.kepler_filenames( "/my/dir/", 1234567, quarters=[3, 4], check_existence=False) self.assertItemsEqual([ "/my/dir/0012/001234567/kplr001234567-2009350155506_llc.fits", "/my/dir/0012/001234567/kplr001234567-2010078095331_llc.fits", "/my/dir/0012/001234567/kplr001234567-2010009091648_llc.fits" ], filenames) # Injected group. filenames = kepler_io.kepler_filenames( "/my/dir/", 1234567, quarters=[3, 4], injected_group="inj1", check_existence=False) # pylint:disable=line-too-long self.assertItemsEqual([ "/my/dir/0012/001234567/kplr001234567-2009350155506_INJECTED-inj1_llc.fits", "/my/dir/0012/001234567/kplr001234567-2010078095331_INJECTED-inj1_llc.fits", "/my/dir/0012/001234567/kplr001234567-2010009091648_INJECTED-inj1_llc.fits" ], filenames) # pylint:enable=line-too-long # Short cadence. filenames = kepler_io.kepler_filenames( "/my/dir/", 1234567, long_cadence=False, quarters=[0, 1], check_existence=False) self.assertItemsEqual([ "/my/dir/0012/001234567/kplr001234567-2009131110544_slc.fits", "/my/dir/0012/001234567/kplr001234567-2009166044711_slc.fits" ], filenames) # Check existence. filenames = kepler_io.kepler_filenames( self.data_dir, 11442793, check_existence=True) expected_filenames = [ os.path.join(self.data_dir, "0114/011442793/kplr011442793-%s_llc.fits") % q for q in ["2009350155506", "2010009091648", "2010174085026"] ] self.assertItemsEqual(expected_filenames, filenames)
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 _process_tce(feature_config): """Reads and process the input features of a Threshold Crossing Event. Args: feature_config: ConfigDict containing the feature configurations. Returns: A dictionary of processed light curve features. Raises: ValueError: If feature_config contains features other than 'global_view' and 'local_view'. """ if not {"global_view", "local_view"}.issuperset(feature_config.keys()): raise ValueError( "Only 'global_view' and 'local_view' features are supported.") kep_id = FLAGS.kepler_id filenames = kepler_io.kepler_filenames('data/kepler', kep_id) all_time, all_flux = kepler_io.read_kepler_light_curve(filenames) for f in all_flux: f /= np.median(f) plt.figure(figsize=(12, 6), dpi=80) plt.scatter(np.concatenate(all_time), np.concatenate(all_flux), s=0.5, cmap='plasma') plt.title('Entire Kepler Mission Data for KEP_ID=' + str(kep_id)) plt.xlabel('Time(days)') plt.ylabel('Brightness') plt.show() # Read and process the light curve. time, flux, spline = preprocess.read_and_process_light_curve( FLAGS.kepler_id, FLAGS.kepler_data_dir) #plt.figure(figsize=(12, 6), dpi=80) #plt.scatter(np.concatenate(all_time), np.concatenate(all_flux), s=0.5, cmap='plasma') plt.plot(spline, 'r') plt.title('Best fit spline') #plt.xlabel('Time(days)') #plt.ylabel('Brightness') #plt.show() plt.figure(figsize=(12, 6), dpi=80) plt.scatter(time, flux, s=0.5, cmap='plasma') plt.title('Divided by best fit spline') plt.xlabel('Time(days)') plt.ylabel('Brightness') plt.show() time, flux = preprocess.phase_fold_and_sort_light_curve( time, flux, FLAGS.period, FLAGS.t0) # Generate the local and global views. features = {} if "global_view" in feature_config: global_view = preprocess.global_view(time, flux, FLAGS.period) # Add a batch dimension. features["global_view"] = np.expand_dims(global_view, 0) if "local_view" in feature_config: local_view = preprocess.local_view(time, flux, FLAGS.period, FLAGS.duration) # Add a batch dimension. features["local_view"] = np.expand_dims(local_view, 0) plt.figure(figsize=(12, 6), dpi=80) plt.plot(range(201), local_view, 'o') plt.title('Folded local view of target TCE') plt.xlabel('Time(days)') plt.ylabel('Brightness') plt.show() # Possibly save plots. if FLAGS.output_image_file: ncols = len(features) fig, axes = plt.subplots(1, ncols, figsize=(10 * ncols, 5), squeeze=False) for i, name in enumerate(sorted(features)): ax = axes[0][i] ax.plot(features[name][0], ".") ax.set_title(name) ax.set_xlabel("Bucketized Time (days)") ax.set_ylabel("Normalized Flux") fig.tight_layout() fig.savefig(FLAGS.output_image_file, bbox_inches="tight") return features