Skip to content

An inject_glitch method for the interferometer object#1055

Closed
narolaharsh wants to merge 25 commits intobilby-dev:mainfrom
narolaharsh:main
Closed

An inject_glitch method for the interferometer object#1055
narolaharsh wants to merge 25 commits intobilby-dev:mainfrom
narolaharsh:main

Conversation

@narolaharsh
Copy link
Copy Markdown

@narolaharsh narolaharsh commented Mar 2, 2026

The method injects a glitch in a user specified interferometer at a desired SNR and onset time.

Also, I added an example to demonstrate the method. The time domain strain of the glitch needs to be provided by the user. For the sake of the example, I use the gengli codebase to inject a blip glitch.

The feautre could be of use for various glitch-mitigation or effect of glithces on PE related simulations. Any feedback on furhter developement, relevance of the feature for Bilby welcome.

@ColmTalbot
Copy link
Copy Markdown
Collaborator

Thanks for opening this PR. Personally, I don't think that making this change directly is appropriate for adding to Bilby. However, I think there are some opportunities here.

  • Adding the ability to inject at fixed SNR is, I think, useful. We should probably integrate this with our existing injection and SNR calculation methods.
  • Glitch injections can already be done. This is an underdocumented/advertised functionality.

The rough pattern is below.
The trick is that the keys in the waveform can be the name of an interferometer, in which case, the signal will be directly added and timeshifted.
The most annoying quirk is that even when the injection is for a specific detector, we need a dummy ra/dec.
We should probably remove that requirement, but the method works, even if it's a bit ugly.

def glitch_model(time_array, amplitude, ..., **kwargs):
    # e.g., kwargs = dict(detector="H1", ...)
    glitch_timeseries = ...
    return {kwargs["detector"]: glitch_timeseries}


wfg = bilby.gw.waveform_generator.WaveformGenerator(...)

ifos = bilby.gw.detector.InterferometerList(...)

ifos.inject_signal(
    parameters=dict(...glitch parameters..., geocent_time=..., ...unfortunately, we need to specify sky position...),
    waveform_generator=wfg,
)

@narolaharsh narolaharsh marked this pull request as draft March 5, 2026 09:13
@narolaharsh
Copy link
Copy Markdown
Author

narolaharsh commented Mar 5, 2026

Thanks, Colm! I re-structured inject_glitch to match closely with inject_signal. Now, internally calling the latter to do the job and swiping ra, dec, psi under the carpet.

def inject_glitch(self, glitch_parameters=None, glitch_time_domain_strain=None,
                      glitch_sample_times=None, glitch_waveform_generator=None):
        """ Inject a glitch into the interferometer data.

        Parameters
        ==========
        glitch_parameters: dict
            Dictionary of glitch parameters.
            Must contain ``onset_time`` (the GPS time at which the glitch peak should occur).
            Must contain ``snr``. The glitch will be rescaled in such a
            way that it's optimal SNR matches to this value.
             Other parameters passed to glitch_waveform_generator.
        ...
     
        """

Directly using inject_signal function was making it difficult to make glitch maxima appear at a specific time. I have to do something like

# Roll the glitch since the glitch maxima of the glitch may not align in the same way as the signal maxima.

glitch_parameters['geocent_time'] = glitch_parameters["onset_time”] - glitch_sample_times[np.argmax(glitch_time_domain_strain)]

since glitch peak at random times. Besides this and adjusting snr, another minor improvement is in book keeping, i.e., using inject_signal was not updating the log properly (it still doesn’t but now there’s an option). In case the feature is not relevant at this stage, I am happy to close the PR.

@narolaharsh narolaharsh force-pushed the main branch 2 times, most recently from 472299f to 198a60b Compare March 17, 2026 14:19
narolaharsh and others added 4 commits March 17, 2026 19:18
@ColmTalbot
Copy link
Copy Markdown
Collaborator

Hi @narolaharsh I've noticed that this branch seems to have diverged from the initial change.
Do you still intend to merge the initial change?
If not, please can you close the PR?

@narolaharsh
Copy link
Copy Markdown
Author

Hi Colm, let me close this pull request. I would like to merge the initial change or parts of it (snr adjustment and example script to inject glitch) if possible.

For that, let me create a new PR with a separate source branch instead of main. Sorry about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants