laddu 0.20.0

Amplitude analysis tools for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
use std::{path::PathBuf, sync::Arc};

use laddu_data::{
    data::{Dataset, EventBatch, MemoryPolicy},
    io::{
        parquet::{ParquetSink, ParquetSource},
        root::{RootSink, RootSource},
    },
    schema::Schema,
};
use laddu_physics::vectors::RealVec4;
use laddu_runtime::{DatasetExprExt, MemoryBudget};
use numpy::{PyArray1, PyReadonlyArray1, PyReadonlyArray2, PyUntypedArrayMethods};
use pyo3::{
    exceptions::PyValueError,
    prelude::*,
    types::{PyAny, PyDict},
};

use super::{
    error::to_py_err,
    expr::PyExpr,
    query::{PyBin, PyPredicate},
    runtime::{PyExecution, memory_decision_dict, parse_memory_budget},
};

fn path_string(path: &Bound<'_, PyAny>) -> PyResult<String> {
    let path: PathBuf = path.extract()?;
    Ok(path.to_string_lossy().into_owned())
}

fn configure(
    dataset: Dataset,
    memory: Option<&Bound<'_, PyAny>>,
    cache: &str,
) -> PyResult<Dataset> {
    let budget = memory
        .map(parse_memory_budget)
        .transpose()?
        .unwrap_or(MemoryBudget::Auto);
    let dataset = dataset.with_memory_budget(budget);
    match cache.trim().to_ascii_lowercase().as_str() {
        "fastest" | "auto" => Ok(dataset.fastest()),
        "resident" => Ok(dataset.resident()),
        "streaming" => Ok(dataset.streaming()),
        _ => Err(PyValueError::new_err(
            "cache must be 'fastest', 'resident', or 'streaming'",
        )),
    }
}

#[pyclass(name = "ParquetSource", module = "laddu", frozen, skip_from_py_object)]
#[derive(Clone)]
/// A configurable Parquet dataset source.
///
/// Parameters
/// ----------
/// path : path-like
///     Parquet file, directory, or glob understood by the Arrow reader.
/// memory : MemoryBudget, int, or str, optional
///     Host-memory budget. Defaults to automatic memory planning.
/// cache : {'fastest', 'resident', 'streaming'}, default='fastest'
///     Prefer the fastest fitting strategy or require a particular cache mode.
/// nulls : {'error', 'nan'}, default='error'
///     Reject null scalar values or replace them with NaN.
/// validate : bool, default=True
///     Validate every matched file when constructing the source.
///
/// Notes
/// -----
/// Constructing a source records the read plan. Data are loaded when the
/// resulting :class:`Dataset` is traversed.
pub struct PyParquetSource {
    inner: Dataset,
}

#[pymethods]
impl PyParquetSource {
    /// Configure a Parquet source.
    ///
    /// Raises
    /// ------
    /// ValueError
    ///     If ``cache`` or ``nulls`` is invalid.
    /// LadduError
    ///     If the source cannot be discovered or validated.
    #[new]
    #[pyo3(signature = (
        path: "PathLike[str] | str",
        *,
        memory: "MemoryBudget | int | str | None" = None,
        cache="fastest",
        nulls="error",
        validate=true
    ))]
    fn new(
        path: &Bound<'_, PyAny>,
        memory: Option<&Bound<'_, PyAny>>,
        cache: &str,
        nulls: &str,
        validate: bool,
    ) -> PyResult<Self> {
        let mut builder = ParquetSource::builder(path_string(path)?).validate_all_files(validate);
        builder = match nulls {
            "error" => builder.error_on_nulls(),
            "nan" => builder.nulls_as_nan(),
            _ => return Err(PyValueError::new_err("nulls must be 'error' or 'nan'")),
        };
        Ok(Self {
            inner: configure(
                Dataset::new(builder.build().map_err(to_py_err)?),
                memory,
                cache,
            )?,
        })
    }
}

