« blib/lib/Algorithm/Classifier/IsolationForest/App/Command/streamc.pm
↑ Summary
blib/lib/Algorithm/Classifier/IsolationForest/Online.pm »
| 1 | package Algorithm::Classifier::IsolationForest::App::Command::info; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 11 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 11 | 'm=s', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 'Input model JSON file path/name.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | { 'default' => 'iforest_model.json', 'completion' => 'files' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | [ 'json', 'Emit machine-readable JSON instead of the text table.' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | } ## end sub opt_spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub abstract { 'Show the constructor params, fit-time metadata, and tree stats of a saved model' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
abstract: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub description { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
description: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | 'Loads a saved Algorithm::Classifier::IsolationForest model and prints the | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | constructor params, fit-time metadata, and a handful of derived tree | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | statistics (count, average/max depth, total nodes). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | Use --json for a machine-readable dump suitable for piping into jq. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 11 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=3 SCAR=13.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 11 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 11 | ▶ | if ( !-f $opt->{'m'} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $self->usage_error( '-m, "' . $opt->{'m'} . '", is not a file or does not exist' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } elsif ( !-r $opt->{'m'} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | $self->usage_error( '-m, "' . $opt->{'m'} . '", is not readable' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 11 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } ## end sub validate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # Tree-shape stats are derived once at load time. Each tree is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # nested arrayref structure -- leaf [0, size] or interior [1, ...] / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # [2, ...] with children at fixed slots. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # $node :: the node to descend from, normally a tree root. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # $depth :: the depth to credit $node with, 0 for a root. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # $acc :: the accumulator hashref, with the keys nodes, leaves, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # max_depth and depth_sum all pre-seeded to 0. Updated in place. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Returns: nothing; everything lands in $acc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # my $acc = { nodes => 0, leaves => 0, max_depth => 0, depth_sum => 0 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # _walk_tree( $model->{trees}[0], 0, $acc ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 6788 | ▶ | sub _walk_tree { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_walk_tree: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 6788 | my ( $node, $depth, $acc ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 6788 | $acc->{nodes}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 6788 | ▶ | if ( $node->[0] == 0 ) { # leaf | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 3549 | $acc->{leaves}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 3549 | ▶ | $acc->{max_depth} = $depth if $depth > $acc->{max_depth}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 3549 | $acc->{depth_sum} += $depth; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 3549 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # Axis interior nodes have children at slots 3,4; oblique at 4,5. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 3239 | ▶ | my ( $li, $ri ) = $node->[0] == 1 ? ( 3, 4 ) : ( 4, 5 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 3239 | _walk_tree( $node->[$li], $depth + 1, $acc ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 3239 | _walk_tree( $node->[$ri], $depth + 1, $acc ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } ## end sub _walk_tree | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # Whole-forest shape summary for the batch-model report: walks every tree | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # into one accumulator, so the averages `info` prints are over the forest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # rather than per tree. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # $trees :: the model's trees, an arrayref of root nodes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # Returns: a hashref of nodes, leaves, max_depth and depth_sum totalled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # across the forest. Divide depth_sum by leaves for the mean leaf depth. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # my $stats = _tree_stats( $model->{trees} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # $stats->{depth_sum} / $stats->{leaves}; # mean leaf depth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 8 | ▶ | sub _tree_stats { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tree_stats: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 8 | my ($trees) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 8 | my $acc = { nodes => 0, leaves => 0, max_depth => 0, depth_sum => 0 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 8 | _walk_tree( $_, 0, $acc ) for @$trees; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 8 | return $acc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # Summary of a model's Algorithm::ToNumberMunger spec as a flat | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # 'key => munger name' map (the full spec can be arbitrarily large -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # frozen count tables and the like -- so info only names the mungers). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # $model :: the loaded model, of either class. Only its mungers slot is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # read. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # Returns: a hashref of feature name => munger name, or undef when the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # model carries no mungers. A name comes back undef when the spec entry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # is not a hashref, which _print_mungers shows as '(?)'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # _munger_summary($model); # { method => 'http_method_enum', ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 11 | ▶ | sub _munger_summary { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_munger_summary: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 11 | my ($model) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 11 | my $mungers = $model->{mungers}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 11 | ▶ | return undef unless ref $mungers eq 'HASH' && %$mungers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $mungers eq "HASH" and %$mungers
MC/DC50%1/2
ref $mungers eq "HASH" and %$mungers
ref $mungers eq "HASH"
%$mungers
Truth table66%2/3
A and B
A= ref $mungers eq "HASH"B= %$mungers
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | ▶ | return { map { $_ => ( ref $mungers->{$_} eq 'HASH' ? $mungers->{$_}{munger} : undef ) } keys %$mungers }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # Text-table rendering of the summary, matching the feature_names style. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # $summary :: the hashref from _munger_summary, or undef. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # Returns: nothing. Prints a count line and one indented line per | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # feature, sorted by name, to STDOUT. An undef summary prints nothing, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # callers need not test first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # _print_mungers( _munger_summary($model) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # # mungers 2 configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | # # method http_method_enum | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 8 | ▶ | sub _print_mungers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_print_mungers: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 8 | my ($summary) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 8 | ▶ | return unless $summary; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | printf " %-20s %s\n", 'mungers', scalar( keys %$summary ) . ' configured'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | for my $k ( sort keys %$summary ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | printf " %-18s %s\n", $k, ( defined $summary->{$k} ? $summary->{$k} : '(?)' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # Online-model counterpart of _walk_tree: nodes are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # [0, count, lo, hi] / [1, count, lo, hi, attr, split, left, right], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | # a tree record is { root, count, depth_limit }, and root may be undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | # on a tree that has not learned anything yet. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # $node :: the node to descend from. Must be defined -- the caller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # skips trees whose root is not. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # $depth :: the depth to credit $node with, 0 for a root. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | # $acc :: the accumulator hashref, same keys as _walk_tree's. Updated in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # place. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | # Returns: nothing; everything lands in $acc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # _walk_tree_online( $tree->{root}, 0, $acc ) if defined $tree->{root}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 268 | ▶ | sub _walk_tree_online { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_walk_tree_online: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 268 | my ( $node, $depth, $acc ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 268 | $acc->{nodes}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 268 | ▶ | if ( $node->[0] == 0 ) { # leaf | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 164 | $acc->{leaves}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 164 | ▶ | $acc->{max_depth} = $depth if $depth > $acc->{max_depth}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 164 | $acc->{depth_sum} += $depth; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 164 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 104 | _walk_tree_online( $node->[6], $depth + 1, $acc ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 104 | _walk_tree_online( $node->[7], $depth + 1, $acc ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | } ## end sub _walk_tree_online | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # Whole-forest shape summary for the online-model report. The online | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # counterpart of _tree_stats, and tolerant of the empty trees a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # barely-started model has. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # $trees :: the model's trees, an arrayref of { root, count, depth_limit } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | # records. A record whose root is undef is skipped. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # Returns: a hashref of nodes, leaves, max_depth and depth_sum totalled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # across the forest. All zeroes for a model that has learned nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | # my $stats = _tree_stats_online( $model->{trees} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 3 | ▶ | sub _tree_stats_online { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tree_stats_online: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 3 | my ($trees) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 3 | my $acc = { nodes => 0, leaves => 0, max_depth => 0, depth_sum => 0 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 3 | for my $tree (@$trees) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 60 | ▶ | _walk_tree_online( $tree->{root}, 0, $acc ) if defined $tree->{root}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 3 | return $acc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # Online models have a different parameter set and tree shape; handled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # in a dedicated path so the batch-model reporting below stays simple. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # $opt :: the parsed command options hashref. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # $model :: the loaded Algorithm::Classifier::IsolationForest::Online | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # model. execute dispatches here on the stored format tag. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # Returns: 1, so execute can return its result directly. The report goes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # to STDOUT. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # return $self->_execute_online( $opt, $model ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # if ref $model eq 'Algorithm::Classifier::IsolationForest::Online'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 3 | ▶ | sub _execute_online { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_execute_online: called CC=17 SCAR=28.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 3 | my ( $self, $opt, $model ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 3 | my $stats = _tree_stats_online( $model->{trees} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 3 | my $n_trees = scalar @{ $model->{trees} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 3 | ▶ | my $avg_depth = $stats->{leaves} ? $stats->{depth_sum} / $stats->{leaves} : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 3 | ▶ | my $avg_nodes = $n_trees ? $stats->{nodes} / $n_trees : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 3 | my $tags = $model->{feature_names}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 3 | ▶ | my $tagged = ( ref $tags eq 'ARRAY' && @$tags ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $tags eq "ARRAY" and @$tags
MC/DC50%1/2
ref $tags eq "ARRAY" and @$tags
ref $tags eq "ARRAY"
@$tags
Truth table66%2/3
A and B
A= ref $tags eq "ARRAY"B= @$tags
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | my %info = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 'file' => $opt->{'m'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 'type' => 'online', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 'tagged' => $tagged, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 'feature_names' => $tagged ? $tags : undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 'n_trees' => $n_trees, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 'n_features' => $model->{n_features}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 'window_size' => $model->{window_size}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 'window_count' => $model->window_count, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 'seen' => $model->{seen}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 'max_leaf_samples' => $model->{max_leaf_samples}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 'growth' => $model->{growth}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 'subsample' => $model->{subsample}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 'contamination' => $model->{contamination}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 'threshold' => $model->{threshold}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 'mungers' => _munger_summary($model), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 'munger_module_version' => $model->{munger_module_version}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 'schema_version' => $model->{schema_version}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 'schema_description' => $model->{schema_description}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 'feature_descriptions' => $model->{feature_descriptions}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 'tree_total_nodes' => $stats->{nodes}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 'tree_total_leaves' => $stats->{leaves}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 'tree_max_depth' => $stats->{max_depth}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 3 | ▶ | 'tree_avg_depth' => $avg_depth, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 'tree_avg_nodes' => $avg_nodes, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 3 | ▶ | if ( $opt->{'json'} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 1 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | print JSON::PP->new->canonical(1)->pretty->encode( \%info ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 2 | my @order = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | file type tagged n_trees n_features window_size window_count seen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | max_leaf_samples growth subsample contamination threshold | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | schema_version schema_description munger_module_version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | tree_total_nodes tree_total_leaves tree_max_depth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | tree_avg_depth tree_avg_nodes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 2 | for my $k (@order) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 42 | my $v = $info{$k}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 42 | ▶ | $v = '(unset)' unless defined $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 42 | ▶ | $v = sprintf( '%.4f', $v ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
defined $v and $v =~ /^-?\d+\.\d+/ and not $k =~ /^tree_total_/ and not $k =~ /\A(?:munger_module_version|schema_version|schema_description)\z/
defined $v and $v =~ /^-?\d+\.\d+/ and not $k =~ /^tree_total_/
defined $v and $v =~ /^-?\d+\.\d+/
MC/DC25%1/4
defined $v and $v =~ /^-?\d+\.\d+/ and not $k =~ /^tree_total_/ and not $k =~ /\A(?:munger_module_version|schema_version|schema_description)\z/
defined $v
$v =~ /^-?\d+\.\d+/
not $k =~ /^tree_total_/
not $k =~ /\A(?:munger_module_version|schema_version|schema_description)\z/
Truth table40%2/5
A and B and not(C) and not(D)
A= defined $vB= $v =~ /^-?\d+\.\d+/C= not $k =~ /^tree_total_/D= not $k =~ /\A(?:munger_module_version|schema_version|schema_description)\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | if defined $v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | && $v =~ /^-?\d+\.\d+/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | && $k !~ /^tree_total_/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | && $k !~ /\A(?:munger_module_version|schema_version|schema_description)\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 42 | printf " %-20s %s\n", $k, $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } ## end for my $k (@order) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | ▶ | if ($tagged) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 1 | printf " %-20s %s\n", 'feature_names', join( ', ', @$tags ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 1 | ▶ | my $fd = ref $model->{feature_descriptions} eq 'HASH' ? $model->{feature_descriptions} : {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 1 | for my $i ( 0 .. $#$tags ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | printf " [%d] %s%s\n", $i, $tags->[$i], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 3 | ▶ | ( defined $fd->{ $tags->[$i] } ? ' -- ' . $fd->{ $tags->[$i] } : '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 2 | _print_mungers( $info{mungers} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } ## end sub _execute_online | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 11 | ▶ | sub execute { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=18 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 11 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 11 | my $model = Algorithm::Classifier::IsolationForest->load( $opt->{'m'} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # load() dispatches on the stored format tag, so this may be an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | # online model -- those have their own parameter set and tree shape. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 11 | ▶ | if ( ref $model eq 'Algorithm::Classifier::IsolationForest::Online' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 3 | return $self->_execute_online( $opt, $model ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | # Tree stats are not stored on the model -- they're cheap to derive. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 8 | my $stats = _tree_stats( $model->{trees} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 8 | my $n_trees = scalar @{ $model->{trees} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 8 | ▶ | my $avg_depth = $stats->{leaves} ? $stats->{depth_sum} / $stats->{leaves} : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 8 | ▶ | my $avg_nodes = $n_trees ? $stats->{nodes} / $n_trees : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | # Feature-name tags are stored as an arrayref (via -t at fit time). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 8 | my $tags = $model->{feature_names}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 8 | ▶ | my $tagged = ( ref $tags eq 'ARRAY' && @$tags ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $tags eq "ARRAY" and @$tags
MC/DC50%1/2
ref $tags eq "ARRAY" and @$tags
ref $tags eq "ARRAY"
@$tags
Truth table66%2/3
A and B
A= ref $tags eq "ARRAY"B= @$tags
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | my %info = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 'file' => $opt->{'m'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 'mode' => $model->{mode}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 'voting' => $model->{voting}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 'tagged' => $tagged, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 'feature_names' => $tagged ? $tags : undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 'n_trees' => $n_trees, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 'n_features' => $model->{n_features}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 'sample_size' => $model->{sample_size}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 'psi_used' => $model->{psi_used}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 'c_psi' => $model->{c_psi}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 'max_depth_used' => $model->{max_depth_used}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 'extension_level' => $model->{extension_level_used}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 'contamination' => $model->{contamination}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 'threshold' => $model->{threshold}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 'mungers' => _munger_summary($model), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 'munger_module_version' => $model->{munger_module_version}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 'schema_version' => $model->{schema_version}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 'schema_description' => $model->{schema_description}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 'feature_descriptions' => $model->{feature_descriptions}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 'tree_total_nodes' => $stats->{nodes}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 'tree_total_leaves' => $stats->{leaves}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 'tree_max_depth' => $stats->{max_depth}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 8 | ▶ | 'tree_avg_depth' => $avg_depth, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 'tree_avg_nodes' => $avg_nodes, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 8 | ▶ | if ( $opt->{'json'} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 2 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 2 | print JSON::PP->new->canonical(1)->pretty->encode( \%info ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | # Text-table output, in a stable order, with undef shown as "(unset)". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | # feature_names, feature_descriptions, and mungers are refs -- rendered | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # separately below. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 6 | my @order = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | file mode voting tagged n_trees n_features sample_size psi_used c_psi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | max_depth_used extension_level contamination threshold | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | schema_version schema_description munger_module_version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | tree_total_nodes tree_total_leaves tree_max_depth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | tree_avg_depth tree_avg_nodes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 6 | for my $k (@order) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 126 | my $v = $info{$k}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 126 | ▶ | $v = '(unset)' unless defined $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # Pretty-print floats with a couple of decimals; leave ints raw. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 126 | ▶ | $v = sprintf( '%.4f', $v ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
defined $v and $v =~ /^-?\d+\.\d+/ and not $k =~ /^tree_total_/ and not $k =~ /\A(?:munger_module_version|schema_version|schema_description)\z/
defined $v and $v =~ /^-?\d+\.\d+/ and not $k =~ /^tree_total_/
defined $v and $v =~ /^-?\d+\.\d+/
MC/DC50%2/4
defined $v and $v =~ /^-?\d+\.\d+/ and not $k =~ /^tree_total_/ and not $k =~ /\A(?:munger_module_version|schema_version|schema_description)\z/
defined $v
$v =~ /^-?\d+\.\d+/
not $k =~ /^tree_total_/
not $k =~ /\A(?:munger_module_version|schema_version|schema_description)\z/
Truth table60%3/5
A and B and not(C) and not(D)
A= defined $vB= $v =~ /^-?\d+\.\d+/C= not $k =~ /^tree_total_/D= not $k =~ /\A(?:munger_module_version|schema_version|schema_description)\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | if defined $v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | && $v =~ /^-?\d+\.\d+/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | && $k !~ /^tree_total_/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | && $k !~ /\A(?:munger_module_version|schema_version|schema_description)\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 126 | printf " %-20s %s\n", $k, $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | } ## end for my $k (@order) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | # Feature-name tags, one per line, in stored (positional) order. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 6 | ▶ | if ($tagged) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 3 | printf " %-20s %s\n", 'feature_names', join( ', ', @$tags ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 3 | ▶ | my $fd = ref $model->{feature_descriptions} eq 'HASH' ? $model->{feature_descriptions} : {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 3 | for my $i ( 0 .. $#$tags ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | printf " [%d] %s%s\n", $i, $tags->[$i], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 9 | ▶ | ( defined $fd->{ $tags->[$i] } ? ' -- ' . $fd->{ $tags->[$i] } : '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 6 | _print_mungers( $info{mungers} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 6 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | } ## end sub execute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | Algorithm::Classifier::IsolationForest::App::Command::info - Show the constructor params, fit-time metadata, and tree stats of a saved model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | Prints what a saved model is: the constructor parameters it was built | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | with, the metadata recorded at fit time, its schema version and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | description, any munger and feature-name tags, and the shape of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | forest itself -- node and leaf counts, maximum and mean depth. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | Batch and online models have different parameter sets and tree shapes, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | each gets its own report; the format tag stored in the file decides which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | one runs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | Run it as C<iforest info>; C<iforest help info> lists every option. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | L<App::Cmd> calls these while dispatching the subcommand. Nothing else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | should. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | =head2 opt_spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | Returns this command's option specifications, as the list of arrayrefs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | L<Getopt::Long::Descriptive> expects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | =head2 abstract | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | Returns the one-line summary C<iforest commands> prints beside the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | command name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | =head2 description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | Returns the long help text C<iforest help info> prints under the option | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | =head2 validate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | Checks the parsed options before anything is read or written, so a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | mistake costs nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | Checks that C<-m> names a readable file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | Takes the parsed options hashref and the arrayref of remaining | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | arguments. Calls C<usage_error>, which prints the usage and exits, on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | the first problem it finds, and returns 1 when everything checks out. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | =head2 execute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | Loads the model, dispatches on its stored format tag, and prints the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | report to STDOUT. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | Takes the parsed options hashref and the arrayref of remaining | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | arguments, and returns 1. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 86 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||