Skip to content

Commit c3a70a2

Browse files
authored
Final version of pycbc_pygrb_plot_snr_timeseries (#4955)
* Handling new option for pycbc_pygrb_plot_snr_timeseries jobs * Avoid single use of variable
1 parent fd06cfb commit c3a70a2

File tree

2 files changed

+74
-60
lines changed

2 files changed

+74
-60
lines changed

Diff for: bin/pygrb/pycbc_pygrb_plot_snr_timeseries

+71-60
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,6 @@ __program__ = "pycbc_pygrb_plot_snr_timeseries"
4848
# =============================================================================
4949
# Functions
5050
# =============================================================================
51-
# Load trigger data
52-
def load_data(input_file, ifos, vetoes, rw_snr_threshold=None,
53-
injections=False, slide_id=None):
54-
"""Load data from a trigger/injection file"""
55-
56-
trigs_or_injs = None
57-
if input_file:
58-
if injections:
59-
logging.info("Loading injections...")
60-
# This will eventually become load_injections
61-
trigs_or_injs = \
62-
ppu.load_triggers(input_file, ifos, vetoes,
63-
rw_snr_threshold=rw_snr_threshold,
64-
slide_id=slide_id)
65-
else:
66-
logging.info("Loading triggers...")
67-
trigs_or_injs = \
68-
ppu.load_triggers(input_file, ifos, vetoes,
69-
rw_snr_threshold=rw_snr_threshold,
70-
slide_id=slide_id)
71-
72-
return trigs_or_injs
73-
74-
7551
# Find start and end times of trigger/injecton data relative to a given time
7652
def get_start_end_times(data_time, central_time):
7753
"""Determine padded start and end times of data relative to central_time"""
@@ -108,6 +84,8 @@ parser.add_argument("--trigger-time", type=float, default=0,
10884
parser.add_argument("-y", "--y-variable", default=None,
10985
choices=['coherent', 'single', 'reweighted', 'null'],
11086
help="Quantity to plot on the vertical axis.")
87+
parser.add_argument("--onsource", default=False, action="store_true",
88+
help="Include onsource data in the plot (CAUTION!)")
11189
ppu.pygrb_add_bestnr_cut_opt(parser)
11290
ppu.pygrb_add_slide_opts(parser)
11391
opts = parser.parse_args()
@@ -132,48 +110,83 @@ outdir = os.path.split(os.path.abspath(opts.output_file))[0]
132110
if not os.path.isdir(outdir):
133111
os.makedirs(outdir)
134112

135-
# Extract IFOs and vetoes
136-
ifos, vetoes = ppu.extract_ifos_and_vetoes(trig_file, opts.veto_files,
137-
opts.veto_category)
113+
# Extract IFOs
114+
ifos = ppu.extract_ifos(trig_file)
115+
116+
# Generate time-slides dictionary
117+
slide_dict = ppu.load_time_slides(trig_file)
118+
119+
# Generate segments dictionary
120+
segment_dict = ppu.load_segment_dict(trig_file)
121+
122+
# Construct trials removing vetoed times
123+
trial_dict, total_trials = ppu.construct_trials(
124+
opts.seg_files,
125+
segment_dict,
126+
ifos,
127+
slide_dict,
128+
opts.veto_file,
129+
hide_onsource=(not opts.onsource)
130+
)
138131

139132
# Load trigger and injections data: when plotting reweighted SNR, keep all
140133
# points to show the impact of the cut, otherwise remove points with
141134
# reweighted SNR below threshold
142-
if snr_type == 'reweighted':
143-
trig_data = load_data(trig_file, ifos, vetoes,
135+
rw_snr_threshold = None if snr_type == 'reweighted' else opts.newsnr_threshold
136+
# When including the onsource, avoid printing to screen via logging the number
137+
# of triggers found
138+
data_tag = 'trigs' if opts.onsource else None
139+
trig_data = ppu.load_data(trig_file, ifos, data_tag=data_tag,
140+
rw_snr_threshold=rw_snr_threshold,
144141
slide_id=opts.slide_id)
145-
trig_data['network/reweighted_snr'] = \
146-
reweightedsnr_cut(trig_data['network/reweighted_snr'],
147-
opts.newsnr_threshold)
148-
inj_data = load_data(inj_file, ifos, vetoes, injections=True,
142+
inj_data = ppu.load_data(inj_file, ifos, data_tag='injs',
143+
rw_snr_threshold=rw_snr_threshold,
149144
slide_id=0)
150-
if inj_data is not None:
151-
inj_data['network/reweighted_snr'] = \
152-
reweightedsnr_cut(inj_data['network/reweighted_snr'],
153-
opts.newsnr_threshold)
154-
else:
155-
trig_data = load_data(trig_file, ifos, vetoes,
156-
rw_snr_threshold=opts.newsnr_threshold,
157-
slide_id=opts.slide_id)
158-
inj_data = load_data(inj_file, ifos, vetoes,
159-
rw_snr_threshold=opts.newsnr_threshold,
160-
injections=True, slide_id=0)
161145

162146
# Specify HDF file keys for x quantity (time) and y quantity (SNR)
163-
if snr_type == 'single':
147+
if opts.y_variable == 'single':
164148
x_key = opts.ifo + '/end_time'
165149
y_key = opts.ifo + '/snr'
166150
else:
167151
x_key = 'network/end_time_gc'
168-
y_key = 'network/' + snr_type + '_snr'
169-
170-
# Obtain times
171-
trig_data_time = trig_data[x_key][:]
172-
inj_data_time = inj_data[x_key][:] if inj_file else None
173-
174-
# Obtain SNRs
175-
trig_data_snr = trig_data[y_key][:]
176-
inj_data_snr = inj_data[y_key][:] if inj_file else None
152+
y_key = 'network/' + opts.y_variable + '_snr'
153+
154+
# Extract needed trigger properties and store them as dictionaries
155+
# Based on trial_dict: if vetoes were applied, trig_* are the veto survivors
156+
found_trigs = ppu.extract_trig_properties(
157+
trial_dict,
158+
trig_data,
159+
slide_dict,
160+
segment_dict,
161+
[x_key, y_key]
162+
)
163+
164+
# Gather injections found surviving vetoes
165+
found_after_vetoes, *_ = ppu.apply_vetoes_to_found_injs(
166+
opts.found_missed_file,
167+
inj_data,
168+
ifos,
169+
veto_file=opts.veto_file,
170+
keys=[x_key, y_key]
171+
)
172+
173+
# Obtain times and SNRs
174+
trig_data_time = numpy.concatenate([found_trigs[x_key][slide_id][:] for slide_id in slide_dict])
175+
trig_data_snr = numpy.concatenate([found_trigs[y_key][slide_id][:] for slide_id in slide_dict])
176+
inj_data_time = found_after_vetoes[x_key][:] if inj_file else None
177+
inj_data_snr = found_after_vetoes[y_key][:] if inj_file else None
178+
179+
# Apply reweighted SNR threshold keeping downweighted points
180+
if snr_type == 'reweighted':
181+
trig_data_snr = reweightedsnr_cut(
182+
trig_data_snr,
183+
opts.newsnr_threshold
184+
)
185+
if inj_file:
186+
inj_data_snr = reweightedsnr_cut(
187+
inj_data_snr,
188+
opts.newsnr_threshold
189+
)
177190

178191
# Determine the central time (t=0) for the plot
179192
central_time = opts.trigger_time
@@ -191,7 +204,7 @@ logging.info("Plotting...")
191204

192205
# Determine what goes on the vertical axis
193206
y_labels = {'coherent': "Coherent SNR",
194-
'single': "%s SNR" % ifo,
207+
'single': f"{ifo} SNR",
195208
'null': "Null SNR",
196209
'reweighted': "Reweighted SNR"}
197210
y_label = y_labels[snr_type]
@@ -205,11 +218,9 @@ if opts.plot_caption is None:
205218
opts.plot_caption += ("Red crosses: injections triggers.")
206219

207220
# Single IFO SNR versus time plots
208-
xlims = [start, end]
209-
if opts.x_lims:
210-
xlims = opts.x_lims
211-
xlims = map(float, xlims.split(','))
221+
if not opts.x_lims:
222+
opts.x_lims = f"{start},{end}"
212223
plu.pygrb_plotter([trig_data_time, trig_data_snr],
213224
[inj_data_time, inj_data_snr],
214-
"Time since %.3f (s)" % (central_time), y_label,
225+
f"Time since {central_time:.3f} (s)", y_label,
215226
opts, cmd=' '.join(sys.argv))

Diff for: pycbc/workflow/grb_utils.py

+3
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,9 @@ def make_pygrb_plot(workflow, exec_name, out_dir,
483483
node.add_input_list_opt('--seg-files', seg_files)
484484
if veto_file:
485485
node.add_input_opt('--veto-file', veto_file)
486+
# Option to show the onsource trial if this is a plot of all data
487+
if exec_name == 'pygrb_plot_snr_timeseries' and 'alltimes' in tags:
488+
node.add_opt('--onsource')
486489
if exec_name in ['pygrb_plot_injs_results',
487490
'pygrb_plot_snr_timeseries']:
488491
trig_time = workflow.cp.get('workflow', 'trigger-time')

0 commit comments

Comments
 (0)