Dumbbench/BoxPlot.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran n/a Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 143 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
new
2.0
show
2.0
CRAP 2.0
1 package Dumbbench::BoxPlot;
2 use strict;
3 use warnings;
4 use List::Util qw/min max sum/;
5 require Dumbbench;
6  
7 BEGIN {
8 eval {
9 require SOOT;
10 SOOT->import( ':all' );
11 1;
12 } or die "You need the SOOT module to use Dumbbench::BoxPlot.\nThis is not an easy installation, which is why we don't require it.\n";
13 }
14  
15 use SOOT qw/:all/;
16  
17 use Class::XSAccessor {
18 getters => [qw/bench/],
19 };
20  
21 # Yes, this is obscure.
22  
23 sub new {
24 my $class = shift;
25 my $self = bless {
26 bench => shift,
27 } => $class;
28  
29 $gStyle->SetFrameFillColor(kWhite);
30 $gStyle->SetPadColor(kWhite);
31 $gStyle->SetCanvasColor(kWhite);
32 $gStyle->SetFrameBorderMode(0);
33 $gStyle->SetCanvasBorderMode(0);
34 $gStyle->SetFillStyle(0);
35  
36 return $self;
37 }
38  
39 sub show {
40 my $self = shift;
41  
42 my @data;
43 foreach my $instance ($self->bench->instances) {
44 my $st = Dumbbench::Stats->new(
45 data => $instance->timings,
46 name => $instance->name,
47 );
48 push @data, $st;
49 }
50  
51 my $keep = $self->_box_n_whisker(\@data);
52 $gApplication->Run();
53 }
54  
55 sub _box_n_whisker {
56 my $self = shift;
57 my $data = shift;
58 my @names;
59 $names[$_] = $data->[$_]->name || "set ".($_+1) for 0..$#$data;
60  
61 my $npop = @$data;
62 my $min = min(map $_->min, @$data);
63 my $max = max(map $_->max, @$data);
64 my $range = $max-$min;
65  
66 my @obj;
67  
68 my $cv = TCanvas->new("box_plot");
69  
70 #$cv->SetStatistics(0);
71 my $bg = TH1D->new("bg", "", $npop, 0.5, $npop+0.5)->keep;
72 $bg->SetStats(kFALSE);
73 $bg->GetYaxis()->SetRangeUser($min-$range*.05, $max+$range*.05);
74 $bg->SetTitle(";;Time [s]");
75 $bg->Fill($names[$_], 0.) for 0..$#names;
76 $bg->Draw();
77  
78 push @obj, $cv, $bg;
79  
80 my $x_coord = 1.;
81 my $dx = 0.07;
82 foreach my $pop (@$data) {
83 my $n = $pop->n;
84 my $median = $pop->median;
85 my $q1 = $pop->first_quartile;
86 my $q3 = $pop->third_quartile;
87 my ($wlow, $whigh) = find_whiskers($pop, $q1, $q3);
88 my $x = $x_coord + 1.e-20;
89 #print "$x $median $wlow $whigh\n";
90 my $g = TGraphAsymmErrors->new(1, [$x], [$median*1.], [0.], [0.], [abs($median-$wlow)*1.], [abs($median-$whigh)*1.])->keep;
91 $g->Draw("l");
92 my $box = TGraphAsymmErrors->new(1, [$x], [$median*1.], [$dx], [$dx], [abs($q1-$median)*1.], [abs($q3-$median)*1.])->keep;
93 $box->SetFillStyle(1001);
94 $box->SetFillColor(17);
95 $box->SetMarkerStyle(kBlue);
96 $box->SetMarkerColor(1);
97 $box->SetMarkerStyle(20);
98 $box->SetMarkerSize(0.7);
99 $box->Draw("2p");
100 #$box->Draw("p");
101 my @outliers = grep {$_ < $wlow or $_ > $whigh} @{$pop->data};
102 $outliers[0] += 1.e-20;
103 my $noutl = scalar(@outliers);
104 my $outl = TGraph->new($noutl, [($x) x $noutl], \@outliers)->keep;
105 $outl->SetMarkerColor(1);
106 $outl->SetMarkerStyle(5);
107 $outl->SetMarkerSize(0.7);
108 $outl->Draw("p");
109  
110 push @obj, $g, $box, $outl;
111 $x_coord++;
112 }
113 $cv->Update();
114 return \@obj;
115 }
116  
117 sub find_whiskers {
118 my $pop = shift;
119 my $q1 = shift;
120 my $q3 = shift;
121 my $iqr = abs($q3-$q1);
122 my $lower_limit = $q1-$iqr*1.5;
123 my $upper_limit = $q3+$iqr*1.5;
124 my $sorted = $pop->sorted_data;
125  
126 my $low = min(grep {$_ >= $lower_limit} @{$pop->data});
127 my $high = max(grep {$_ <= $upper_limit} @{$pop->data});
128  
129 return($low, $high);
130 }
131  
132 1;