laddu-core 0.17.1

Core of the laddu library
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
//! # laddu-core
//!
//! This is an internal crate used by `laddu`.
#![warn(clippy::perf, clippy::style, missing_docs)]
#![allow(clippy::excessive_precision)]
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

use ganesh::core::{MCMCSummary, MinimizationSummary};
#[cfg(feature = "python")]
use pyo3::PyErr;

/// Re-exported alias for `std::f64` to ease dependent crates transitioning to the 64-bit
/// floating point API.
pub use std::f64;

/// MPI backend for `laddu`
///
/// Message Passing Interface (MPI) is a protocol which enables communication between multiple
/// CPUs in a high-performance computing environment. While [`rayon`] can parallelize tasks on a
/// single CPU, MPI can also parallelize tasks on multiple CPUs by running independent
/// processes on all CPUs at once (tasks) which are assigned ids (ranks) which tell each
/// process what to do and where to send results. This backend coordinates processes which would
/// typically be parallelized over the events in a [`Dataset`](`crate::data::Dataset`).
///
/// To use this backend, the library must be built with the `mpi` feature, which requires an
/// existing implementation of MPI like OpenMPI or MPICH. All processing code should be
/// sandwiched between calls to [`use_mpi`] and [`finalize_mpi`]:
/// ```ignore
/// fn main() {
///     laddu_core::mpi::use_mpi(true);
///     // laddu analysis code here
///     laddu_core::mpi::finalize_mpi();
/// }
/// ```
///
/// [`finalize_mpi`] must be called to trigger all the methods which clean up the MPI
/// environment. While these are called by default when the [`Universe`](`mpi::environment::Universe`) is dropped, `laddu` uses a static `Universe` that can be accessed by all of the methods that need it, rather than passing the context to each method. This simplifies the way programs can be converted to use MPI, but means that the `Universe` is not automatically dropped at the end of the program (so it must be dropped manually).
#[cfg(feature = "mpi")]
#[cfg_attr(coverage_nightly, coverage(off))]
pub mod mpi {
    use std::ops::Range;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::OnceLock;

    use lazy_static::lazy_static;
    use mpi::datatype::PartitionMut;
    use mpi::environment::Universe;
    use mpi::topology::{Process, SimpleCommunicator};
    use mpi::traits::{Communicator, CommunicatorCollectives, Equivalence};
    use parking_lot::RwLock;

    lazy_static! {
        static ref USE_MPI: AtomicBool = AtomicBool::new(false);
    }

    static MPI_UNIVERSE: OnceLock<RwLock<Option<Universe>>> = OnceLock::new();

    /// The default root rank for MPI processes
    pub const ROOT_RANK: i32 = 0;

    /// Check if the current MPI process is the root process
    pub fn is_root() -> bool {
        if let Some(world) = crate::mpi::get_world() {
            world.rank() == ROOT_RANK
        } else {
            true
        }
    }

    /// Shortcut method to just get the global MPI communicator without accessing `size` and `rank`
    /// directly
    pub fn get_world() -> Option<SimpleCommunicator> {
        if let Some(universe_lock) = MPI_UNIVERSE.get() {
            if let Some(universe) = &*universe_lock.read() {
                return Some(universe.world());
            }
        }
        None
    }

    /// Get the rank of the current process
    pub fn get_rank() -> i32 {
        get_world().map(|w| w.rank()).unwrap_or(ROOT_RANK)
    }

    /// Get number of available processes/ranks
    pub fn get_size() -> i32 {
        get_world().map(|w| w.size()).unwrap_or(1)
    }

