Skip to content

Batched simulation

gwmock_signal.jax_batch

Batched, on-device CBC simulation for catalogue-scale generation.

Generates a whole catalogue of compact-binary signals on device: ripple frequency-domain waveforms under jax.vmap (one shared, worst-case grid), then the JAX antenna pattern + geocenter delay + inverse FFT per event and detector. The result is raw strain arrays plus timing metadata; injecting those signals into fixed-duration data-segment files (including signals spanning several segments) is a separate assembly step.

Requires the optional [jax] extra (via :class:RippleBackend). JAX is imported lazily so the package still imports without it.

BatchedDetectorStrain dataclass

Catalogue-scale detector strain as raw arrays plus timing metadata.

strain has shape (n_events, n_detectors, n_samples) and is a JAX array (on device). Each event/detector row is a time series with sample spacing 1 / sampling_frequency; coalescence sits -epoch seconds from the start of the buffer, near its end. The row is the waveform segment zero-padded by :func:~gwmock_signal.projection.resampling.edge_padding samples at each end before projection, as :func:~gwmock_signal.projection.network.project_polarizations_to_network pads, so n_samples here is the waveform's length plus twice that padding and epoch (and start_index) describe the padded row.

Where the buffer begins depends on whether it was aligned to an output lattice:

  • Aligned (grid and start_index set): the first sample is at grid.time_of(start_index), exactly on the lattice, so superposing the signal onto a segment of that grid is an integer-offset add.
  • Unaligned (both None): the first sample is at epoch + coa_time[event], an arbitrary time, and a consumer must resample to place it -- which is accurate only for heavily oversampled strain.

The signals are not yet placed on a shared timeline or segmented into files; that assembly step is handled separately.

Source code in src/gwmock_signal/jax_batch.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
@dataclass(frozen=True)
class BatchedDetectorStrain:
    """Catalogue-scale detector strain as raw arrays plus timing metadata.

    ``strain`` has shape ``(n_events, n_detectors, n_samples)`` and is a JAX array
    (on device). Each event/detector row is a time series with sample spacing
    ``1 / sampling_frequency``; coalescence sits ``-epoch`` seconds from the start of the
    buffer, near its end. The row is the waveform segment zero-padded by
    :func:`~gwmock_signal.projection.resampling.edge_padding` samples at each end before
    projection, as :func:`~gwmock_signal.projection.network.project_polarizations_to_network`
    pads, so ``n_samples`` here is the waveform's length plus twice that padding and ``epoch``
    (and ``start_index``) describe the padded row.

    Where the buffer *begins* depends on whether it was aligned to an output lattice:

    - **Aligned** (``grid`` and ``start_index`` set): the first sample is at
      ``grid.time_of(start_index)``, exactly on the lattice, so superposing the signal onto a
      segment of that grid is an integer-offset add.
    - **Unaligned** (both ``None``): the first sample is at ``epoch + coa_time[event]``, an
      arbitrary time, and a consumer must resample to place it -- which is accurate only for
      heavily oversampled strain.

    The signals are not yet placed on a shared timeline or segmented into files; that assembly
    step is handled separately.
    """

    strain: Array
    detector_names: tuple[str, ...]
    coa_time: np.ndarray
    epoch: float
    sampling_frequency: float
    #: Lattice index of each event's first sample, when the batch was generated against an
    #: output grid. Set means every event starts exactly on that grid, so superposing it is an
    #: integer-offset add; ``None`` means the starts are arbitrary and the consumer has to
    #: resample, which is accurate only for heavily oversampled signals.
    start_index: np.ndarray | None = None
    #: The grid the indices refer to, or ``None`` when unaligned.
    grid: SamplingGrid | None = None

assemble_segments(batch, *, segment_duration, segment_start_times, backgrounds=None, interpolate_if_offset=True)

Scatter the batched signals into fixed-duration data segments (in memory).

Each output segment spans [start, start + segment_duration). A signal longer than segment_duration contributes its overlapping part to each of the consecutive segments it spans.

When batch was generated against a :class:~gwmock_signal.sampling_grid.SamplingGrid -- see output_grid on :func:simulate_cbc_batch -- every signal already starts on the output lattice and superposition is an exact integer-offset add. The segment starts are then required to lie on that same grid, and are rejected rather than rounded if they do not. Otherwise signals fall between samples and each is resampled onto the segment's lattice, as :func:~gwmock_signal.injection.inject_strain does, which is only accurate for heavily oversampled strain.

Parameters:

Name Type Description Default
batch BatchedDetectorStrain

Batched per-event/detector strain from :func:simulate_cbc_batch.

required
segment_duration float

Duration of every output segment, in seconds.

required
segment_start_times Sequence[float]

GPS start time of each output segment (typically a contiguous tiling, e.g. start + k * segment_duration).

required
backgrounds Sequence[Mapping[str, TimeSeries | TimeSeries]] | None

Optional per-segment backgrounds, aligned with segment_start_times; each maps detector name to a background time series to inject into, a :class:~gwmock_signal.timeseries.TimeSeries or a GWpy TimeSeries. When None (default), zero-noise segments are created.

None
interpolate_if_offset bool

Whether to resample, as inject_strain does, a signal whose start is not on a segment-sample boundary. Unused when the batch carries a sampling grid, because then nothing needs interpolating.

True

Returns:

Name Type Description
One list[DetectorStrainStack]

class:~gwmock_signal.multichannel.stack.DetectorStrainStack per

list[DetectorStrainStack]

entry in segment_start_times (same order), with channels in

list[DetectorStrainStack]

batch.detector_names order.

