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

filter: Statementstmt 32.2% 19 / 59 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 50.0% 6 / 12 Podpod 100% 6 / 6 totaltotal 27.9% 31 / 111 CC 16 SCAR 37.7
CC
16
Cov
22%
validate
72.0
execute
72.0
gaussian
6.0
CRAP 43.6
« blib/lib/Algorithm/Classifier/IsolationForest/App/Command/explain.pm ↑ Summary blib/lib/Algorithm/Classifier/IsolationForest/App/Command/predict.pm »
1 package Algorithm::Classifier::IsolationForest::App::Command::gblob;
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(write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 86 ▶use constant PI => 3.14159265358979;
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 [ 'o=s', 'Output file path/name.', { 'default' => 'blob.csv', 'completion' => 'files' } ],
14 [ 's=i', 'Seed int' ],
15 [ 'p', 'Print the output instead of writing it a file.' ],
16 [ 'w', 'If the file already exists, overwrite it.' ],
17 [ 'n=i', 'Number of normal points to generate.', { 'default' => '500' } ],
18 [
19 'a=i',
20 'Number of abnormal points to generate. If less than 1, none will be generated.',
21 { 'default' => '20' }
22 ],
23 [ 'd=i', 'Number of dimensions (features) per point.', { 'default' => '2' } ],
24 );
25 } ## end sub opt_spec
26  
27 0 ▶sub abstract { 'Generates a gaussian blob of points.' }
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 'Generates a gaussian blob of points.
31  
32 The output format is as below...
33  
34 $feat1,...,$featN,$truth
35  
36 $truth is a 0/1 with 1 meaning it is a abnormal value.
37  
38 Normal points are drawn from N(0,1) in each dimension. Anomalous points are
39 placed on a hyperspherical shell at radius 5-8 from the origin.
40  
41 Use -D to control the number of dimensions (default: 2).
42 ';
43 } ## end sub description
44  
45 0 ▶sub validate {
Subroutine validate: not called CC=8 SCAR=42.8
Pod documented
46 0 my ( $self, $opt, $args ) = @_;
47  
48 0 ▶ if ( defined( $opt->{'s'} ) && $opt->{'s'} <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"s"} and $opt->{"s"} <= 0) 0 0 0
Condition0%0/3
defined $opt->{"s"} and $opt->{"s"} <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt->{"s"} and $opt->{"s"} <= 0
defined $opt->{"s"} $opt->{"s"} <= 0
Truth table0%0/3
A and B
A= defined $opt->{"s"}B= $opt->{"s"} <= 0
ABresult
0X0
100
111
49 0 $self->usage_error( '-s, "' . $opt->{'s'} . '", is less than or equal to 0, should be a positive int' );
50 }
51  
52 0 ▶ if ( !$opt->{'p'} && -e $opt->{'o'} && !$opt->{'w'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt->{"p"} and -e $opt->{"o"} and not $opt->{"w"}) 0 0 0
Condition0%0/6
not $opt->{"p"} and -e $opt->{"o"} and not $opt->{"w"}
!ll&&!rl&&r
000
not $opt->{"p"} and -e $opt->{"o"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $opt->{"p"} and -e $opt->{"o"} and not $opt->{"w"}
not $opt->{"p"} -e $opt->{"o"} not $opt->{"w"}
Truth table0%0/4
not(A) and B and not(C)
A= not $opt->{"p"}B= -e $opt->{"o"}C= not $opt->{"w"}
ABCresult
0XX0
10X0
1100
1111
53 $self->usage_error(
54 0 '-o "' . $opt->{'o'} . '", already exists. Specify -w to overwrite it or use a different value.' );
55 }
56  
57 0 ▶ if ( $opt->{'n'} < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"n"} < 1) 0 0 0
58 0 $self->usage_error( '-n, "' . $opt->{'n'} . '", must be be 1 or greater' );
59 }
60  
61 0 ▶ if ( $opt->{'d'} < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"d"} < 1) 0 0 0
62 0 $self->usage_error( '-D, "' . $opt->{'d'} . '", must be 1 or greater' );
63 }
64  
65 0 return 1;
66 } ## end sub validate
67  
68 # Box-Muller; see the POD below. Self-contained on purpose: this command
69 # generates test data, so it has no business reaching into the module's
70 # internals for _randn.
71 0 ▶sub gaussian {
Subroutine gaussian: not called CC=2 SCAR=17.9
Pod documented
72 0 my ( $mu, $sigma ) = @_;
73 0 ▶ my $u1 = rand() || 1e-12;
Condition0%0/2
rand() || 1e-12
l!l
00
MC/DC0%0/1
rand() || 1e-12
rand()
Truth table0%0/2
A
A= rand()
Aresult
11
00
74 0 my $u2 = rand();
75 0 return $mu + $sigma * sqrt( -2 * log($u1) ) * cos( 2 * PI * $u2 );
76 }
77  
78 0 ▶sub execute {
Subroutine execute: not called CC=8 SCAR=42.8
Pod documented
79 0 my ( $self, $opt, $args ) = @_;
80  
81 0 my $dims = $opt->{'d'};
82 0 ▶ srand( $opt->{'s'} ) if defined $opt->{'s'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"s"} 0 0 0
83  
84 0 my $data = '';
85  
86 # Normal points: each feature is drawn from N(0,1)
87 0 for ( 1 .. $opt->{'n'} ) {
88 0 my @feats = map { gaussian( 0, 1 ) } 1 .. $dims;
89 0 $data = $data . join( ',', @feats ) . ",0\n";
90 }
91  
92 # Anomalous points: random direction in D-space scaled to radius 5-8.
93 # Direction is a normalised vector of D Gaussian draws.
94 0 ▶ if ( $opt->{'a'} >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"a"} >= 1) 0 0 0
95 0 for ( 1 .. $opt->{'a'} ) {
96 0 my $radius = 5 + rand() * 3;
97 0 my @raw = map { gaussian( 0, 1 ) } 1 .. $dims;
98 0 my $norm = 0;
99 0 $norm += $_ * $_ for @raw;
100 0 ▶ $norm = sqrt($norm) || 1;
Condition0%0/2
sqrt $norm || 1
l!l
00
MC/DC0%0/1
sqrt $norm || 1
sqrt $norm
Truth table0%0/2
A
A= sqrt $norm
Aresult
11
00
101 0 my @feats = map { $_ / $norm * $radius } @raw;
102 0 $data = $data . join( ',', @feats ) . ",1\n";
103 }
104 } ## end if ( $opt->{'a'} >= 1 )
105  
106 0 ▶ if ( $opt->{'p'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"p"}) 0 0 0
107 0 print $data;
108 0 exit 0;
109 }
110  
111 0 write_file( $opt->{'o'}, $data );
112  
113 } ## end sub execute
114  
115 =head1 NAME
116  
117 Algorithm::Classifier::IsolationForest::App::Command::gblob - Generates a gaussian blob of points.
118  
119 =head1 DESCRIPTION
120  
121 Generates a synthetic dataset: a Gaussian cluster of normal points plus a
122 handful of outliers placed away from it, as CSV. Useful for exercising
123 the other commands without needing real data, and for the examples in the
124 documentation.
125  
126 Seeding with C<-s> makes a blob reproducible.
127  
128 Run it as C<iforest gblob>; C<iforest help gblob> lists every option.
129  
130 =head1 METHODS
131  
132 L<App::Cmd> calls these while dispatching the subcommand. Nothing else
133 should.
134  
135 =head2 opt_spec
136  
137 Returns this command's option specifications, as the list of arrayrefs
138 L<Getopt::Long::Descriptive> expects.
139  
140 =head2 abstract
141  
142 Returns the one-line summary C<iforest commands> prints beside the
143 command name.
144  
145 =head2 description
146  
147 Returns the long help text C<iforest help gblob> prints under the option
148 list.
149  
150 =head2 validate
151  
152 Checks the parsed options before anything is read or written, so a
153 mistake costs nothing.
154  
155 Checks that C<-s>, C<-n> and C<-D> are sane numbers.
156  
157 Takes the parsed options hashref and the arrayref of remaining
158 arguments. Calls C<usage_error>, which prints the usage and exits, on
159 the first problem it finds, and returns 1 when everything checks out.
160  
161 =head2 execute
162  
163 Writes the generated CSV to C<-o>, or to STDOUT.
164  
165 Takes the parsed options hashref and the arrayref of remaining
166 arguments, and returns 1.
167  
168 =head2 gaussian
169  
170 One draw from a normal distribution, via Box-Muller.
171  
172 Takes the distribution's mean and standard deviation, and returns a
173 single value -- typically within four standard deviations of the mean.
174 It consumes two C<rand()> draws, so seeding once with C<-s> makes a whole
175 blob reproducible.
176  
177 my $v = gaussian( 0, 1 );
178  
179 =cut
180  
181 86 return 1;
« blib/lib/Algorithm/Classifier/IsolationForest/App/Command/explain.pm ↑ Summary blib/lib/Algorithm/Classifier/IsolationForest/App/Command/predict.pm »