#[pyclass(name = "RootSource", module = "laddu", frozen, skip_from_py_object)]
#[derive(Clone)]
/// A configurable ROOT TTree dataset source.
///
/// Parameters
/// ----------
/// path : path-like
///     ROOT file, directory, or glob.
/// tree : str, optional
///     TTree name. If omitted, the reader discovers a compatible tree.
/// memory : MemoryBudget, int, or str, optional
///     Host-memory budget. Defaults to automatic memory planning.
/// cache : {'fastest', 'resident', 'streaming'}, default='fastest'
///     Prefer the fastest fitting strategy or require a particular cache mode.
/// validate : bool, default=True
///     Validate every matched file when constructing the source.
pub struct PyRootSource {
    inner: Dataset,
}

#[pymethods]
impl PyRootSource {
    /// Configure a ROOT source.
    ///
    /// Raises
    /// ------
    /// ValueError
    ///     If ``cache`` is invalid.
    /// LadduError
    ///     If the source, tree, or schema is invalid.
    #[new]
    #[pyo3(signature = (
        path: "PathLike[str] | str",
        *,
        tree=None,
        memory: "MemoryBudget | int | str | None" = None,
        cache="fastest",
        validate=true
    ))]
    fn new(
        path: &Bound<'_, PyAny>,
        tree: Option<&str>,
        memory: Option<&Bound<'_, PyAny>>,
        cache: &str,
        validate: bool,
    ) -> PyResult<Self> {
        let mut builder = RootSource::builder(path_string(path)?).validate_all_files(validate);
        if let Some(tree) = tree {
            builder = builder.tree(tree);
        }
        Ok(Self {
            inner: configure(
                Dataset::new(builder.build().map_err(to_py_err)?),
                memory,
                cache,
            )?,
        })
    }
}

#[pyclass(name = "ParquetSink", module = "laddu", frozen, skip_from_py_object)]
#[derive(Clone)]
/// A destination for writing a dataset as Parquet.
///
/// Parameters
/// ----------
/// path : path-like
///     Output file or dataset directory.
/// precision : {'f32', 'f64'}, default='f64'
///     Floating-point storage precision.
pub struct PyParquetSink {
    path: PathBuf,
    precision: String,
}

#[pymethods]
impl PyParquetSink {
    /// Configure a Parquet destination.
    ///
    /// Raises
    /// ------
    /// ValueError
    ///     If ``precision`` is not ``'f32'`` or ``'f64'``.
    #[new]
    #[pyo3(signature = (path, *, precision="f64"))]
    fn new(path: PathBuf, precision: &str) -> PyResult<Self> {
        validate_precision(precision)?;
        Ok(Self {
            path,
            precision: precision.to_owned(),
        })
    }
}

#[pyclass(name = "RootSink", module = "laddu", frozen, skip_from_py_object)]
#[derive(Clone)]
/// A destination for writing a dataset as a ROOT TTree.
///
/// Parameters
/// ----------
/// path : path-like
///     Output ROOT file.
/// tree : str, default='tree'
///     Name of the output TTree.
/// precision : {'f32', 'f64'}, default='f64'
///     Floating-point storage precision.
pub struct PyRootSink {
    path: PathBuf,
    tree: String,
    precision: String,
}

#[pymethods]
impl PyRootSink {
    /// Configure a ROOT destination.
    ///
    /// Raises
    /// ------
    /// ValueError
    ///     If ``precision`` is not ``'f32'`` or ``'f64'``.
    #[new]
    #[pyo3(signature = (path, *, tree="tree", precision="f64"))]
    fn new(path: PathBuf, tree: &str, precision: &str) -> PyResult<Self> {
        validate_precision(precision)?;
        Ok(Self {
            path,
            tree: tree.to_owned(),
            precision: precision.to_owned(),
        })
    }
}

fn validate_precision(precision: &str) -> PyResult<()> {
    match precision {
        "f32" | "f64" => Ok(()),
        _ => Err(PyValueError::new_err("precision must be 'f32' or 'f64'")),
    }
}