    /// Use the MPI backend
    ///
    /// # Notes
    ///
    /// You must have MPI installed for this to work, and you must call the program with
    /// `mpirun <executable>`, or bad things will happen.
    ///
    /// MPI runs an identical program on each process, but gives the program an ID called its
    /// "rank". Only the results of methods on the root process (rank 0) should be
    /// considered valid, as other processes only contain portions of each dataset. To ensure
    /// you don't save or print data at other ranks, use the provided [`is_root()`]
    /// method to check if the process is the root process.
    ///
    /// Once MPI is enabled, it cannot be disabled. If MPI could be toggled (which it can't),
    /// the other processes will still run, but they will be independent of the root process
    /// and will no longer communicate with it. The root process stores no data, so it would
    /// be difficult (and convoluted) to get the results which were already processed via
    /// MPI.
    ///
    /// Additionally, MPI must be enabled at the beginning of a script, at least before any
    /// other `laddu` functions are called.
    ///
    /// If [`use_mpi()`] is called multiple times, the subsequent calls will have no
    /// effect.
    ///
    /// <div class="warning">
    ///
    /// You **must** call [`finalize_mpi()`] before your program exits for MPI to terminate
    /// smoothly.
    ///
    /// </div>
    ///
    /// # Examples
    ///
    /// ```ignore
    /// fn main() {
    ///     laddu_core::use_mpi();
    ///
    ///     // ... your code here ...
    ///
    ///     laddu_core::finalize_mpi();
    /// }
    ///
    /// ```
    pub fn use_mpi(trigger: bool) {
        if trigger {
            USE_MPI.store(true, Ordering::SeqCst);
            MPI_UNIVERSE.get_or_init(|| {
                #[cfg(feature = "rayon")]
                let threading = mpi::Threading::Funneled;
                #[cfg(not(feature = "rayon"))]
                let threading = mpi::Threading::Single;
                let (universe, _threading) = mpi::initialize_with_threading(threading).unwrap();
                let world = universe.world();
                if world.size() == 1 {
                    eprintln!("Warning: MPI is enabled, but only one process is available. MPI will not be used, but single-CPU parallelism may still be used if enabled.");
                    finalize_mpi();
                    USE_MPI.store(false, Ordering::SeqCst);
                    RwLock::new(None)
                } else {
                    RwLock::new(Some(universe))
                }
            });
        }
    }

    /// Drop the MPI universe and finalize MPI at the end of a program
    ///
    /// This function will do nothing if MPI is not initialized.
    ///
    /// <div class="warning">
    ///
    /// This should only be called once and should be called at the end of all `laddu`-related
    /// function calls. This must be called at the end of any program which uses MPI.
    ///
    /// </div>
    pub fn finalize_mpi() {
        if get_world().is_some() {
            if let Some(universe_lock) = MPI_UNIVERSE.get() {
                let mut universe = universe_lock.write();
                *universe = None;
            }
        }
        USE_MPI.store(false, Ordering::SeqCst);
    }

    /// Check if MPI backend is enabled
    pub fn using_mpi() -> bool {
        USE_MPI.load(Ordering::SeqCst)
    }

    fn counts_displs(size: usize, total: usize, stride: usize) -> (Vec<i32>, Vec<i32>) {
        let mut counts = vec![0i32; size];
        let mut displs = vec![0i32; size];
        if size == 0 {
            return (counts, displs);
        }
        let base = total / size;
        let remainder = total % size;
        let mut offset = 0i32;
        for rank in 0..size {
            let n = if rank < remainder { base + 1 } else { base };
            let scaled = (n * stride) as i32;
            counts[rank] = scaled;
            displs[rank] = offset;
            offset += scaled;
        }
        (counts, displs)
    }

    #[inline]
    fn rank_local_from_global(i_global: usize, size: usize, total: usize) -> (usize, usize) {
        assert!(size > 0, "Communicator must have at least one rank");
        assert!(total > 0, "Cannot map global indices when dataset is empty");
        assert!(
            i_global < total,
            "Global index {} out of bounds for {} events",
            i_global,
            total
        );
        let base = total / size;
        let remainder = total % size;
        let big_block = base + 1;
        let threshold = remainder * big_block;
        if i_global < threshold {
            let rank = i_global / big_block;
            let local = i_global % big_block;
            (rank, local)
        } else {
            let adjusted = i_global - threshold;
            let rank = remainder + adjusted / base;
            let local = adjusted % base;
            (rank, local)
        }
    }