Source code in src/gwmock_signal/jax_batch.py
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
def assemble_segments(
    batch: BatchedDetectorStrain,
    *,
    segment_duration: float,
    segment_start_times: Sequence[float],
    backgrounds: Sequence[Mapping[str, TimeSeries | gwpy.timeseries.TimeSeries]] | None = None,
    interpolate_if_offset: bool = True,
) -> list[DetectorStrainStack]:
    """Scatter the batched signals into fixed-duration data segments (in memory).

    Each output segment spans ``[start, start + segment_duration)``. A signal longer than
    ``segment_duration`` contributes its overlapping part to each of the consecutive segments
    it spans.

    When ``batch`` was generated against a :class:`~gwmock_signal.sampling_grid.SamplingGrid`
    -- see ``output_grid`` on :func:`simulate_cbc_batch` -- every signal already starts on the
    output lattice and superposition is an exact integer-offset add. The segment starts are
    then required to lie on that same grid, and are rejected rather than rounded if they do
    not. Otherwise signals fall between samples and
    each is resampled onto the segment's lattice, as
    :func:`~gwmock_signal.injection.inject_strain` does, which is only accurate for heavily
    oversampled strain.

    Args:
        batch: Batched per-event/detector strain from :func:`simulate_cbc_batch`.
        segment_duration: Duration of every output segment, in seconds.
        segment_start_times: GPS start time of each output segment (typically a
            contiguous tiling, e.g. ``start + k * segment_duration``).
        backgrounds: Optional per-segment backgrounds, aligned with
            ``segment_start_times``; each maps detector name to a background
            time series to inject into, a :class:`~gwmock_signal.timeseries.TimeSeries` or a GWpy
            ``TimeSeries``. When ``None`` (default), zero-noise
            segments are created.
        interpolate_if_offset: Whether to resample, as ``inject_strain`` does, a signal whose
            start is not on a segment-sample boundary. Unused when the batch
            carries a sampling grid, because then nothing needs interpolating.

    Returns:
        One :class:`~gwmock_signal.multichannel.stack.DetectorStrainStack` per
        entry in ``segment_start_times`` (same order), with channels in
        ``batch.detector_names`` order.
    """
    if backgrounds is not None and len(backgrounds) != len(segment_start_times):
        raise ValueError("backgrounds must be aligned one-to-one with segment_start_times.")
    if backgrounds is not None:
        backgrounds = [{name: as_timeseries(segment[name]) for name in batch.detector_names} for segment in backgrounds]

    strain = np.asarray(batch.strain)
    _, _, n_samples = strain.shape
    sampling_frequency = batch.sampling_frequency
    dt = 1.0 / sampling_frequency
    aligned = batch.start_index is not None and batch.grid is not None
    if aligned:
        # Where the buffer actually begins, which differs from epoch + coa_time by the
        # fractional remainder the device absorbed. Using the requested time here would
        # misattribute overlap for events sitting within a fraction of a sample of a boundary.
        signal_start = np.asarray(batch.grid.time_of(batch.start_index), dtype=float)
    else:
        signal_start = batch.epoch + np.asarray(batch.coa_time, dtype=float)
    signal_end = signal_start + n_samples * dt
    n_segment_samples = round(segment_duration * sampling_frequency)
    detectors = batch.detector_names

    if aligned:
        # The catalogue wrapper checks this, but a direct caller reaches here too, and integer
        # overlap arithmetic on a rounded length would silently describe a different interval
        # from the one requested.
        exact_samples = segment_duration * sampling_frequency
        if abs(exact_samples - round(exact_samples)) > _WHOLE_SAMPLE_TOLERANCE:
            raise ValueError(
                f"segment_duration * sampling_frequency must be a whole number of samples for a "
                f"grid-aligned batch; {segment_duration} x {sampling_frequency} = {exact_samples}."
            )
        segment_index = batch.grid.require_on_lattice(
            np.asarray(segment_start_times, dtype=float), name="segment_start_times"
        )
        event_index = np.asarray(batch.start_index, dtype=np.int64)
        # Checked up front rather than per channel: a mismatched background is a caller error, and
        # discovering it on the last segment after assembling every earlier one wastes the work.
        _require_backgrounds_match_segments(
            backgrounds,
            detectors=detectors,
            segment_index=segment_index,
            n_segment_samples=n_segment_samples,
            grid=batch.grid,
            sampling_frequency=sampling_frequency,
        )

    segments: list[DetectorStrainStack] = []
    for k, raw_start in enumerate(segment_start_times):
        seg_start = float(raw_start)
        seg_end = seg_start + segment_duration
        if aligned:
            # Integer lattice arithmetic, not reconstructed GPS times. Both are on one lattice
            # by construction here, so comparing sample counts is exact and implements the
            # half-open convention [start, start + duration) without float round-off deciding
            # whether a signal ending exactly on a boundary belongs to the next segment.
            offset = event_index - int(segment_index[k])
            overlapping = np.nonzero((offset < n_segment_samples) & (offset + n_samples > 0))[0]
        else:
            overlapping = np.nonzero((signal_start < seg_end) & (signal_end > seg_start))[0]
        channels: dict[str, TimeSeries] = {}
        for d, name in enumerate(detectors):
            if aligned:
                channels[name] = _aligned_channel(
                    background=None if backgrounds is None else backgrounds[k][name],
                    strain=strain[:, d],
                    offsets=offset,
                    overlapping=overlapping,
                    n_segment_samples=n_segment_samples,
                    segment_start=seg_start,
                    sampling_frequency=sampling_frequency,
                )
                continue
            channels[name] = _unaligned_channel(
                background=None if backgrounds is None else backgrounds[k][name],
                strain=strain[:, d],
                signal_start=signal_start,
                overlapping=overlapping,
                n_segment_samples=n_segment_samples,
                segment_start=seg_start,
                sampling_frequency=sampling_frequency,
                interpolate_if_offset=interpolate_if_offset,
            )
        segments.append(DetectorStrainStack.from_mapping(detectors, channels))
    return segments