fn precision(value: &str) -> laddu_data::schema::Precision {
    match value {
        "f32" => laddu_data::schema::Precision::F32,
        _ => laddu_data::schema::Precision::F64,
    }
}

fn p4_array(values: &Bound<'_, PyAny>, name: &str) -> PyResult<(usize, Arc<[RealVec4]>)> {
    if let Ok(values) = values.extract::<PyReadonlyArray2<'_, f64>>() {
        let shape = values.shape();
        if shape[1] != 4 {
            return Err(PyValueError::new_err(format!(
                "p4 column {name:?} must have shape (events, 4), got ({}, {})",
                shape[0], shape[1]
            )));
        }
        let column = values
            .as_array()
            .rows()
            .into_iter()
            .map(|row| RealVec4::new(row[0], row[1], row[2], row[3]))
            .collect();
        return Ok((shape[0], column));
    }
    if let Ok(values) = values.extract::<PyReadonlyArray2<'_, f32>>() {
        let shape = values.shape();
        if shape[1] != 4 {
            return Err(PyValueError::new_err(format!(
                "p4 column {name:?} must have shape (events, 4), got ({}, {})",
                shape[0], shape[1]
            )));
        }
        let column = values
            .as_array()
            .rows()
            .into_iter()
            .map(|row| RealVec4::new(row[0] as f64, row[1] as f64, row[2] as f64, row[3] as f64))
            .collect();
        return Ok((shape[0], column));
    }
    Err(PyValueError::new_err(format!(
        "p4 column {name:?} must be a float32 or float64 NumPy array"
    )))
}

fn scalar_array(values: &Bound<'_, PyAny>, name: &str) -> PyResult<Arc<[f64]>> {
    if let Ok(values) = values.extract::<PyReadonlyArray1<'_, f64>>() {
        return Ok(values.as_array().iter().copied().collect());
    }
    if let Ok(values) = values.extract::<PyReadonlyArray1<'_, f32>>() {
        return Ok(values
            .as_array()
            .iter()
            .map(|&value| value as f64)
            .collect());
    }
    Err(PyValueError::new_err(format!(
        "scalar column {name:?} must be a float32 or float64 NumPy array"
    )))
}

#[pyclass(name = "Dataset", module = "laddu", frozen, skip_from_py_object)]
#[derive(Clone)]
/// A reusable collection of weighted physics events.
///
/// Parameters
/// ----------
/// source : ParquetSource or RootSource
///     Lazy source that defines the dataset schema and read plan.
///
/// Examples
/// --------
/// Construct a dataset directly from NumPy arrays:
///
/// >>> import laddu as ld
/// >>> import numpy as np
/// >>> events = ld.Dataset.from_arrays(
/// ...     p4s={"beam": np.array([[5.0, 0.0, 0.0, 5.0]])},
/// ...     scalars={"run": np.array([1.0])},
/// ... )
/// >>> len(events)
/// 1
/// >>> events.p4_names()
/// ['beam']
pub struct PyDataset {
    pub(crate) inner: Dataset,
}