    /// Canonical partitioning information for distributing a dataset across MPI ranks.
    #[derive(Clone, Debug)]
    pub struct Partition {
        counts: Vec<i32>,
        displs: Vec<i32>,
        total: usize,
    }

    impl Partition {
        /// Build a new distribution for `total` items across `size` ranks.
        pub fn new(size: usize, total: usize) -> Self {
            assert!(size > 0, "Communicator must have at least one rank");
            let (counts, displs) = counts_displs(size, total, 1);
            Self {
                counts,
                displs,
                total,
            }
        }

        /// Total number of items tracked by this partition.
        pub fn total(&self) -> usize {
            self.total
        }

        /// Number of ranks described by this partition.
        pub fn n_ranks(&self) -> usize {
            self.counts.len()
        }

        /// Number of items assigned to `rank`.
        pub fn len_for_rank(&self, rank: usize) -> usize {
            self.counts[rank] as usize
        }

        /// Starting global index for `rank`.
        pub fn start_for_rank(&self, rank: usize) -> usize {
            self.displs[rank] as usize
        }

        /// Contiguous global range owned by `rank`.
        pub fn range_for_rank(&self, rank: usize) -> Range<usize> {
            let start = self.start_for_rank(rank);
            start..start + self.len_for_rank(rank)
        }

        /// Determine the owning rank and local index for a global dataset index.
        pub fn owner_of(&self, global_index: usize) -> (usize, usize) {
            assert!(
                self.total > 0,
                "Cannot map global indices when dataset is empty"
            );
            rank_local_from_global(global_index, self.n_ranks(), self.total)
        }

        /// Convert into raw `(counts, displacements)` buffers.
        pub fn into_raw(self) -> (Vec<i32>, Vec<i32>) {
            (self.counts, self.displs)
        }
    }

    /// A trait including some useful auxiliary methods for MPI
    pub trait LadduMPI {
        /// Get the process at the root rank
        fn process_at_root(&self) -> Process<'_>;
        /// Check if the current rank is the root rank
        fn is_root(&self) -> bool;
        /// Gather arbitrarily-sized local slices into a buffer ordered by the
        /// canonical dataset partition.
        fn all_gather_partitioned<T: Equivalence + Default + Clone>(
            &self,
            local: &[T],
            total: usize,
            stride: Option<usize>,
        ) -> Vec<T>;
        /// Gather local slices into a buffer using explicit
        /// `(counts, displacements)` in element units.
        fn all_gather_with_counts<T: Equivalence + Default + Clone>(
            &self,
            local: &[T],
            counts: &[i32],
            displs: &[i32],
        ) -> Vec<T>;
        /// Gather batches corresponding to arbitrary global indices while
        /// preserving the order of `global_indices`.
        fn all_gather_batched_partitioned<T: Equivalence + Default + Clone>(
            &self,
            local: &[T],
            global_indices: &[usize],
            total: usize,
            stride: Option<usize>,
        ) -> Vec<T>;
        /// Return the `(rank, local_index)` pair owning `global_index` in a
        /// dataset containing `total` events.
        fn owner_of_global_index(&self, global_index: usize, total: usize) -> (i32, usize);
        /// Translate a list of global dataset indices into the corresponding
        /// local indices owned by this rank, preserving their original order.
        fn locals_from_globals(&self, global_indices: &[usize], total: usize) -> Vec<usize>;
        /// Get the counts/displacements for partitioning a buffer of length
        /// `buf_len`
        fn get_counts_displs(&self, buf_len: usize) -> (Vec<i32>, Vec<i32>);
        /// Build a [`Partition`] describing how `total` items are distributed
        /// across ranks.
        fn partition(&self, total: usize) -> Partition;
        /// Get the counts/displacements for partitioning a nested buffer (like
        /// a [`Vec<Vec<T>>`]). If the internal vectors all have the same length
        /// `internal_len` and there are `unflattened_len` elements in the
        /// outer vector, then this will give the correct counts/displacements for a
        /// flattened version of the nested buffer.
        fn get_flattened_counts_displs(
            &self,
            unflattened_len: usize,
            internal_len: usize,
        ) -> (Vec<i32>, Vec<i32>);
    }

