Algorithm-Classifier-IsolationForest
view release on metacpan or search on metacpan
t/41-mungers.t view on Meta::CPAN
n_trees => 40,
sample_size => 64,
contamination => 0.05,
feature_names => [@TAGS],
mungers => mungers(),
);
$f->fit_tagged( raw_rows(200) );
my $before = $f->score_sample_tagged( {%WEIRD} );
my $path = "$dir/munged_model.json";
$f->save($path);
my $re = $batch_class->load($path);
is_deeply( $re->{mungers}, mungers(), 'munger spec survived the round trip' );
is(
$re->{munger_module_version},
$Algorithm::ToNumberMunger::VERSION,
'munger module version recorded with the model'
);
ok( !$re->{_munger_plan}, 'plan is not compiled at load time (lazy)' );
my $after = $re->score_sample_tagged( {%WEIRD} );
cmp_ok( abs( $after - $before ), '<', 1e-12, 'reloaded model munges and scores identically' );
ok( $re->{_munger_plan}, 'plan compiled lazily on first tagged use' );
# A spec naming a munger the installed module lacks croaks lazily,
# with a message naming it -- the stale-module failure mode.
my $broken = $batch_class->load($path);
$broken->{mungers}{method}{munger} = 'bogus_from_the_future';
ok( !eval { $broken->score_sample_tagged( {%WEIRD} ); 1 }, 'unknown munger in a loaded model croaks' );
like( $@, qr/unknown munger 'bogus_from_the_future'/, 'error names the unknown munger' );
}; ## end 'persistence round trip (batch)' => sub
subtest 'Online: learn_tagged batches, prequential scoring, persistence' => sub {
my $dir = tempdir( CLEANUP => 1 );
srand(10);
my $m = $online_class->new(
seed => 42,
n_trees => 40,
window_size => 128,
max_leaf_samples => 16,
feature_names => [@TAGS],
mungers => mungers(),
);
my $ret = $m->learn_tagged( raw_rows(200) );
is( $ret, $m, 'learn_tagged (batch form) chains' );
is( $m->seen, 200, 'batch form learned every row' );
$m->learn_tagged( { method => 'GET', path => '/one.html', host => 'www.example.com', bytes => 640 } );
is( $m->seen, 201, 'single-hashref form still works' );
my $normal = $m->score_sample_tagged( {%NORMAL} );
my $weird = $m->score_learn_tagged( {%WEIRD} );
cmp_ok( $weird, '>', $normal, 'raw anomalous row scores above a normal one prequentially' );
my $path = "$dir/oiforest_munged.json";
$m->save($path);
# Parent-class load dispatches AND keeps the munger spec.
my $re = Algorithm::Classifier::IsolationForest->load($path);
isa_ok( $re, $online_class, 'parent load() returns the online model' );
is_deeply( $re->{mungers}, mungers(), 'munger spec survived the online round trip' );
my $s1 = $m->score_sample_tagged( {%WEIRD} );
my $s2 = $re->score_sample_tagged( {%WEIRD} );
cmp_ok( abs( $s2 - $s1 ), '<', 1e-12, 'reloaded online model munges and scores identically' );
}; ## end 'Online: learn_tagged batches, prequential scoring, persistence' => sub
done_testing;
( run in 1.734 second using v1.01-cache-2.11-cpan-6aa56a78535 )