#[pymethods]
impl PyDataset {
    /// Create a dataset from a file source.
    ///
    /// Raises
    /// ------
    /// TypeError
    ///     If ``source`` is not a :class:`ParquetSource` or
    ///     :class:`RootSource`.
    #[new]
    #[pyo3(signature = (source: "ParquetSource | RootSource"))]
    fn new(source: &Bound<'_, PyAny>) -> PyResult<Self> {
        if let Ok(source) = source.extract::<PyRef<'_, PyParquetSource>>() {
            return Ok(Self {
                inner: source.inner.clone(),
            });
        }
        if let Ok(source) = source.extract::<PyRef<'_, PyRootSource>>() {
            return Ok(Self {
                inner: source.inner.clone(),
            });
        }
        Err(pyo3::exceptions::PyTypeError::new_err(
            "Dataset source must be a ParquetSource or RootSource",
        ))
    }

    #[staticmethod]
    #[pyo3(signature = (
        *,
        p4s: "dict",
        scalars: "dict",
        weights: "Sequence[float] | None" = None
    ))]
    /// Create an in-memory dataset from NumPy columns.
    ///
    /// Parameters
    /// ----------
    /// p4s : dict[str, numpy.ndarray]
    ///     Four-vector columns with shape ``(n_events, 4)`` and components in
    ///     ``(E, px, py, pz)`` order.
    /// scalars : dict[str, numpy.ndarray]
    ///     One-dimensional scalar columns.
    /// weights : numpy.ndarray, optional
    ///     One-dimensional event weights. Unit weights are used by default.
    ///
    /// Returns
    /// -------
    /// Dataset
    ///     A resident, in-memory dataset.
    ///
    /// Raises
    /// ------
    /// ValueError
    ///     If a dtype or shape is invalid, or column lengths differ.
    /// LadduError
    ///     If the column names do not form a valid schema.
    fn from_arrays(
        p4s: &Bound<'_, PyDict>,
        scalars: &Bound<'_, PyDict>,
        weights: Option<&Bound<'_, PyAny>>,
    ) -> PyResult<Self> {
        let mut p4_names = Vec::with_capacity(p4s.len());
        let mut p4_columns = Vec::with_capacity(p4s.len());
        let mut expected_len = None;

        for (name, values) in p4s.iter() {
            let name = name.extract::<String>()?;
            let (len, column) = p4_array(&values, &name)?;
            if expected_len.is_some_and(|expected| expected != len) {
                return Err(PyValueError::new_err(
                    "all dataset columns must have the same number of events",
                ));
            }
            expected_len = Some(len);
            p4_names.push(name);
            p4_columns.push(column);
        }

        let mut scalar_names = Vec::with_capacity(scalars.len());
        let mut scalar_columns = Vec::with_capacity(scalars.len());
        for (name, values) in scalars.iter() {
            let name = name.extract::<String>()?;
            let values = scalar_array(&values, &name)?;
            let len = values.len();
            if expected_len.is_some_and(|expected| expected != len) {
                return Err(PyValueError::new_err(
                    "all dataset columns must have the same number of events",
                ));
            }
            expected_len = Some(len);
            scalar_names.push(name);
            scalar_columns.push(values);
        }

        let weights = weights
            .map(|weights| {
                let values = scalar_array(weights, "weights")?;
                if expected_len.is_some_and(|expected| expected != values.len()) {
                    return Err(PyValueError::new_err(
                        "weights must have the same number of events as the dataset columns",
                    ));
                }
                Ok(values)
            })
            .transpose()?;
        let schema =
            Arc::new(Schema::new(p4_names, scalar_names, weights.is_some()).map_err(to_py_err)?);
        let batch =
            EventBatch::new(schema, p4_columns, scalar_columns, weights).map_err(to_py_err)?;
        Ok(Self {
            inner: Dataset::from_batch(batch),
        })
    }

    fn __repr__(&self) -> String {
        let policy = match self.inner.memory_policy() {
            MemoryPolicy::Fastest => "fastest",
            MemoryPolicy::Resident => "resident",
            MemoryPolicy::Streaming => "streaming",
        };
        format!(
            "Dataset(memory_policy={policy:?}, memory={:?})",
            self.inner.memory_budget()
        )
    }

    /// Return the most recent memory-derived read decision, if data were read.
    fn memory_decision<'py>(&self, py: Python<'py>) -> PyResult<Option<Bound<'py, PyDict>>> {
        self.inner
            .last_memory_decision()
            .as_ref()
            .map(|decision| memory_decision_dict(py, decision))
            .transpose()
    }

    /// Return the names of all four-vector columns.
    ///
    /// Returns
    /// -------
    /// list of str
    ///     Names in schema order.
    fn p4_names(&self) -> PyResult<Vec<String>> {
        Ok(self
            .inner
            .schema()
            .map_err(to_py_err)?
            .p4s()
            .iter()
            .map(ToString::to_string)
            .collect())
    }

    /// Return the names of all scalar columns.
    ///
    /// Returns
    /// -------
    /// list of str
    ///     Names in schema order.
    fn scalar_names(&self) -> PyResult<Vec<String>> {
        Ok(self
            .inner
            .schema()
            .map_err(to_py_err)?
            .scalars()
            .iter()
            .map(ToString::to_string)
            .collect())
    }

    #[pyo3(signature = (
        expr,
        *,
        execution=None,
        real=false
    ) -> "Sequence[float]")]
    /// Evaluate an expression for every event.
    ///
    /// Parameters
    /// ----------
    /// expr : Expr
    ///     Symbolic expression to evaluate.
    /// execution : Execution, optional
    ///     Runtime configuration. Automatic local execution is used by default.
    /// real : bool, default=False
    ///     Return ``float64`` real components instead of complex values.
    ///
    /// Returns
    /// -------
    /// numpy.ndarray
    ///     One value per event.
    ///
    /// Raises
    /// ------
    /// LadduError
    ///     If the expression cannot be compiled or the dataset cannot be read.
    fn evaluate<'py>(
        &self,
        py: Python<'py>,
        expr: &PyExpr,
        execution: Option<&PyExecution>,
        real: bool,
    ) -> PyResult<Bound<'py, PyAny>> {
        let execution = execution
            .cloned()
            .map(Ok)
            .unwrap_or_else(PyExecution::default_inner)?;
        let dataset = self.inner.clone();
        let expr = expr.inner.clone();
        if real {
            let values = py
                .detach(move || dataset.evaluate_real(&expr, &execution.inner))
                .map_err(to_py_err)?;
            Ok(PyArray1::from_vec(py, values).into_any())
        } else {
            let values = py
                .detach(move || dataset.evaluate_expr(&expr, &execution.inner))
                .map_err(to_py_err)?;
            Ok(PyArray1::from_vec(py, values).into_any())
        }
    }

    /// Sum the event weights.
    ///
    /// Returns
    /// -------
    /// float
    ///     Sum over all explicit or implicit unit weights.
    fn sum_weights(&self, py: Python<'_>) -> PyResult<f64> {
        let dataset = self.inner.clone();
        py.detach(move || dataset.sum_weights()).map_err(to_py_err)
    }

    /// Materialize the event weights.
    ///
    /// Returns
    /// -------
    /// numpy.ndarray
    ///     Array with shape ``(n_events,)``.
    #[pyo3(signature = () -> "Sequence[float]")]
    fn weights<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyArray1<f64>>> {
        let dataset = self.inner.clone();
        let weights = py
            .detach(move || {
                dataset.try_fold_events(Vec::new(), |mut weights, event| {
                    weights.push(event.weight());
                    Ok(weights)
                })
            })
            .map_err(to_py_err)?;
        Ok(PyArray1::from_vec(py, weights))
    }

    fn __len__(&self, py: Python<'_>) -> PyResult<usize> {
        let dataset = self.inner.clone();
        py.detach(move || dataset.try_fold_events(0usize, |count, _| Ok(count + 1)))
            .map_err(to_py_err)
    }

    #[pyo3(signature = (fraction, *, seed=0))]
    /// Select a reproducible random fraction of events.
    ///
    /// Parameters
    /// ----------
    /// fraction : float
    ///     Selection probability in the closed interval ``[0, 1]``.
    /// seed : int, default=0
    ///     Random seed.
    ///
    /// Returns
    /// -------
    /// Dataset
    ///     A lazily transformed dataset.
    ///
    /// Raises
    /// ------
    /// LadduError
    ///     If ``fraction`` is outside its valid range.
    fn subsample(&self, fraction: f64, seed: u64) -> PyResult<Self> {
        Ok(Self {
            inner: self
                .inner
                .clone()
                .subsample(fraction, seed)
                .map_err(to_py_err)?,
        })
    }

    #[pyo3(signature = (*, seed=0))]
    /// Create a Poisson-bootstrap replica of the dataset.
    ///
    /// Each event weight is multiplied by an independent Poisson(1) draw. The
    /// transformation is deterministic for a given seed.
    ///
    /// Parameters
    /// ----------
    /// seed : int, default=0
    ///     Random seed.
    ///
    /// Returns
    /// -------
    /// Dataset
    ///     A lazily reweighted dataset.
    fn bootstrap(&self, seed: u64) -> Self {
        Self {
            inner: self.inner.clone().bootstrap(seed),
        }
    }

    #[pyo3(signature = (predicate, *, execution=None))]
    /// Retain events satisfying a symbolic predicate.
    ///
    /// Parameters
    /// ----------
    /// predicate : Predicate
    ///     Event-wise selection condition.
    /// execution : Execution, optional
    ///     Runtime configuration.
    ///
    /// Returns
    /// -------
    /// Dataset
    ///     The selected events.
    fn select(&self, predicate: &PyPredicate, execution: Option<&PyExecution>) -> PyResult<Self> {
        let execution = execution
            .cloned()
            .map(Ok)
            .unwrap_or_else(PyExecution::default_inner)?;
        Ok(Self {
            inner: self
                .inner
                .select(&predicate.inner, &execution.inner)
                .map_err(to_py_err)?,
        })
    }

    #[pyo3(signature = (expr, bins, *, execution=None))]
    /// Partition events into bins of an evaluated expression.
    ///
    /// Parameters
    /// ----------
    /// expr : Expr
    ///     Real-valued binning expression.
    /// bins : Bin
    ///     Uniform or explicitly edged bin specification.
    /// execution : Execution, optional
    ///     Runtime configuration.
    ///
    /// Returns
    /// -------
    /// list of BinnedDataset
    ///     Bin metadata and the events assigned to each bin.
    fn bin_by(
        &self,
        expr: &PyExpr,
        bins: &PyBin,
        execution: Option<&PyExecution>,
    ) -> PyResult<Vec<PyBinDataset>> {
        let execution = execution
            .cloned()
            .map(Ok)
            .unwrap_or_else(PyExecution::default_inner)?;
        Ok(self
            .inner
            .bin_by(&expr.inner, bins.inner.clone(), &execution.inner)
            .map_err(to_py_err)?
            .into_iter()
            .map(|bin| PyBinDataset {
                index: bin.index(),
                low: bin.lower(),
                high: bin.upper(),
                dataset: PyDataset {
                    inner: bin.into_dataset(),
                },
            })
            .collect())
    }

    /// Write all events to a configured file destination.
    ///
    /// Parameters
    /// ----------
    /// sink : ParquetSink or RootSink
    ///     Output format and path.
    ///
    /// Raises
    /// ------
    /// TypeError
    ///     If ``sink`` has an unsupported type.
    /// LadduError
    ///     If reading or writing fails.
    #[pyo3(signature = (sink: "ParquetSink | RootSink"))]
    fn write_to(&self, py: Python<'_>, sink: &Bound<'_, PyAny>) -> PyResult<()> {
        let dataset = self.inner.clone();
        if let Ok(sink) = sink.extract::<PyRef<'_, PyParquetSink>>() {
            let mut sink = ParquetSink::builder(sink.path.clone())
                .precision(precision(&sink.precision))
                .build();
            return py
                .detach(move || dataset.write_to(&mut sink))
                .map_err(to_py_err);
        }
        if let Ok(sink) = sink.extract::<PyRef<'_, PyRootSink>>() {
            let mut sink = RootSink::builder(sink.path.clone())
                .tree(sink.tree.as_str())
                .precision(precision(&sink.precision))
                .build();
            return py
                .detach(move || dataset.write_to(&mut sink))
                .map_err(to_py_err);
        }
        Err(pyo3::exceptions::PyTypeError::new_err(
            "Dataset sink must be a ParquetSink or RootSink",
        ))
    }
}

