blib/lib/Algorithm/Classifier/IsolationForest/App/Command/csv2plot.pm

filter: Statementstmt 32.3% 22 / 68 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 58.3% 7 / 12 Podpod 100% 5 / 5 totaltotal 19.1% 34 / 178 CC 32 SCAR 54.7
CC
32
Cov
16%
execute
380.0
validate
210.0
opt_spec
2.0
CRAP 236.6
↑ Summary blib/lib/Algorithm/Classifier/IsolationForest/App/Command/stream.pm »
1 package Algorithm::Classifier::IsolationForest::App::Command::csv2plot;
2  
3 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 86 ▶use Algorithm::Classifier::IsolationForest ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 86 ▶use Algorithm::Classifier::IsolationForest::App -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 86 ▶use File::Slurp qw(read_file write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 86 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 86 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub opt_spec {
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
12 return (
13 0 [ 'i=s', 'Input CSV for processing.', { 'completion' => 'files' } ],
14 [ 'o=s', 'PNG file to output to. Default: plot.png', { 'default' => 'plot.png', 'completion' => 'files' } ],
15 [ 'w', 'If the file specified via -o exists, over write it.' ],
16 [
17 'p=s',
18 'Type of plot. Default: auto',
19 { 'default' => 'auto', completion => [ 'auto', '2heat', '3range', '3binary' ] }
20 ],
21 [ 'print', 'Print what would be used with gnuplot instead of calling gnuplot' ],
22 [ 'open', 'Call xdg-open to open the generated graph.' ],
23 [ 'small-points', 'Use smaller points (ps 0.8) for dense 3range datasets.' ],
24 );
25 } ## end sub opt_spec
26  
27 0 ▶sub abstract { 'Plot the CSV data used with iforest via gnuplot.' }
Subroutine abstract: not called CC=1 SCAR=6.9
Pod documented
28  
29 0 ▶sub description {
Subroutine description: not called CC=1 SCAR=6.9
Pod documented
30 0 'Plot the CSV data used with iforest via gnuplot.
31  
32 Plot types are as below.
33  
34 auto: If there are two columns, 2heat. If there are 4 or more columns, 3range.
35 2heat: Use column 1 and 2 to generate a splatter plot over a heat map.
36 3range: Use columns 1 and 2 for x/y, and the second-to-last column for the
37 score gradient. Suitable for predict -d output (any number of features).
38 3binary: Use columns 1 and 2 for x/y, and the last column for normal/abnormal.
39 Suitable for predict -d output or gblob output.
40  
41 3range and 3binary require data outputted from predict with the -d flag.
42 For N-dimensional data, columns 1 and 2 are always used for the x/y axes.
43 ';
44 } ## end sub description
45  
46 0 ▶sub validate {
Subroutine validate: not called CC=14 SCAR=53.5
Pod documented
47 0 my ( $self, $opt, $args ) = @_;
48  
49 0 ▶ if ( !defined( $opt->{'i'} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opt->{'i'}) { } 0 0 0
elsif (not -f $opt->{'i'}) { } 0 0 0
elsif (not -r $opt->{'i'}) { } 0 0 0
50 0 $self->usage_error('-i has not been specified for a file to process');
51 } elsif ( !-f $opt->{'i'} ) {
52 0 $self->usage_error( '-i, "' . $opt->{'i'} . '", is not a file or does not exist' );
53 } elsif ( !-r $opt->{'i'} ) {
54 0 $self->usage_error( '-i, "' . $opt->{'i'} . '", is not readable' );
55 }
56  
57 0 ▶ if ( -e $opt->{'o'} && !$opt->{'w'} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (-e $opt->{'o'} and not $opt->{'w'}) { } 0 0 0
elsif (-e $opt->{'o'} and not -f $opt->{'o'}) { } 0 0 0
elsif (-e $opt->{'o'} and not -w $opt->{'o'}) { } 0 0 0
Condition0%0/3
-e $opt->{"o"} and not $opt->{"w"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $opt->{"o"} and not $opt->{"w"}
-e $opt->{"o"} not $opt->{"w"}
Truth table0%0/3
A and not(B)
A= -e $opt->{"o"}B= not $opt->{"w"}
ABresult
0X0
100
111
58 0 $self->usage_error( '-o, "' . $opt->{'o'} . '", already exists and -w is not given' );
59 ▶ } elsif ( -e $opt->{'o'} && !-f $opt->{'o'} ) {
Condition0%0/3
-e $opt->{"o"} and not -f $opt->{"o"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $opt->{"o"} and not -f $opt->{"o"}
-e $opt->{"o"} not -f $opt->{"o"}
Truth table0%0/3
A and not(B)
A= -e $opt->{"o"}B= not -f $opt->{"o"}
ABresult
0X0
100
111
60 0 $self->usage_error( '-o, "' . $opt->{'o'} . '", already exists and -w given but file is not a file' );
61 ▶ } elsif ( -e $opt->{'o'} && !-w $opt->{'o'} ) {
Condition0%0/3
-e $opt->{"o"} and not -w $opt->{"o"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $opt->{"o"} and not -w $opt->{"o"}
-e $opt->{"o"} not -w $opt->{"o"}
Truth table0%0/3
A and not(B)
A= -e $opt->{"o"}B= not -w $opt->{"o"}
ABresult
0X0
100
111
62 0 $self->usage_error( '-o, "' . $opt->{'o'} . '", already exists and -w given but file is not writable' );
63 }
64  
65 0 ▶ if ( ( $opt->{'p'} ne 'auto' )
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"p"} ne "auto" and $opt->{"p"} ne "2heat" and $opt->{"p"} ne "3range" and $opt->{"p"} ne "3binary") 0 0 0
Condition0%0/9
$opt->{"p"} ne "auto" and $opt->{"p"} ne "2heat" and $opt->{"p"} ne "3range" and $opt->{"p"} ne "3binary"
!ll&&!rl&&r
000
$opt->{"p"} ne "auto" and $opt->{"p"} ne "2heat" and $opt->{"p"} ne "3range"
!ll&&!rl&&r
000
$opt->{"p"} ne "auto" and $opt->{"p"} ne "2heat"
!ll&&!rl&&r
000
MC/DC0%0/4
$opt->{"p"} ne "auto" and $opt->{"p"} ne "2heat" and $opt->{"p"} ne "3range" and $opt->{"p"} ne "3binary"
$opt->{"p"} ne "auto" $opt->{"p"} ne "2heat" $opt->{"p"} ne "3range" $opt->{"p"} ne "3binary"
Truth table0%0/5
A and B and C and D
A= $opt->{"p"} ne "auto"B= $opt->{"p"} ne "2heat"C= $opt->{"p"} ne "3range"D= $opt->{"p"} ne "3binary"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
66 && ( $opt->{'p'} ne '2heat' )
67 && ( $opt->{'p'} ne '3range' )
68 && ( $opt->{'p'} ne '3binary' ) )
69 {
70 0 $self->usage_error( '-p, "' . $opt->{'p'} . '", is not set to auto, 2heat, 3range, or 3binary' );
71 }
72  
73 0 return 1;
74 } ## end sub validate
75  
76 0 ▶sub execute {
Subroutine execute: not called CC=19 SCAR=59.4
Pod documented
77 0 my ( $self, $opt, $args ) = @_;
78  
79 0 my @raw_csv = read_file( $opt->{'i'} );
80 0 my @first_fields = split( /,/, $raw_csv[0] );
81 0 my $ncols = scalar @first_fields;
82  
83 0 ▶ if ( $opt->{'p'} eq 'auto' && $ncols >= 4 ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($opt->{'p'} eq 'auto' and $ncols >= 4) { } 0 0 0
elsif ($opt->{'p'} eq 'auto' and $ncols >= 2) { } 0 0 0
elsif ($opt->{'p'} eq 'auto') { } 0 0 0
elsif ($opt->{'p'} eq '2heat' and $ncols < 2) { } 0 0 0
elsif ($opt->{'p'} eq '3range' and $ncols < 3) { } 0 0 0
elsif ($opt->{'p'} eq '3binary' and $ncols < 3) { } 0 0 0
Condition0%0/3
$opt->{"p"} eq "auto" and $ncols >= 4
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->{"p"} eq "auto" and $ncols >= 4
$opt->{"p"} eq "auto" $ncols >= 4
Truth table0%0/3
A and B
A= $opt->{"p"} eq "auto"B= $ncols >= 4
ABresult
0X0
100
111
84 0 $opt->{'p'} = '3range';
85 ▶ } elsif ( $opt->{'p'} eq 'auto' && $ncols >= 2 ) {
Condition0%0/3
$opt->{"p"} eq "auto" and $ncols >= 2
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->{"p"} eq "auto" and $ncols >= 2
$opt->{"p"} eq "auto" $ncols >= 2
Truth table0%0/3
A and B
A= $opt->{"p"} eq "auto"B= $ncols >= 2
ABresult
0X0
100
111
86 0 $opt->{'p'} = '2heat';
87 } elsif ( $opt->{'p'} eq 'auto' ) {
88 0 die('-p is set to auto and the specified CSV does not have enough columns');
89 ▶ } elsif ( $opt->{'p'} eq '2heat' && $ncols < 2 ) {
Condition0%0/3
$opt->{"p"} eq "2heat" and $ncols < 2
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->{"p"} eq "2heat" and $ncols < 2
$opt->{"p"} eq "2heat" $ncols < 2
Truth table0%0/3
A and B
A= $opt->{"p"} eq "2heat"B= $ncols < 2
ABresult
0X0
100
111
90 0 die('2heat specified but there is no column for y');
91 ▶ } elsif ( $opt->{'p'} eq '3range' && $ncols < 3 ) {
Condition0%0/3
$opt->{"p"} eq "3range" and $ncols < 3
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->{"p"} eq "3range" and $ncols < 3
$opt->{"p"} eq "3range" $ncols < 3
Truth table0%0/3
A and B
A= $opt->{"p"} eq "3range"B= $ncols < 3
ABresult
0X0
100
111
92 0 die('3range specified but there is no column for score');
93 ▶ } elsif ( $opt->{'p'} eq '3binary' && $ncols < 3 ) {
Condition0%0/3
$opt->{"p"} eq "3binary" and $ncols < 3
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->{"p"} eq "3binary" and $ncols < 3
$opt->{"p"} eq "3binary" $ncols < 3
Truth table0%0/3
A and B
A= $opt->{"p"} eq "3binary"B= $ncols < 3
ABresult
0X0
100
111
94 0 die('3binary specified but there is no column for truth');
95 }
96  
97 # For predict -d output: score is the second-to-last column, prediction is last.
98 0 my $score_col = $ncols - 1;
99 0 my $pred_col = $ncols;
100  
101 0 $opt->{'i'} = File::Spec->rel2abs( $opt->{'i'} );
102 0 $opt->{'o'} = File::Spec->rel2abs( $opt->{'o'} );
103  
104 0 my ( $tempfh, $tempfile ) = tempfile( 'UNLINK' => 1 );
105  
106 0 ▶ my $ps = $opt->{'small_points'} ? '0.8' : '1.5';
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'small_points'} ? : 0 0 0
107  
108 my $gnu_plot_stuff = 'set terminal pngcairo size 1200,900 font "Sans,11"
109 0 set output "' . $opt->{'o'} . '"
110  
111 set datafile separator ","
112 set key autotitle columnhead
113  
114 set grid lc "gray70" lw 0.5 dt 2
115  
116 ';
117  
118 0 ▶ if ( $opt->{'p'} eq '3range' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opt->{'p'} eq '3range') { } 0 0 0
elsif ($opt->{'p'} eq '3binary') { } 0 0 0
elsif ($opt->{'p'} eq '2heat') { } 0 0 0
119 $gnu_plot_stuff = $gnu_plot_stuff . '
120 set palette defined (0 "#4575b4", 0.5 "#ffffbf", 1 "#d73027")
121 set cblabel "outlier score"
122 0 plot "' . $opt->{'i'} . '" using 1:2:' . $score_col . ' with points pt 7 ps ' . $ps . ' palette title ""
123 ';
124 } elsif ( $opt->{'p'} eq '3binary' ) {
125 $gnu_plot_stuff
126 = $gnu_plot_stuff
127 . 'plot "'
128 . $opt->{'i'}
129 . '" using 1:($'
130 . $pred_col
131 . '==0 ? $2 : 1/0) with points pt 7 ps 1.2 lc rgb "#4575b4" title "normal", \
132 "'
133 0 . $opt->{'i'}
134 . '" using 1:($'
135 . $pred_col
136 . '==1 ? $2 : 1/0) with points pt 13 ps 2.0 lc rgb "#d73027" title "abnormal"
137 ';
138 } elsif ( $opt->{'p'} eq '2heat' ) {
139 $gnu_plot_stuff = $gnu_plot_stuff . '
140 unset key
141 set view map
142 set palette cubehelix negative
143  
144 set dgrid 25,25 gauss kdensity 3, 3
145  
146 0 splot "' . $opt->{'i'} . '" using 1:2:(1) with pm3d, \
147 "" using 1:2:(1) with points lc "black" pt 7 ps 0.6 nogrid
148 ';
149 } ## end elsif ( $opt->{'p'} eq '2heat' )
150  
151 0 ▶ if ( $opt->{'print'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"print"}) 0 0 0
152 0 print $gnu_plot_stuff;
153 0 exit 0;
154 }
155  
156 0 write_file( $tempfile, $gnu_plot_stuff );
157  
158 0 system( 'gnuplot', $tempfile );
159 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
160 0 die( '"gnuplot ' . $tempfile . '" exited non-zero' );
161 }
162  
163 0 ▶ if ( $opt->{'open'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"open"}) 0 0 0
164 0 system( 'xdg-open', $opt->{'o'} );
165 }
166  
167 } ## end sub execute
168  
169 =head1 NAME
170  
171 Algorithm::Classifier::IsolationForest::App::Command::csv2plot - Plot the CSV data used with iforest via gnuplot
172  
173 =head1 DESCRIPTION
174  
175 Renders a CSV through gnuplot. The plot types are
176  
177 - auto :: 2heat for two columns, 3range for four or more
178 - 2heat :: columns 1 and 2 as a scatter plot over a heat map
179 - 3range :: columns 1 and 2 for x/y, the second-to-last column for the
180 score gradient
181 - 3binary :: columns 1 and 2 for x/y, the last column for the
182 normal/abnormal split
183  
184 C<3range> and C<3binary> expect the output of C<iforest predict -d>. For
185 data with more than two features, columns 1 and 2 are always the axes.
186  
187 Run it as C<iforest csv2plot>; C<iforest help csv2plot> lists every option.
188  
189 =head1 METHODS
190  
191 L<App::Cmd> calls these while dispatching the subcommand. Nothing else
192 should.
193  
194 =head2 opt_spec
195  
196 Returns this command's option specifications, as the list of arrayrefs
197 L<Getopt::Long::Descriptive> expects.
198  
199 =head2 abstract
200  
201 Returns the one-line summary C<iforest commands> prints beside the
202 command name.
203  
204 =head2 description
205  
206 Returns the long help text C<iforest help csv2plot> prints under the option
207 list.
208  
209 =head2 validate
210  
211 Checks the parsed options before anything is read or written, so a
212 mistake costs nothing.
213  
214 Checks that C<-i> is a readable file, that C<-o> does not already exist
215 unless C<-w> was given, and that C<-p> names one of the plot types above.
216  
217 Takes the parsed options hashref and the arrayref of remaining
218 arguments. Calls C<usage_error>, which prints the usage and exits, on
219 the first problem it finds, and returns 1 when everything checks out.
220  
221 =head2 execute
222  
223 Writes the gnuplot input and runs it, leaving the image at C<-o>.
224  
225 Takes the parsed options hashref and the arrayref of remaining
226 arguments, and returns 1.
227  
228 =cut
229  
230 86 return 1;
↑ Summary blib/lib/Algorithm/Classifier/IsolationForest/App/Command/stream.pm »