Skip to content

Commit 623f65a

Browse files
committed
internally call inject_signal function for glitch injection task
1 parent c8c10b3 commit 623f65a

1 file changed

Lines changed: 38 additions & 31 deletions

File tree

bilby/gw/detector/interferometer.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -401,42 +401,42 @@ def check_signal_duration(self, parameters, raise_error=True):
401401
logger.warning(msg)
402402

403403
def adjust_optimal_snr(self, frequency_domain_strain, target_snr):
404-
""" Scale the time-domain strain data of the glitch so that it's optimal SNR is equal to the the glitch_snr
405-
Roll the glitch in such a way that the maxima coincides with the glitch_injection_time.
404+
""" Rescale a frequency-domain strain array to a target optimal SNR.
406405
407406
Parameters
408407
==========
409-
glitch_strain: numpy array
410-
Time-domain strain data of the glitch to scale.
411-
glitch_snr: float
412-
Desired optimal SNR of the glitch.
413-
glitch_injection_time: float
414-
GPS time of the occurrence of the glitch
408+
frequency_domain_strain: numpy.array
409+
Frequency-domain strain to rescale.
410+
target_snr: float
411+
Desired optimal SNR.
415412
416413
Returns
417414
=======
418-
scaled_glitch: numpy array
419-
frequency-domain strain of the glitch rescaled so that its optimal SNR matches
420-
`target_glitch_snr` and rolled in such a way thatthe maxima coincides with the glitch_injection_time.
415+
numpy.ndarray
416+
Frequency-domain strain rescaled so that its optimal SNR equals ``target_snr``.
421417
"""
422418
temporary_snr_squared = np.real(
423419
self.optimal_snr_squared(signal=frequency_domain_strain))
424420
return frequency_domain_strain * target_snr / np.sqrt(temporary_snr_squared)
425421

426-
def inject_glitch(self, glitch_snr, glitch_parameters=None, glitch_time_domain_strain=None,
427-
glitch_sample_times=None, glitch_injection_time=None, glitch_waveform_generator=None):
422+
def inject_glitch(self, glitch_parameters=None, glitch_time_domain_strain=None,
423+
glitch_sample_times=None, glitch_waveform_generator=None):
428424
""" Inject a glitch into the interferometer data.
429425
430426
Parameters
431427
==========
432-
glitch: numpy array
428+
glitch_parameters: dict, optional
429+
Dictionary of glitch parameters.
430+
Must contain ``onset_time`` (the GPS time at which the glitch peak should occur).
431+
Must contain ``snr``. The glitch will be rescaled in such a
432+
way that it's optimal SNR matches to this value.
433+
glitch_time_domain_strain: numpy.array, optional
433434
Time-domain strain data of the glitch to inject.
434-
glitch_injection_time: float
435-
The maxima of the time domain strain of the glitch will conicide with the glitch_injection_time.
436-
glitch_sampling_frequency: float
437-
Sampling frequency of the input glitch array.
438-
glitch_snr: float
439-
The glitch will be rescaled in such a way that it's optimal SNR matches with the glitch_snr.
435+
glitch_sample_times: numpy.array
436+
Array of sample times corresponding to ``glitch_time_domain_strain``.
437+
glitch_waveform_generator: bilby.gw.WaveformGenerator,
438+
A waveform generator used to produce the glitch frequency-domain
439+
strain.
440440
"""
441441

442442
if glitch_parameters is None:
@@ -449,6 +449,7 @@ def inject_glitch(self, glitch_snr, glitch_parameters=None, glitch_time_domain_s
449449
"inject_glitch needs one of glitch_waveform_generator or "
450450
"glitch_time_domain_strain.")
451451
elif glitch_time_domain_strain is not None:
452+
# Populate ra, dec, psi. Necessary to make an injection.
452453
glitch_sampling_frequency = 1.0 / (glitch_sample_times[1] - glitch_sample_times[0])
453454
# Resample the glitch to match with the interferometer's sampling frequency
454455
if glitch_sampling_frequency != self.sampling_frequency:
@@ -463,20 +464,20 @@ def inject_glitch(self, glitch_snr, glitch_parameters=None, glitch_time_domain_s
463464
padded_glitch[:len(glitch_time_domain_strain)
464465
] += glitch_time_domain_strain
465466

466-
# Since the maxima of the glitch is not at 0.
467-
glitch_parameters['geocent_time'] = glitch_injection_time - \
468-
(glitch_sample_times[np.argmax(padded_glitch)])
469-
470467
# Convert to frequency domain glitch
471468
glitch_frequency_domain_strain, _ = utils.nfft(
472469
padded_glitch, self.sampling_frequency)
473470

474471
# Adjust SNR
475472
glitch_frequency_domain_strain = self.adjust_optimal_snr(
476-
glitch_frequency_domain_strain, glitch_snr)
473+
glitch_frequency_domain_strain, glitch_parameters['snr'])
477474
injection_polarizations = {
478475
self.name: glitch_frequency_domain_strain}
479476

477+
# Roll the glitch since the glitch maxima of the glitch may not align in the same way as the signal maxima.
478+
glitch_parameters['geocent_time'] = glitch_parameters["onset_time"] \
479+
- glitch_sample_times[np.argmax(glitch_time_domain_strain)]
480+
480481
# Usual inject method
481482
self.inject_signal_from_waveform_polarizations(parameters=glitch_parameters,
482483
injection_polarizations=injection_polarizations)
@@ -485,16 +486,22 @@ def inject_glitch(self, glitch_snr, glitch_parameters=None, glitch_time_domain_s
485486
glitch_frequency_domain_strain = glitch_waveform_generator.frequency_domain_strain(
486487
glitch_parameters)
487488
glitch_frequency_domain_strain = self.adjust_optimal_snr(
488-
glitch_frequency_domain_strain, glitch_snr)
489-
injection_polarizations = {
490-
self.name: glitch_frequency_domain_strain}
489+
np.asarray(glitch_frequency_domain_strain[0]), glitch_parameters['snr'])
490+
injection_polarizations = {self.name: glitch_frequency_domain_strain}
491+
492+
# Populate ra, dec, psi. Necessary to make an injection.
493+
for key, val in [('ra', 0.0), ('dec', 0.0), ('psi', 0.0)]:
494+
glitch_parameters.setdefault(key, val)
495+
# Roll the glitch since the glitch maxima of the glitch may not align in the same way as the signal maxima.
496+
glitch_time_domain_strain = glitch_waveform_generator.time_domain_strain(glitch_parameters)
497+
glitch_parameters['geocent_time'] = glitch_parameters["onset_time"] - \
498+
glitch_waveform_generator.time_array[np.argmax(glitch_time_domain_strain)]
499+
491500
self.inject_signal_from_waveform_polarizations(parameters=glitch_parameters,
492501
injection_polarizations=injection_polarizations)
493502

494503
logger.info("Injected a glitch in: {}".format(self.name))
495-
logger.info("Optimal SNR of the glitch: {}".format(glitch_snr))
496-
logger.info("Matched filter SNR of the glitch: {}".format(
497-
self.matched_filter_snr(signal=glitch_frequency_domain_strain)))
504+
logger.info("Optimal SNR of the glitch: {}".format(glitch_parameters['snr']))
498505

499506
return glitch_time_domain_strain
500507

0 commit comments

Comments
 (0)