74 gi.require_version(
'Gst',
'1.0')
75 from gi.repository
import GObject, Gst
76 GObject.threads_init()
80 from gstlal
import datasource
81 from gstlal
import multirate_datasource
82 from gstlal
import pipeparts
83 from gstlal
import pipeio
84 from gstlal.stats.inspiral_lr
import LnLRDensity
85 from lal
import iterutils
128 def mkcontrolsnksrc(pipeline, rate, verbose = False, suffix = None, control_peak_samples = None):
130 This function implements a portion of a gstreamer graph to provide a
131 control signal for deciding when to reconstruct physical SNRS
133 @param pipeline A reference to the gstreamer pipeline in which to add this graph
134 @param rate An integer representing the target sample rate of the resulting src
135 @param verbose Make verbose
136 @param suffix Log name for verbosity
137 @param control_peak_samples If nonzero, this would do peakfinding on the control signal with the window specified by this parameter. The peak finding would give a single sample of "on" state at the peak. This will cause far less CPU to be used if you only want to reconstruct SNR around the peak of the control signal.
143 snk = pipeparts.mkadder(pipeline,
None)
144 src = pipeparts.mkcapsfilter(pipeline, snk,
"audio/x-raw, rate=%d" % rate)
150 if control_peak_samples > 0:
151 src = pipeparts.mkpeak(pipeline, src, control_peak_samples)
157 logname = suffix
and "_%s" % suffix
or ""
159 src = pipeparts.mkprogressreport(pipeline, src,
"progress_sumsquares%s" % logname)
160 src = pipeparts.mkchecktimestamps(pipeline, src,
"timestamps%s_sumsquares" % logname)
161 src = pipeparts.mktee(pipeline, src)
230 def mkLLOIDbranch(pipeline, src, bank, bank_fragment, control_input_tpl, gate_attack_length, gate_hold_length, block_duration, nxydump_segment = None, fir_stride = None, control_peak_time = None, reconstruction_segment_list = None):
232 Make a single slice of one branch of the lloid graph, e.g. one instrument and one
233 template bank fragment. For details see: http://arxiv.org/abs/1107.2665
235 Specifically this implements the filtering of multirate svd basis and
236 (conditional) resampling and reconstruction of the physical SNR
238 @param pipeline The gstreamer pipeline in which to place this graph
239 @param src The source of data for this graph provided by a gstreamer element
240 @param bank The template bank class
241 @param bank_fragment The specific fragment (time slice) of the template bank in question
242 @param (control_snk, control_src) An optional tuple of the sink and source elements for a graph that will construct a control time series for the gate which aggregates the orthogonal snrs from each template slice. This is used to conditionally reconstruct the physical SNR of interesting times
243 @param gate_attack_length The attack length in samples for the lal_gate element that controls the reconstruction of physical SNRs
244 @param gate_hold_length The hold length in samples for the lal_gate element that controls the reconstruction of physical SNRs
245 @param block_duration The characteristic buffer size that is passed around, which is useful for constructing queues.
246 @param nxydump_segment Not used
247 @param fir_stride The target length of output buffers from lal_firbank in seconds. Directly effects latency. Making this short will force time-domain convolution. Otherwise FFT convolution will be done to save CPU cycles, but at higher latency.
248 @param control_peak_time The window over which to find peaks in the control signal. Shorter windows increase computational cost but probably also detection efficiency.
249 @param reconstruction_segment_list A segment list object that describes when the control signal should be on. This can be useful in e.g., only reconstructing physical SNRS around the time of injections, which can save an enormous amount of CPU time.
251 logname =
"%s_%.2f.%.2f" % (bank.logname, bank_fragment.start, bank_fragment.end)
253 control_snk, control_src = control_input_tpl
261 latency = -int(round(bank_fragment.start * bank_fragment.rate))
262 block_stride = int(fir_stride * bank_fragment.rate)
268 time_domain = 5 * numpy.log2((bank_fragment.end - bank_fragment.start) * bank_fragment.rate + block_stride) > block_stride
270 src = pipeparts.mkfirbank(pipeline, src, latency = latency, fir_matrix = bank_fragment.orthogonal_template_bank, block_stride = block_stride, time_domain = time_domain)
271 src = pipeparts.mkchecktimestamps(pipeline, src,
"timestamps_%s_after_firbank" % logname)
286 if control_snk
is not None and control_src
is not None:
287 src = pipeparts.mktee(pipeline, src)
288 elem = pipeparts.mkqueue(pipeline, pipeparts.mksumsquares(pipeline, src, weights = bank_fragment.sum_of_squares_weights), max_size_buffers = 0, max_size_bytes = 0, max_size_time = block_duration)
289 elem = pipeparts.mkchecktimestamps(pipeline, elem,
"timestamps_%s_after_sumsquare" % logname)
290 elem = pipeparts.mkresample(pipeline, elem, quality = 9)
291 elem = pipeparts.mkchecktimestamps(pipeline, elem,
"timestamps_%s_after_sumsquare_resampler" % logname)
292 elem.link(control_snk)
317 src = pipeparts.mkgate(
319 pipeparts.mkqueue(pipeline, src, max_size_buffers = 0, max_size_bytes = 0, max_size_time = 1 * int((2. * control_peak_time + float(abs(gate_attack_length) + abs(gate_hold_length)) / bank_fragment.rate) * Gst.SECOND)),
321 attack_length = gate_attack_length,
322 hold_length = gate_hold_length,
323 control = control_src
325 src = pipeparts.mkchecktimestamps(pipeline, src,
"timestamps_%s_after_gate" % logname)
335 if reconstruction_segment_list
is not None:
336 src = datasource.mksegmentsrcgate(pipeline, src, reconstruction_segment_list, invert_output =
False)
342 if bank_fragment.mix_matrix
is not None:
343 src = pipeparts.mkmatrixmixer(pipeline, src, matrix = bank_fragment.mix_matrix)
344 src = pipeparts.mkchecktimestamps(pipeline, src,
"timestamps_%s_after_matrixmixer" % logname)
351 del bank_fragment.orthogonal_template_bank
352 del bank_fragment.sum_of_squares_weights
353 del bank_fragment.mix_matrix
358 def mkLLOIDhoftToSnrSlices(pipeline, hoftdict, bank, control_snksrc, block_duration, verbose = False, logname = "", nxydump_segment = None, fir_stride = None, control_peak_time = None, snrslices = None, reconstruction_segment_list = None):
360 Build the pipeline fragment that creates the SnrSlices associated with
361 different sample rates from hoft.
363 @param reconstruction_segment_list A segment list object that describes when the control signal should be on. This can be useful in e.g., only reconstructing physical SNRS around the time of injections, which can save an enormous amount of CPU time.
369 rates = sorted(bank.get_rates())
370 output_rate = max(rates)
373 upsample_factor = dict((rate, rates[i+1] / rate)
for i, rate
in list(enumerate(rates))[:-1])
374 upsample_factor[output_rate] = 0
376 autocorrelation_length = bank.autocorrelation_bank.shape[1]
377 assert autocorrelation_length % 2 == 1
378 autocorrelation_latency = -(autocorrelation_length - 1) / 2
384 branch_heads = dict((rate, set())
for rate
in rates)
385 for bank_fragment
in bank.bank_fragments:
400 peak_half_width = upsample_factor[bank_fragment.rate] * 16 + int(math.ceil(-autocorrelation_latency * (float(bank_fragment.rate) / output_rate))) + int(math.ceil(bank_fragment.rate / 8.))
408 pipeparts.mkqueue(pipeline, hoftdict[bank_fragment.rate], max_size_bytes = 0, max_size_buffers = 0, max_size_time = int((1 * fir_stride + int(math.ceil(bank.filter_length))) * Gst.SECOND)),
415 nxydump_segment = nxydump_segment,
416 fir_stride = fir_stride,
417 control_peak_time = control_peak_time,
418 reconstruction_segment_list = reconstruction_segment_list
430 if snrslices
is not None:
431 rate, heads = output_rate, branch_heads[output_rate]
439 branch_heads[rate] = pipeparts.mkadder(pipeline, (pipeparts.mkqueue(pipeline, head, max_size_bytes = 0, max_size_buffers = 0, max_size_time = block_duration)
for head
in heads))
446 branch_heads[rate], = heads
447 branch_heads[rate] = pipeparts.mktee(pipeline, branch_heads[rate])
448 snrslices[rate] = pipeparts.mktogglecomplex(pipeline, branch_heads[rate])
454 branch_heads[rate] = set([branch_heads[rate]])
461 next_rate = dict(zip(rates, rates[1:]))
463 next_rate = dict((rate, output_rate)
for rate
in rates
if rate != output_rate)
465 for rate, heads
in sorted(branch_heads.items()):
473 branch_heads[rate] = pipeparts.mkadder(pipeline, heads)
474 branch_heads[rate] = pipeparts.mkchecktimestamps(pipeline, branch_heads[rate],
"timestamps_%s_after_%d_snr_adder" % (logname, rate))
481 branch_heads[rate], = heads
487 if rate
in next_rate:
498 branch_heads[rate] = pipeparts.mkcapsfilter(pipeline, pipeparts.mkinterpolator(pipeline, branch_heads[rate]),
"audio/x-raw, rate=%d" % next_rate[rate])
499 branch_heads[rate] = pipeparts.mkchecktimestamps(pipeline, branch_heads[rate],
"timestamps_%s_after_%d_to_%d_snr_resampler" % (logname, rate, next_rate[rate]))
507 if snrslices
is not None and rate != output_rate:
508 branch_heads[rate] = pipeparts.mktee(pipeline, branch_heads[rate])
509 snrslices[rate] = pipeparts.mktogglecomplex(pipeline, branch_heads[rate])
515 if rate
in next_rate:
516 branch_heads[next_rate[rate]].add(branch_heads.pop(rate))
522 snr, = branch_heads.values()
523 return pipeparts.mktogglecomplex(pipeline, snr)
528 Build pipeline fragment that computes the TimeSliceChisq from SnrSlices.
534 rates = sorted(bank.get_rates())
540 chifacsdict = dict((rate, [])
for rate
in rates)
541 for bank_fragment
in bank.bank_fragments:
542 chifacsdict[bank_fragment.rate].append(bank_fragment.chifacs)
544 for rate, facs
in sorted(chifacsdict.items()):
545 chifacs.append(facs[0][0::2])
546 chifacs[-1] += facs[0][1::2]
548 chifacs[-1] += fac[0::2]
549 chifacs[-1] += fac[1::2]
556 chisq = Gst.ElementFactory.make(
"lal_timeslicechisq",
None)
563 for rate, snrslice
in sorted(branch_heads.items()):
564 pipeparts.mkqueue(pipeline, snrslice, max_size_bytes = 0, max_size_buffers = 0, max_size_time = block_duration).link(chisq)
570 chisq.set_property(
"chifacs-matrix", chifacs)
572 return pipeparts.mkqueue(pipeline, chisq, max_size_bytes = 0, max_size_buffers = 0, max_size_time = block_duration)
577 Build pipeline fragment that converts single detector SNR and Chisq
584 head = pipeparts.mktriggergen(pipeline, snr, chisq, template_bank_filename = bank.template_bank_filename, snr_threshold = bank.snr_threshold, sigmasq = bank.sigmasq)
588 head = pipeparts.mkprogressreport(pipeline, head,
"progress_xml_%s" % logname)
602 def mkLLOIDmulti(pipeline, detectors, banks, psd, psd_fft_length = 32, ht_gate_threshold = float(
"inf"), veto_segments =
None, verbose =
False, nxydump_segment =
None, chisq_type =
'autochisq', track_psd =
False, fir_stride = 16, control_peak_time = 2, block_duration = Gst.SECOND, reconstruction_segment_list =
None, track_latency =
False):
604 The multiple instrument, multiple bank LLOID algorithm
611 if chisq_type
not in [
'autochisq',
'timeslicechisq']:
612 raise ValueError(
"chisq_type must be either 'autochisq' or 'timeslicechisq', given %s" % chisq_type)
613 if any(tuple(iterutils.nonuniq(bank.bank_id
for bank
in banklist))
for banklist
in banks.values()):
614 raise ValueError(
"bank IDs are not unique: %s" %
"; ".join(
"for %s: %s" % (instrument, iterutils.nonuniq(bank.bank_id
for bank
in banklist))
for instrument, banklist
in banks.items()))
624 assert psd_fft_length % 4 == 0,
"psd_fft_length (= %g) must be multiple of 4" % psd_fft_length
626 for instrument
in detectors.channel_dict:
627 src, statevector, dqvector = datasource.mkbasicsrc(pipeline, detectors, instrument, verbose)
629 src = pipeparts.mklatency(pipeline, src, name =
"%s_datasource_latency" % instrument, silent =
True)
630 hoftdicts[instrument] = multirate_datasource.mkwhitened_multirate_src(
633 rates = set(rate
for bank
in banks[instrument]
for rate
in bank.get_rates()),
634 instrument = instrument,
635 psd = psd[instrument],
636 psd_fft_length = psd_fft_length,
637 ht_gate_threshold = ht_gate_threshold,
638 veto_segments = veto_segments[instrument]
if veto_segments
is not None else None,
639 nxydump_segment = nxydump_segment,
640 track_psd = track_psd,
642 statevector = statevector,
644 fir_whiten_reference_psd = banks[instrument][0].processed_psd,
645 track_latency = track_latency,
652 if control_peak_time > 0:
654 for instrument, bank
in [(instrument, bank)
for instrument, banklist
in banks.items()
for bank
in banklist]:
655 suffix =
"%s%s" % (instrument, (bank.logname
and "_%s" % bank.logname
or ""))
656 if instrument !=
"H2":
657 control_branch[(instrument, bank.bank_id)] =
mkcontrolsnksrc(pipeline, max(bank.get_rates()), verbose = verbose, suffix = suffix, control_peak_samples = control_peak_time * max(bank.get_rates()))
660 control_branch =
None
668 for i, (instrument, bank)
in enumerate([(instrument, bank)
for instrument, banklist
in banks.items()
for bank
in banklist]):
669 suffix =
"%s%s" % (instrument, (bank.logname
and "_%s" % bank.logname
or ""))
670 if control_branch
is not None:
671 if instrument !=
"H2":
672 control_snksrc = control_branch[(instrument, bank.bank_id)]
674 control_snksrc = (
None, control_branch[(
"H1", bank.bank_id)][1])
676 control_snksrc = (
None,
None)
677 if chisq_type ==
'timeslicechisq':
683 hoftdicts[instrument],
689 nxydump_segment = nxydump_segment,
690 control_peak_time = control_peak_time,
691 fir_stride = fir_stride,
692 snrslices = snrslices,
693 reconstruction_segment_list = reconstruction_segment_list
695 if instrument
not in checked_ifo:
697 snr = pipeparts.mklatency(pipeline, snr, name =
"%s_snrSlice_latency" % instrument, silent =
True)
698 checked_ifo.append(instrument)
699 snr = pipeparts.mkchecktimestamps(pipeline, snr,
"timestamps_%s_snr" % suffix)
703 if chisq_type ==
'autochisq':
708 nsamps_window = 1 * max(bank.get_rates())
709 if bank.bank_id
not in itacac_dict:
710 itacac_dict[bank.bank_id] = pipeparts.mkgeneric(pipeline,
None,
"lal_itacac")
712 head = itacac_dict[bank.bank_id]
713 pad = head.get_request_pad(
"sink%d" % len(head.sinkpads))
714 if instrument ==
'H1' or instrument ==
'L1':
715 for prop, val
in [(
"n", nsamps_window), (
"snr-thresh", LnLRDensity.snr_min), (
"bank_filename", bank.template_bank_filename), (
"sigmasq", bank.sigmasq), (
"autocorrelation_matrix", pipeio.repack_complex_array_to_real(bank.autocorrelation_bank)), (
"autocorrelation_mask", bank.autocorrelation_mask)]:
716 pad.set_property(prop, val)
718 for prop, val
in [(
"n", nsamps_window), (
"snr-thresh", LnLRDensity.snr_min), (
"bank_filename", bank.template_bank_filename), (
"sigmasq", bank.sigmasq), (
"autocorrelation_matrix", pipeio.repack_complex_array_to_real(bank.autocorrelation_bank)), (
"autocorrelation_mask", bank.autocorrelation_mask)]:
719 pad.set_property(prop, val)
721 raise NotImplementedError(
"Currently only 'autochisq' is supported")
723 snr.srcpads[0].link(pad)
726 del bank.autocorrelation_bank
734 assert any(itacac_dict.values())
736 itacac_dict[list(itacac_dict.keys())[0]] = pipeparts.mklatency(pipeline, itacac_dict[list(itacac_dict.keys())[0]], name =
"all_itacac_latency", silent =
True)
738 for bank_id, head
in itacac_dict.items():
741 itacac_dict[bank_id] = pipeparts.mkprogressreport(pipeline, pipeparts.mkqueue(pipeline, head, max_size_buffers = 10, max_size_bytes = 0, max_size_time = 0),
"progress_xml_bank_%s" % bank_id)
def mkLLOIDbranch
Gstreamer graph describing this function:
def mkcontrolsnksrc
A "sum-of-squares" aggregator.
def mkLLOIDmulti
The multiple instrument, multiple bank LLOID algorithm.
def mkLLOIDSnrChisqToTriggers
Build pipeline fragment that converts single detector SNR and Chisq into triggers.
def mkLLOIDhoftToSnrSlices
Build the pipeline fragment that creates the SnrSlices associated with different sample rates from ho...
def mkLLOIDSnrSlicesToTimeSliceChisq
Build pipeline fragment that computes the TimeSliceChisq from SnrSlices.