Skip to content

Commit

Permalink
Indices: use pairwise calcs with abc2&3, centralise via a dispatcher
Browse files Browse the repository at this point in the history
The pairwise abc will make all the other calcs faster.

The dispatcher allows centralisation of all
the logic controlling which inner sub is used,
thus removing a lot of repetition.
  • Loading branch information
shawnlaffan committed Feb 21, 2024
1 parent cf18e67 commit 12b69e4
Showing 1 changed file with 31 additions and 46 deletions.
77 changes: 31 additions & 46 deletions lib/Biodiverse/Indices/Indices.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1626,26 +1626,8 @@ sub calc_abc { # wrapper for _calc_abc - use the other wrappers for actual GUI
my ($self, %args) = @_;

delete @args{qw/count_samples count_labels}/};
my $have_lb_lists = defined (
$args{label_hash1}
// $args{label_hash2}
// $args{label_list1}
// $args{label_list2}
);

return $self->_calc_abc_pairwise_mode(%args)
if $self->get_pairwise_mode
&& @{$args{element_list1} // []} == 1
&& @{$args{element_list2} // []} == 1
&& !$have_lb_lists;

return $self->_calc_abc(%args)
if is_hashref($args{element_list1})
|| @{$args{element_list1} // []} != 1
|| defined $args{element_list2}
|| $have_lb_lists;

return $self->_calc_abc_one_element(%args);
return $self->_calc_abc_dispatcher(%args);
}

sub get_metadata_calc_abc2 {
Expand All @@ -1662,22 +1644,11 @@ sub get_metadata_calc_abc2 {
return $metadata_class->new(\%metadata);
}

# run calc_abc, but keep a track of the label counts across groups
sub calc_abc2 {
# run calc_abc, but keep a track of the label counts across groups
my ($self, %args) = @_;

return $self->_calc_abc(%args, count_labels => 1)
if is_hashref($args{element_list1})
|| @{$args{element_list1} // []} != 1
|| defined(
$args{element_list2}
// $args{label_hash1}
// $args{label_hash2}
// $args{label_list1}
// $args{label_list2}
);
my $self = shift;

return $self->_calc_abc_one_element(%args, count_labels => 1);
return $self->_calc_abc_dispatcher(@_, count_labels => 1);
}

sub get_metadata_calc_abc3 {
Expand All @@ -1697,20 +1668,35 @@ sub get_metadata_calc_abc3 {

# run calc_abc, but keep a track of the label counts and samples across groups
sub calc_abc3 {
my $self = shift;

return $self->_calc_abc_dispatcher(@_, count_samples => 1);
}

# keep a lot of logic in one place
sub _calc_abc_dispatcher {
my ($self, %args) = @_;

return $self->_calc_abc(%args, count_samples => 1)
my $have_lb_lists = defined (
$args{label_hash1}
// $args{label_hash2}
// $args{label_list1}
// $args{label_list2}
);

return $self->_calc_abc_pairwise_mode(%args)
if $self->get_pairwise_mode
&& @{$args{element_list1} // []} == 1
&& @{$args{element_list2} // []} == 1
&& !$have_lb_lists;

return $self->_calc_abc(%args)
if is_hashref($args{element_list1})
|| @{$args{element_list1} // []} != 1
|| defined(
$args{element_list2}
// $args{label_hash1}
// $args{label_hash2}
// $args{label_list1}
// $args{label_list2}
);
|| defined $args{element_list2}
|| $have_lb_lists;

return $self->_calc_abc_one_element(%args, count_samples => 1);
return $self->_calc_abc_one_element(%args);
}

# A simplified version of _calc_abc for a single element.
Expand Down Expand Up @@ -1766,14 +1752,13 @@ sub _calc_abc_pairwise_mode {
my $count_labels = !$count_samples && $args{count_labels};

my (%label_hash1, %label_hash2);

my $cache = $self->get_cached_value_dor_set_default_href (
'_calc_abc_pairwise_mode_' . ($count_labels || '_') . ($count_samples || '_')
'_calc_abc_pairwise_mode_' . ($count_labels ? 2 : $count_samples ? 3 : 1)
);

if (!$cache->{$element1}) {
\my %labels = $self->get_basedata_ref->get_labels_in_group_as_hash_aa($element1);
if ($count_labels) {
if ($count_samples) {
%label_hash1 = %labels;
}
else {
Expand All @@ -1787,7 +1772,7 @@ sub _calc_abc_pairwise_mode {

if (!$cache->{$element2}) {
\my %labels = $self->get_basedata_ref->get_labels_in_group_as_hash_aa($element2);
if ($count_labels) {
if ($count_samples) {
%label_hash2 = %labels;
}
else {
Expand Down

0 comments on commit 12b69e4

Please sign in to comment.