11//! Module that contains helpers for binning observables
22
33use super :: convert:: f64_from_usize;
4- use itertools:: Itertools ;
5- use serde:: { Deserialize , Serialize } ;
4+ use serde:: Deserialize ;
65use std:: f64;
7- use std:: ops:: Range ;
8- use thiserror:: Error ;
96
10- #[ derive( Clone , Debug , Deserialize , PartialEq , Serialize ) ]
7+ #[ derive( Deserialize ) ]
118enum Limits {
129 Equal { left : f64 , right : f64 , bins : usize } ,
1310 Unequal { limits : Vec < f64 > } ,
1411}
1512
16- /// Error type which is returned when two `BinLimits` objects are merged which are not
17- /// connected/non-consecutive.
18- #[ derive( Debug , Error ) ]
19- pub enum MergeBinError {
20- /// Returned when two `BinLimits` objects `a` and `b` were tried to be merged using
21- /// `a.merge(b)`, but when the right-most limit of `a` does not match the left-most limit of
22- /// `b`.
23- #[ error( "can not merge bins which end at {lhs} with bins that start at {rhs}" ) ]
24- NonConsecutiveBins {
25- /// right-most limit of the `BinLimits` object that is being merged into.
26- lhs : f64 ,
27- /// left-most limit of the `BinLimits` object that is being merged.
28- rhs : f64 ,
29- } ,
30-
31- /// Returned by [`BinRemapper::merge_bins`] whenever it can not merge bins.
32- #[ error( "can not merge bins with indices {0:?}" ) ]
33- NonConsecutiveRange ( Range < usize > ) ,
34-
35- /// Returned by [`BinLimits::merge_bins`] whenever the range is outside the available bins.
36- #[ error( "tried to merge bins with indices {range:?}, but there are only {bins} bins" ) ]
37- InvalidRange {
38- /// Range given to [`BinLimits::merge_bins`].
39- range : Range < usize > ,
40- /// Number of bins.
41- bins : usize ,
42- } ,
43-
44- /// Returned by [`BinRemapper::merge`] whenever the dimensions of two `BinRemapper` are not the
45- /// same.
46- #[ error( "tried to merge bins with different dimensions {lhs} and {rhs}" ) ]
47- IncompatibleDimensions {
48- /// Dimension of the bins of the first `BinRemapper`.
49- lhs : usize ,
50- /// Dimension of the bins of the second `BinRemapper`.
51- rhs : usize ,
52- } ,
53- }
54-
5513/// Structure representing bin limits.
56- #[ derive( Clone , Debug , Deserialize , PartialEq , Serialize ) ]
14+ #[ derive( Deserialize ) ]
5715pub struct BinLimits ( Limits ) ;
5816
59- /// Error type that is returned by the constructor of `BinRemapper`.
60- #[ derive( Debug , Error ) ]
61- pub enum BinRemapperNewError {
62- /// Returned if the lengths of the normalization and limits vectors do not allow to determine a
63- /// well-defined number of dimensions.
64- #[ error( "could not determine the dimensions from a normalization vector with length {normalizations_len} and limits vector with length {limits_len}" ) ]
65- DimensionUnknown {
66- /// Length of the normalization vector.
67- normalizations_len : usize ,
68- /// Length of the limits vector.
69- limits_len : usize ,
70- } ,
71- /// Returned if bins overlap.
72- #[ error( "the bin limits for the bins with indices {} overlap with other bins" , overlaps. iter( ) . map( ToString :: to_string) . join( "," ) ) ]
73- OverlappingBins {
74- /// Indices of the bins that overlap with other bins.
75- overlaps : Vec < usize > ,
76- } ,
77- }
78-
7917/// Structure for remapping bin limits.
80- #[ derive( Clone , Debug , Deserialize , Serialize ) ]
18+ #[ derive( Deserialize ) ]
8119pub struct BinRemapper {
8220 normalizations : Vec < f64 > ,
8321 limits : Vec < ( f64 , f64 ) > ,
8422}
8523
8624/// Captures all information about the bins in a grid.
87- #[ derive( Debug ) ]
8825pub struct BinInfo < ' a > {
8926 limits : & ' a BinLimits ,
9027 remapper : Option < & ' a BinRemapper > ,
9128}
9229
93- /// Error type returned by [`BinRemapper::from_str`]
94- #[ derive( Debug , Error ) ]
95- pub enum ParseBinRemapperError {
96- /// An error that occured while parsing the string in [`BinRemapper::from_str`].
97- #[ error( "{0}" ) ]
98- Error ( String ) ,
99- /// An error that occured while constructing the remapper with [`BinRemapper::new`].
100- #[ error( "{source}" ) ]
101- BinRemapperNewError {
102- // TODO: enable #[backtrace] whenever the feature is stable
103- /// The error returned by [`BinRemapper::new`].
104- source : BinRemapperNewError ,
105- } ,
106- }
107-
10830impl < ' a > BinInfo < ' a > {
10931 /// Constructor.
11032 #[ must_use]
@@ -156,12 +78,6 @@ impl<'a> BinInfo<'a> {
15678 }
15779}
15880
159- impl PartialEq < BinInfo < ' _ > > for BinInfo < ' _ > {
160- fn eq ( & self , other : & BinInfo ) -> bool {
161- ( self . limits ( ) == other. limits ( ) ) && ( self . normalizations ( ) == other. normalizations ( ) )
162- }
163- }
164-
16581impl BinRemapper {
16682 /// Return the number of dimensions.
16783 #[ must_use]
@@ -182,12 +98,6 @@ impl BinRemapper {
18298 }
18399}
184100
185- impl PartialEq < Self > for BinRemapper {
186- fn eq ( & self , other : & Self ) -> bool {
187- ( self . limits == other. limits ) && ( self . normalizations == other. normalizations )
188- }
189- }
190-
191101impl BinLimits {
192102 /// Returns the number of bins.
193103 #[ must_use]
0 commit comments