13
13
use std:: cmp:: Ordering :: { self , Less , Greater , Equal } ;
14
14
use std:: collections:: hash_map:: Entry :: { Occupied , Vacant } ;
15
15
use std:: collections:: hash_map:: { self , Hasher } ;
16
- use std:: fmt;
17
16
use std:: hash:: Hash ;
18
- use std:: io;
19
17
use std:: mem;
20
18
use std:: num:: { Float , FromPrimitive } ;
21
19
@@ -332,111 +330,6 @@ pub fn winsorize<T: Float + FromPrimitive>(samples: &mut [T], pct: T) {
332
330
}
333
331
}
334
332
335
- /// Render writes the min, max and quartiles of the provided `Summary` to the provided `Writer`.
336
- pub fn write_5_number_summary < W : Writer , T : Float + fmt:: Display + fmt:: Debug > ( w : & mut W ,
337
- s : & Summary < T > ) -> io:: IoResult < ( ) > {
338
- let ( q1, q2, q3) = s. quartiles ;
339
- write ! ( w, "(min={}, q1={}, med={}, q3={}, max={})" ,
340
- s. min,
341
- q1,
342
- q2,
343
- q3,
344
- s. max)
345
- }
346
-
347
- /// Render a boxplot to the provided writer. The boxplot shows the min, max and quartiles of the
348
- /// provided `Summary` (thus includes the mean) and is scaled to display within the range of the
349
- /// nearest multiple-of-a-power-of-ten above and below the min and max of possible values, and
350
- /// target `width_hint` characters of display (though it will be wider if necessary).
351
- ///
352
- /// As an example, the summary with 5-number-summary `(min=15, q1=17, med=20, q3=24, max=31)` might
353
- /// display as:
354
- ///
355
- /// ```{.ignore}
356
- /// 10 | [--****#******----------] | 40
357
- /// ```
358
- pub fn write_boxplot < W : Writer , T : Float + fmt:: Display + fmt:: Debug + FromPrimitive > (
359
- w : & mut W ,
360
- s : & Summary < T > ,
361
- width_hint : uint )
362
- -> io:: IoResult < ( ) > {
363
-
364
- let ( q1, q2, q3) = s. quartiles ;
365
-
366
- // the .abs() handles the case where numbers are negative
367
- let ten: T = FromPrimitive :: from_uint ( 10 ) . unwrap ( ) ;
368
- let lomag = ten. powf ( s. min . abs ( ) . log10 ( ) . floor ( ) ) ;
369
- let himag = ten. powf ( s. max . abs ( ) . log10 ( ) . floor ( ) ) ;
370
-
371
- // need to consider when the limit is zero
372
- let zero: T = Float :: zero ( ) ;
373
- let lo = if lomag == Float :: zero ( ) {
374
- zero
375
- } else {
376
- ( s. min / lomag) . floor ( ) * lomag
377
- } ;
378
-
379
- let hi = if himag == Float :: zero ( ) {
380
- zero
381
- } else {
382
- ( s. max / himag) . ceil ( ) * himag
383
- } ;
384
-
385
- let range = hi - lo;
386
-
387
- let lostr = lo. to_string ( ) ;
388
- let histr = hi. to_string ( ) ;
389
-
390
- let overhead_width = lostr. len ( ) + histr. len ( ) + 4 ;
391
- let range_width = width_hint - overhead_width;
392
- let range_float = FromPrimitive :: from_uint ( range_width) . unwrap ( ) ;
393
- let char_step = range / range_float;
394
-
395
- try!( write ! ( w, "{} |" , lostr) ) ;
396
-
397
- let mut c = 0 ;
398
- let mut v = lo;
399
-
400
- while c < range_width && v < s. min {
401
- try!( write ! ( w, " " ) ) ;
402
- v = v + char_step;
403
- c += 1 ;
404
- }
405
- try!( write ! ( w, "[" ) ) ;
406
- c += 1 ;
407
- while c < range_width && v < q1 {
408
- try!( write ! ( w, "-" ) ) ;
409
- v = v + char_step;
410
- c += 1 ;
411
- }
412
- while c < range_width && v < q2 {
413
- try!( write ! ( w, "*" ) ) ;
414
- v = v + char_step;
415
- c += 1 ;
416
- }
417
- try!( write ! ( w, "#" ) ) ;
418
- c += 1 ;
419
- while c < range_width && v < q3 {
420
- try!( write ! ( w, "*" ) ) ;
421
- v = v + char_step;
422
- c += 1 ;
423
- }
424
- while c < range_width && v < s. max {
425
- try!( write ! ( w, "-" ) ) ;
426
- v = v + char_step;
427
- c += 1 ;
428
- }
429
- try!( write ! ( w, "]" ) ) ;
430
- while c < range_width {
431
- try!( write ! ( w, " " ) ) ;
432
- v = v + char_step;
433
- c += 1 ;
434
- }
435
-
436
- try!( write ! ( w, "| {}" , histr) ) ;
437
- Ok ( ( ) )
438
- }
439
-
440
333
/// Returns a HashMap with the number of occurrences of every element in the
441
334
/// sequence that the iterator exposes.
442
335
pub fn freq_count < T , U > ( mut iter : T ) -> hash_map:: HashMap < U , uint >
@@ -458,8 +351,6 @@ pub fn freq_count<T, U>(mut iter: T) -> hash_map::HashMap<U, uint>
458
351
mod tests {
459
352
use stats:: Stats ;
460
353
use stats:: Summary ;
461
- use stats:: write_5_number_summary;
462
- use stats:: write_boxplot;
463
354
use std:: io;
464
355
use std:: f64;
465
356
@@ -479,10 +370,6 @@ mod tests {
479
370
let mut w = io:: stdout ( ) ;
480
371
let w = & mut w;
481
372
( write ! ( w, "\n " ) ) . unwrap ( ) ;
482
- write_5_number_summary ( w, & summ2) . unwrap ( ) ;
483
- ( write ! ( w, "\n " ) ) . unwrap ( ) ;
484
- write_boxplot ( w, & summ2, 50 ) . unwrap ( ) ;
485
- ( write ! ( w, "\n " ) ) . unwrap ( ) ;
486
373
487
374
assert_eq ! ( summ. sum, summ2. sum) ;
488
375
assert_eq ! ( summ. min, summ2. min) ;
@@ -1028,23 +915,6 @@ mod tests {
1028
915
check ( val, summ) ;
1029
916
}
1030
917
1031
- #[ test]
1032
- fn test_boxplot_nonpositive ( ) {
1033
- fn t ( s : & Summary < f64 > , expected : String ) {
1034
- let mut m = Vec :: new ( ) ;
1035
- write_boxplot ( & mut m, s, 30 ) . unwrap ( ) ;
1036
- let out = String :: from_utf8 ( m) . unwrap ( ) ;
1037
- assert_eq ! ( out, expected) ;
1038
- }
1039
-
1040
- t ( & Summary :: new ( & [ -2.0f64 , -1.0f64 ] ) ,
1041
- "-2 |[------******#*****---]| -1" . to_string ( ) ) ;
1042
- t ( & Summary :: new ( & [ 0.0f64 , 2.0f64 ] ) ,
1043
- "0 |[-------*****#*******---]| 2" . to_string ( ) ) ;
1044
- t ( & Summary :: new ( & [ -2.0f64 , 0.0f64 ] ) ,
1045
- "-2 |[------******#******---]| 0" . to_string ( ) ) ;
1046
-
1047
- }
1048
918
#[ test]
1049
919
fn test_sum_f64s ( ) {
1050
920
assert_eq ! ( [ 0.5f64 , 3.2321f64 , 1.5678f64 ] . sum( ) , 5.2999 ) ;
0 commit comments