available_device_memory_bytes()

Return the memory limit of the default JAX device, or None if unknown.

CPU devices do not report a limit, and neither do some older backends, so callers must treat None as "cannot check" rather than as "no limit".

Source code in src/gwmock_signal/jax_batch.py
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
def available_device_memory_bytes() -> int | None:
    """Return the memory limit of the default JAX device, or ``None`` if unknown.

    CPU devices do not report a limit, and neither do some older backends, so callers must
    treat ``None`` as "cannot check" rather than as "no limit".
    """
    try:
        import jax  # noqa: PLC0415 — optional [jax] dep, kept out of module import
    except ImportError:
        return None
    devices = jax.devices()
    if not devices:
        return None
    stats = getattr(devices[0], "memory_stats", lambda: None)()
    if not stats:
        return None
    limit = stats.get("bytes_limit")
    return int(limit) if limit else None

estimate_batch_memory_bytes(n_events, n_detectors, n_samples, *, earth_rotation=True)

Estimate peak device memory for one :func:simulate_cbc_batch call.

A vmapped batch holds far more than the strain it returns: the measured peak for an IMRPhenomXPHM batch was about 28x its own output. The estimate is therefore n_events * n_samples * 8 * (generation + per_detector * n_detectors), with the coefficients above.

One calibration point

The coefficients come from a single A100 measurement with IMRPhenomXPHM, and the split between detector-independent and per-detector buffers is assumed rather than measured. Treat this as an order-of-magnitude guard that produces a useful error message, not as an accurate predictor. Approximants with smaller graphs than IMRPhenomXPHM will be over-estimated, which only costs a smaller chunk.

Parameters:

Name Type Description Default
n_events int

Events in the batch.

required
n_detectors int

Detectors projected onto.

required
n_samples int

Samples per event segment.

required
earth_rotation bool

Whether the rotating projection is used, which needs more simultaneous buffers per detector.

True

Returns:

Type Description
int

Estimated peak bytes.

Source code in src/gwmock_signal/jax_batch.py
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
def estimate_batch_memory_bytes(
    n_events: int,
    n_detectors: int,
    n_samples: int,
    *,
    earth_rotation: bool = True,
) -> int:
    """Estimate peak device memory for one :func:`simulate_cbc_batch` call.

    A vmapped batch holds far more than the strain it returns: the measured peak for an
    IMRPhenomXPHM batch was about 28x its own output. The estimate is therefore
    ``n_events * n_samples * 8 * (generation + per_detector * n_detectors)``, with the
    coefficients above.

    !!! warning "One calibration point"

        The coefficients come from a single A100 measurement with IMRPhenomXPHM, and the
        split between detector-independent and per-detector buffers is assumed rather than
        measured. Treat this as an order-of-magnitude guard that produces a useful error
        message, not as an accurate predictor. Approximants with smaller graphs than
        IMRPhenomXPHM will be over-estimated, which only costs a smaller chunk.

    Args:
        n_events: Events in the batch.
        n_detectors: Detectors projected onto.
        n_samples: Samples per event segment.
        earth_rotation: Whether the rotating projection is used, which needs more
            simultaneous buffers per detector.

    Returns:
        Estimated peak bytes.
    """
    if min(n_events, n_detectors, n_samples) < 1:
        raise ValueError("n_events, n_detectors and n_samples must all be >= 1")
    per_detector = _PROJECTION_BUFFERS_PER_DETECTOR * (_ROTATION_BUFFER_MULTIPLIER if earth_rotation else 1.0)
    buffers = _GENERATION_BUFFERS + per_detector * n_detectors
    return int(n_events * n_samples * 8 * buffers)

recommend_chunk_size(n_detectors, n_samples, *, earth_rotation=True, memory_fraction=_DEFAULT_MEMORY_FRACTION, available_bytes=None)

Return the largest event count expected to fit, or None if unknown.

Parameters:

Name Type Description Default
n_detectors int

Detectors projected onto.

required
n_samples int

Samples per event segment.

required
earth_rotation bool

Whether the rotating projection is used.

True
memory_fraction float

Fraction of device memory the batch may occupy; must be in (0, 1].

_DEFAULT_MEMORY_FRACTION
available_bytes int | None

Device memory limit; queried from JAX when omitted.

None

Returns:

Type Description
int | None

A chunk size of at least 1, or None when the device limit is unknown.

Raises:

Type Description
ValueError

If memory_fraction is outside (0, 1].

