Skip to content

Commit

Permalink
Workflow changes needed to manage offline gracedb upload prep (#4535)
Browse files Browse the repository at this point in the history
* Workflow changes needed to manage preparation of offline gracedb upload files

* fix change removed after rebase

* Allow waveforms to not be given

* IH comments
  • Loading branch information
GarethCabournDavies committed Feb 5, 2024
1 parent ec8bd5d commit 806750c
Show file tree
Hide file tree
Showing 7 changed files with 716 additions and 104 deletions.
78 changes: 43 additions & 35 deletions bin/minifollowups/pycbc_foreground_minifollowup
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
""" Followup foreground events
"""
import os, sys, argparse, logging, re, h5py, pycbc.workflow as wf
import os
import sys
import argparse
import logging
import re
import h5py

import pycbc.workflow as wf
from pycbc.results import layout
from pycbc.types import MultiDetOptionAction
from pycbc.events import select_segments_by_definer, coinc
Expand Down Expand Up @@ -108,26 +115,34 @@ 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

# 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]
times[ifo] = f_cat[ifo]['time'][:][event_idx]
tids[ifo] = f_cat[ifo]['trigger_id'][:][event_idx]
bank_ids = f_cat['template_id'][:][event_idx]
f.close()

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

Expand Down Expand Up @@ -187,7 +202,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 +212,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 @@ -294,6 +294,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 @@ -333,8 +335,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 @@ -353,16 +354,17 @@ for num_event in range(num_events):
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 @@ -371,8 +373,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 @@ -381,6 +384,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:
# Finding loudest template in this detector near to the injection:
Expand Down Expand Up @@ -431,12 +435,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))

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 @@ -291,6 +291,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 @@ -306,12 +307,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
Loading

0 comments on commit 806750c

Please sign in to comment.