#[pyclass(name = "BinnedDataset", module = "laddu", frozen, skip_from_py_object)]
/// A dataset and the numeric interval that selected it.
///
/// Attributes
/// ----------
/// index : int
///     Zero-based bin index.
/// low : float
///     Inclusive lower edge.
/// high : float
///     Exclusive upper edge, except where the binning policy includes the final
///     boundary.
/// dataset : Dataset
///     Events assigned to this interval.
pub struct PyBinDataset {
    #[pyo3(get)]
    index: usize,
    #[pyo3(get)]
    low: f64,
    #[pyo3(get)]
    high: f64,
    #[pyo3(get)]
    dataset: PyDataset,
}

#[pyfunction]
#[pyo3(signature = (
    path: "PathLike[str] | str",
    *,
    memory: "MemoryBudget | int | str | None" = None,
    cache="fastest",
    nulls="error",
    validate=true
))]
/// Read a Parquet dataset.
///
/// This is shorthand for ``Dataset(ParquetSource(...))``.
///
/// Parameters
/// ----------
/// path : path-like
///     Parquet file, directory, or glob.
/// memory : MemoryBudget, int, or str, optional
///     Host-memory budget. Defaults to automatic memory planning.
/// cache : {'fastest', 'resident', 'streaming'}, default='fastest'
///     Dataset cache policy.
/// nulls : {'error', 'nan'}, default='error'
///     Null-value policy.
/// validate : bool, default=True
///     Validate every matched file.
///
/// Returns
/// -------
/// Dataset
///     Configured dataset.
pub fn read_parquet(
    path: &Bound<'_, PyAny>,
    memory: Option<&Bound<'_, PyAny>>,
    cache: &str,
    nulls: &str,
    validate: bool,
) -> PyResult<PyDataset> {
    let mut builder = ParquetSource::builder(path_string(path)?).validate_all_files(validate);
    builder = match nulls {
        "error" => builder.error_on_nulls(),
        "nan" => builder.nulls_as_nan(),
        _ => return Err(PyValueError::new_err("nulls must be 'error' or 'nan'")),
    };
    let dataset = Dataset::new(builder.build().map_err(to_py_err)?);
    Ok(PyDataset {
        inner: configure(dataset, memory, cache)?,
    })
}

