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

filter: Statementstmt 84.8% 56 / 66 Branchbran 68.4% 26 / 38 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 80.0% 8 / 10 Podpod 100% 5 / 5 totaltotal 72.3% 97 / 134 CC 26 SCAR 27.0
CC
26
Cov
74%
execute
22.4
validate
13.6
abstract
2.0
CRAP 14.8
« blib/lib/Algorithm/Classifier/IsolationForest.pm ↑ Summary blib/lib/Algorithm/Classifier/IsolationForest/App/Command/set_voting.pm »
1 package Algorithm::Classifier::IsolationForest::App::Command::proto;
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  
9 5 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
10 return (
11 [
12 5 'from-model=s',
13 'Extract a prototype from this saved model JSON (batch or online).',
14 { 'completion' => 'files' }
15 ],
16 [
17 'check=s',
18 'Validate this prototype file and print a summary of it; exits non-zero when invalid.',
19 { 'completion' => 'files' }
20 ],
21 [
22 'o=s',
23 'Output the prototype to this file instead of printing (--from-model only).',
24 { 'completion' => 'files' }
25 ],
26 [ 'w', 'If the file specified via -o exists, over write it.' ],
27 );
28 } ## end sub opt_spec
29  
30 0 ▶sub abstract { 'Extract a prototype from a saved model, or validate a prototype file' }
Subroutine abstract: not called CC=1 SCAR=6.9
Pod documented
31  
32 0 ▶sub description {
Subroutine description: not called CC=1 SCAR=6.9
Pod documented
33 0 'Works with model prototypes: small JSON documents holding the variable
34 schema (feature names, per-feature descriptions, munger specs, missing
35 policy), a user-owned schema_version and schema_description, and
36 optionally the tuning knobs. `iforest fit --prototype` and
37 `iforest stream --prototype` create models from one; see PROTOTYPES in
38 the Algorithm::Classifier::IsolationForest POD for the file format.
39  
40 --from-model extracts a prototype from a saved model, closing the loop:
41 pull the schema and knobs out of a good model, edit the metadata, and
42 create fresh models from it. A model with no recorded schema_version /
43 schema_description gets placeholder values to edit in.
44  
45 --check validates a prototype file and prints a summary of what it
46 describes, exiting non-zero when the file is not a valid prototype.
47  
48 Exactly one of --from-model or --check must be given.
49 ';
50 } ## end sub description
51  
52 5 ▶sub validate {
Subroutine validate: called CC=11 SCAR=26.1
Pod documented
53 5 my ( $self, $opt, $args ) = @_;
54  
55 5 ▶ my $from = defined $opt->{'from_model'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'from_model'} ? : 2 3 5
56 5 ▶ my $check = defined $opt->{'check'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'check'} ? : 3 2 5
57 5 ▶ if ( $from + $check != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from + $check != 1) 2 3 5
58 2 $self->usage_error('exactly one of --from-model or --check must be specified');
59 }
60  
61 3 ▶ my ( $switch, $file ) = $from ? ( '--from-model', $opt->{'from_model'} ) : ( '--check', $opt->{'check'} );
Branch: 1 branch
BranchTrueFalseTotal
$from ? : 1 2 3
62 3 ▶ if ( !-f $file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not -f $file) { } 0 3 3
elsif (not -r $file) { } 0 3 3
63 0 $self->usage_error( $switch . ', "' . $file . '", is not a file or does not exist' );
64 } elsif ( !-r $file ) {
65 0 $self->usage_error( $switch . ', "' . $file . '", is not readable' );
66 }
67  
68 3 ▶ if ( defined $opt->{'o'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"o"}) 1 2 3
69 1 ▶ if ($check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check) 0 1 1
70 0 $self->usage_error('-o may only be used with --from-model');
71 }
72 1 ▶ if ( -e $opt->{'o'} && !$opt->{'w'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $opt->{"o"} and not $opt->{"w"}) 0 1 1
Condition33%1/3
-e $opt->{"o"} and not $opt->{"w"}
!ll&&!rl&&r
100
MC/DC0%0/2
-e $opt->{"o"} and not $opt->{"w"}
-e $opt->{"o"} not $opt->{"w"}
Truth table33%1/3
A and not(B)
A= -e $opt->{"o"}B= not $opt->{"w"}
ABresult
0X0
100
111
73 0 $self->usage_error( '-o, "' . $opt->{'o'} . '", already exists and -w is not specified' );
74 }
75 }
76  
77 3 return 1;
78 } ## end sub validate
79  
80 3 ▶sub execute {
Subroutine execute: called CC=16 SCAR=31.1
Pod documented
81 3 my ( $self, $opt, $args ) = @_;
82  
83 3 ▶ if ( defined $opt->{'from_model'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"from_model"}) 1 2 3
84 1 my $model = Algorithm::Classifier::IsolationForest->load( $opt->{'from_model'} );
85 1 my $proto_json = $model->to_prototype;
86 1 ▶ if ( defined $opt->{'o'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{'o'}) { } 1 0 1
87 1 write_file( $opt->{'o'}, { 'atomic' => 1 }, $proto_json . "\n" );
88 } else {
89 0 print $proto_json. "\n";
90 }
91 1 return 1;
92 } ## end if ( defined $opt->{'from_model'} )
93  
94 # --check: structural validation plus a human summary of the file.
95 2 my $raw = read_file( $opt->{'check'} );
96 2 my $proto = eval { Algorithm::Classifier::IsolationForest->validate_prototype($raw) };
97 2 ▶ die( '--check, "' . $opt->{'check'} . '", is not a valid prototype: ' . $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
98  
99 1 my $schema = $proto->{schema};
100 1 my $tags = $schema->{feature_names};
101 1 printf " %-20s %s\n", 'file', $opt->{'check'};
102 1 printf " %-20s %s\n", 'class', $proto->{class};
103 1 printf " %-20s %s\n", 'schema_version', $proto->{schema_version};
104 1 printf " %-20s %s\n", 'schema_description', $proto->{schema_description};
105 1 ▶ printf " %-20s %s\n", 'missing', ( defined $schema->{missing} ? $schema->{missing} : '(unset)' );
Branch: 1 branch
BranchTrueFalseTotal
defined $schema->{'missing'} ? : 1 0 1
106 1 printf " %-20s %s\n", 'feature_names', join( ', ', @$tags );
107 1 ▶ my $fd = ref $schema->{feature_descriptions} eq 'HASH' ? $schema->{feature_descriptions} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $schema->{'feature_descriptions'} eq 'HASH' ? : 1 0 1
108  
109 1 for my $i ( 0 .. $#$tags ) {
110 printf " [%d] %s%s\n", $i, $tags->[$i],
111 3 ▶ ( defined $fd->{ $tags->[$i] } ? ' -- ' . $fd->{ $tags->[$i] } : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $$fd{$tags->[$i]} ? : 1 2 3
112 }
113 1 my $mungers = $schema->{mungers};
114 1 ▶ if ( ref $mungers eq 'HASH' && %$mungers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mungers eq "HASH" and %$mungers) 0 1 1
Condition33%1/3
ref $mungers eq "HASH" and %$mungers
!ll&&!rl&&r
100
MC/DC0%0/2
ref $mungers eq "HASH" and %$mungers
ref $mungers eq "HASH" %$mungers
Truth table33%1/3
A and B
A= ref $mungers eq "HASH"B= %$mungers
ABresult
0X0
100
111
115 0 printf " %-20s %s\n", 'mungers', scalar( keys %$mungers ) . ' configured';
116 0 for my $k ( sort keys %$mungers ) {
117 printf " %-18s %s\n", $k,
118 0 ▶ ( ref $mungers->{$k} eq 'HASH' && defined $mungers->{$k}{munger} ? $mungers->{$k}{munger} : '(?)' );
Branch: 1 branch
BranchTrueFalseTotal
ref $mungers->{$k} eq 'HASH' && defined $mungers->{$k}{'munger'} ? : 0 0 0
Condition0%0/3
ref $mungers->{$k} eq "HASH" and defined $mungers->{$k}{"munger"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $mungers->{$k} eq "HASH" and defined $mungers->{$k}{"munger"}
ref $mungers->{$k} eq "HASH" defined $mungers->{$k}{"munger"}
Truth table0%0/3
A and B
A= ref $mungers->{$k} eq "HASH"B= defined $mungers->{$k}{"munger"}
ABresult
0X0
100
111
119 }
120 }
121 1 ▶ my $params = ref $proto->{params} eq 'HASH' ? $proto->{params} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $proto->{'params'} eq 'HASH' ? : 1 0 1
122 1 for my $k ( sort keys %$params ) {
123 4 ▶ printf " %-20s %s\n", $k, ( defined $params->{$k} ? $params->{$k} : '(unset)' );
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{$k} ? : 4 0 4
124 }
125  
126 1 return 1;
127 } ## end sub execute
128  
129 =head1 NAME
130  
131 Algorithm::Classifier::IsolationForest::App::Command::proto - Extract a prototype from a saved model, or validate a prototype file
132  
133 =head1 DESCRIPTION
134  
135 Works with model prototypes: small JSON documents holding the variable
136 schema -- feature names, per-feature descriptions, munger specs, missing
137 policy -- a user-owned C<schema_version> and C<schema_description>, and
138 optionally the tuning knobs. C<iforest fit --prototype> and
139 C<iforest stream --prototype> create models from one.
140  
141 C<--from-model> extracts a prototype from a saved model, which closes the
142 loop: pull the schema and knobs out of a model that works, edit the
143 metadata, and mint fresh models from it. A model with no recorded schema
144 version or description gets placeholders to fill in.
145  
146 C<--check> validates a prototype and summarises what it describes,
147 exiting non-zero when the file is not a valid prototype.
148  
149 See PROTOTYPES in L<Algorithm::Classifier::IsolationForest> for the file
150 format.
151  
152 Run it as C<iforest proto>; C<iforest help proto> lists every option.
153  
154 =head1 METHODS
155  
156 L<App::Cmd> calls these while dispatching the subcommand. Nothing else
157 should.
158  
159 =head2 opt_spec
160  
161 Returns this command's option specifications, as the list of arrayrefs
162 L<Getopt::Long::Descriptive> expects.
163  
164 =head2 abstract
165  
166 Returns the one-line summary C<iforest commands> prints beside the
167 command name.
168  
169 =head2 description
170  
171 Returns the long help text C<iforest help proto> prints under the option
172 list.
173  
174 =head2 validate
175  
176 Checks the parsed options before anything is read or written, so a
177 mistake costs nothing.
178  
179 Requires exactly one of C<--from-model> or C<--check>, checks that the
180 named file is readable, and allows C<-o> only with C<--from-model> (and
181 only over an existing file when C<-w> is given).
182  
183 Takes the parsed options hashref and the arrayref of remaining
184 arguments. Calls C<usage_error>, which prints the usage and exits, on
185 the first problem it finds, and returns 1 when everything checks out.
186  
187 =head2 execute
188  
189 Extracts or validates as asked, writing the prototype to C<-o> or the
190 summary to STDOUT.
191  
192 Takes the parsed options hashref and the arrayref of remaining
193 arguments, and returns 1.
194  
195 =cut
196  
197 86 return 1;
« blib/lib/Algorithm/Classifier/IsolationForest.pm ↑ Summary blib/lib/Algorithm/Classifier/IsolationForest/App/Command/set_voting.pm »