utils – Utility functions

This module contains functions that are common to several other modules in this directory.

class utils.ColoredFormatter(msg, use_color=True)

Formatter compatible with Python logging module. Allows colored messages to be written to the terminal, determined by the level of the message (warning, error, etc.).

format(record)
utils.bin_single_segment_slow(time, flux, fluxerr, nbins, segstart, segend)

Returns a binned segment. This should not be used in the pipeline! It is useful for plotting individual segments and debugging.

See http://stackoverflow.com/questions/6163334/binning-data-in-python-with-scipy-numpy

Parameters:
  • time (np.ndarray) – Vector of times
  • flux (np.ndarray) – Vector of fluxes
  • fluxerr (np.ndarray) – Vector of flux errors
  • nbins (int) – Number of bins per segment
  • segstart (float) – Start time of this segment
  • segend (float) – End time of this segment
Return type:

tuple

utils.boxcar(time, duration, depth, midtime)

General function for producing a boxcar signal; useful for plotting.

Parameters:
  • time (np.ndarray) – Vector of observation times
  • duration (float) – Duration of the signal
  • depth (float) – Depth of the signal
  • midtime (float) – Midtime of the signal
utils.decode_array(s)

base64-decode the given string.

Parameters:s (str) – String to decode
Return type:list
utils.encode_array(arr)

base64-encodes the given numpy array.

Parameters:arr (numpy.ndarray) – Array to encode
Return type:str
utils.encode_list(lst)

base64-encodes the given Python list.

Parameters:lst (list) – List to encode
Return type:str
utils.handle_exception(exc_tuple)

If these commands are called as a pipe chain with bash, etc., the chain needs to be stopped with a sys.exit(1) call, which prevents printing the information about the exception we are used to. Call this function with the exception tuple to print out the traceback to STDERR before calling the exit.

Parameters:exc_tuple – Exception tuple (from sys.exc_info())
Tupe exc_tuple:tuple
utils.read_mapper_output(f, separator='\t', uri=False)

Reads data from the input file, assuming the given separator, in base64 format; yields the decoded and split line. The format is KIC ID, quarter, [uri], time, flux, error for each line.

Parameters:
  • f (file) – File to read; usually sys.stdin
  • separator (str) – Sepearator between parts of an entry
  • uri (bool) – Whether the URI is included on the line
Return type:

tuple

utils.read_pipeline_output(f, separator='\t')

Reads data from the input file, assuming the given separator, in base64 format; yields the decoded and split line. The format for each line is KIC ID, quarter, segment_start, segment_end, srsq_dip, duration_dip, depth_dip, midtime_dip, srsq_blip, duration_blip, depth_blip, midtime_blip.

Parameters:
  • f (file) – File to read; usually sys.stdin
  • separator (str) – Sepearator between parts of an entry
Return type:

tuple

utils.setup_logging(fname)

Standard way to set up logging for this library.

Parameters:fname (str) – File name of the calling module (from __file__)