Commit 67ac968b authored by Leo Pound Singer's avatar Leo Pound Singer

Fix PEP8 issues

Original: 9fc8d07a36169b01f044d12a44f2afc811928610
parent 585a7e82
......@@ -54,21 +54,31 @@ command.skymap_parser.add_argument(
'--method', choices=methods, default=[default_method], nargs='*',
help='Sky localization methods [default: %(default)s]')
parser = command.ArgumentParser(
parents=[command.waveform_parser, command.prior_parser, command.skymap_parser])
parser.add_argument('--keep-going', '-k', default=False, action='store_true',
parents=[
command.waveform_parser, command.prior_parser, command.skymap_parser])
parser.add_argument(
'--keep-going', '-k', default=False, action='store_true',
help='Keep processing events if a sky map fails to converge [default: no]')
parser.add_argument('input', metavar='INPUT.xml[.gz]', default='-', nargs='+',
parser.add_argument(
'input', metavar='INPUT.xml[.gz]', default='-', nargs='+',
type=argparse.FileType('rb'),
help='Input LIGO-LW XML file [default: stdin] or PyCBC HDF5 files. If PyCBC files, must be bank file, coinc file, and trigger files, in that order.')
parser.add_argument('--pycbc-sample', default='foreground',
help='Input LIGO-LW XML file [default: stdin] or PyCBC HDF5 files. If '
'PyCBC files, must be bank file, coinc file, and trigger files, in that '
'order.')
parser.add_argument(
'--pycbc-sample', default='foreground',
help='sample population [PyCBC only; default: %(default)s]')
parser.add_argument('--psd-files', nargs='*',
parser.add_argument(
'--psd-files', nargs='*',
help='pycbc-style merged HDF5 PSD files')
parser.add_argument('--coinc-event-id', type=int, nargs='*',
parser.add_argument(
'--coinc-event-id', type=int, nargs='*',
help='run on only these specified events')
parser.add_argument('--output', '-o', default='.',
parser.add_argument(
'--output', '-o', default='.',
help='output directory [default: current directory]')
parser.add_argument('--condor-submit', action='store_true',
parser.add_argument(
'--condor-submit', action='store_true',
help='submit to Condor instead of running locally')
opts = parser.parse_args()
......@@ -90,7 +100,7 @@ from lalinference.io import fits
from lalinference.bayestar import ligolw as ligolw_bayestar
from lalinference.bayestar import filter
from lalinference.bayestar import timing
from lalinference.bayestar.sky_map import ligolw_sky_map, rasterize
from lalinference.bayestar.sky_map import ligolw_sky_map
# Other imports.
import os
......@@ -137,14 +147,16 @@ if opts.condor_submit:
'/dev/null']
os.execvp('condor_submit', cmd)
if opts.psd_files: # read pycbc psds here
if opts.psd_files: # read pycbc psds here
import lal
from glue.segments import segment, segmentlist
import h5py
class psd_segment(segment):
def __new__(cls, psd, *args):
return segment.__new__(cls, *args)
def __init__(self, psd, *args):
self.psd = psd
......@@ -154,7 +166,7 @@ if opts.psd_files: # read pycbc psds here
psd = [group['psds'][str(i)] for i in range(len(group['psds']))]
psdseglistdict[ifo] = segmentlist(
psd_segment(*segargs) for segargs in zip(
psd, group['start_time'], group['end_time']))
psd, group['start_time'], group['end_time']))
def reference_psd_for_sngl(sngl):
psd = psdseglistdict[sngl.ifo]
......@@ -163,7 +175,7 @@ if opts.psd_files: # read pycbc psds here
except ValueError:
raise ValueError(
'No PSD found for detector {0} at GPS time {1}'.format(
sngl.ifo, sngl.end))
sngl.ifo, sngl.end))
flow = psd.file.attrs['low_frequency_cutoff']
df = psd.attrs['delta_f']
......@@ -172,7 +184,8 @@ if opts.psd_files: # read pycbc psds here
fseries = lal.CreateREAL8FrequencySeries(
'psd', 0, kmin * df, df,
lal.StrainUnit**2 / lal.HertzUnit, len(psd.value) - kmin)
fseries.data.data = psd.value[kmin:] / np.square(ligolw_bayestar.PYCBC_DYN_RANGE_FAC)
fseries.data.data = psd.value[kmin:] / np.square(
ligolw_bayestar.PYCBC_DYN_RANGE_FAC)
return timing.InterpolatedPSD(
filter.abscissa(fseries), fseries.data.data,
......@@ -241,15 +254,19 @@ for int_coinc_event_id, sngl_inspirals in coinc_and_sngl_inspirals.items():
snr_series=snrs, enable_snr_series=opts.enable_snr_series)
sky_map.meta['objid'] = coinc_event_id
except (ArithmeticError, ValueError):
log.exception("%s:method '%s':sky localization failed", coinc_event_id, method)
log.exception(
"%s:method '%s':sky localization failed",
coinc_event_id, method)
count_sky_maps_failed += 1
if not opts.keep_going:
raise
else:
log.info("%s:method '%s':saving sky map", coinc_event_id, method)
log.info(
"%s:method '%s':saving sky map",
coinc_event_id, method)
filename = '%d.%s.fits' % (int_coinc_event_id, method)
fits.write_sky_map(os.path.join(opts.output, filename),
sky_map, nest=True)
fits.write_sky_map(
os.path.join(opts.output, filename), sky_map, nest=True)
if count_sky_maps_failed > 0:
......
......@@ -39,7 +39,8 @@ terminal, or redirected from a fifo):
from lalinference.bayestar import command
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
logging.basicConfig(
level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
log = logging.getLogger('BAYESTAR')
methods = '''
......@@ -51,15 +52,19 @@ command.skymap_parser.add_argument(
'--method', choices=methods, default=default_method,
help='Sky localization method [default: %(default)s]')
parser = command.ArgumentParser(
parents=[command.waveform_parser, command.prior_parser, command.skymap_parser])
parser.add_argument('-N', '--dry-run', default=False, action='store_true',
parents=[
command.waveform_parser, command.prior_parser, command.skymap_parser])
parser.add_argument(
'-N', '--dry-run', default=False, action='store_true',
help='Dry run; do not update GraceDB entry [default: %(default)s]')
parser.add_argument('--no-tag', default=False, action='store_true',
parser.add_argument(
'--no-tag', default=False, action='store_true',
help='Do not set lvem tag for GraceDB entry [default: %(default)s]')
parser.add_argument('-o', '--output', metavar='FILE.fits[.gz]',
default='bayestar.fits.gz',
parser.add_argument(
'-o', '--output', metavar='FILE.fits[.gz]', default='bayestar.fits.gz',
help='Name for uploaded file [default: %(default)s]')
parser.add_argument('graceid', metavar='G123456', nargs='*',
parser.add_argument(
'graceid', metavar='G123456', nargs='*',
help='Run on this GraceDB ID [default: listen to lvalert]')
opts = parser.parse_args()
......@@ -85,11 +90,12 @@ graceids = opts.graceid if opts.graceid else command.iterlines(sys.stdin)
# environment variable GRACEDB_SERVICE_URL overrides the default service URL.
# It would be nice to get this behavior into the gracedb package itself.
gracedb = ligo.gracedb.rest.GraceDb(
os.environ.get('GRACEDB_SERVICE_URL',
ligo.gracedb.rest.DEFAULT_SERVICE_URL))
os.environ.get(
'GRACEDB_SERVICE_URL', ligo.gracedb.rest.DEFAULT_SERVICE_URL))
if opts.chain_dump:
chain_dump = opts.output.replace('.fits.gz', '').replace('.fits', '') + '.chain.npy'
chain_dump = opts.output.replace('.fits.gz', '').replace('.fits', '') + \
'.chain.npy'
else:
chain_dump = None
......@@ -124,7 +130,8 @@ for graceid in graceids:
method=opts.method, chain_dump=chain_dump,
enable_snr_series=opts.enable_snr_series))
sky_map.meta['objid'] = str(graceid)
sky_map.meta['url'] = 'https://gracedb.ligo.org/events/{0}'.format(graceid)
sky_map.meta['url'] = 'https://gracedb.ligo.org/events/{0}'.format(
graceid)
log.info("sky localization complete")
# upload FITS file
......
......@@ -18,8 +18,6 @@
"""
LIGO-LW convenience functions.
"""
__author__ = "Leo Singer <leo.singer@ligo.org>"
# Python standard library imports.
import itertools
......@@ -40,12 +38,11 @@ from lalinspiral.inspinjfind import InspiralSCExactCoincDef
# Third-party imports.
import numpy as np
from astropy.table import Table
# FIXME: This should be imported from pycbc, or even better, embedded in the
# pycbc PSD files.
PYCBC_DYN_RANGE_FAC = 5.9029581035870565e+20
PYCBC_DYN_RANGE_FAC = 5.9029581035870565e+20
def get_template_bank_f_low(xmldoc):
......@@ -55,17 +52,18 @@ def get_template_bank_f_low(xmldoc):
line for the low-frequency cutoff; instead, it is recorded in a row
of the search_summvars table."""
try:
template_bank_f_low, = ligolw_process.get_process_params(xmldoc,
'tmpltbank', '--low-frequency-cutoff')
template_bank_f_low, = ligolw_process.get_process_params(
xmldoc, 'tmpltbank', '--low-frequency-cutoff')
except ValueError:
try:
template_bank_f_low, = ligolw_process.get_process_params(xmldoc,
'lalapps_cbc_sbank', '--flow')
template_bank_f_low, = ligolw_process.get_process_params(
xmldoc, 'lalapps_cbc_sbank', '--flow')
except ValueError:
try:
search_summvars_table = ligolw_table.get_table(xmldoc,
lsctables.SearchSummVarsTable.tableName)
template_bank_f_low, = (search_summvars.value
search_summvars_table = ligolw_table.get_table(
xmldoc, lsctables.SearchSummVarsTable.tableName)
template_bank_f_low, = (
search_summvars.value
for search_summvars in search_summvars_table
if search_summvars.name == 'low-frequency cutoff')
except ValueError:
......@@ -75,13 +73,17 @@ def get_template_bank_f_low(xmldoc):
def sim_coinc_and_sngl_inspirals_for_xmldoc(xmldoc):
"""Retrieve (as a generator) all of the
(sim_inspiral, coinc_event, (sngl_inspiral, sngl_inspiral, ... sngl_inspiral)
tuples from found coincidences in a LIGO-LW XML document."""
(sim_inspiral, coinc_event, (sngl_inspiral, sngl_inspiral,
... sngl_inspiral) tuples from found coincidences in a LIGO-LW XML
document."""
# Look up necessary tables.
coinc_table = ligolw_table.get_table(xmldoc, lsctables.CoincTable.tableName)
coinc_def_table = ligolw_table.get_table(xmldoc, lsctables.CoincDefTable.tableName)
coinc_map_table = ligolw_table.get_table(xmldoc, lsctables.CoincMapTable.tableName)
coinc_table = ligolw_table.get_table(
xmldoc, lsctables.CoincTable.tableName)
coinc_def_table = ligolw_table.get_table(
xmldoc, lsctables.CoincDefTable.tableName)
coinc_map_table = ligolw_table.get_table(
xmldoc, lsctables.CoincMapTable.tableName)
# Look up coinc_def ids.
sim_coinc_def_id = coinc_def_table.get_coinc_def_id(
......@@ -92,7 +94,8 @@ def sim_coinc_and_sngl_inspirals_for_xmldoc(xmldoc):
def events_for_coinc_event_id(coinc_event_id):
for coinc_map in coinc_map_table:
if coinc_map.coinc_event_id == coinc_event_id:
for row in ligolw_table.get_table(xmldoc, coinc_map.table_name):
for row in ligolw_table.get_table(
xmldoc, coinc_map.table_name):
column_name = coinc_map.event_id.column_name
if getattr(row, column_name) == coinc_map.event_id:
yield coinc_map.event_id, row
......@@ -107,20 +110,28 @@ def sim_coinc_and_sngl_inspirals_for_xmldoc(xmldoc):
# Locate the sim_inspiral and coinc events.
sim_inspiral = None
coinc = None
for event_id, event in events_for_coinc_event_id(sim_coinc.coinc_event_id):
if event_id.table_name == ligolw_table.Table.TableName(lsctables.SimInspiralTable.tableName):
for event_id, event in events_for_coinc_event_id(
sim_coinc.coinc_event_id):
if event_id.table_name == ligolw_table.Table.TableName(
lsctables.SimInspiralTable.tableName):
if sim_inspiral is not None:
raise RuntimeError("Found more than one matching sim_inspiral entry")
raise RuntimeError(
'Found more than one matching sim_inspiral entry')
sim_inspiral = event
elif event_id.table_name == ligolw_table.Table.TableName(lsctables.CoincTable.tableName):
elif event_id.table_name == ligolw_table.Table.TableName(
lsctables.CoincTable.tableName):
if coinc is not None:
raise RuntimeError("Found more than one matching coinc entry")
raise RuntimeError(
'Found more than one matching coinc entry')
coinc = event
else:
raise RuntimeError("Did not expect coincidence to contain an event of type '%s'" % event_id.table_name)
raise RuntimeError(
"Did not expect coincidence to contain an event of "
"type '{}'".format(event_id.table_name))
sngl_inspirals = tuple(event
for event_id, event in events_for_coinc_event_id(coinc.coinc_event_id))
sngl_inspirals = tuple(
event for event_id, event in events_for_coinc_event_id(
coinc.coinc_event_id))
yield sim_inspiral, coinc, sngl_inspirals
......@@ -139,7 +150,8 @@ class coinc_and_sngl_inspirals_for_hdf(collections.Mapping):
coinc_group = coinc_file[sample]
self.timeslide_interval = coinc_file.attrs.get('timeslide_interval', 0)
self.template_ids = coinc_group['template_id']
self.timeslide_ids = coinc_group.get('timeslide_id', np.zeros(len(self.template_ids)))
self.timeslide_ids = coinc_group.get(
'timeslide_id', np.zeros(len(self.template_ids)))
self.trigger_ids = [
coinc_group['trigger_id' + template_num]
for template_num in template_nums]
......@@ -152,8 +164,9 @@ class coinc_and_sngl_inspirals_for_hdf(collections.Mapping):
self.triggers = tuple(triggers[ifo] for ifo in self.ifos)
# Declare types for ersatz CoincEventTable and SnglInspiralTable rows.
self.SnglInspiral = collections.namedtuple('SnglInspiral',
['event_id', 'ifo', 'snr', 'coa_phase', 'end'] + bank_file.keys())
self.SnglInspiral = collections.namedtuple(
'SnglInspiral', ['event_id', 'ifo', 'snr', 'coa_phase', 'end']
+ bank_file.keys())
def __getitem__(self, coinc_id):
template_id = self.template_ids[coinc_id]
......@@ -236,9 +249,11 @@ class coinc_and_sngl_inspirals_for_xmldoc(collections.Mapping):
def psd_filenames_by_process_id_for_xmldoc(xmldoc):
"""Retrieve a dictionary mapping process_ids to reference PSD filenames."""
return {process_param.process_id: process_param.value
return {
process_param.process_id: process_param.value
for process_param
in ligolw_table.get_table(xmldoc, lsctables.ProcessParamsTable.tableName)
in ligolw_table.get_table(
xmldoc, lsctables.ProcessParamsTable.tableName)
if process_param.param == '--reference-psd'}
......@@ -247,7 +262,6 @@ def _snr_series_by_sngl_inspiral_id_for_xmldoc(xmldoc):
try:
if elem.Name != lal.COMPLEX8TimeSeries.__name__:
continue
array_elem = ligolw_array.get_array(elem, 'snr')
event_id = ligolw_param.get_pyvalue(elem, 'event_id')
if not isinstance(event_id, lsctables.SnglInspiralID):
continue
......@@ -263,7 +277,7 @@ def snr_series_by_sngl_inspiral_id_for_xmldoc(xmldoc):
@lsctables.use_in
class LSCTablesContentHandler(ligolw.LIGOLWContentHandler):
"""Content handler for reading LIGO-LW XML files with LSC table schema."""
"""Content handler for reading LIGO-LW XML files with LSC table schema."""
@ligolw_array.use_in
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment