bilby-batched 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ __pycache__/
2
+ *.egg-info/
3
+ dist/
4
+ build/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Osvaldo Gramaxo Freitas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,187 @@
1
+ Metadata-Version: 2.5
2
+ Name: bilby-batched
3
+ Version: 0.1.0
4
+ Summary: Batched bilby gravitational-wave likelihoods in torch (CPU/GPU), with time, distance and orbital-phase marginalisation, for nessai and pocoMC
5
+ Project-URL: Repository, https://github.com/osvaldogramaxo/bilby-batched
6
+ Author: Osvaldo Gramaxo Freitas
7
+ License: MIT License
8
+
9
+ Copyright (c) 2026 Osvaldo Gramaxo Freitas
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ License-File: LICENSE
29
+ Classifier: Development Status :: 3 - Alpha
30
+ Classifier: Intended Audience :: Science/Research
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Programming Language :: Python :: 3
33
+ Classifier: Topic :: Scientific/Engineering :: Physics
34
+ Requires-Python: >=3.11
35
+ Requires-Dist: bilby>=2.7.0
36
+ Requires-Dist: lalsuite>=7.26.1
37
+ Requires-Dist: numpy>=2.0
38
+ Requires-Dist: torch>=2.9.0
39
+ Provides-Extra: nessai
40
+ Requires-Dist: nessai>=0.15; extra == 'nessai'
41
+ Provides-Extra: pocomc
42
+ Requires-Dist: pocomc>=1.2; extra == 'pocomc'
43
+ Description-Content-Type: text/markdown
44
+
45
+ # bilby_batched
46
+
47
+ Batched gravitational-wave likelihoods for [bilby](https://git.ligo.org/lscsoft/bilby),
48
+ evaluated in [PyTorch](https://pytorch.org) on CPU or GPU, and samplers that use them.
49
+
50
+ `BatchedLikelihood` computes the same log-likelihood ratio as bilby's
51
+ `GravitationalWaveTransient`, for thousands of parameter points per call, from a waveform
52
+ model that can generate those points as a batch. It follows bilby's time and distance
53
+ marginalisation, and can also marginalise over the orbital phase exactly for models with
54
+ higher modes (bilby's own phase marginalisation is exact only for the (2,2) mode).
55
+ `BatchedNessai` and `BatchedPocoMC` run [nessai](https://github.com/mj-will/nessai) and
56
+ [pocoMC](https://github.com/minaskar/pocomc) with it through `bilby.run_sampler`, and the
57
+ likelihood's `generate_all_bbh_parameters` does bilby's post-processing in batches too.
58
+
59
+ ## Install
60
+
61
+ ```bash
62
+ pip install "bilby-batched[nessai]" # or [pocomc], or no extra for the likelihood only
63
+ ```
64
+
65
+ To install the latest version from GitHub instead:
66
+
67
+ ```bash
68
+ pip install "bilby-batched[nessai] @ git+https://github.com/osvaldogramaxo/bilby-batched"
69
+ ```
70
+
71
+ ## The waveform
72
+
73
+ The likelihood needs a batched version of the waveform generator's
74
+ `time_domain_source_model`: a function `waveform(parameters, times) -> {m: H_m}`.
75
+
76
+ - `parameters` is a dict of numpy arrays of length n: the sampled parameters after the
77
+ generator's `parameter_conversion`.
78
+ - `times` is the generator's `time_array`.
79
+ - Each `H_m` is a complex torch tensor of shape (n, len(times)) on the likelihood's
80
+ device, such that
81
+
82
+     h₊ − i h× = Σ_m H_m exp(−i m φ),   φ = `parameters['phase']`,
83
+
84
+ with the merger at `times[0]`, as bilby expects of a time-domain model.
85
+
86
+ For an aligned-spin model with higher modes, H_m = Σ_l ₋₂Y_lm(θ_jn, π/2) h_lm. A model whose
87
+ dependence on the phase is not of this form can return `{0: h₊ − i h×}` at the given phase
88
+ and use the likelihood without orbital-phase marginalisation.
89
+
90
+ `bilby_batched.selfcheck` has a toy example (`toy_source` and its batched `toy_waveform`).
91
+ [DANSur_HM](https://github.com/osvaldogramaxo/DANSur_HM) is a real one: pass
92
+ `dansur.batched_waveform.dansur_modes(DANSur(device=..., hybridize=True))`, where `hybridize`
93
+ continues the templates before the network's ~4000 M window with an IMRPhenomT(HM) inspiral.
94
+ See `examples/dansur_injection.py`.
95
+
96
+ ## Usage
97
+
98
+ ```python
99
+ import bilby
100
+ from bilby_batched import BatchedLikelihood, BatchedNessai
101
+
102
+ likelihood = bilby.gw.likelihood.GravitationalWaveTransient(
103
+ interferometers, waveform_generator, priors=priors,
104
+ time_marginalization=True, distance_marginalization=True, jitter_time=False)
105
+ batched = BatchedLikelihood(likelihood, waveform, device="cuda",
106
+ orbital_phase_marginalization=True, time_upsampling="auto")
107
+ log_l = batched.log_likelihood_ratio(priors.sample(4096)) # array of shape (4096,)
108
+
109
+ sampler = BatchedNessai(batched, likelihood, priors, outdir=outdir, label=label, nlive=1000,
110
+ use_ratio=True, flow_class="gwflowproposal")
111
+ result = bilby.run_sampler(likelihood, priors, sampler=sampler, outdir=outdir, label=label,
112
+ conversion_function=batched.generate_all_bbh_parameters)
113
+ ```
114
+
115
+ `BatchedPocoMC(batched, likelihood, priors, n_effective=512, n_active=256, ...)` is used the
116
+ same way; its keyword arguments go to `pocomc.Sampler` and its `run()`.
117
+ `BatchedNessai(..., importance_nested_sampler=True)` runs nessai's importance nested sampler.
118
+
119
+ - **bilby's likelihood** sets up the marginalisations (and fixes the marginalised parameters
120
+ in `priors`); bilby still uses it for everything except the sampling.
121
+ - **Orbital phase:** with `orbital_phase_marginalization=True`, the strain is built once per
122
+ point and the phase integrated on a grid of ~16 × SNR points. The samplers fix `phase`
123
+ in the priors, and `generate_all_bbh_parameters` draws it afterwards.
124
+ - **Time:** bilby sums over arrival times every 2 / sampling_frequency (~1 ms at 2048 Hz),
125
+ much coarser than the time resolution of a loud signal, so the result varies by tens in
126
+ log L with bilby's `time_jitter`. `time_upsampling="auto"` refines that grid by the
127
+ matched-filter SNR (per batch), so the sum converges and `jitter_time=False` can be used;
128
+ the default (1) reproduces bilby.
129
+ - **Post-processing:** `generate_all_bbh_parameters` is a bilby `conversion_function` that
130
+ draws the marginalised parameters from their conditional posteriors (the arrival time on a
131
+ grid 64 times finer than bilby's) and computes the per-detector log likelihoods and
132
+ matched-filter and optimal SNRs, all in batches; bilby's own version evaluates the
133
+ waveform point by point.
134
+ - `chunk_size` (default 512) sets how many points are evaluated together; lower it if GPU
135
+ memory runs out.
136
+
137
+ Not supported: calibration models, bilby's `phase_marginalization`, frequency-domain source
138
+ models, and reference frames or time references other than the sky frame and geocentre.
139
+
140
+ ## Checks
141
+
142
+ ```bash
143
+ python -m bilby_batched.selfcheck [cpu|cuda]
144
+ ```
145
+
146
+ compares the batched likelihood with bilby's for each combination of time and distance
147
+ marginalisation, the reconstructed SNRs, per-detector log likelihoods and time and distance
148
+ draws with bilby's, and the orbital phase marginalisation with brute-force integration over
149
+ the phase. With the toy waveform everything agrees to ~1e-12 in log L. `selfcheck.run(source_model, waveform)`
150
+ runs the same checks for any aligned-spin BBH model. `examples/dansur_check.py [cpu|cuda] [--hybridize]` runs them for DANSur_HM.
151
+
152
+ ## Examples and the sampler comparison
153
+
154
+ - `examples/dansur_injection.py`: injection and recovery with DANSur_HM, with nessai, i-nessai or pocoMC.
155
+ - `examples/dansur_check.py`: the checks above for DANSur_HM.
156
+ - `comparison/`: a comparison of samplers on the batched likelihood (plan, pilot runs and results).
157
+
158
+ The examples need DANSur_HM installed; bilby_batched itself does not depend on it.
159
+
160
+ ## Performance
161
+
162
+ With DANSur_HM, 3 detectors and 4 s of data at 2048 Hz, a V100 evaluates ~6000 points/s
163
+ with time and distance marginalisation and ~2200 points/s with orbital phase too, against
164
+ ~20/s for bilby one point at a time. On an injection with all three marginalised, pocoMC
165
+ (512 particles) needed 65k likelihood evaluations and 5 min of sampling on a V100, nessai
166
+ 310k evaluations and 10 min; post-processing 4–5k samples takes seconds.
167
+
168
+ ## License
169
+
170
+ MIT
171
+
172
+ ## Releasing
173
+
174
+ `.github/workflows/release.yml` builds the package, runs `python -m bilby_batched.selfcheck cpu` on the built
175
+ wheel, and publishes to PyPI when a version tag is pushed:
176
+
177
+ ```bash
178
+ # bump `version` in pyproject.toml, commit, then
179
+ git tag v0.1.0 && git push origin v0.1.0
180
+ ```
181
+
182
+ The tag must match the version in `pyproject.toml`, or the release stops before publishing.
183
+
184
+ One-time set-up (PyPI trusted publishing, no API token): on PyPI, *Your projects → Publishing → Add a pending
185
+ publisher*, with project `bilby-batched`, owner `osvaldogramaxo`, repository `bilby-batched`, workflow
186
+ `release.yml` and environment `pypi`. On GitHub, create the environment `pypi` under *Settings → Environments*;
187
+ adding yourself as a required reviewer there makes every release wait for your approval.
@@ -0,0 +1,143 @@
1
+ # bilby_batched
2
+
3
+ Batched gravitational-wave likelihoods for [bilby](https://git.ligo.org/lscsoft/bilby),
4
+ evaluated in [PyTorch](https://pytorch.org) on CPU or GPU, and samplers that use them.
5
+
6
+ `BatchedLikelihood` computes the same log-likelihood ratio as bilby's
7
+ `GravitationalWaveTransient`, for thousands of parameter points per call, from a waveform
8
+ model that can generate those points as a batch. It follows bilby's time and distance
9
+ marginalisation, and can also marginalise over the orbital phase exactly for models with
10
+ higher modes (bilby's own phase marginalisation is exact only for the (2,2) mode).
11
+ `BatchedNessai` and `BatchedPocoMC` run [nessai](https://github.com/mj-will/nessai) and
12
+ [pocoMC](https://github.com/minaskar/pocomc) with it through `bilby.run_sampler`, and the
13
+ likelihood's `generate_all_bbh_parameters` does bilby's post-processing in batches too.
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ pip install "bilby-batched[nessai]" # or [pocomc], or no extra for the likelihood only
19
+ ```
20
+
21
+ To install the latest version from GitHub instead:
22
+
23
+ ```bash
24
+ pip install "bilby-batched[nessai] @ git+https://github.com/osvaldogramaxo/bilby-batched"
25
+ ```
26
+
27
+ ## The waveform
28
+
29
+ The likelihood needs a batched version of the waveform generator's
30
+ `time_domain_source_model`: a function `waveform(parameters, times) -> {m: H_m}`.
31
+
32
+ - `parameters` is a dict of numpy arrays of length n: the sampled parameters after the
33
+ generator's `parameter_conversion`.
34
+ - `times` is the generator's `time_array`.
35
+ - Each `H_m` is a complex torch tensor of shape (n, len(times)) on the likelihood's
36
+ device, such that
37
+
38
+     h₊ − i h× = Σ_m H_m exp(−i m φ),   φ = `parameters['phase']`,
39
+
40
+ with the merger at `times[0]`, as bilby expects of a time-domain model.
41
+
42
+ For an aligned-spin model with higher modes, H_m = Σ_l ₋₂Y_lm(θ_jn, π/2) h_lm. A model whose
43
+ dependence on the phase is not of this form can return `{0: h₊ − i h×}` at the given phase
44
+ and use the likelihood without orbital-phase marginalisation.
45
+
46
+ `bilby_batched.selfcheck` has a toy example (`toy_source` and its batched `toy_waveform`).
47
+ [DANSur_HM](https://github.com/osvaldogramaxo/DANSur_HM) is a real one: pass
48
+ `dansur.batched_waveform.dansur_modes(DANSur(device=..., hybridize=True))`, where `hybridize`
49
+ continues the templates before the network's ~4000 M window with an IMRPhenomT(HM) inspiral.
50
+ See `examples/dansur_injection.py`.
51
+
52
+ ## Usage
53
+
54
+ ```python
55
+ import bilby
56
+ from bilby_batched import BatchedLikelihood, BatchedNessai
57
+
58
+ likelihood = bilby.gw.likelihood.GravitationalWaveTransient(
59
+ interferometers, waveform_generator, priors=priors,
60
+ time_marginalization=True, distance_marginalization=True, jitter_time=False)
61
+ batched = BatchedLikelihood(likelihood, waveform, device="cuda",
62
+ orbital_phase_marginalization=True, time_upsampling="auto")
63
+ log_l = batched.log_likelihood_ratio(priors.sample(4096)) # array of shape (4096,)
64
+
65
+ sampler = BatchedNessai(batched, likelihood, priors, outdir=outdir, label=label, nlive=1000,
66
+ use_ratio=True, flow_class="gwflowproposal")
67
+ result = bilby.run_sampler(likelihood, priors, sampler=sampler, outdir=outdir, label=label,
68
+ conversion_function=batched.generate_all_bbh_parameters)
69
+ ```
70
+
71
+ `BatchedPocoMC(batched, likelihood, priors, n_effective=512, n_active=256, ...)` is used the
72
+ same way; its keyword arguments go to `pocomc.Sampler` and its `run()`.
73
+ `BatchedNessai(..., importance_nested_sampler=True)` runs nessai's importance nested sampler.
74
+
75
+ - **bilby's likelihood** sets up the marginalisations (and fixes the marginalised parameters
76
+ in `priors`); bilby still uses it for everything except the sampling.
77
+ - **Orbital phase:** with `orbital_phase_marginalization=True`, the strain is built once per
78
+ point and the phase integrated on a grid of ~16 × SNR points. The samplers fix `phase`
79
+ in the priors, and `generate_all_bbh_parameters` draws it afterwards.
80
+ - **Time:** bilby sums over arrival times every 2 / sampling_frequency (~1 ms at 2048 Hz),
81
+ much coarser than the time resolution of a loud signal, so the result varies by tens in
82
+ log L with bilby's `time_jitter`. `time_upsampling="auto"` refines that grid by the
83
+ matched-filter SNR (per batch), so the sum converges and `jitter_time=False` can be used;
84
+ the default (1) reproduces bilby.
85
+ - **Post-processing:** `generate_all_bbh_parameters` is a bilby `conversion_function` that
86
+ draws the marginalised parameters from their conditional posteriors (the arrival time on a
87
+ grid 64 times finer than bilby's) and computes the per-detector log likelihoods and
88
+ matched-filter and optimal SNRs, all in batches; bilby's own version evaluates the
89
+ waveform point by point.
90
+ - `chunk_size` (default 512) sets how many points are evaluated together; lower it if GPU
91
+ memory runs out.
92
+
93
+ Not supported: calibration models, bilby's `phase_marginalization`, frequency-domain source
94
+ models, and reference frames or time references other than the sky frame and geocentre.
95
+
96
+ ## Checks
97
+
98
+ ```bash
99
+ python -m bilby_batched.selfcheck [cpu|cuda]
100
+ ```
101
+
102
+ compares the batched likelihood with bilby's for each combination of time and distance
103
+ marginalisation, the reconstructed SNRs, per-detector log likelihoods and time and distance
104
+ draws with bilby's, and the orbital phase marginalisation with brute-force integration over
105
+ the phase. With the toy waveform everything agrees to ~1e-12 in log L. `selfcheck.run(source_model, waveform)`
106
+ runs the same checks for any aligned-spin BBH model. `examples/dansur_check.py [cpu|cuda] [--hybridize]` runs them for DANSur_HM.
107
+
108
+ ## Examples and the sampler comparison
109
+
110
+ - `examples/dansur_injection.py`: injection and recovery with DANSur_HM, with nessai, i-nessai or pocoMC.
111
+ - `examples/dansur_check.py`: the checks above for DANSur_HM.
112
+ - `comparison/`: a comparison of samplers on the batched likelihood (plan, pilot runs and results).
113
+
114
+ The examples need DANSur_HM installed; bilby_batched itself does not depend on it.
115
+
116
+ ## Performance
117
+
118
+ With DANSur_HM, 3 detectors and 4 s of data at 2048 Hz, a V100 evaluates ~6000 points/s
119
+ with time and distance marginalisation and ~2200 points/s with orbital phase too, against
120
+ ~20/s for bilby one point at a time. On an injection with all three marginalised, pocoMC
121
+ (512 particles) needed 65k likelihood evaluations and 5 min of sampling on a V100, nessai
122
+ 310k evaluations and 10 min; post-processing 4–5k samples takes seconds.
123
+
124
+ ## License
125
+
126
+ MIT
127
+
128
+ ## Releasing
129
+
130
+ `.github/workflows/release.yml` builds the package, runs `python -m bilby_batched.selfcheck cpu` on the built
131
+ wheel, and publishes to PyPI when a version tag is pushed:
132
+
133
+ ```bash
134
+ # bump `version` in pyproject.toml, commit, then
135
+ git tag v0.1.0 && git push origin v0.1.0
136
+ ```
137
+
138
+ The tag must match the version in `pyproject.toml`, or the release stops before publishing.
139
+
140
+ One-time set-up (PyPI trusted publishing, no API token): on PyPI, *Your projects → Publishing → Add a pending
141
+ publisher*, with project `bilby-batched`, owner `osvaldogramaxo`, repository `bilby-batched`, workflow
142
+ `release.yml` and environment `pypi`. On GitHub, create the environment `pypi` under *Settings → Environments*;
143
+ adding yourself as a required reviewer there makes every release wait for your approval.
@@ -0,0 +1,6 @@
1
+ """Batched bilby gravitational-wave likelihoods in torch (CPU or GPU), with nessai and pocoMC samplers."""
2
+ from .likelihood import BatchedLikelihood
3
+ from .samplers import BatchedNessai, BatchedPocoMC
4
+
5
+ __all__ = ["BatchedLikelihood", "BatchedNessai", "BatchedPocoMC"]
6
+ __version__ = "0.1.0"