Commit 4c08262e authored by Leo Pound Singer's avatar Leo Pound Singer

Add BAYESTAR option to disable specific detectors

This is useful for setting up separate HL and HLV analyses.
Original: 51c27d236eb9b7f3ce0fd27c597e2e4636a49fa0
parent 907e473c
......@@ -49,6 +49,9 @@ command.skymap_parser.add_argument(
parser = command.ArgumentParser(
parents=[
command.waveform_parser, command.prior_parser, command.skymap_parser])
parser.add_argument(
'-d', '--disable-detector', metavar='X1', type=str, nargs='+',
help='disable certain detectors [default: enable all]')
parser.add_argument(
'-N', '--dry-run', default=False, action='store_true',
help='Dry run; do not update GraceDB entry [default: %(default)s]')
......@@ -110,6 +113,10 @@ if not opts.no_tag:
event_source = events.gracedb.open(graceids, gracedb)
if opts.disable_detector:
event_source = events.detector_disabled.open(
event_source, opts.disable_detector)
for graceid in six.iterkeys(event_source):
try:
......
......@@ -10,6 +10,7 @@ pymoduledir = $(pkgpythondir)/io/events
pymodule_PYTHON = \
__init__.py \
base.py \
detector_disabled.py \
ligolw.py \
gracedb.py \
hdf.py \
......
......@@ -14,12 +14,13 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
from . import base, ligolw, gracedb, hdf, magic
from . import base, detector_disabled, ligolw, gracedb, hdf, magic
from .base import *
from .detector_disabled import *
from .ligolw import *
from .gracedb import *
from .hdf import *
from .magic import *
from .magic import open
__all__ = (base.__all__ + ligolw.__all__ + gracedb.__all__ + hdf.__all__ +
magic.__all__)
__all__ = (base.__all__ + detector_disabled.__all__ + ligolw.__all__ +
gracedb.__all__ + hdf.__all__ + magic.__all__)
# Copyright (C) 2017 Leo Singer
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
"""
Modify events by artificially disabling specified detectors.
"""
from .base import *
__all__ = ('DetectorDisabledEventSource',)
class DetectorDisabledEventSource(EventSource):
def __init__(self, base_source, disabled_detectors, raises=True):
self.base_source = base_source
self.disabled_detectors = set(disabled_detectors)
self.raises = raises
def __iter__(self):
return iter(self.base_source)
def __getitem__(self, key):
return DetectorDisabledEvent(self, self.base_source[key])
def __len__(self):
return len(self.base_source)
class DetectorDisabledEvent(Event):
def __init__(self, source, base_event):
self.source = source
self.base_event = base_event
@property
def singles(self):
disabled_detectors = self.source.disabled_detectors
if self.source.raises:
detectors = {s.instrument for s in self.base_event.singles}
if not detectors & disabled_detectors:
raise ValueError(
'Disabling detectors {{{}}} would have no effect on this '
'event with detectors {{{}}}'.format(
' '.join(disabled_detectors),
' '.join(detectors)))
if not detectors - disabled_detectors:
raise ValueError(
'Disabling detectors {{{}}} would exclude all data for '
'this event with detectors {{{}}}'.format(
' '.join(disabled_detectors),
' '.join(detectors)))
return tuple(s for s in self.base_event.singles
if s.instrument not in disabled_detectors)
@property
def template_args(self):
return self.base_event.template_args
open = DetectorDisabledEventSource
......@@ -11,6 +11,7 @@ import h5py
import numpy as np
import errno
import gzip
import re
from lalinference.io import events
......@@ -30,6 +31,10 @@ class MockGraceDb(object):
return gzip.GzipFile(path + '.gz', 'rb')
def raises(expected_exception, msg):
return pytest.raises(expected_exception, match='^' + re.escape(msg) + '$')
def test_ligolw():
"""Test reading events from LIGO-LW XML files."""
source = events.open(os.path.join(DATA_PATH, '2016_subset.xml.gz'))
......@@ -130,6 +135,45 @@ def test_gracedb():
'spin2z': -0.53029484}
def test_detector_disabled():
"""Test reading from event sources with certain detectors disabled."""
client = MockGraceDb()
graceids = ('G211117', 'G197392')
base_source = events.gracedb.open(graceids, client)
source = events.detector_disabled.open(base_source, ['H1'])
assert len(source) == 2
for graceid, (event_id, event) in zip(graceids, source.items()):
assert event_id == graceid
assert len(event.singles) == 1
assert event.singles[0].instrument == 'L1'
# Now test that exceptions are raised when they are called for.
expected_message = ('Disabling detectors {H1, L1} would have no effect on '
'this event with detectors {H1 L1}')
nonraising_source = events.detector_disabled.open(
base_source, ['H1, L1'], raises=False)
raising_source = events.detector_disabled.open(
base_source, ['H1, L1'])
for event in nonraising_source.values():
event.singles
for event in raising_source.values():
with raises(ValueError, expected_message):
event.singles
# Now test that exceptions are raised when they are called for.
expected_message = ('Disabling detectors {V1} would have no effect on '
'this event with detectors {H1 L1}')
nonraising_source = events.detector_disabled.open(
base_source, ['V1'], raises=False)
raising_source = events.detector_disabled.open(
base_source, ['V1'])
for event in nonraising_source.values():
event.singles
for event in raising_source.values():
with raises(ValueError, expected_message):
event.singles
def test_hdf(tmpdir):
"""Test reading events from HDF5 files."""
......
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