Skip to content

Commit

Permalink
Workflow changes needed to manage preparation of offline gracedb uplo…
Browse files Browse the repository at this point in the history
…ad files
  • Loading branch information
GarethCabournDavies committed Nov 6, 2023
1 parent 642b08f commit 292ab2c
Show file tree
Hide file tree
Showing 7 changed files with 694 additions and 101 deletions.
64 changes: 32 additions & 32 deletions bin/minifollowups/pycbc_foreground_minifollowup
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,33 @@ if args.sort_variable not in f[file_val]:
raise KeyError(f'Sort variable {args.sort_variable} not in {file_val}: sort'
f'choices in {file_val} are ' + ', '.join(all_datasets))

# In case we are doing background minifollowup with repeated events,
# we must include the ordering / template / trigger / time info for
# _many_ more events to make sure we get enough
events_to_read = num_events * 100

# Hold your horses! - we've asked for more events than there are!
if len(stat) < num_events:
num_events = len(stat)
if len(stat) < events_to_read:
events_to_read = len(stat)

# Get the indices of the events we are considering in the order specified
sorting = f[file_val + '/' + args.sort_variable][:].argsort()
if args.sort_order == 'descending':
sorting = sorting[::-1]
event_idx = sorting[0:events_to_read]
stat = stat[event_idx]

# Save the time / trigger / template ids for the events
times = {}
tids = {}
# Version used for multi-ifo coinc code
ifo_list = f.attrs['ifos'].split(' ')
f_cat = f[file_val]
for ifo in ifo_list:
times[ifo] = f[f'{file_val}/{ifo}/time'][:][event_idx]
tids[ifo] = f[f'{file_val}/{ifo}/trigger_id'][:][event_idx]
bank_ids = f_cat['template_id'][:][event_idx]

bank_data = h5py.File(args.bank_file, 'r')

Expand Down Expand Up @@ -187,7 +194,7 @@ while event_count < num_events and curr_idx < (events_to_read - 1):
tags=[f'SKIP_{event_count}']),)
skipped_data = []

bank_id = f[f'{file_val}/template_id'][:][sorting][curr_idx]
bank_id = bank_ids[curr_idx]