Source code in src/gwmock_signal/jax_batch.py
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
def recommend_chunk_size(
    n_detectors: int,
    n_samples: int,
    *,
    earth_rotation: bool = True,
    memory_fraction: float = _DEFAULT_MEMORY_FRACTION,
    available_bytes: int | None = None,
) -> int | None:
    """Return the largest event count expected to fit, or ``None`` if unknown.

    Args:
        n_detectors: Detectors projected onto.
        n_samples: Samples per event segment.
        earth_rotation: Whether the rotating projection is used.
        memory_fraction: Fraction of device memory the batch may occupy; must be in ``(0, 1]``.
        available_bytes: Device memory limit; queried from JAX when omitted.

    Returns:
        A chunk size of at least 1, or ``None`` when the device limit is unknown.

    Raises:
        ValueError: If ``memory_fraction`` is outside ``(0, 1]``.
    """
    # A fraction above 1 would recommend a chunk larger than the device, i.e. it would hand
    # back exactly the out-of-memory abort this function exists to prevent.
    if not 0.0 < memory_fraction <= 1.0:
        raise ValueError(f"memory_fraction must be in (0, 1]; got {memory_fraction}.")
    limit = available_device_memory_bytes() if available_bytes is None else available_bytes
    if not limit:
        return None
    per_event = estimate_batch_memory_bytes(1, n_detectors, n_samples, earth_rotation=earth_rotation)
    return max(1, int(limit * memory_fraction // per_event))

simulate_cbc_batch(approximant, detector_names, *, sampling_frequency, minimum_frequency, parameters, backend=None, effects=None, ephemeris=None, output_grid=None, earth_rotation=None)

Simulate a catalogue of CBC signals on device, one strain per event and detector.

Evaluates ripple frequency-domain waveforms for the whole catalogue under jax.vmap (a single grid sized worst-case for the longest inspiral), then projects each event onto each detector with the JAX antenna pattern and geocenter delay and inverse-FFTs to strain. The antenna pattern and delay are evaluated per sample by default and once per event at the segment midpoint when earth_rotation=False, matching the two branches of :func:gwmock_signal.projection.network.project_polarizations_to_network.

Like that function, the batch pads each waveform segment by :func:~gwmock_signal.projection.resampling.edge_padding samples at each end before projecting, so the geocentre-to-detector delay cannot move signal past either end. Each row is therefore 2 * edge_padding(sampling_frequency) samples longer than the waveform segment and starts edge_padding(sampling_frequency) samples earlier, with the same start and length as the host projection of the same waveform; epoch and start_index in the result describe that padded row.

Parameters:

Name Type Description Default
approximant str

A supported ripple approximant name.

required
detector_names Sequence[DetectorSpec]

Built-in LAL interferometer codes (e.g. "H1", "L1") and/or :class:~gwmock_signal.detector.CustomDetector instances. A custom detector is resolved through its own geometry, and its output channel is keyed by its name.

required
sampling_frequency float

Sample rate in Hz.

required
minimum_frequency float

Low-frequency cutoff in Hz.

required
parameters Mapping[str, object]

Mapping of canonical gwmock-pop parameter names (no aliases) to equal-length 1-D arrays. In addition to the waveform parameters (masses, spins, distance, inclination, coa_phase) this must include right_ascension, declination, polarization_angle and coa_time.

required
backend RippleBackend | None

Optional configured :class:RippleBackend (e.g. with a fixed segment_duration or f_ref). Defaults to RippleBackend().

None
output_grid SamplingGrid | None

Sample lattice the returned strain should start on. When given, each event's first sample is placed exactly on the grid and the sub-sample remainder is absorbed into the shift the projection already applies -- an exact resampling rather than a second, cruder one downstream. Superposition then becomes an integer-offset add. When omitted, buffers start at the arbitrary time epoch + coa_time and the consumer must resample; see :mod:gwmock_signal.sampling_grid for what that costs.

None
effects ProjectionEffects | None

Which physical effects the projection includes, as on the host; see :class:~gwmock_signal.projection.effects.ProjectionEffects. None, the default, means all of them. Without earth_rotation the antenna pattern and delays are evaluated once at the segment midpoint and applied as an exact frequency-domain shift, which suits signals short compared with an hour: a binary neutron star in the Einstein Telescope band occupies 2048 s at 10 Hz and 16384 s at 5 Hz, over which the detector sweeps tens of degrees. The solar-system delays are absorbed at each event's coa_time.

None
ephemeris SolarSystemEphemeris | None

The Earth and Sun tables for the solar-system delays; the LALPulsar DE405 tables by default.

None
earth_rotation bool | None

Deprecated; effects=ProjectionEffects(earth_rotation=...).

None

Returns:

Name Type Description
A BatchedDetectorStrain

class:BatchedDetectorStrain with the ``(n_events, n_detectors,

BatchedDetectorStrain

n_samples + 2 * edge_padding(sampling_frequency))strain,n_samples`` being the

BatchedDetectorStrain

waveform segment's length, and per-event timing metadata describing the padded rows.

Source code in src/gwmock_signal/jax_batch.py
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
def simulate_cbc_batch(  # noqa: PLR0913
    approximant: str,
    detector_names: Sequence[DetectorSpec],
    *,
    sampling_frequency: float,
    minimum_frequency: float,
    parameters: Mapping[str, object],
    backend: RippleBackend | None = None,
    effects: ProjectionEffects | None = None,
    ephemeris: SolarSystemEphemeris | None = None,
    output_grid: SamplingGrid | None = None,
    earth_rotation: bool | None = None,
) -> BatchedDetectorStrain:
    """Simulate a catalogue of CBC signals on device, one strain per event and detector.

    Evaluates ripple frequency-domain waveforms for the whole catalogue under
    ``jax.vmap`` (a single grid sized worst-case for the longest inspiral), then
    projects each event onto each detector with the JAX antenna pattern and geocenter
    delay and inverse-FFTs to strain. The antenna pattern and delay are evaluated per
    sample by default and once per event at the **segment midpoint** when
    ``earth_rotation=False``, matching the two branches of
    :func:`gwmock_signal.projection.network.project_polarizations_to_network`.

    Like that function, the batch pads each waveform segment by
    :func:`~gwmock_signal.projection.resampling.edge_padding` samples at each end before projecting,
    so the geocentre-to-detector delay cannot move signal past either end. Each row is therefore
    ``2 * edge_padding(sampling_frequency)`` samples longer than the waveform segment and starts
    ``edge_padding(sampling_frequency)`` samples earlier, with the same start and length as the host
    projection of the same waveform; ``epoch`` and ``start_index`` in the result describe that
    padded row.

    Args:
        approximant: A supported ripple approximant name.
        detector_names: Built-in LAL interferometer codes (e.g. ``"H1"``, ``"L1"``) and/or
            :class:`~gwmock_signal.detector.CustomDetector` instances. A custom detector is
            resolved through its own geometry, and its output channel is keyed by its ``name``.
        sampling_frequency: Sample rate in Hz.
        minimum_frequency: Low-frequency cutoff in Hz.
        parameters: Mapping of **canonical** gwmock-pop parameter names (no aliases)
            to equal-length 1-D arrays. In addition to the waveform parameters
            (masses, spins, distance, inclination, coa_phase) this must include
            ``right_ascension``, ``declination``, ``polarization_angle`` and
            ``coa_time``.
        backend: Optional configured :class:`RippleBackend` (e.g. with a fixed
            ``segment_duration`` or ``f_ref``). Defaults to ``RippleBackend()``.
        output_grid: Sample lattice the returned strain should start on. When given, each
            event's first sample is placed exactly on the grid and the sub-sample remainder is
            absorbed into the shift the projection already applies -- an exact resampling
            rather than a second, cruder one downstream. Superposition then becomes an
            integer-offset add. When omitted, buffers start at the arbitrary time
            ``epoch + coa_time`` and the consumer must resample; see
            :mod:`gwmock_signal.sampling_grid` for what that costs.
        effects: Which physical effects the projection includes, as on the host; see
            :class:`~gwmock_signal.projection.effects.ProjectionEffects`. ``None``, the default, means
            all of them. Without ``earth_rotation`` the antenna pattern and delays are evaluated once at
            the segment midpoint and applied as an exact frequency-domain shift, which suits signals
            short compared with an hour: a binary neutron star in the Einstein Telescope band occupies
            2048 s at 10 Hz and 16384 s at 5 Hz, over which the detector sweeps tens of degrees. The
            solar-system delays are absorbed at each event's ``coa_time``.
        ephemeris: The Earth and Sun tables for the solar-system delays; the LALPulsar DE405 tables by
            default.
        earth_rotation: Deprecated; ``effects=ProjectionEffects(earth_rotation=...)``.

    Returns:
        A :class:`BatchedDetectorStrain` with the ``(n_events, n_detectors,
        n_samples + 2 * edge_padding(sampling_frequency))`` strain, ``n_samples`` being the
        waveform segment's length, and per-event timing metadata describing the padded rows.
    """
    import jax.numpy as jnp  # noqa: PLC0415

    effects = _resolve_effects(effects, earth_rotation=earth_rotation, caller="simulate_cbc_batch")
    earth_rotation = effects.earth_rotation
    backend = backend or RippleBackend()
    # Resolved before anything expensive: an unknown detector should fail here, not after a
    # catalogue has been generated. The two halves are used for different things -- lookup_keys
    # index LAL's registry, output_names key the result -- and conflating them is what limited
    # this path to built-in interferometer codes.
    output_names, lookup_keys = _resolve_detector_specs(detector_names)
    lengths = _arm_lengths(list(detector_names)) if effects.finite_arm_length else None

    # Before generating anything: the estimate includes the waveform-generation buffers, so a
    # check placed after generation could never fire for a batch that exhausts memory *during*
    # generation -- which is most of the estimate. The grid length does not need the waveform, only
    # the masses (or a pinned segment duration, which _segment_samples handles), so it can be sized
    # up front. Sized with the projection's padding included, which counts it against generation
    # too: pessimistic by a few hundred samples, in the direction the estimate is meant to err.
    _check_batch_fits(
        len(np.atleast_1d(np.asarray(_required(parameters, "coa_time")))),
        len(lookup_keys),
        _planned_n_samples(backend, parameters, minimum_frequency, sampling_frequency)
        + 2 * edge_padding(sampling_frequency),
        earth_rotation=earth_rotation,
    )
    ephemeris = SolarSystemEphemeris() if ephemeris is None else ephemeris
    _require_ephemeris_span(
        effects,
        ephemeris,
        backend,
        parameters,
        minimum_frequency=minimum_frequency,
        sampling_frequency=sampling_frequency,
        output_grid=output_grid,
    )

    fd = backend.generate_fd_polarizations_batch(
        approximant,
        sampling_frequency=sampling_frequency,
        minimum_frequency=minimum_frequency,
        parameters=parameters,
    )
    n_samples = fd.n_samples
    dt = 1.0 / sampling_frequency
    merger_index, waveform_epoch = backend.coalescence_placement(n_samples, sampling_frequency)
    # Padded as project_polarizations_to_network pads, so the geocentre delay cannot move signal
    # past either end: dropped on the rotating branch, wrapped round to the other end on the
    # phase-shift branch. Every event and detector gets the same padding, start and length as the
    # host projection of the same waveform, and the returned epoch and start indices describe the
    # padded buffer, so a consumer placing it by either needs no change.
    padding = edge_padding(sampling_frequency)
    projected_samples = n_samples + 2 * padding
    epoch = waveform_epoch - padding * dt

    right_ascension = jnp.asarray(_required(parameters, "right_ascension"), dtype=jnp.float64)
    declination = jnp.asarray(_required(parameters, "declination"), dtype=jnp.float64)
    polarization_angle = jnp.asarray(_required(parameters, "polarization_angle"), dtype=jnp.float64)
    coa_time = np.asarray(_required(parameters, "coa_time"), dtype=float)

    start_index, alignment_shift = _alignment(coa_time, waveform_epoch, output_grid, sampling_frequency)

    sky = _event_sky(
        effects,
        ephemeris,
        right_ascension=np.asarray(right_ascension, dtype=float),
        declination=np.asarray(declination, dtype=float),
        coa_time=coa_time,
        input_start=coa_time + waveform_epoch - alignment_shift / sampling_frequency,
        span=(projected_samples - 1) * dt,
        padding_seconds=padding * dt,
    )

    if earth_rotation:
        # The aligned buffer starts one fractional sample earlier than requested, so the
        # sidereal anchor must move with it or F(t) and tau(t) are evaluated up to a full
        # sample after the samples they multiply. `epoch` is the padded buffer's, so the anchor is
        # its first padded sample, where the kernel's sample offsets count from.
        segment_start_gps = coa_time + epoch - alignment_shift / sampling_frequency
        strain = _project_rotating(
            fd,
            lookup_keys,
            n_samples=n_samples,
            padding=padding,
            sampling_frequency=sampling_frequency,
            merger_index=merger_index,
            segment_start_gps=segment_start_gps,
            # The kernel counts from each event's first padded sample, so the sky line is carried there
            # from the first unpadded one, where it was fitted, as on the host.
            right_ascension=jnp.asarray(sky.right_ascension - sky.right_ascension_rate * padding * dt),
            declination=jnp.asarray(sky.declination - sky.declination_rate * padding * dt),
            right_ascension_rate=jnp.asarray(sky.right_ascension_rate),
            declination_rate=jnp.asarray(sky.declination_rate),
            polarization_angle=polarization_angle,
            alignment_shift=alignment_shift,
            arm_lengths={name: lengths[name] for name in output_names} if lengths is not None else None,
            output_names=output_names,
            solar=sky.solar,
        )
        return BatchedDetectorStrain(
            strain=strain,
            detector_names=output_names,
            coa_time=coa_time,
            epoch=epoch,
            sampling_frequency=sampling_frequency,
            start_index=start_index,
            grid=output_grid,
        )

    per_detector = _project_static(
        fd,
        lookup_keys,
        output_names,
        n_samples=n_samples,
        padding=padding,
        sampling_frequency=sampling_frequency,
        merger_index=merger_index,
        epoch=epoch,
        coa_time=coa_time,
        alignment_shift=alignment_shift,
        polarization_angle=polarization_angle,
        sky=sky,
        arm_lengths=lengths,
    )
    strain = jnp.stack(per_detector, axis=1)  # (n_events, n_detectors, n_samples + 2 * padding)
    return BatchedDetectorStrain(
        strain=strain,
        detector_names=output_names,
        coa_time=coa_time,
        epoch=epoch,
        sampling_frequency=sampling_frequency,
        start_index=start_index,
        grid=output_grid,
    )

simulate_cbc_catalogue(approximant, detector_names, *, sampling_frequency, minimum_frequency, parameters, segment_duration, start_time, end_time, backend=None, effects=None, ephemeris=None, n_chirp_mass_bins=1, chunk_size=None, memory_fraction=_DEFAULT_MEMORY_FRACTION, align_to_output_grid=True, interpolate_if_offset=True, earth_rotation=None)

Generate a catalogue on device and assemble it into fixed-duration segments.

Convenience wrapper that runs :func:simulate_cbc_batch and then :func:assemble_segments, tiling [start_time, end_time) into contiguous zero-noise segments of segment_duration. Signals are placed at their coa_time and split across the segments they span; signals outside the span simply do not appear. For non-zero backgrounds use the two-step API (:func:simulate_cbc_batch then :func:assemble_segments) so you can supply a background per segment.

Two independent memory controls (composable):

  • chunk_size bounds the peak generation memory by processing at most that many events per batched call. All chunks of a bin share that bin's grid, so chunking leaves the model untouched -- it agrees with processing the whole bin at once to a few times 1e-13 of peak, measured on both the aligned and unaligned assembly paths. It is not bit-for-bit: XLA picks different reduction orderings for different batch shapes, so the same event generated in a batch of four and a batch of two differs in the last few bits. Nothing physical changes; see :func:_check_batch_fits.
  • n_chirp_mass_bins bounds the buffer length by generating heavier events on shorter grids. Because each bin uses a different frequency resolution, binning is not bit-identical to a single grid (see below).

Parameters:

Name Type Description Default
approximant str

A supported ripple approximant name.

required
detector_names Sequence[DetectorSpec]

Built-in LAL interferometer codes (e.g. "H1", "L1") and/or :class:~gwmock_signal.detector.CustomDetector instances, as for :func:simulate_cbc_batch.

required
sampling_frequency float

Sample rate in Hz.

required
minimum_frequency float

Low-frequency cutoff in Hz.

required
parameters Mapping[str, object]

Canonical catalogue parameters as struct-of-arrays (see :func:simulate_cbc_batch).

required
segment_duration float

Duration of every output segment, in seconds.

required
start_time float

GPS start of the first segment.

required
end_time float

GPS time the tiling must cover up to; the final segment is the first one whose span reaches or passes end_time.

required
backend RippleBackend | None

Optional configured :class:RippleBackend. If it pins a segment_duration that grid is used for every bin (binning then saves no buffer memory, though it still splits the catalogue into per-bin calls, and the run keeps the single-grid model).

None
effects ProjectionEffects | None

Forwarded to :func:simulate_cbc_batch; all on by default. Its earth_rotation also makes the automatic chunk size smaller, because the rotating path holds more buffers per detector.

None
ephemeris SolarSystemEphemeris | None

Forwarded to :func:simulate_cbc_batch.

None
n_chirp_mass_bins int

Number of chirp-mass groups generated separately, each on its own worst-case grid (lightest first), injected on top of the earlier bins. 1 (default) uses a single grid sized for the lowest-mass event. Binned output agrees with a single-grid run only at the per-event grid discretization level (a fraction of a percent in overlap) — the resolution the per-event path uses.

1
chunk_size int | None

Generate at most this many events per batched call (within each bin). Model-preserving whatever the value -- it only bounds peak memory, and agrees with an unchunked run to a few times 1e-13 of peak rather than bitwise. When omitted, a size is chosen from the device memory limit and the grid actually selected (see :func:recommend_chunk_size) — previously the default was no chunking at all, which is what made a large catalogue abort with a bare XLA out-of-memory error. Pass an explicit value to override the estimate.

None
memory_fraction float

Fraction of device memory an automatically chosen chunk may occupy. Ignored when chunk_size is given.

_DEFAULT_MEMORY_FRACTION
align_to_output_grid bool

Generate every batch on the lattice defined by this function's own segment starts, so superposition is an exact integer-offset add. Defaults to True because the alternative resamples each already-projected signal a second time during assembly. Set False only to reproduce that older behaviour deliberately; it is a legacy mode, not a fallback.

True
interpolate_if_offset bool

Forwarded to :func:assemble_segments.

True
earth_rotation bool | None

Deprecated; effects=ProjectionEffects(earth_rotation=...).

None

Returns:

Name Type Description
One list[DetectorStrainStack]

class:~gwmock_signal.multichannel.stack.DetectorStrainStack per

list[DetectorStrainStack]

segment, in time order.

Source code in src/gwmock_signal/jax_batch.py
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
def simulate_cbc_catalogue(  # noqa: PLR0913
    approximant: str,
    detector_names: Sequence[DetectorSpec],
    *,
    sampling_frequency: float,
    minimum_frequency: float,
    parameters: Mapping[str, object],
    segment_duration: float,
    start_time: float,
    end_time: float,
    backend: RippleBackend | None = None,
    effects: ProjectionEffects | None = None,
    ephemeris: SolarSystemEphemeris | None = None,
    n_chirp_mass_bins: int = 1,
    chunk_size: int | None = None,
    memory_fraction: float = _DEFAULT_MEMORY_FRACTION,
    align_to_output_grid: bool = True,
    interpolate_if_offset: bool = True,
    earth_rotation: bool | None = None,
) -> list[DetectorStrainStack]:
    """Generate a catalogue on device and assemble it into fixed-duration segments.

    Convenience wrapper that runs :func:`simulate_cbc_batch` and then
    :func:`assemble_segments`, tiling ``[start_time, end_time)`` into contiguous
    zero-noise segments of ``segment_duration``. Signals are placed at their
    ``coa_time`` and split across the segments they span; signals outside the span
    simply do not appear. For non-zero backgrounds use the two-step API
    (:func:`simulate_cbc_batch` then :func:`assemble_segments`) so you can supply a
    background per segment.

    Two independent memory controls (composable):

    - ``chunk_size`` bounds the *peak* generation memory by processing at most that
      many events per batched call. All chunks of a bin share that bin's grid, so
      chunking leaves the *model* untouched -- it agrees with processing the whole
      bin at once to a few times 1e-13 of peak, measured on both the aligned and
      unaligned assembly paths. It is **not** bit-for-bit: XLA picks different
      reduction orderings for different batch shapes, so the same event generated in
      a batch of four and a batch of two differs in the last few bits. Nothing
      physical changes; see :func:`_check_batch_fits`.
    - ``n_chirp_mass_bins`` bounds the *buffer length* by generating heavier events
      on shorter grids. Because each bin uses a different frequency resolution,
      binning is **not** bit-identical to a single grid (see below).

    Args:
        approximant: A supported ripple approximant name.
        detector_names: Built-in LAL interferometer codes (e.g. ``"H1"``, ``"L1"``) and/or
            :class:`~gwmock_signal.detector.CustomDetector` instances, as for
            :func:`simulate_cbc_batch`.
        sampling_frequency: Sample rate in Hz.
        minimum_frequency: Low-frequency cutoff in Hz.
        parameters: Canonical catalogue parameters as struct-of-arrays (see
            :func:`simulate_cbc_batch`).
        segment_duration: Duration of every output segment, in seconds.
        start_time: GPS start of the first segment.
        end_time: GPS time the tiling must cover up to; the final segment is the
            first one whose span reaches or passes ``end_time``.
        backend: Optional configured :class:`RippleBackend`. If it pins a
            ``segment_duration`` that grid is used for every bin (binning then
            saves no *buffer* memory, though it still splits the catalogue into
            per-bin calls, and the run keeps the single-grid model).
        effects: Forwarded to :func:`simulate_cbc_batch`; all on by default. Its ``earth_rotation``
            also makes the automatic chunk size smaller, because the rotating path holds more buffers
            per detector.
        ephemeris: Forwarded to :func:`simulate_cbc_batch`.
        n_chirp_mass_bins: Number of chirp-mass groups generated separately, each on
            its own worst-case grid (lightest first), injected on top of the
            earlier bins. ``1`` (default) uses a single grid sized for the
            lowest-mass event. Binned output agrees with a single-grid run only at
            the per-event grid discretization level (a fraction of a percent in
            overlap) — the resolution the per-event path uses.
        chunk_size: Generate at most this many events per batched call (within each bin).
            Model-preserving whatever the value -- it only bounds peak memory, and agrees
            with an unchunked run to a few times 1e-13 of peak rather than bitwise. When omitted,
            a size is chosen from the device memory limit and the grid actually selected
            (see :func:`recommend_chunk_size`) — previously the default was no chunking at
            all, which is what made a large catalogue abort with a bare XLA
            out-of-memory error. Pass an explicit value to override the estimate.
        memory_fraction: Fraction of device memory an automatically chosen chunk may
            occupy. Ignored when ``chunk_size`` is given.
        align_to_output_grid: Generate every batch on the lattice defined by this function's
            own segment starts, so superposition is an exact integer-offset add. Defaults to
            ``True`` because the alternative resamples each already-projected signal a second
            time during assembly. Set ``False`` only to reproduce that older behaviour
            deliberately; it is a legacy mode, not a fallback.
        interpolate_if_offset: Forwarded to :func:`assemble_segments`.

        earth_rotation: Deprecated; ``effects=ProjectionEffects(earth_rotation=...)``.

    Returns:
        One :class:`~gwmock_signal.multichannel.stack.DetectorStrainStack` per
        segment, in time order.
    """
    effects = _resolve_effects(effects, earth_rotation=earth_rotation, caller="simulate_cbc_catalogue")
    if segment_duration <= 0:
        raise ValueError("segment_duration must be > 0")
    if end_time <= start_time:
        raise ValueError("end_time must be greater than start_time")
    if n_chirp_mass_bins < 1:
        raise ValueError("n_chirp_mass_bins must be >= 1")
    if chunk_size is not None and chunk_size < 1:
        raise ValueError("chunk_size must be >= 1")

    backend = backend or RippleBackend()
    # Resolved once here purely so an unusable detector is reported before any generation. Each
    # chunk resolves again inside simulate_cbc_batch, which is cheap: a CustomDetector caches its
    # LAL detector on first use, and a built-in code is a dictionary lookup.
    _resolve_detector_specs(detector_names)
    n_segments = int(np.ceil((end_time - start_time) / segment_duration))
    segment_start_times = start_time + np.arange(n_segments) * segment_duration

    output_grid: SamplingGrid | None = None
    if align_to_output_grid:
        samples_per_segment = segment_duration * sampling_frequency
        if abs(samples_per_segment - round(samples_per_segment)) > _WHOLE_SAMPLE_TOLERANCE:
            raise ValueError(
                f"segment_duration * sampling_frequency must be a whole number of samples for the "
                f"segments to share one lattice; {segment_duration} x {sampling_frequency} = "
                f"{samples_per_segment}. Adjust one of them, or pass align_to_output_grid=False to "
                f"accept resampled superposition."
            )
        # One grid for the whole catalogue, so every chunk and every chirp-mass bin lands on the
        # same lattice and can be superposed with integer offsets. The starts are then rebuilt
        # *from integer sample indices* rather than kept as start_time + k * segment_duration:
        # over a long span the repeated float multiplication accumulates representation error,
        # so a start intended to be on the lattice can drift off it even when the spacing is a
        # whole number of samples.
        output_grid = SamplingGrid(epoch=float(start_time), sampling_frequency=sampling_frequency)
        segment_start_times = output_grid.time_of(np.arange(n_segments, dtype=np.int64) * round(samples_per_segment))

    bins = _chirp_mass_bins(parameters, n_chirp_mass_bins)
    # Every bin before the first is generated: chunks run lightest first, so an event outside the
    # ephemeris in a heavy bin would otherwise fail after the lighter bins had all been generated.
    ephemeris = SolarSystemEphemeris() if ephemeris is None else ephemeris
    for bin_indices in bins:
        _require_ephemeris_span(
            effects,
            ephemeris,
            _bin_backend(backend, parameters, bin_indices, minimum_frequency, sampling_frequency),
            {key: np.asarray(values)[bin_indices] for key, values in parameters.items()},
            minimum_frequency=minimum_frequency,
            sampling_frequency=sampling_frequency,
            output_grid=output_grid,
        )

    segments: list[DetectorStrainStack] | None = None
    for bin_indices in bins:
        # Pin the grid to this bin's worst case so every chunk of the bin shares it
        # (so chunking within the bin does not change the model). A user-pinned backend is left as-is.
        bin_backend = _bin_backend(backend, parameters, bin_indices, minimum_frequency, sampling_frequency)
        # Size the chunk from this bin's own grid: bins differ in n_samples by design, so a
        # single catalogue-wide chunk size would be wrong for all but one of them.
        effective_chunk = chunk_size
        if effective_chunk is None:
            # Plus the projection's padding, which every returned row carries; see simulate_cbc_batch.
            bin_samples = _bin_n_samples(
                bin_backend, parameters, bin_indices, minimum_frequency, sampling_frequency
            ) + 2 * edge_padding(sampling_frequency)
            effective_chunk = recommend_chunk_size(
                len(tuple(detector_names)),
                bin_samples,
                earth_rotation=effects.earth_rotation,
                memory_fraction=memory_fraction,
            )
        for chunk_indices in _count_chunks(bin_indices, effective_chunk):
            chunk_parameters = {key: np.asarray(values)[chunk_indices] for key, values in parameters.items()}
            batch = simulate_cbc_batch(
                approximant,
                detector_names,
                sampling_frequency=sampling_frequency,
                minimum_frequency=minimum_frequency,
                parameters=chunk_parameters,
                backend=bin_backend,
                effects=effects,
                ephemeris=ephemeris,
                output_grid=output_grid,
            )
            # Chain groups: inject each on top of the segments built from earlier ones.
            backgrounds = [stack.to_dict() for stack in segments] if segments is not None else None
            segments = assemble_segments(
                batch,
                segment_duration=segment_duration,
                segment_start_times=segment_start_times,
                backgrounds=backgrounds,
                interpolate_if_offset=interpolate_if_offset,
            )
    return segments if segments is not None else []

For the narrative guide and examples, see Batched GPU simulation. For the single-event CPU path, see Pipeline and Simulator.