Esempio n. 1
0
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 testReadKeplerLightCurve(self):
   filenames = [
       os.path.join(self.data_dir, "0114/011442793/kplr011442793-%s_llc.fits")
       % q for q in ["2009350155506", "2010009091648", "2010174085026"]
   ]
   all_time, all_flux = kepler_io.read_kepler_light_curve(filenames)
   self.assertLen(all_time, 3)
   self.assertLen(all_flux, 3)
   self.assertLen(all_time[0], 4134)
   self.assertLen(all_flux[0], 4134)
   self.assertLen(all_time[1], 1008)
   self.assertLen(all_flux[1], 1008)
   self.assertLen(all_time[2], 4486)
   self.assertLen(all_flux[2], 4486)
Esempio n. 3
0
    def testReadKeplerLightCurve(self):
        filenames = [
            os.path.join(self.data_dir,
                         "0114/011442793/kplr011442793-{}_llc.fits".format(q))
            for q in ["2009350155506", "2010009091648", "2010174085026"]
        ]
        all_time, all_flux = kepler_io.read_kepler_light_curve(filenames)
        self.assertLen(all_time, 3)
        self.assertLen(all_flux, 3)
        self.assertLen(all_time[0], 4134)
        self.assertLen(all_flux[0], 4134)
        self.assertLen(all_time[1], 1008)
        self.assertLen(all_flux[1], 1008)
        self.assertLen(all_time[2], 4486)
        self.assertLen(all_flux[2], 4486)

        for time, flux in zip(all_time, all_flux):
            self.assertTrue(np.isfinite(time).all())
            self.assertTrue(np.isfinite(flux).all())
Esempio n. 4
0
  def testReadKeplerLightCurve(self):
    filenames = [
        os.path.join(self.data_dir,
                     "0114/011442793/kplr011442793-{}_llc.fits".format(q))
        for q in ["2009350155506", "2010009091648", "2010174085026"]
    ]
    all_time, all_flux = kepler_io.read_kepler_light_curve(filenames)
    self.assertLen(all_time, 3)
    self.assertLen(all_flux, 3)
    self.assertLen(all_time[0], 4134)
    self.assertLen(all_flux[0], 4134)
    self.assertLen(all_time[1], 1008)
    self.assertLen(all_flux[1], 1008)
    self.assertLen(all_time[2], 4486)
    self.assertLen(all_flux[2], 4486)

    for time, flux in zip(all_time, all_flux):
      self.assertTrue(np.isfinite(time).all())
      self.assertTrue(np.isfinite(flux).all())
Esempio n. 5
0
    def testReadKeplerLightCurveScrambled(self):
        filenames = [
            os.path.join(self.data_dir,
                         "0114/011442793/kplr011442793-{}_llc.fits".format(q))
            for q in ["2009350155506", "2010009091648", "2010174085026"]
        ]
        all_time, all_flux = kepler_io.read_kepler_light_curve(
            filenames, scramble_type="SCR1")
        self.assertLen(all_time, 3)
        self.assertLen(all_flux, 3)

        # Arrays are shorter than above due to separation of time and flux NaNs.
        self.assertLen(all_time[0], 4344)
        self.assertLen(all_flux[0], 4344)
        self.assertLen(all_time[1], 4041)
        self.assertLen(all_flux[1], 4041)
        self.assertLen(all_time[2], 1008)
        self.assertLen(all_flux[2], 1008)

        for time, flux in zip(all_time, all_flux):
            self.assertTrue(np.isfinite(time).all())
            self.assertTrue(np.isfinite(flux).all())
Esempio n. 6
0
  def testReadKeplerLightCurveScrambled(self):
    filenames = [
        os.path.join(self.data_dir,
                     "0114/011442793/kplr011442793-{}_llc.fits".format(q))
        for q in ["2009350155506", "2010009091648", "2010174085026"]
    ]
    all_time, all_flux = kepler_io.read_kepler_light_curve(
        filenames, scramble_type="SCR1")
    self.assertLen(all_time, 3)
    self.assertLen(all_flux, 3)

    # Arrays are shorter than above due to separation of time and flux NaNs.
    self.assertLen(all_time[0], 4344)
    self.assertLen(all_flux[0], 4344)
    self.assertLen(all_time[1], 4041)
    self.assertLen(all_flux[1], 4041)
    self.assertLen(all_time[2], 1008)
    self.assertLen(all_flux[2], 1008)

    for time, flux in zip(all_time, all_flux):
      self.assertTrue(np.isfinite(time).all())
      self.assertTrue(np.isfinite(flux).all())
Esempio n. 7
0
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)
Esempio n. 8
0
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)
Esempio n. 9
0
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 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
Esempio n. 11
0
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