layouts += (mini.make_coinc_info(workflow, single_triggers, tmpltbank_file,
coinc_file, args.output_dir, n_loudest=curr_idx,
Expand All @@ -197,40 +204,33 @@ while event_count < num_events and curr_idx < (events_to_read - 1):
ifo_times, args.output_dir, special_tids=ifo_tids,
tags=args.tags + [str(event_count)])

params = {}
for ifo in times:
params['%s_end_time' % ifo] = times[ifo][curr_idx]
try:
# Only present for precessing case
params['u_vals_%s' % ifo] = \
fsdt[ifo][ifo]['u_vals'][tids[ifo][curr_idx]]
except:
pass

params['mass1'] = bank_data['mass1'][bank_id]
params['mass2'] = bank_data['mass2'][bank_id]
params['spin1z'] = bank_data['spin1z'][bank_id]
params['spin2z'] = bank_data['spin2z'][bank_id]
params['f_lower'] = bank_data['f_lower'][bank_id]
# don't require precessing template info if not present
try:
params['spin1x'] = bank_data['spin1x'][bank_id]
params['spin1y'] = bank_data['spin1y'][bank_id]
params['spin2x'] = bank_data['spin2x'][bank_id]
params['spin2y'] = bank_data['spin2y'][bank_id]
params['inclination'] = bank_data['inclination'][bank_id]
except KeyError:
pass

files += mini.make_single_template_plots(workflow, insp_segs,
args.inspiral_data_read_name,
args.inspiral_data_analyzed_name, params,
args.output_dir,
tags=args.tags + [str(event_count)])
params = mini.get_single_template_params(
curr_idx,
times,
bank_data,
bank_ids[curr_idx],
fsdt,
tids
)

_, sngl_tmplt_plots = mini.make_single_template_plots(
workflow,
insp_segs,
args.inspiral_data_read_name,
args.inspiral_data_analyzed_name,
params,
args.output_dir,
data_segments=insp_data_seglists,
tags=args.tags + [str(event_count)]
)
files += sngl_tmplt_plots


for single in single_triggers:
time = times[single.ifo][curr_idx]
if time==-1:
# If this detector did not trigger, still make the plot, but use
# the average time of detectors which did trigger
time = coinc.mean_if_greater_than_zero([times[sngl.ifo][curr_idx]
for sngl in single_triggers])[0]
for seg in insp_analysed_seglists[single.ifo]:
Expand Down
17 changes: 11 additions & 6 deletions bin/minifollowups/pycbc_injection_minifollowup
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ for num_event in range(num_events):

ifo_times += ' %s:%s ' % (ifo, ifo_time)
inj_params[ifo + '_end_time'] = ifo_time
all_times = [inj_params[sngl.ifo + '_end_time'] for sngl in single_triggers]
inj_params['mean_time'] = coinc.mean_if_greater_than_zero(all_times)[0]

layouts += [(mini.make_inj_info(workflow, injection_file, injection_index, num_event,
args.output_dir, tags=args.tags + [str(num_event)])[0],)]
Expand Down Expand Up @@ -256,8 +258,7 @@ for num_event in range(num_events):
for single in single_triggers:
checkedtime = time
if (inj_params[single.ifo + '_end_time'] == -1.0):
all_times = [inj_params[sngl.ifo + '_end_time'] for sngl in single_triggers]
checkedtime = coinc.mean_if_greater_than_zero(all_times)[0]
checkedtime = inj_params['mean_time']
for seg in insp_analysed_seglists[single.ifo]:
if checkedtime in seg:
files += mini.make_singles_timefreq(workflow, single, tmpltbank_file,
Expand All @@ -275,16 +276,17 @@ for num_event in range(num_events):
'skipping singles plots'.format(checkedtime,
single.ifo))

files += mini.make_single_template_plots(workflow, insp_segs,
_, norm_plot = mini.make_single_template_plots(workflow, insp_segs,
args.inspiral_data_read_name,
args.inspiral_data_analyzed_name, inj_params,
args.output_dir, inj_file=injection_xml_file,
tags=args.tags+['INJ_PARAMS',str(num_event)],
params_str='injection parameters as template, ' +\
'here the injection is made as normal',
use_exact_inj_params=True)
files += norm_plot

files += mini.make_single_template_plots(workflow, insp_segs,
_, inv_plot = mini.make_single_template_plots(workflow, insp_segs,
args.inspiral_data_read_name,
args.inspiral_data_analyzed_name, inj_params,
args.output_dir, inj_file=injection_xml_file,
Expand All @@ -293,8 +295,9 @@ for num_event in range(num_events):
params_str='injection parameters as template, ' +\
'here the injection is made inverted',
use_exact_inj_params=True)
files += inv_plot

files += mini.make_single_template_plots(workflow, insp_segs,
_, noinj_plot = mini.make_single_template_plots(workflow, insp_segs,
args.inspiral_data_read_name,
args.inspiral_data_analyzed_name, inj_params,
args.output_dir, inj_file=injection_xml_file,
Expand All @@ -303,6 +306,7 @@ for num_event in range(num_events):
params_str='injection parameters, here no ' +\
'injection was actually performed',
use_exact_inj_params=True)
files += noinj_plot

for curr_ifo in args.single_detector_triggers:
single_fname = args.single_detector_triggers[curr_ifo]
Expand Down Expand Up @@ -341,12 +345,13 @@ for num_event in range(num_events):

curr_tags = ['TMPLT_PARAMS_%s' %(curr_ifo,)]
curr_tags += [str(num_event)]
files += mini.make_single_template_plots(workflow, insp_segs,
_, loudest_plot = mini.make_single_template_plots(workflow, insp_segs,
args.inspiral_data_read_name,
args.inspiral_data_analyzed_name, curr_params,
args.output_dir, inj_file=injection_xml_file,
tags=args.tags + curr_tags,
params_str='loudest template in %s' % curr_ifo )
files += loudest_plot

layouts += list(layout.grouper(files, 2))
num_event += 1
Expand Down
9 changes: 7 additions & 2 deletions bin/minifollowups/pycbc_sngl_minifollowup
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ for rank, num_event in enumerate(order):
curr_params['spin2z'] = trigs.spin2z[num_event]
curr_params['f_lower'] = trigs.f_lower[num_event]
curr_params[args.instrument + '_end_time'] = time
curr_params['mean_time'] = time
# don't require precessing template info if not present
try:
curr_params['spin1x'] = trigs.spin1x[num_event]
Expand All @@ -207,12 +208,16 @@ for rank, num_event in enumerate(order):
except:
pass

files += mini.make_single_template_plots(workflow, insp_segs,
_, sngl_plot = mini.make_single_template_plots(workflow, insp_segs,
args.inspiral_data_read_name,
args.inspiral_data_analyzed_name, curr_params,
args.inspiral_data_analyzed_name,
curr_params,
args.output_dir,
data_segments={args.instrument : insp_data_seglists},
tags=args.tags+[str(rank)])

files += sngl_plot

files += mini.make_plot_waveform_plot(workflow, curr_params,
args.output_dir, [args.instrument],
tags=args.tags + [str(rank)])
Expand Down
191 changes: 191 additions & 0 deletions bin/minifollowups/pycbc_upload_prep_minifollowup
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#!/bin/env python
# Copyright (C) 2015-2023 Alexander Harvey Nitz, Gareth Cabourn Davies
#
# 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 3 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.
""" Prepare files for upload to GraceDB for foreground events
"""
import os, sys, argparse, logging, re, h5py, pycbc.workflow as wf
from pycbc.results import layout
from pycbc.types import MultiDetOptionAction
from pycbc.events import select_segments_by_definer, coinc
from pycbc.io import get_all_subkeys
import pycbc.workflow.minifollowups as mini
from pycbc.workflow.core import resolve_url_to_file, resolve_td_option
from ligo import segments

import numpy as np
import pycbc.version

parser = argparse.ArgumentParser(description=__doc__[1:])
parser.add_argument('--verbose', action='count',
help='Add progressively more verbose output, '
'default=info')
parser.add_argument('--version', action='version', version=pycbc.version.git_verbose_msg)
parser.add_argument('--bank-file',
help="HDF format template bank file")
parser.add_argument('--statmap-file',
help="HDF format clustered coincident trigger result file")
parser.add_argument('--xml-all-file',
help="XML format result file containing all events")
parser.add_argument('--single-detector-triggers', nargs='+', action=MultiDetOptionAction,
help="HDF format merged single detector trigger files")
parser.add_argument('--inspiral-segments',
help="xml segment files containing the inspiral analysis times")
parser.add_argument('--inspiral-data-read-name',
help="Name of inspiral segmentlist containing data read in "
"by each analysis job.")
parser.add_argument('--inspiral-data-analyzed-name',
help="Name of inspiral segmentlist containing data "
"analyzed by each analysis job.")
parser.add_argument('--psd-files', nargs='+', action=MultiDetOptionAction,
help="HDF format merged single detector PSD files")
parser.add_argument('--ifar-thresh', type=float,
help="IFAR threshold for preparing SNR timeseries "
"files for upload. Default=No upload prep")

wf.add_workflow_command_line_group(parser)
wf.add_workflow_settings_cli(parser, include_subdax_opts=True)
args = parser.parse_args()

if args.verbose:
args.verbose += 1
else:
args.verbose = 1
pycbc.init_logging(args.verbose)

workflow = wf.Workflow(args)

wf.makedir(args.output_dir)

channel_opts = {}
for ifo in workflow.ifos:
channel_opts[ifo] = workflow.cp.get_opt_tags(
"workflow",
"%s-channel-name" % ifo.lower(),
"")

# create a FileList that will contain all output files
layouts = []

tmpltbank_file = resolve_url_to_file(os.path.abspath(args.bank_file))
insp_segs = resolve_url_to_file(os.path.abspath(args.inspiral_segments))
xml_all = resolve_url_to_file(os.path.abspath(args.xml_all_file))

single_triggers = []
psd_files = []
fsdt = {}
insp_data_seglists = {}
insp_analysed_seglists = {}
for ifo in args.single_detector_triggers:
strig_fname = args.single_detector_triggers[ifo]
strig_file = resolve_url_to_file(os.path.abspath(strig_fname),
attrs={'ifos': ifo})
single_triggers.append(strig_file)

psd_fname = args.psd_files[ifo]
psd_file = resolve_url_to_file(os.path.abspath(psd_fname),
attrs={'ifos': ifo})
psd_files.append(psd_file)

fsdt[ifo] = h5py.File(args.single_detector_triggers[ifo], 'r')
insp_data_seglists[ifo] = select_segments_by_definer(
args.inspiral_segments,
segment_name=args.inspiral_data_read_name,
ifo=ifo)
insp_analysed_seglists[ifo] = select_segments_by_definer(
args.inspiral_segments,
segment_name=args.inspiral_data_analyzed_name,
ifo=ifo)
# NOTE: make_singles_timefreq needs a coalesced set of segments. If this is
# being used to determine command-line options for other codes,
# please think if that code requires coalesced, or not, segments.
insp_data_seglists[ifo].coalesce()
insp_analysed_seglists[ifo].coalesce()

f = h5py.File(args.statmap_file, 'r')
stat = f['foreground/stat'][:]

bank_data = h5py.File(args.bank_file, 'r')

ifar_limit = args.ifar_thresh
# Get indices of all events which pass the IFAR threshold
event_ifars = f['foreground/ifar'][:]
events_to_read = np.count_nonzero(event_ifars > ifar_limit)
# Sort by IFAR, descending
event_idx = event_ifars.argsort()[::-1][:events_to_read]
# Times and tids need to be reset for this set of events:
times = {}
tids = {}
bank_ids = {}

ifo_list = f.attrs['ifos'].split(' ')
for ifo in ifo_list:
times[ifo] = f[f'foreground/{ifo}/time'][:][event_idx]
tids[ifo] = f[f'foreground/{ifo}/trigger_id'][:][event_idx]
bank_ids = f['foreground/template_id'][:][event_idx]

for curr_idx in range(event_idx.size):
params = mini.get_single_template_params(
curr_idx,
times,
bank_data,
bank_ids[curr_idx],
fsdt,
tids
)
# Extract approximant
appx = params['approximant']
del params['approximant']

channel_name = ""
for ifo in ifo_list:
ifo_chname = resolve_td_option(
channel_opts[ifo],
segments.segment(params['mean_time'], params['mean_time'])
)
channel_name += ifo_chname + " "

single_temp_files = []
for ifo in ifo_list:
if params['mean_time'] not in insp_analysed_seglists[ifo]:
logging.info("Mean time %.3f not in segment list",
params['mean_time'])
continue
# Make single-template files to put into the XML file for upload
single_temp_files += mini.make_single_template_files(
workflow,
insp_segs,
ifo,
args.inspiral_data_read_name,
args.inspiral_data_analyzed_name,
params,
args.output_dir,
store_file=True,
tags=args.tags+['upload', str(curr_idx)],
)

mini.make_upload_files(
workflow,
psd_files,
single_temp_files,
xml_all,
curr_idx,
appx,
args.output_dir,
channel_name,
tags=args.tags+['upload', str(curr_idx)]
)

workflow.save()
Loading

0 comments on commit 292ab2c

Please sign in to comment.