Skip to content

Commit

Permalink
Rand spatial curveball: more tests
Browse files Browse the repository at this point in the history
And try to use any defined spatial condition.
Let the parser handle the rest.
  • Loading branch information
shawnlaffan committed Nov 7, 2024
1 parent 31e93ad commit 1370ae7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/Biodiverse/Randomise/CurveBall.pm
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ END_PROGRESS_TEXT
$progress_bar->reset;

my (%sp_swap_list, @gps_with_nbrs);
if (my $sp_conditions = $args{spatial_condition_for_swap_pairs}) {
my $sp_conditions = $args{spatial_condition_for_swap_pairs};
if (defined $sp_conditions) {
my $sp_swapper = $self->get_spatial_output_for_label_allocation (
%args,
spatial_conditions_for_label_allocation => $sp_conditions,
Expand Down Expand Up @@ -178,7 +179,7 @@ END_PROGRESS_TEXT
@gps_with_nbrs = sort keys %sp_swap_list;
my $n_gps_w_nbrs = @gps_with_nbrs;
say "[Randomise] $n_gps_w_nbrs of $n_groups groups have swappable neighbours";
croak "No groups have neighbours, cannot swap labels with neighbours"
croak "[Randomise] Curveball spatial: No groups have neighbours, cannot swap labels with neighbours"
if !@gps_with_nbrs;
}
}
Expand Down
43 changes: 42 additions & 1 deletion t/28-Randomisation.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use 5.010;
use 5.036;
use strict;
use warnings;

Expand Down Expand Up @@ -146,6 +146,7 @@ sub test_rand_curveball_sp_cond {
};
my $i;
foreach my $bd (@$rand_bd_array) {
# $bd->save; # for debug
$i++;
my %collated_labels;
foreach my $gp (qw /3250000:3050000 3150000:2950000 3250000:2950000 3250000:2850000/) {
Expand All @@ -156,6 +157,45 @@ sub test_rand_curveball_sp_cond {
}
is \%collated_labels, $expected, "Curveball spatial: labels and counts for isolated subregion, rand bd $i";
}

# use one of the random basedatas to test some oddball spatial conditions that should throw errors
my $bd = $rand_bd_array->[0];
my %die_combos = (
Rando1 => 'sp_self_only()',
Rando2 => '0 ',
Rando3 => '0',
Rando4 => '0 # zero with comment',
);
foreach my $name (sort keys %die_combos) {
my $condition = $die_combos{$name};
my $rand = $bd->add_randomisation_output(name => $name);
ok dies {
$rand->run_analysis(
function => 'rand_curveball',
iterations => 1,
spatial_condition_for_swap_pairs => $condition,
)
}, qq{rand_curveball with condition "$condition" dies};
}

# use another of the random basedatas to test some oddball spatial conditions that should be ignored
$bd = $rand_bd_array->[1];
my %ignore_combos = (
Rando1i => ' ',
Rando2i => '# just a comment',
);
foreach my $name (sort keys %ignore_combos) {
my $condition = $ignore_combos{$name};
my $rand = $bd->add_randomisation_output(name => $name);
ok lives {
$rand->run_analysis(
function => 'rand_curveball',
iterations => 1,
spatial_condition_for_swap_pairs => $condition,
)
}, "rand_curveball with condition '$condition' lives";
}

}

sub test_rand_structured_richness_same {
Expand Down Expand Up @@ -202,6 +242,7 @@ sub test_rand_structured_richness_same {
iterations => 3,
seed => $prng_seed,
return_rand_bd_array => 1,
retain_outputs => 1,
%args,
);

Expand Down

0 comments on commit 1370ae7

Please sign in to comment.