    impl LadduMPI for SimpleCommunicator {
        fn process_at_root(&self) -> Process<'_> {
            self.process_at_rank(crate::mpi::ROOT_RANK)
        }

        fn is_root(&self) -> bool {
            self.rank() == crate::mpi::ROOT_RANK
        }

        /// Gather arbitrarily-sized local slices into a buffer ordered by the
        /// canonical dataset partition.
        fn all_gather_partitioned<T: Equivalence + Default + Clone>(
            &self,
            local: &[T],
            total: usize,
            stride: Option<usize>,
        ) -> Vec<T> {
            let size = self.size() as usize;
            let stride = stride.unwrap_or(1);
            assert!(stride > 0, "Stride must be greater than zero");
            let mut out = vec![T::default(); total * stride];
            if total == 0 || size == 0 {
                return out;
            }
            let (counts, displs) = counts_displs(size, total, stride);
            {
                let mut partition = PartitionMut::new(&mut out, counts, displs);
                self.all_gather_varcount_into(local, &mut partition);
            }
            out
        }

        fn all_gather_with_counts<T: Equivalence + Default + Clone>(
            &self,
            local: &[T],
            counts: &[i32],
            displs: &[i32],
        ) -> Vec<T> {
            assert_eq!(
                counts.len(),
                displs.len(),
                "Counts and displacements must have the same length"
            );
            assert_eq!(
                counts.len(),
                self.size() as usize,
                "Counts/displacements must match communicator size"
            );
            let total = counts.iter().map(|count| *count as usize).sum();
            let mut out = vec![T::default(); total];
            {
                let mut partition = PartitionMut::new(&mut out, counts.to_vec(), displs.to_vec());
                self.all_gather_varcount_into(local, &mut partition);
            }
            out
        }

        /// Gather batches corresponding to arbitrary global indices while
        /// preserving the order of `global_indices`.
        fn all_gather_batched_partitioned<T: Equivalence + Default + Clone>(
            &self,
            local: &[T],
            global_indices: &[usize],
            total: usize,
            stride: Option<usize>,
        ) -> Vec<T> {
            let size = self.size() as usize;
            let stride = stride.unwrap_or(1);
            assert!(stride > 0, "Stride must be greater than zero");
            let n_indices = global_indices.len();
            let mut gathered = vec![T::default(); n_indices * stride];
            if n_indices == 0 || size == 0 {
                return gathered;
            }

            assert!(
                total > 0,
                "Cannot gather batched data from an empty dataset"
            );

            let partition = Partition::new(size, total);
            let mut locals_by_rank = vec![Vec::<usize>::new(); size];
            let mut targets_by_rank = vec![Vec::<usize>::new(); size];
            for (position, &global_index) in global_indices.iter().enumerate() {
                let (rank, local_index) = partition.owner_of(global_index);
                locals_by_rank[rank].push(local_index);
                targets_by_rank[rank].push(position);
            }

            let mut counts = vec![0i32; size];
            let mut displs = vec![0i32; size];
            for rank in 0..size {
                counts[rank] = (locals_by_rank[rank].len() * stride) as i32;
                displs[rank] = if rank == 0 {
                    0
                } else {
                    displs[rank - 1] + counts[rank - 1]
                };
            }

            let expected_local = locals_by_rank[self.rank() as usize].len() * stride;
            debug_assert_eq!(
                local.len(),
                expected_local,
                "Local buffer length does not match expected gathered size for rank {}",
                self.rank()
            );

            {
                let mut partition =
                    PartitionMut::new(&mut gathered, counts.clone(), displs.clone());
                self.all_gather_varcount_into(local, &mut partition);
            }

            let mut result = vec![T::default(); n_indices * stride];
            for rank in 0..size {
                let mut cursor = displs[rank] as usize;
                for &target in &targets_by_rank[rank] {
                    let dst = target * stride;
                    result[dst..(stride + dst)]
                        .clone_from_slice(&gathered[cursor..(stride + cursor)]);
                    cursor += stride;
                }
            }

            result
        }

