Skip to content

Commit

Permalink
Randomisations: make sure group props are handled in spatial subsets
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnlaffan committed Nov 12, 2024
1 parent f2136a4 commit abe3f48
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
34 changes: 21 additions & 13 deletions lib/Biodiverse/Randomise.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2316,24 +2316,28 @@ sub get_rand_structured_subset {
allow_empty_groups => 1,
);
}
$bd->transfer_group_properties (
receiver => $subset_bd,
);
# tests dont trigger index-related errors,
# but we need to play safe nonetheless
$subset_bd->rebuild_spatial_index;
$cached_subset_basedatas->{$group} = $subset_bd;
}

$self->process_group_props (
orig_bd => $bd,
rand_bd => $subset_bd,
function => $args{randomise_group_props_by},
rand_object => $rand_object,
);
# say STDERR "Adding rand ", $self->get_name, " to basedata ", $subset_bd->get_name;
my $subset_rand = $subset_bd->add_randomisation_output (name => $self->get_name);
my $subset_rand_bd = $subset_rand->$rand_function (
%args,
rand_object => $rand_object,
basedata_ref => $subset_bd,
);
$self->process_group_props (
orig_bd => $subset_bd,
rand_bd => $subset_rand_bd,
function => $args{randomise_group_props_by},
rand_object => $rand_object,
);

my $gps = $subset_bd->get_groups;
@done{@$gps} = (1) x scalar @$gps;
Expand All @@ -2356,7 +2360,7 @@ sub get_rand_structured_subset {
if ($to_do != scalar keys %done) {
my $group = '__leftovers__';
while ($bd->exists_group_aa($group)) {
# do not clash with existing group name
# do not clash with pre-existing group name
$group .= '!';
}
my $subset_bd = $cached_subset_basedatas->{$group};
Expand All @@ -2374,24 +2378,28 @@ sub get_rand_structured_subset {
allow_empty_groups => 1,
);
}
$bd->transfer_group_properties (
receiver => $subset_bd,
);
# tests don't trigger index-related errors,
# but we need to play safe nonetheless
$subset_bd->rebuild_spatial_index;
$cached_subset_basedatas->{$group} = $subset_bd;
}

$self->process_group_props (
orig_bd => $bd,
rand_bd => $subset_bd,
function => $args{randomise_group_props_by},
rand_object => $rand_object,
);
my $subset_rand = $subset_bd->add_randomisation_output (name => $self->get_name);
my $subset_rand_bd = $subset_rand->$rand_function (
%args,
rand_object => $rand_object,
basedata_ref => $subset_bd,
);
$self->process_group_props (
orig_bd => $subset_bd,
rand_bd => $subset_rand_bd,
function => $args{randomise_group_props_by},
rand_object => $rand_object,
);

say 'Merging basedata ' . $subset_rand_bd->get_name . ' into ' . $new_bd->get_name;
$new_bd->merge (from => $subset_rand_bd);

Expand Down
13 changes: 12 additions & 1 deletion t/28-Randomisation.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use FindBin qw/$Bin/;
use rlib;
use List::Util qw /first sum0/;
use List::MoreUtils qw /any_u/;
use Ref::Util qw /is_arrayref/;

use Test2::V0;
use Test::Deep::NoTest qw/eq_deeply/;
Expand Down Expand Up @@ -1138,13 +1139,23 @@ sub test_group_properties_reassigned {
by_set => 1,
by_item => 1,
);

use experimental qw /for_list/;
my $subset_sp_cond = $args{spatial_conditions_for_subset} // [];
if (!is_arrayref $subset_sp_cond) {
$subset_sp_cond = [$subset_sp_cond];
}
if (!@$subset_sp_cond) {
$subset_sp_cond = undef;
}

while (my ($props_func, $negate_expected) = each %prop_handlers) {
for my ($props_func, $negate_expected) (%prop_handlers) {

my $rand_name = 'r' . $object_name . $props_func;

my $rand = $bd->add_randomisation_output (name => $rand_name);
my $rand_bd_array = $rand->run_analysis (
spatial_conditions_for_subset => $subset_sp_cond,
function => $rand_func,
iterations => 1,
retain_outputs => 1,
Expand Down

0 comments on commit abe3f48

Please sign in to comment.