Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indices metadata - numeric bounds #894

Merged
merged 13 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions lib/Biodiverse/Indices.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use List::MoreUtils qw /uniq/;
use List::Util qw /sum/;
use English ( -no_match_vars );
use Ref::Util qw { :all };
use JSON::MaybeXS;

#use MRO::Compat;
use Class::Inspector;
Expand Down Expand Up @@ -96,7 +97,7 @@ sub get_calculations {

my %calculations;

my $list = Class::Inspector->methods( blessed $self);
my $list = Class::Inspector->methods( blessed $self );

foreach my $method ( grep { $_ =~ /^calc_/ } @$list ) {
next if $method =~ /calc_abc\d?$/; # skip calc_abc1,2&3
Expand Down Expand Up @@ -355,6 +356,36 @@ sub get_calculation_metadata_as_wiki {
return $html;
}

sub get_calculation_metadata {
my $self = shift;

if (!blessed $self) {
state $default_bd = Biodiverse::BaseData->new (
NAME => 'for indices',
CELL_SIZES => [1,1],
);
$self = __PACKAGE__->new (BASEDATA_REF => $default_bd);
}

my %calculations = $self->get_calculations_as_flat_hash();

my %calculation_hash;
foreach my $calc_sub ( sort keys %calculations ) {
my $ref = $self->get_metadata( sub => $calc_sub );
$calculation_hash{$calc_sub} = $ref;
}

return wantarray ? %calculation_hash : \%calculation_hash;
}

sub get_calculation_metadata_as_json {
my $self = shift;
my $metadata = $self->get_calculation_metadata;
my $json_obj = JSON::MaybeXS::JSON()->new;
$json_obj->convert_blessed(1);
return $json_obj->encode($metadata);
}

# now we have moved to github
sub get_calculation_metadata_as_markdown {
my $self = shift;
Expand Down Expand Up @@ -1303,8 +1334,7 @@ sub get_ratio_indices {
foreach my $calculations ( keys %$list ) {
my $meta = $self->get_metadata( sub => $calculations );
INDEX:
foreach my $index ( keys %{ $meta->get_indices } ) {
next INDEX if !$meta->get_index_is_ratio($index);
foreach my $index ( grep {$meta->get_index_is_ratio($_)} keys %{ $meta->get_indices } ) {
$indices{$index} = $meta->get_index_description($index);
}
}
Expand Down Expand Up @@ -1348,6 +1378,32 @@ sub index_is_divergent {
return !!$hash->{$args{index} // ''};
}

sub get_index_bounds {
my ($self, %args) = @_;
my $index = $args{index};

# return undef if !$self->index_is_scalar(index => $index);

my $index_source = $self->get_index_source(index => $index);
my $meta = $self->get_metadata( sub => $index_source );

my $bounds = $meta->get_index_bounds ($index);

return $bounds;
}

sub index_distribution_is_valid {
my $self = shift;
my %args = @_;
my $index = $args{index};

my $index_source = $self->get_index_source(index => $index);
my $meta = $self->get_metadata( sub => $index_source );

return $meta->index_distribution_is_valid ($index);
}


sub get_valid_calculations_to_run {
my $self = shift;

Expand Down
28 changes: 20 additions & 8 deletions lib/Biodiverse/Indices/Endemism.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sub get_metadata_calc_endemism_central_normalised {
calc_elements_used
}],
uses_nbr_lists => 1, # how many sets of lists it must have
distribution => 'nonnegative',
indices => {
ENDC_CWE_NORM => {
description => 'Corrected weighted endemism normalised by groups',
Expand Down Expand Up @@ -72,16 +73,18 @@ sub get_metadata_calc_endemism_whole_normalised {
uses_nbr_lists => 1, # how many sets of lists it must have
indices => {
ENDW_CWE_NORM => {
description => 'Corrected weighted endemism normalised by groups',
formula => [
description => 'Corrected weighted endemism normalised by groups',
formula => [
'= \frac{ENDW\_CWE}{EL\_COUNT\_ALL}',
],
distribution => 'unit_interval',
},
ENDW_WE_NORM => {
description => 'Weighted endemism normalised by groups',
formula => [
'= \frac{ENDW\_WE}{EL\_COUNT\_ALL}',
],
distribution => 'nonnegative',
},
},
); # add to if needed
Expand Down Expand Up @@ -153,13 +156,15 @@ sub get_metadata_calc_endemism_central {
pre_calc => [qw /_calc_endemism_central/],
reference => $ref,
uses_nbr_lists => 1, # how many sets of lists it must have
distribution => 'nonnegative',
indices => {
ENDC_CWE => {
description => 'Corrected weighted endemism',
lumper => 0,
formula => [
'= \frac{ENDC\_WE}{ENDC\_RICHNESS}',
],
distribution => 'unit_interval',
},
ENDC_WE => {
description => 'Weighted endemism',
Expand Down Expand Up @@ -547,12 +552,14 @@ sub get_metadata_calc_endemism_whole {
type => 'Endemism',
pre_calc => '_calc_endemism_whole',
uses_nbr_lists => 1, # how many sets of lists it must have
distribution => 'nonnegative',
indices => {
ENDW_CWE => {
description => 'Corrected weighted endemism',
formula => [
'= \frac{ENDW\_WE}{ENDW\_RICHNESS}',
],
distribution => 'unit_interval',
},
ENDW_WE => {
description => 'Weighted endemism',
Expand Down Expand Up @@ -597,7 +604,7 @@ sub get_metadata_calc_endemism_whole {
't',
' across the data set (the number of groups it is found in, '
. 'unless the range is specified at import).'
]
],
},
},
);
Expand Down Expand Up @@ -771,12 +778,13 @@ sub get_metadata_calc_endemism_absolute_lists {
type => 'Endemism',
pre_calc => ['_calc_endemism_absolute'],
uses_nbr_lists => 1, # how many sets of lists it must have
distribution => 'nonnegative',
indices => {
END_ABS1_LIST => {
END_ABS1_LIST => {
description => 'List of labels entirely endemic to neighbour set 1',
type => 'list',
},
END_ABS2_LIST => {
END_ABS2_LIST => {
description => 'List of labels entirely endemic to neighbour set 1',
type => 'list',
},
Expand Down Expand Up @@ -811,6 +819,7 @@ sub get_metadata_calc_endemism_absolute {
type => 'Endemism',
pre_calc => ['_calc_endemism_absolute'],
uses_nbr_lists => 1, # how many sets of lists it must have
distribution => 'nonnegative',
indices => {
END_ABS1 => {
description => 'Count of labels entirely endemic to neighbour set 1',
Expand All @@ -822,13 +831,16 @@ sub get_metadata_calc_endemism_absolute {
description => 'Count of labels entirely endemic to neighbour sets 1 and 2 combined',
},
END_ABS1_P => {
description => 'Proportion of labels entirely endemic to neighbour set 1',
description => 'Proportion of labels entirely endemic to neighbour set 1',
distribution => 'unit_interval',
},
END_ABS2_P => {
description => 'Proportion of labels entirely endemic to neighbour set 2',
description => 'Proportion of labels entirely endemic to neighbour set 2',
distribution => 'unit_interval',
},
END_ABS_ALL_P => {
description => 'Proportion of labels entirely endemic to neighbour sets 1 and 2 combined',
description => 'Proportion of labels entirely endemic to neighbour sets 1 and 2 combined',\
distribution => 'unit_interval',
},
},
); # add to if needed
Expand Down
2 changes: 1 addition & 1 deletion lib/Biodiverse/Indices/GroupProperties.pm
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ sub get_metadata_calc_gpprop_gistar {
GPPROP_GISTAR_LIST => {
description => 'List of Gi* scores',
type => 'list',
is_zscore => 1,
distribution => 'zscore',
},
},
);
Expand Down
Loading