Skip to content

Commit

Permalink
reduce calls to TreeNode->get_length
Browse files Browse the repository at this point in the history
The Tree class has a method to get a hash of lengths.
This is cached so later calls are very cheap.

By using this we can avoid many repeated calls
to the get_length method.  As fast as it is,
they can add up across randomisations.
  • Loading branch information
shawnlaffan committed Feb 28, 2024
1 parent 1bd027d commit cac1d78
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
14 changes: 7 additions & 7 deletions lib/Biodiverse/Indices/Phylogenetic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1783,14 +1783,14 @@ sub get_inverse_range_weighted_path_lengths {
my %args = @_;

my $tree = $args{tree_ref};
my $node_ranges = $args{node_range};

\my %node_ranges = $args{node_range};
\my %node_length_hash = $tree->get_node_length_hash;

my %range_weighted;

foreach my $node ($tree->get_node_refs) {
my $name = $node->get_name;
next if !$node_ranges->{$name};
$range_weighted{$name} = $node->get_length / $node_ranges->{$name};

foreach my $name (keys %node_length_hash) {
next if !$node_ranges{$name};
$range_weighted{$name} = $node_length_hash{$name} / $node_ranges{$name};
}

my %results = (inverse_range_weighted_node_lengths => \%range_weighted);
Expand Down
27 changes: 13 additions & 14 deletions lib/Biodiverse/Indices/PhylogeneticRelative.pm
Original file line number Diff line number Diff line change
Expand Up @@ -656,21 +656,20 @@ sub get_metadata_get_trimmed_tree_eq_branch_lengths_node_length_hash {
return $metadata_class->new(\%metadata);
}

# should just be a wrapper around Tree::get_node_length_hash
sub get_trimmed_tree_eq_branch_lengths_node_length_hash {
my $self = shift;
my %args = @_;

my $tree_ref = $args{TREE_REF_EQUALISED_BRANCHES_TRIMMED}
// croak 'Missing TREE_REF_EQUALISED_BRANCHES_TRIMMED arg';
my $node_hash = $tree_ref->get_node_hash;

my (%len_hash, $nonzero_length);
foreach my $node_name (keys %$node_hash) {
my $node_ref = $node_hash->{$node_name};
my $length = $node_ref->get_length;
$len_hash{$node_name} = $length;
$nonzero_length ||= $length;

\my %len_hash = $tree_ref->get_node_length_hash;

my $nonzero_length;

foreach my $len (values %len_hash) {
$nonzero_length ||= $len;
last if $len;
}

my %results = (
Expand Down Expand Up @@ -737,14 +736,14 @@ sub get_trimmed_tree_range_inverse_hash_nonzero_len {
my %args = @_;

my $tree = $args{trimmed_tree};
my $node_ranges = $args{node_range};
\my %node_ranges = $args{node_range};
\my %length_hash = $tree->get_node_length_hash;

my %range_weighted;

foreach my $name (keys %$node_ranges) {
my $range = $node_ranges->{$name} || next;
my $numerator = $tree->get_node_ref_aa($name)->get_length ? 1 : 0;
$range_weighted{$name} = $numerator / $range;
foreach my $name (keys %node_ranges) {
my $range = $node_ranges{$name} || next;
$range_weighted{$name} = ($length_hash{$name} ? 1 : 0) / $range;
}

my %results = (trimmed_tree_range_inverse_hash_nonzero_len => \%range_weighted);
Expand Down
17 changes: 7 additions & 10 deletions lib/Biodiverse/Tree.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3095,20 +3095,17 @@ sub clone_tree_with_equalised_branch_lengths {

my $name = $args{name} // ( $self->get_param('NAME') . ' EQ' );

my $non_zero_len = $args{node_length};
my $non_zero_len = $args{node_length}
// ($self->get_total_tree_length / ( $self->get_nonzero_length_count || 1 ));

if ( !defined $non_zero_len ) {
# my $non_zero_node_count = grep { $_->get_length } $self->get_node_refs;
# this caches
my $non_zero_node_count = $self->get_nonzero_length_count;
$non_zero_len =
$self->get_total_tree_length / ( $non_zero_node_count || 1 );
}
\my %orig_node_length_hash = $self->get_node_length_hash;

my $new_tree = $self->clone_without_caches;
\my %new_node_hash = $new_tree->get_node_hash;

foreach my $node ( $new_tree->get_node_refs ) {
$node->set_length_aa ( $node->get_length ? $non_zero_len : 0 );
foreach my $name ( keys %new_node_hash ) {
my $node = $new_node_hash{$name};
$node->set_length_aa ( $orig_node_length_hash{$name} ? $non_zero_len : 0 );
}
$new_tree->rename( new_name => $name );

Expand Down

0 comments on commit cac1d78

Please sign in to comment.