#[pyfunction]
#[pyo3(signature = (
    path: "PathLike[str] | str",
    *,
    tree=None,
    memory: "MemoryBudget | int | str | None" = None,
    cache="fastest",
    validate=true
))]
/// Read a ROOT TTree dataset.
///
/// This is shorthand for ``Dataset(RootSource(...))``.
///
/// Parameters
/// ----------
/// path : path-like
///     ROOT file, directory, or glob.
/// tree : str, optional
///     TTree name, or automatic discovery when omitted.
/// memory : MemoryBudget, int, or str, optional
///     Host-memory budget. Defaults to automatic memory planning.
/// cache : {'fastest', 'resident', 'streaming'}, default='fastest'
///     Dataset cache policy.
/// validate : bool, default=True
///     Validate every matched file.
///
/// Returns
/// -------
/// Dataset
///     Configured dataset.
pub fn read_root(
    path: &Bound<'_, PyAny>,
    tree: Option<&str>,
    memory: Option<&Bound<'_, PyAny>>,
    cache: &str,
    validate: bool,
) -> PyResult<PyDataset> {
    let mut builder = RootSource::builder(path_string(path)?).validate_all_files(validate);
    if let Some(tree) = tree {
        builder = builder.tree(tree);
    }
    let dataset = Dataset::new(builder.build().map_err(to_py_err)?);
    Ok(PyDataset {
        inner: configure(dataset, memory, cache)?,
    })
}

#[pymodule]
/// Dataset sources, sinks, transformations, and file-reading helpers.
pub mod data {
    #[pymodule_export]
    use super::{
        PyBinDataset as BinnedDataset, PyDataset as Dataset, PyParquetSink as ParquetSink,
        PyParquetSource as ParquetSource, PyRootSink as RootSink, PyRootSource as RootSource,
        read_parquet, read_root,
    };
}