def main(): ''' Main function for this module. Parses all command line arguments, reads in data from stdin, and sends it to the proper BLS algorithm. ''' # This is a global list of default values that will be used by the argument parser # and the configuration parser. defaults = {'min_duration':'0.0416667', 'max_duration':'0.5', 'n_bins':'100', 'direction':'0', 'mode':'vec', 'print_format':'encoded', 'verbose':'0', 'profiling':'0'} # Set up the parser for command line arguments and read them. parser = __init_parser(defaults) args = parser.parse_args() if not args.config: # No configuration file specified -- read in command line arguments. if not args.segment: parser.error('No trial segment specified and no configuration file given.') segment = args.segment mindur = args.mindur maxdur = args.maxdur nbins = args.nbins direction = args.direction mode = args.mode fmt = args.fmt verbose = args.verbose profile = args.profile else: # Configuration file was given; read in that instead. cp = SafeConfigParser(defaults) cp.read(args.config) segment = cp.getfloat('DEFAULT', 'segment') mindur = cp.getfloat('DEFAULT', 'min_duration') maxdur = cp.getfloat('DEFAULT', 'max_duration') nbins = cp.getint('DEFAULT', 'n_bins') direction = cp.getint('DEFAULT', 'direction') mode = cp.get('DEFAULT', 'mode') fmt = cp.get('DEFAULT', 'print_format') verbose = cp.getboolean('DEFAULT', 'verbose') profile = cp.getboolean('DEFAULT', 'profiling') # Perform any sanity-checking on the arguments. __check_args(segment, mindur, maxdur, nbins, direction) # Send the data to the algorithm. for k, q, time, flux, fluxerr in read_mapper_output(sys.stdin): # Extract the array columns. time = np.array(time, dtype='float64') flux = np.array(flux, dtype='float64') fluxerr = np.array(fluxerr, dtype='float64') if profile: # Turn on profiling. pr = cProfile.Profile() pr.enable() if mode == 'python': raise NotImplementedError out = bls_pulse_python(time, flux, fluxerr, nbins, segment, mindur, maxdur, direction=direction) elif mode == 'vec': raise NotImplementedError out = bls_pulse_vec(time, flux, fluxerr, nbins, segment, mindur, maxdur, direction=direction) elif mode == 'cython': out = bls_pulse_cython(time, flux, fluxerr, nbins, segment, mindur, maxdur, direction=direction) else: raise ValueError('Invalid mode: %s' % mode) if profile: # Turn off profiling. pr.disable() ps = pstats.Stats(pr, stream=sys.stderr).sort_stats('time') ps.print_stats() if direction == 2: srsq_dip = out['srsq_dip'] duration_dip = out['duration_dip'] depth_dip = out['depth_dip'] midtime_dip = out['midtime_dip'] srsq_blip = out['srsq_blip'] duration_blip = out['duration_blip'] depth_blip = out['depth_blip'] midtime_blip = out['midtime_blip'] segstart = out['segstart'] segend = out['segend'] # Print output. if fmt == 'encoded': print "\t".join([k, q, encode_array(segstart), encode_array(segend), encode_array(srsq_dip), encode_array(duration_dip), encode_array(depth_dip), encode_array(midtime_dip), encode_array(srsq_blip), encode_array(duration_blip), encode_array(depth_blip), encode_array(midtime_blip)]) elif fmt == 'normal': print "-" * 120 print "Kepler " + k print "Quarters: " + q print "-" * 120 print '{0: <7s} {1: <13s} {2: <13s} {3: <13s} {4: <13s} {5: <13s} {6: <13s} {7: <13s} ' \ '{8: <13s}'.format('Segment', 'Dip SR^2', 'Dip dur.', 'Dip depth', 'Dip mid.', 'Blip SR^2', 'Blip dur.', 'Blip depth', 'Blip mid.') for i in xrange(len(srsq_dip)): print '{0: <7d} {1: <13.6f} {2: <13.6f} {3: <13.6f} {4: <13.6f} ' \ '{5: <13.6f} {6: <13.6f} {7: <13.6f} {8: <13.6f}'.format(i, srsq_dip[i], duration_dip[i], depth_dip[i], midtime_dip[i], srsq_blip[i], duration_blip[i], depth_blip[i], midtime_blip[i]) print "-" * 120 print print else: srsq = out['srsq'] duration = out['duration'] depth = out['depth'] midtime = out['midtime'] segstart = out['segstart'] segend = out['segend'] # Print output. if fmt == 'encoded': print "\t".join([k, q, encode_array(segstart), encode_array(segend), encode_array(srsq), encode_array(duration), encode_array(depth), encode_array(midtime)]) elif fmt == 'normal': print "-" * 80 print "Kepler " + k print "Quarters: " + q print "-" * 80 print '{0: <7s} {1: <13s} {2: <10s} {3: <9s} {4: <13s}'.format('Segment', 'SR^2', 'Duration', 'Depth', 'Midtime') for i in xrange(len(srsq)): print '{0: <7d} {1: <13.6f} {2: <10.6f} {3: <9.6f} {4: <13.6f}'.format(i, srsq[i], duration[i], depth[i], midtime[i]) print "-" * 80 print print
def main(): ''' Main function for this module. Parses all command line arguments, reads in data from stdin, and sends it to the proper BLS algorithm. ''' # This is a global list of default values that will be used by the argument parser # and the configuration parser. defaults = { 'min_duration': '0.0416667', 'max_duration': '0.5', 'n_bins': '100', 'direction': '0', 'mode': 'vec', 'print_format': 'encoded', 'verbose': '0', 'profiling': '0' } # Set up the parser for command line arguments and read them. parser = __init_parser(defaults) args = parser.parse_args() if not args.config: # No configuration file specified -- read in command line arguments. if not args.segment: parser.error( 'No trial segment specified and no configuration file given.') segment = args.segment mindur = args.mindur maxdur = args.maxdur nbins = args.nbins direction = args.direction mode = args.mode fmt = args.fmt verbose = args.verbose profile = args.profile else: # Configuration file was given; read in that instead. cp = SafeConfigParser(defaults) cp.read(args.config) segment = cp.getfloat('DEFAULT', 'segment') mindur = cp.getfloat('DEFAULT', 'min_duration') maxdur = cp.getfloat('DEFAULT', 'max_duration') nbins = cp.getint('DEFAULT', 'n_bins') direction = cp.getint('DEFAULT', 'direction') mode = cp.get('DEFAULT', 'mode') fmt = cp.get('DEFAULT', 'print_format') verbose = cp.getboolean('DEFAULT', 'verbose') profile = cp.getboolean('DEFAULT', 'profiling') # Perform any sanity-checking on the arguments. __check_args(segment, mindur, maxdur, nbins, direction) # Send the data to the algorithm. for k, q, time, flux, fluxerr in read_mapper_output(sys.stdin): # Extract the array columns. time = np.array(time, dtype='float64') flux = np.array(flux, dtype='float64') fluxerr = np.array(fluxerr, dtype='float64') if profile: # Turn on profiling. pr = cProfile.Profile() pr.enable() if mode == 'python': raise NotImplementedError out = bls_pulse_python(time, flux, fluxerr, nbins, segment, mindur, maxdur, direction=direction) elif mode == 'vec': raise NotImplementedError out = bls_pulse_vec(time, flux, fluxerr, nbins, segment, mindur, maxdur, direction=direction) elif mode == 'cython': out = bls_pulse_cython(time, flux, fluxerr, nbins, segment, mindur, maxdur, direction=direction) else: raise ValueError('Invalid mode: %s' % mode) if profile: # Turn off profiling. pr.disable() ps = pstats.Stats(pr, stream=sys.stderr).sort_stats('time') ps.print_stats() if direction == 2: srsq_dip = out['srsq_dip'] duration_dip = out['duration_dip'] depth_dip = out['depth_dip'] midtime_dip = out['midtime_dip'] srsq_blip = out['srsq_blip'] duration_blip = out['duration_blip'] depth_blip = out['depth_blip'] midtime_blip = out['midtime_blip'] segstart = out['segstart'] segend = out['segend'] # Print output. if fmt == 'encoded': print "\t".join([ k, q, encode_array(segstart), encode_array(segend), encode_array(srsq_dip), encode_array(duration_dip), encode_array(depth_dip), encode_array(midtime_dip), encode_array(srsq_blip), encode_array(duration_blip), encode_array(depth_blip), encode_array(midtime_blip) ]) elif fmt == 'normal': print "-" * 120 print "Kepler " + k print "Quarters: " + q print "-" * 120 print '{0: <7s} {1: <13s} {2: <13s} {3: <13s} {4: <13s} {5: <13s} {6: <13s} {7: <13s} ' \ '{8: <13s}'.format('Segment', 'Dip SR^2', 'Dip dur.', 'Dip depth', 'Dip mid.', 'Blip SR^2', 'Blip dur.', 'Blip depth', 'Blip mid.') for i in xrange(len(srsq_dip)): print '{0: <7d} {1: <13.6f} {2: <13.6f} {3: <13.6f} {4: <13.6f} ' \ '{5: <13.6f} {6: <13.6f} {7: <13.6f} {8: <13.6f}'.format(i, srsq_dip[i], duration_dip[i], depth_dip[i], midtime_dip[i], srsq_blip[i], duration_blip[i], depth_blip[i], midtime_blip[i]) print "-" * 120 print print else: srsq = out['srsq'] duration = out['duration'] depth = out['depth'] midtime = out['midtime'] segstart = out['segstart'] segend = out['segend'] # Print output. if fmt == 'encoded': print "\t".join([ k, q, encode_array(segstart), encode_array(segend), encode_array(srsq), encode_array(duration), encode_array(depth), encode_array(midtime) ]) elif fmt == 'normal': print "-" * 80 print "Kepler " + k print "Quarters: " + q print "-" * 80 print '{0: <7s} {1: <13s} {2: <10s} {3: <9s} {4: <13s}'.format( 'Segment', 'SR^2', 'Duration', 'Depth', 'Midtime') for i in xrange(len(srsq)): print '{0: <7d} {1: <13.6f} {2: <10.6f} {3: <9.6f} {4: <13.6f}'.format( i, srsq[i], duration[i], depth[i], midtime[i]) print "-" * 80 print print
def main(fname_fits, datasrc, datapath=None): ''' ''' # Load the FITS file using the custom class to wrap the data. fits = BLSOutput(fname_fits) kic = fits.kic # Use the existing get_data functionality to load the raw Kepler data. dataspec = StringIO('%s\t*\tllc' % kic) outstream1 = StringIO() outstream2 = StringIO() get_data(datasrc, datapath, instream=dataspec, outstream=outstream1) outstream1.seek(0) join_quarters(instream=outstream1, outstream=outstream2) outstream2.seek(0) for _, _, t, f, e in read_mapper_output(outstream2, uri=False): time, flux, fluxerr = t, f, e time = np.array(time) flux = np.array(flux) fluxerr = np.array(fluxerr) for i in xrange(fits.num_passes): # Get the detrended light curve for this pass. lc = fits.lightcurves[i] dtime = lc['Time'] dflux = lc['Flux'] dfluxerr = lc['Flux error'] # Get the BLS output for this pass. bls = fits.dipblips[i] mask = (bls['srsq_dip'] > 0.) & (bls['srsq_blip'] > 0.) duration_dip = bls['duration_dip'][mask] depth_dip = -1. * bls['depth_dip'][mask] midtime_dip = bls['midtime_dip'][mask] duration_blip = bls['duration_blip'][mask] depth_blip = bls['depth_blip'][mask] midtime_blip = bls['midtime_blip'][mask] segstart = bls['segstart'][mask] segend = bls['segend'][mask] # This is needed for the plot interaction. data = np.column_stack((depth_dip,depth_blip)) kdtree = scipy.spatial.cKDTree(data) # Set up the canvas. fig = plt.figure() ax = fig.add_subplot(111) ax.set_aspect('equal') cid = fig.canvas.mpl_connect('button_press_event', lambda e: __onclick(e, ax, kdtree, segstart, segend, duration_dip, depth_dip, midtime_dip, duration_blip, depth_blip, midtime_blip, time, flux, fluxerr, dtime, dflux, dfluxerr)) # Plot the dip and blip depths. ax.scatter(depth_dip, depth_blip, marker='x', color='k') # Draw a dashed y = x line. ax.plot([0.,1.], [0.,1], transform=plt.gca().transAxes, ls='--', color='r') # The limits of the plot are set by the maximum absolute depth. Use the # same dimension in both directions so y = x has slope 1 when displayed # on the screen. size = max(np.amax(depth_dip), np.amax(depth_blip)) ax.set_xlim(0., size) ax.set_ylim(0., size) ax.set_title('KIC ' + kic) ax.set_xlabel('Dip depth') ax.set_ylabel('Blip depth') ax.set_title('Pass #' + str(fits.num_passes - i)) # Show the plot; halts execution until the user exits. plt.tight_layout() plt.show()
import sys import logging import numpy as np from itertools import groupby from operator import itemgetter from utils import read_mapper_output, encode_list # Basic logging configuration. logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) if __name__ == '__main__': # input comes from STDIN (standard input) data = read_mapper_output(sys.stdin, uri=True) # groupby groups multiple quarters together for each Kepler ID # current_kic is current Kepler ID # group - iterator yielding all ["<current_word>", "<count>"] items for current_kic, group in groupby(data, itemgetter(0)): try: all_quarters = [[q, time, flux, eflux] for _, q, time, flux, eflux in group] concatenated_time = list() concatenated_flux = list() concatenated_eflux = list() for _, t, f, e in all_quarters: concatenated_time.extend(t) concatenated_flux.extend(f)
def main(file_data, file_pipeline): ''' ''' f1 = open(file_data, 'r') f2 = open(file_pipeline, 'r') for out1, out2 in zip(read_mapper_output(f1), read_pipeline_output(f2)): kic, q, time, flux, fluxerr = out1 _, _, segstart, segend, _, duration_dip, depth_dip, midtime_dip, \ _, duration_blip, depth_blip, midtime_blip = out2 # Save the data in NumPy arrays. time = np.array(time) flux = np.array(flux) fluxerr = np.array(fluxerr) # Filter out NaNs in pipeline output and save parameters as arrays. ndx = np.where(np.isfinite(depth_dip)) segstart = np.array(segstart)[ndx] segend = np.array(segend)[ndx] duration_dip = np.array(duration_dip)[ndx] depth_dip = np.absolute(np.array(depth_dip)[ndx]) midtime_dip = np.array(midtime_dip)[ndx] duration_blip = np.array(duration_blip)[ndx] depth_blip = np.absolute(np.array(depth_blip)[ndx]) midtime_blip = np.array(midtime_blip)[ndx] # This is needed for the plot interaction. data = np.column_stack((depth_dip,depth_blip)) kdtree = scipy.spatial.cKDTree(data) # Set up the canvas. fig = plt.figure() ax = fig.add_subplot(111) ax.set_aspect('equal') cid = fig.canvas.mpl_connect('button_press_event', lambda e: __onclick(e, ax, kdtree, segstart, segend, duration_dip, depth_dip, midtime_dip, duration_blip, depth_blip, midtime_blip, time, flux, fluxerr)) # Plot the dip and blip depths. ax.scatter(depth_dip, depth_blip, marker='x', color='k') # Draw a dashed y = x line. ax.plot([0.,1.], [0.,1], transform=plt.gca().transAxes, ls='--', color='r') # The limits of the plot are set by the maximum absolute depth. Use the same # dimension in both directions so y = x has slope 1 when displayed on the # screen. size = max(np.amax(depth_dip), np.amax(depth_blip)) ax.set_xlim(0., size) ax.set_ylim(0., size) ax.set_title('KIC ' + kic) ax.set_xlabel('Dip depth') ax.set_ylabel('Blip depth') # Show the plot; halts execution until the user exits. plt.show() f1.close() f2.close()
''' import sys import logging import numpy as np from itertools import groupby from operator import itemgetter from utils import read_mapper_output, encode_list # Basic logging configuration. logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) if __name__ == '__main__': # input comes from STDIN (standard input) data = read_mapper_output(sys.stdin, uri=True) # groupby groups multiple quarters together for each Kepler ID # current_kic is current Kepler ID # group - iterator yielding all ["<current_word>", "<count>"] items for current_kic, group in groupby(data, itemgetter(0)): try: all_quarters = [[q, time, flux, eflux] for _, q, time, flux, eflux in group] concatenated_time = list() concatenated_flux = list() concatenated_eflux = list() for _, t, f, e in all_quarters: concatenated_time.extend(t)
def main(): ''' Main function for this module. Parses all command line arguments, reads in data from stdin, and sends it to the proper BLS algorithm. ''' # This is a global list of default values that will be used by the # argument parser and the configuration parser. defaults = {'min_duration':'0.0416667', 'max_duration':'0.5', 'n_bins':'100', 'direction':'0', 'print_format':'encoded', 'verbose':'0', 'profiling':'0', 'clean_max':'5', 'fits_output':'1', 'fits_dir':'', 'model_type':'box'} # Set up the parser for command line arguments and read them. parser = __init_parser(defaults) args = parser.parse_args() cfg = dict() if not args.config: # No configuration file specified -- read in command line arguments. if not args.segment: parser.error('No trial segment specified and no configuration ' 'file given.') cfg['segment'] = args.segment cfg['mindur'] = args.mindur cfg['maxdur'] = args.maxdur cfg['nbins'] = args.nbins cfg['direction'] = args.direction cfg['fmt'] = args.fmt cfg['verbose'] = args.verbose cfg['profile'] = args.profile cfg['clean_max'] = args.clean_max cfg['fitsout'] = args.fitsout cfg['fitsdir'] = args.fitsdir cfg['model'] = args.model_type else: # Configuration file was given; read it instead. cp = ConfigParser(defaults) cp.read(args.config) cfg['segment'] = cp.getfloat('DEFAULT', 'segment') cfg['mindur'] = cp.getfloat('DEFAULT', 'min_duration') cfg['maxdur'] = cp.getfloat('DEFAULT', 'max_duration') cfg['nbins'] = cp.getint('DEFAULT', 'n_bins') cfg['direction'] = cp.getint('DEFAULT', 'direction') cfg['fmt'] = cp.get('DEFAULT', 'print_format') cfg['verbose'] = cp.getboolean('DEFAULT', 'verbose') cfg['profile'] = cp.getboolean('DEFAULT', 'profiling') cfg['clean_max'] = cp.getint('DEFAULT', 'clean_max') cfg['fitsout'] = cp.getboolean('DEFAULT', 'fits_output') cfg['fitsdir'] = cp.get('DEFAULT', 'fits_dir') cfg['model'] = cp.get('DEFAULT', 'model_type') if cfg['fitsout'] and cfg['fitsdir'] == '': parser.error('No FITS output directory specified.') # Perform any sanity-checking on the arguments. __check_args(cfg['segment'], cfg['mindur'], cfg['maxdur'], cfg['nbins'], cfg['direction']) # Send the data to the algorithm. for k, q, time, flux, fluxerr in read_mapper_output(sys.stdin): logger.info('Beginning analysis for ' + k) # Extract the array columns. time = np.array(time, dtype='float64') flux = np.array(flux, dtype='float64') fluxerr = np.array(fluxerr, dtype='float64') # Don't assume the times are sorted already! ndx = np.argsort(time) time = time[ndx] flux = flux[ndx] fluxerr = fluxerr[ndx] if cfg['profile']: # Turn on profiling. pr = cProfile.Profile() pr.enable() if cfg['fitsout']: # Set up the FITS bundler. bundler = BLSFitsBundler() bundler.make_header(k) clean_out = None for i in xrange(cfg['clean_max']): # Do ALL detrending and binning here. The main algorithm # function is now separate from this functionality. dtime, dflux, dfluxerr, samples, segstart, segend = \ bin_and_detrend(time, flux, fluxerr, cfg['nbins'], cfg['segment'], detrend_order=3) if np.count_nonzero(~np.isnan(dflux)) == 0: logger.warning('Not enough points left to continue BLS pulse') bls_out = None break bls_out = bls_pulse(dtime, dflux, dfluxerr, samples, cfg['nbins'], cfg['segment'], cfg['mindur'], cfg['maxdur'], direction=cfg['direction']) if cfg['direction'] != 2: # Cleaning iterations currently won't work unless direction # is 2, so we don't loop in this case. break srsq_dip = bls_out['srsq_dip'] duration_dip = bls_out['duration_dip'] depth_dip = bls_out['depth_dip'] midtime_dip = bls_out['midtime_dip'] srsq_blip = bls_out['srsq_blip'] duration_blip = bls_out['duration_blip'] depth_blip = bls_out['depth_blip'] midtime_blip = bls_out['midtime_blip'] try: clean_out = clean_signal(time, flux, dtime, dflux, dfluxerr, bls_out, model=cfg['model']) except RuntimeError: break if cfg['fitsout']: ndx = np.where(np.isfinite(dflux)) bundler.push_detrended_lightcurve(dtime[ndx], dflux[ndx], dfluxerr[ndx], clean_out=clean_out) bundler.push_bls_output(bls_out, segstart, segend) if cfg['fitsout'] and bls_out is not None: # Save the detrended light curve and BLS output from the last # iteration. There won't be any output from `clean_signal`, # either because of the `direction` parameter or because there # are no more strong periodic signals. ndx = np.where(np.isfinite(dflux)) bundler.push_detrended_lightcurve(dtime[ndx], dflux[ndx], dfluxerr[ndx], clean_out=None) bundler.push_bls_output(bls_out, segstart, segend) if cfg['fitsout']: # Save the entire FITS file, including the configuration. bundler.push_config(cfg) outfile = os.path.abspath(os.path.expanduser(os.path.join( cfg['fitsdir'], 'KIC' + k + '.fits'))) bundler.write_file(outfile, clobber=True) if cfg['profile']: # Turn off profiling and print results to STDERR. pr.disable() ps = pstats.Stats(pr, stream=sys.stderr).sort_stats('time') ps.print_stats() if cfg['direction'] == 2: # Print output. if cfg['fmt'] == 'encoded': print "\t".join([k, q, encode_array(segstart), encode_array(segend), encode_array(srsq_dip), encode_array(duration_dip), encode_array(depth_dip), encode_array(midtime_dip), encode_array(srsq_blip), encode_array(duration_blip), encode_array(depth_blip), encode_array(midtime_blip)]) elif cfg['fmt'] == 'normal': print "-" * 120 print "Kepler " + k print "Quarters: " + q print "-" * 120 print '{0: <7s} {1: <13s} {2: <13s} {3: <13s} {4: <13s} ' \ '{5: <13s} {6: <13s} {7: <13s} {8: <13s}'.format('Segment', 'Dip SR^2', 'Dip dur.', 'Dip depth', 'Dip mid.', 'Blip SR^2', 'Blip dur.', 'Blip depth', 'Blip mid.') for i in xrange(len(srsq_dip)): print '{0: <7d} {1: <13.6f} {2: <13.6f} {3: <13.6f} ' \ '{4: <13.6f} {5: <13.6f} {6: <13.6f} {7: <13.6f} ' \ '{8: <13.6f}'.format(i, srsq_dip[i], duration_dip[i], depth_dip[i], midtime_dip[i], srsq_blip[i], duration_blip[i], depth_blip[i], midtime_blip[i]) print "-" * 120 print print elif cfg['fmt'] == 'outfile' and cfg['fitsout']: print outfile else: srsq = out['srsq'] duration = out['duration'] depth = out['depth'] midtime = out['midtime'] segstart = out['segstart'] segend = out['segend'] # Print output. if cfg['fmt'] == 'encoded': print "\t".join([k, q, encode_array(segstart), encode_array(segend), encode_array(srsq), encode_array(duration), encode_array(depth), encode_array(midtime)]) elif cfg['fmt'] == 'normal': print "-" * 80 print "Kepler " + k print "Quarters: " + q print "-" * 80 print '{0: <7s} {1: <13s} {2: <10s} {3: <9s} {4: <13s}'.format( 'Segment', 'SR^2', 'Duration', 'Depth', 'Midtime') for i in xrange(len(srsq)): print '{0: <7d} {1: <13.6f} {2: <10.6f} {3: <9.6f} ' \ '{4: <13.6f}'.format(i, srsq[i], duration[i], depth[i], midtime[i]) print "-" * 80 print print elif cfg['fmt'] == 'outfile' and cfg['fitsout']: print outfile