44
44
//! [`Rice`]: struct.Rice.html
45
45
//! [`Sqrt`]: struct.Sqrt.html
46
46
//! [iqr]: https://www.wikiwand.com/en/Interquartile_range
47
+ #![ warn( missing_docs, clippy:: all, clippy:: pedantic) ]
48
+
47
49
use crate :: {
48
50
histogram:: { errors:: BinsBuildError , Bins , Edges } ,
49
51
quantile:: { interpolate:: Nearest , Quantile1dExt , QuantileExt } ,
@@ -62,6 +64,7 @@ use num_traits::{FromPrimitive, NumOps, Zero};
62
64
/// [`GridBuilder`]: ../struct.GridBuilder.html
63
65
/// [`Grid`]: ../struct.Grid.html
64
66
pub trait BinsBuildingStrategy {
67
+ #[ allow( missing_docs) ]
65
68
type Elem : Ord ;
66
69
/// Returns a strategy that has learnt the required parameter fo building [`Bins`] for given
67
70
/// 1-dimensional array, or an `Err` if it is not possible to infer the required parameter
@@ -265,6 +268,11 @@ where
265
268
S : Data < Elem = Self :: Elem > ,
266
269
{
267
270
let n_elems = a. len ( ) ;
271
+ // casting `n_elems: usize` to `f64` may casus off-by-one error here if `n_elems` > 2 ^ 53,
272
+ // but it's not relevant here
273
+ #[ allow( clippy:: cast_precision_loss) ]
274
+ // casting the rounded square root from `f64` to `usize` is safe
275
+ #[ allow( clippy:: cast_possible_truncation, clippy:: cast_sign_loss) ]
268
276
let n_bins = ( n_elems as f64 ) . sqrt ( ) . round ( ) as usize ;
269
277
let min = a. min ( ) ?;
270
278
let max = a. max ( ) ?;
@@ -306,6 +314,11 @@ where
306
314
S : Data < Elem = Self :: Elem > ,
307
315
{
308
316
let n_elems = a. len ( ) ;
317
+ // casting `n_elems: usize` to `f64` may casus off-by-one error here if `n_elems` > 2 ^ 53,
318
+ // but it's not relevant here
319
+ #[ allow( clippy:: cast_precision_loss) ]
320
+ // casting the rounded cube root from `f64` to `usize` is safe
321
+ #[ allow( clippy:: cast_possible_truncation, clippy:: cast_sign_loss) ]
309
322
let n_bins = ( 2. * ( n_elems as f64 ) . powf ( 1. / 3. ) ) . round ( ) as usize ;
310
323
let min = a. min ( ) ?;
311
324
let max = a. max ( ) ?;
@@ -347,6 +360,11 @@ where
347
360
S : Data < Elem = Self :: Elem > ,
348
361
{
349
362
let n_elems = a. len ( ) ;
363
+ // casting `n_elems: usize` to `f64` may casus off-by-one error here if `n_elems` > 2 ^ 53,
364
+ // but it's not relevant here
365
+ #[ allow( clippy:: cast_precision_loss) ]
366
+ // casting the rounded base-2 log from `f64` to `usize` is safe
367
+ #[ allow( clippy:: cast_possible_truncation, clippy:: cast_sign_loss) ]
350
368
let n_bins = ( n_elems as f64 ) . log2 ( ) . round ( ) as usize + 1 ;
351
369
let min = a. min ( ) ?;
352
370
let max = a. max ( ) ?;
@@ -418,6 +436,9 @@ where
418
436
T : Ord + Clone + FromPrimitive + NumOps + Zero ,
419
437
{
420
438
fn compute_bin_width ( n_bins : usize , iqr : T ) -> T {
439
+ // casting `n_bins: usize` to `f64` may casus off-by-one error here if `n_bins` > 2 ^ 53,
440
+ // but it's not relevant here
441
+ #[ allow( clippy:: cast_precision_loss) ]
421
442
let denominator = ( n_bins as f64 ) . powf ( 1. / 3. ) ;
422
443
T :: from_usize ( 2 ) . unwrap ( ) * iqr / T :: from_f64 ( denominator) . unwrap ( )
423
444
}
@@ -495,8 +516,8 @@ where
495
516
}
496
517
}
497
518
498
- /// Given a range (max, min) and the number of bins, it returns
499
- /// the associated bin_width:
519
+ /// Returns the `bin_width`, given the two end points of a range (` max`, ` min`), and the number of
520
+ /// bins, consuming endpoints
500
521
///
501
522
/// `bin_width = (max - min)/n`
502
523
///
@@ -505,13 +526,13 @@ fn compute_bin_width<T>(min: T, max: T, n_bins: usize) -> T
505
526
where
506
527
T : Ord + Clone + FromPrimitive + NumOps + Zero ,
507
528
{
508
- let range = max. clone ( ) - min. clone ( ) ;
529
+ let range = max - min;
509
530
range / T :: from_usize ( n_bins) . unwrap ( )
510
531
}
511
532
512
533
#[ cfg( test) ]
513
534
mod equispaced_tests {
514
- use super :: * ;
535
+ use super :: EquiSpaced ;
515
536
516
537
#[ test]
517
538
fn bin_width_has_to_be_positive ( ) {
@@ -526,7 +547,7 @@ mod equispaced_tests {
526
547
527
548
#[ cfg( test) ]
528
549
mod sqrt_tests {
529
- use super :: * ;
550
+ use super :: { BinsBuildingStrategy , Sqrt } ;
530
551
use ndarray:: array;
531
552
532
553
#[ test]
@@ -546,7 +567,7 @@ mod sqrt_tests {
546
567
547
568
#[ cfg( test) ]
548
569
mod rice_tests {
549
- use super :: * ;
570
+ use super :: { BinsBuildingStrategy , Rice } ;
550
571
use ndarray:: array;
551
572
552
573
#[ test]
@@ -566,7 +587,7 @@ mod rice_tests {
566
587
567
588
#[ cfg( test) ]
568
589
mod sturges_tests {
569
- use super :: * ;
590
+ use super :: { BinsBuildingStrategy , Sturges } ;
570
591
use ndarray:: array;
571
592
572
593
#[ test]
@@ -586,7 +607,7 @@ mod sturges_tests {
586
607
587
608
#[ cfg( test) ]
588
609
mod fd_tests {
589
- use super :: * ;
610
+ use super :: { BinsBuildingStrategy , FreedmanDiaconis } ;
590
611
use ndarray:: array;
591
612
592
613
#[ test]
@@ -615,7 +636,7 @@ mod fd_tests {
615
636
616
637
#[ cfg( test) ]
617
638
mod auto_tests {
618
- use super :: * ;
639
+ use super :: { Auto , BinsBuildingStrategy } ;
619
640
use ndarray:: array;
620
641
621
642
#[ test]
0 commit comments