        fn owner_of_global_index(&self, global_index: usize, total: usize) -> (i32, usize) {
            let partition = Partition::new(self.size() as usize, total);
            let (rank, local) = partition.owner_of(global_index);
            (rank as i32, local)
        }

        /// Translate a list of global dataset indices into the corresponding
        /// local indices owned by this rank, preserving their original order.
        fn locals_from_globals(&self, global_indices: &[usize], total: usize) -> Vec<usize> {
            let partition = Partition::new(self.size() as usize, total);
            let this_rank = self.rank() as usize;
            let mut locals = Vec::new();
            if total == 0 {
                return locals;
            }
            for &global_index in global_indices {
                let (rank, local_index) = partition.owner_of(global_index);
                if rank == this_rank {
                    locals.push(local_index);
                }
            }
            locals
        }
        fn get_counts_displs(&self, buf_len: usize) -> (Vec<i32>, Vec<i32>) {
            self.partition(buf_len).into_raw()
        }

        fn partition(&self, total: usize) -> Partition {
            Partition::new(self.size() as usize, total)
        }

        fn get_flattened_counts_displs(
            &self,
            unflattened_len: usize,
            internal_len: usize,
        ) -> (Vec<i32>, Vec<i32>) {
            let mut counts = vec![0; self.size() as usize];
            let mut displs = vec![0; self.size() as usize];
            let chunk_size = unflattened_len / self.size() as usize;
            let surplus = unflattened_len % self.size() as usize;
            for i in 0..self.size() as usize {
                counts[i] = if i < surplus {
                    (chunk_size + 1) * internal_len
                } else {
                    chunk_size * internal_len
                } as i32;
                displs[i] = if i == 0 {
                    0
                } else {
                    displs[i - 1] + counts[i - 1]
                };
            }
            (counts, displs)
        }
    }
}

use thiserror::Error;

/// [`Amplitude`](crate::amplitudes::Amplitude)s and methods for making and evaluating them.
pub mod amplitudes;
/// Methods for loading and manipulating [`EventData`]-based data.
pub mod data;
/// Prototype execution context API for thread-policy and scratch reuse.
#[cfg(feature = "execution-context-prototype")]
pub mod execution_context;
/// Utilities for tracking parameter state across expressions and likelihoods.
pub mod parameter_manager;
/// Structures for manipulating the cache and free parameters.
pub mod resources;
/// Shared per-call thread-pool reuse utilities.
pub mod thread_pool;
/// Utility functions, enums, and traits
pub mod utils;
/// Useful traits for all crate structs
pub mod traits {
    pub use crate::amplitudes::Amplitude;
    pub use crate::utils::variables::Variable;
    pub use crate::ReadWrite;
}

pub use crate::data::{
    BinnedDataset, Dataset, DatasetMetadata, DatasetReadOptions, Event, EventData,
};
#[cfg(feature = "execution-context-prototype")]
pub use crate::execution_context::{ExecutionContext, ScratchAllocator, ThreadPolicy};
pub use crate::resources::{
    Cache, ComplexMatrixID, ComplexScalarID, ComplexVectorID, MatrixID, ParameterID, Parameters,
    Resources, ScalarID, VectorID,
};
pub use crate::thread_pool::ThreadPoolManager;
pub use crate::utils::angular_momentum::{
    allowed_projections, helicity_combinations, AngularMomentum, AngularMomentumProjection,
    HelicityCombination, OrbitalAngularMomentum, Parity, SpinState,
};
pub use crate::utils::enums::{Channel, Frame, Sign};
pub use crate::utils::kinematics::{DecayAngles, FrameAxes, RestFrame};
pub use crate::utils::reaction::{
    Decay, Particle, ParticleSource, Reaction, ReactionTopology, ResolvedTwoToTwo, TwoToTwoReaction,
};
pub use crate::utils::variables::{
    AngleVariables, Angles, CosTheta, Mandelstam, Mass, Phi, PolAngle, PolMagnitude, Polarization,
};
pub use crate::utils::vectors::{Vec3, Vec4};
pub use amplitudes::{
    constant, parameter, AmplitudeID, CompiledExpression, CompiledExpressionNode, Evaluator,
    Expression, ParameterLike,
};

