PDL-Algorithm-Center
view release on metacpan or search on metacpan
lib/PDL/Algorithm/Center.pm view on Meta::CPAN
package PDL::Algorithm::Center;
# ABSTRACT: Various methods of finding the center of a sample
use strict;
use warnings;
require v5.10;
use feature 'state';
our $VERSION = '0.15';
use Carp;
use Try::Tiny;
use Safe::Isa;
use Ref::Util qw< is_arrayref is_ref is_coderef is_hashref >;
use Hash::Wrap ( { -as => '_wrap_hash' } );
use PDL::Algorithm::Center::Failure ':all';
use PDL::Algorithm::Center::Types -all;
use Types::Standard qw[ Optional ArrayRef Undef CodeRef Num ];
use Types::Common::Numeric -types;
use Type::Params qw[ compile_named ];
use PDL::Lite ();
use Exporter 'import';
our @EXPORT_OK = qw[ sigma_clip iterate ];
# as of Types::Standard 1.003_003, Bool coerces any value into a boolean,
# which we don't want.
use constant Bool => Types::Standard::Bool->no_coercions();
sub _weighted_mean_center {
my ( $coords, $mask, $weight, $total_weight ) = @_;
my $wmask = $mask * $weight;
$total_weight //= $wmask->dsum;
iteration_empty_failure->throw( 'weighted mean center: all elements excluded or sum(weight) == 0' )
if $total_weight == 0;
return ( $coords * $wmask->dummy( 0 ) )->xchg( 0, 1 )->dsumover / $total_weight;
}
sub _distance {
my ( $prev, $current ) = @_;
return sqrt( ( ( $prev->center - $current->center )**2 )->dsum );
}
sub _sigma_clip_initialize { ## no critic (Subroutines::ProhibitManyArgs)
my ( $init_clip, undef, $coords, $mask, $weight, $current, $work ) = @_;
# initialize the sigma_clip specific fields the docs promise will be there
$current->{dist} = undef;
$current->{clip} = $init_clip;
my $r2 = $work->{r2} = PDL->null;
$r2 .= ( ( $coords - $current->center )**2 )->dsumover;
$mask *= ( $r2 <= $init_clip**2 )
if defined $init_clip;
my $wmask = $work->{wmask} = $mask * $weight;
$current->total_weight( $wmask->dsum );
$current->nelem( $mask->sum );
if ( $current->total_weight == 0 ) {
( run in 0.754 second using v1.01-cache-2.11-cpan-39bf76dae61 )