Skip to content

ENH: Improve strain data read in and downsampling - #926

Open
GregoryAshton wants to merge 6 commits into
bilby-dev:mainfrom
GregoryAshton:improve-strain-data-read-in
Open

ENH: Improve strain data read in and downsampling#926
GregoryAshton wants to merge 6 commits into
bilby-dev:mainfrom
GregoryAshton:improve-strain-data-read-in

Conversation

@GregoryAshton

Copy link
Copy Markdown
Collaborator

This MR:

  • Adds find_and_read_data function to enable rapid finding of data using gw_data_find.
  • Adds a helper function for downsampling enabling either gwpy or lal downsampling (ported from bilby_pipe)

@mj-will

mj-will commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

@GregoryAshton is this still a draft or is it ready for review?

@GregoryAshton GregoryAshton changed the title DRAFT: Improve strain data read in and downsampling ENH: Improve strain data read in and downsampling Aug 25, 2026
@GregoryAshton

Copy link
Copy Markdown
Collaborator Author

@mj-will this is ready to review - I updated the title

@mj-will
mj-will requested a review from a team August 25, 2026 15:13

@mj-will mj-will left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial comments @GregoryAshton.

I also think this would benefit from some tests.

Comment thread bilby/gw/detector/strain_data.py Outdated
Comment thread bilby/gw/detector/strain_data.py Outdated
Comment thread bilby/gw/detector/strain_data.py
Comment thread bilby/gw/detector/strain_data.py Outdated
Comment thread bilby/gw/detector/strain_data.py Outdated
Comment thread bilby/gw/detector/strain_data.py Outdated
AI summary:
1. resample_with_gwpy now accepts **kwargs passed through to
   .resample(), and fixed the bug where it discarded the result instead
of returning it (it was returning None).
2. resample_with_lal accepts **kwargs for interface consistency, raising
   a clear ValueError if any are passed (LAL's resampler doesn't support
extra options).
3. resample_timeseries passes **kwargs through to whichever resampling
   function is selected, and now returns data unchanged when no
resampling is needed (previously returned None in that branch too).
4. find_and_read_data gained a resample_kwargs parameter so resampling
   kwargs can flow all the way through; fixed the docstring to clarify
ifo isn't limited to H1/L1/V1 (K1, A1 work fine via ifo[0]); fixed the
"not include" grammar; and fixed a latent bug where
find_url_kwargs/read_kwargs defaulting to None would crash on **None.
5. Added a TestResampling and TestFindAndReadData test class covering
   the resampling functions and find_and_read_data (mocking
gwdatafind.find_urls and TimeSeries.read), addressing his request for
test coverage.
Copilot AI lite review requested due to automatic review settings August 26, 2026 16:08
@GregoryAshton

Copy link
Copy Markdown
Collaborator Author

@mj-will I pushed a change addressing your comments

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances Bilby’s GW strain data utilities by adding (1) a gwdatafind-backed helper to locate and read frame data via GWPy, and (2) a unified downsampling/resampling interface that can use either GWPy or LAL, with accompanying unit tests.

Changes:

  • Added find_and_read_data() to locate frame URLs with gwdatafind.find_urls() and load them via gwpy.timeseries.TimeSeries.read().
  • Added resample_timeseries() plus backend helpers resample_with_gwpy() / resample_with_lal() to support configurable downsampling.
  • Added tests covering resampling behavior and the new data-finding/read helper.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
bilby/gw/detector/strain_data.py Adds new resampling helpers and a gwdatafind + GWPy data discovery/read function.
test/gw/detector/strain_data_test.py Adds unit tests for the new resampling functions and find_and_read_data().
Suppressed comments (1)

bilby/gw/detector/strain_data.py:1006

  • Other optional dependency entrypoints in this module wrap gwpy imports to raise a clearer ModuleNotFoundError message. find_and_read_data() currently imports gwpy/gwdatafind directly, which will surface a less helpful error if those extras aren't installed.
    from gwpy.timeseries import TimeSeries
    from gwdatafind import find_urls

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +1021 to +1024
type_kwargs = dict(dtype=dtype, subok=True, copy=False)
data = TimeSeries.read(urls, channel_with_ifo, start=start, end=end, **read_kwargs).astype(**type_kwargs)
data = resample_timeseries(data, sampling_frequency, resampling_method, **resample_kwargs)
return data
Comment on lines +915 to +919
if kwargs:
raise ValueError(f"resample_with_lal does not support additional kwargs: {kwargs}")
import lal
from gwpy.timeseries import TimeSeries
lal_timeseries = data.to_lal()
Comment on lines +531 to +542
@mock.patch("gwdatafind.find_urls")
@mock.patch("gwpy.timeseries.TimeSeries.read")
def test_find_and_read_data_resamples(self, mock_read, mock_find_urls):
mock_find_urls.return_value = ["file://fake/H-H1_HOFT_C00_AR-0-4.gwf"]
mock_read.return_value = self.data

data = find_and_read_data(
self.start, self.end, self.ifo, self.frametype, self.channel,
sampling_frequency=self.sampling_frequency / 2,
)

self.assertEqual(data.sample_rate.value, self.sampling_frequency / 2)
@mj-will
mj-will requested a lite review from Copilot August 27, 2026 08:39

@mj-will mj-will left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Greg, LGTM now!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Suppressed comments (1)

test/gw/detector/strain_data_test.py:490

  • These tests rely on gwdatafind (via the patch decorators) and GWPy. Without an explicit skip in setUp, they will error in environments where gwdatafind isn’t installed. Add a guard so the test class is skipped unless both GWPy and gwdatafind are available.
    def setUp(self):
        self.start = 0
        self.end = 4
        self.ifo = "H1"

Comment on lines 4 to +6
import numpy as np
import scipy.signal
from gwpy.timeseries import TimeSeries
Comment on lines +431 to +433
def setUp(self):
self.sampling_frequency = 512
self.data = TimeSeries(
Comment on lines +967 to +972
if data.sample_rate.value == sampling_frequency:
logger.info("Sample rate matches data no resampling")
return data.copy()
elif resampling_method in RESAMPLING_FUNCTIONS:
logger.info(f"Resampling data to sampling_frequency {sampling_frequency} using {resampling_method}")
return RESAMPLING_FUNCTIONS[resampling_method](data, sampling_frequency, **kwargs)
@mj-will

mj-will commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Hmm, I had hoped asking copilot for a re-review would make it close the threads it opened but seems not :/

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

Labels

enhancement New feature or request >100 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants