Skip to content

Commit

Permalink
Indices: simplify dependencies for RW turnover
Browse files Browse the repository at this point in the history
No need to calculate calc_abc for the phylo
variant.

This has the knock-on effect of needing to handle
arrays and hashes for the element_list[12] args.
The intent is to clean that up as part of issue #919.
  • Loading branch information
shawnlaffan committed Feb 22, 2024
1 parent 17a344d commit efd90ff
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions lib/Biodiverse/Indices/RWTurnover.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ no warnings 'experimental::refaliasing';

use Carp;
use List::Util qw /sum reduce/;
use Ref::Util qw /is_hashref/;

our $VERSION = '4.99_002';

Expand Down Expand Up @@ -65,12 +66,20 @@ sub calc_rw_turnover {
# or inverse of ranges
my $cache
= $self->get_cached_value_dor_set_default_href ('_calc_phylo_rwt_pairwise_branch_sum_cache');
# use postfix idiom?
# ideally we would only be passed arrays, but see issue #919
my $key1 = is_hashref ($args{element_list1})
? ((keys %{$args{element_list1}})[0])
: (${$args{element_list1}}[0]);
my $key2 = is_hashref ($args{element_list2})
? ((keys %{$args{element_list2}})[0])
: (${$args{element_list2} //[]}[0]);
# Could use a reduce call to collapse the "sum map {} @list" idiom,
# thus avoiding a list generation. These are only run once per group,
# though, so it might not matter.
my $sum_i = $cache->{(keys %{$args{element_list1}})[0]} # use postfix deref?
my $sum_i = $cache->{$key1}
//= (sum map {1 / $_} @ranges{keys %list1}) // 0;
my $sum_j = $cache->{(keys %{$args{element_list2}})[0]}
my $sum_j = $cache->{$key2}
//= (sum map {1 / $_} @ranges{keys %list2}) // 0;
# save some looping, mainly when there are large differences in key counts
if (keys %list1 <= keys %list2) {
Expand Down Expand Up @@ -121,7 +130,7 @@ sub get_metadata_calc_phylo_rw_turnover {
name => 'Phylo Range weighted Turnover',
reference => 'Laffan et al. (2016) https://doi.org/10.1111/2041-210X.12513',
type => 'Phylogenetic Turnover',
pre_calc => [qw /calc_abc _calc_pe_lists_per_element_set/],
pre_calc => [qw /_calc_pe_lists_per_element_set/],
# pre_calc_global => [qw /
# get_node_range_hash_as_lists
# get_trimmed_tree_parent_name_hash
Expand Down Expand Up @@ -164,9 +173,20 @@ sub calc_phylo_rw_turnover {
# simplify the calcs as we only need to find $aa
my $cache
= $self->get_cached_value_dor_set_default_href ('_calc_phylo_rwt_pairwise_branch_sum_cache');
my $sum_i = $cache->{(keys %{$args{element_list1}})[0]} # use postfix deref?
# use postfix idiom?
# ideally we would only be passed arrays, but see issue #919
my $key1 = is_hashref ($args{element_list1})
? ((keys %{$args{element_list1}})[0])
: (${$args{element_list1}}[0]);
my $key2 = is_hashref ($args{element_list2})
? ((keys %{$args{element_list2}})[0])
: (${$args{element_list2} //[]}[0]);
# Could use a reduce call to collapse the "sum map {} @list" idiom,
# thus avoiding a list generation. These are only run once per group,
# though, so it might not matter.
my $sum_i = $cache->{$key1}
//= (sum values %list1) // 0;
my $sum_j = $cache->{(keys %{$args{element_list2}})[0]}
my $sum_j = $cache->{$key2}
//= (sum values %list2) // 0;
# save some looping, mainly when there are large differences in key counts
if (keys %list1 <= keys %list2) {
Expand Down

0 comments on commit efd90ff

Please sign in to comment.