/// The mathematical constant $`\pi`$.
pub const PI: f64 = std::f64::consts::PI;

/// A [`Result`] type alias for [`LadduError`]s.
pub type LadduResult<T> = Result<T, LadduError>;

/// The error type used by all `laddu` internal methods
#[derive(Error, Debug)]
pub enum LadduError {
    /// An alias for [`std::io::Error`].
    #[error(transparent)]
    IOError(#[from] std::io::Error),
    /// An alias for [`parquet::errors::ParquetError`].
    #[error(transparent)]
    ParquetError(#[from] parquet::errors::ParquetError),
    /// An alias for [`arrow::error::ArrowError`].
    #[error(transparent)]
    ArrowError(#[from] arrow::error::ArrowError),
    /// An alias for [`shellexpand::LookupError`].
    #[error(transparent)]
    LookupError(#[from] shellexpand::LookupError<std::env::VarError>),
    /// An error which occurs when the user tries to register two amplitudes by the same name.
    #[error("An amplitude by the name \"{name}\" is already registered!")]
    RegistrationError {
        /// Name of amplitude which is already registered
        name: String,
    },
    /// An error which occurs when the user tries to use an unregistered amplitude.
    #[error("No registered amplitude with name \"{name}\"!")]
    AmplitudeNotFoundError {
        /// Name of amplitude which failed lookup
        name: String,
    },
    /// An error which occurs when the user tries to parse an invalid string of text, typically
    /// into an enum variant.
    #[error("Failed to parse string: \"{name}\" does not correspond to a valid \"{object}\"!")]
    ParseError {
        /// The string which was parsed
        name: String,
        /// The name of the object it failed to parse into
        object: String,
    },
    /// An error returned by internal bitcode serialization
    #[error(transparent)]
    BitcodeError(#[from] bitcode::Error),
    /// An error returned by the Python pickle (de)serializer
    #[error(transparent)]
    PickleError(#[from] serde_pickle::Error),
    /// An error which occurs when parameter definitions conflict or clash.
    #[error("Parameter \"{name}\" conflict: {reason}")]
    ParameterConflict {
        /// Name of parameter
        name: String,
        /// Description of conflict
        reason: String,
    },
    /// An error which occurs when attempting to use an unregistered or unnamed parameter.
    #[error("Parameter \"{name}\" could not be registered: {reason}")]
    UnregisteredParameter {
        /// Name of parameter
        name: String,
        /// Reason for failure
        reason: String,
    },
    /// An error which occurs during execution-context setup.
    #[error("Execution context setup failed: {reason}")]
    ExecutionContextError {
        /// Description of setup failure
        reason: String,
    },
    /// An error type for [`rayon`] thread pools
    #[cfg(feature = "rayon")]
    #[error(transparent)]
    ThreadPoolError(#[from] rayon::ThreadPoolBuildError),
    /// An error type for [`numpy`]-related conversions
    #[cfg(feature = "numpy")]
    #[error(transparent)]
    NumpyError(#[from] numpy::FromVecError),
    /// A required column was not found in the input
    #[error("Required column \"{name}\" was not found in the dataset")]
    MissingColumn {
        /// Name of the missing column
        name: String,
    },
    /// A column has an unsupported type
    #[error("Column \"{name}\" has unsupported type \"{datatype}\"")]
    InvalidColumnType {
        /// Column name
        name: String,
        /// Detected data type
        datatype: String,
    },
    /// A value has an unexpected length.
    #[error("{context} length mismatch: expected {expected}, received {actual}")]
    LengthMismatch {
        /// Description of what length was validated.
        context: String,
        /// Expected length.
        expected: usize,
        /// Actual length observed.
        actual: usize,
    },
    /// A duplicate name was provided for p4 or aux data
    #[error("Duplicate {category} name \"{name}\" provided")]
    DuplicateName {
        /// Category (p4 or aux)
        category: &'static str,
        /// Duplicate name
        name: String,
    },
    /// An unknown name was referenced (e.g., for boosts)
    #[error("Unknown {category} name \"{name}\"")]
    UnknownName {
        /// Category (p4 or aux)
        category: &'static str,
        /// Name that could not be resolved
        name: String,
    },
    /// A custom fallback error for errors too complex or too infrequent to warrant their own error
    /// category.
    #[error("{0}")]
    Custom(String),
}

/// Validate the number of free parameters supplied to a public entrypoint.
pub fn validate_free_parameter_len(input_len: usize, expected_len: usize) -> LadduResult<()> {
    if input_len != expected_len {
        return Err(LadduError::LengthMismatch {
            context: "free parameter vector".to_string(),
            expected: expected_len,
            actual: input_len,
        });
    }
    Ok(())
}

impl Clone for LadduError {
    // This is a little hack because error types are rarely cloneable, but I need to store them in a
    // cloneable box for minimizers and MCMC methods
    fn clone(&self) -> Self {
        let err_string = self.to_string();
        LadduError::Custom(err_string)
    }
}

#[cfg(feature = "python")]
impl From<LadduError> for PyErr {
    fn from(err: LadduError) -> Self {
        use pyo3::exceptions::*;
        let err_string = err.to_string();
        match err {
            LadduError::LookupError(_)
            | LadduError::RegistrationError { .. }
            | LadduError::AmplitudeNotFoundError { .. }
            | LadduError::ParseError { .. } => PyValueError::new_err(err_string),
            LadduError::ParquetError(_)
            | LadduError::ArrowError(_)
            | LadduError::IOError(_)
            | LadduError::BitcodeError(_)
            | LadduError::PickleError(_) => PyIOError::new_err(err_string),
            LadduError::MissingColumn { .. } | LadduError::UnknownName { .. } => {
                PyKeyError::new_err(err_string)
            }
            LadduError::InvalidColumnType { .. }
            | LadduError::LengthMismatch { .. }
            | LadduError::DuplicateName { .. }
            | LadduError::ParameterConflict { .. }
            | LadduError::UnregisteredParameter { .. } => PyValueError::new_err(err_string),
            LadduError::ExecutionContextError { .. } => PyRuntimeError::new_err(err_string),
            LadduError::Custom(_) => PyRuntimeError::new_err(err_string),
            #[cfg(feature = "rayon")]
            LadduError::ThreadPoolError(_) => PyRuntimeError::new_err(err_string),
            #[cfg(feature = "numpy")]
            LadduError::NumpyError(_) => PyValueError::new_err(err_string),
        }
    }
}

use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
/// A trait which allows structs with [`Serialize`] and [`Deserialize`](`serde::Deserialize`) to
/// have a null constructor which Python can fill with data. This allows such structs to be
/// pickle-able from the Python API.
pub trait ReadWrite: Serialize + DeserializeOwned {
    /// Create a null version of the object which acts as a shell into which Python's `pickle` module
    /// can load data. This generally shouldn't be used to construct the struct in regular code.
    fn create_null() -> Self;
}
impl ReadWrite for MCMCSummary {
    fn create_null() -> Self {
        MCMCSummary::default()
    }
}
impl ReadWrite for MinimizationSummary {
    fn create_null() -> Self {
        MinimizationSummary::default()
    }
}