Skip to content

Commit

Permalink
Randomisations: Disable GUI progress under "fast" processing
Browse files Browse the repository at this point in the history
The progress dialogue takes a decent chunk of time
under profiling.  This is largely because it triggers
the main loop which then refreshes the display.

The threshold is currently set to one second which
should be good enough for most purposes.
  • Loading branch information
shawnlaffan committed Feb 13, 2024
1 parent ea697cf commit 900dc75
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
4 changes: 3 additions & 1 deletion lib/Biodiverse/BaseData.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,9 @@ sub transfer_element_properties {
my $to_name = $to_bd->get_param('NAME');
my $text = "Transferring $type properties from $name to $to_name";

my $progress_bar = Biodiverse::Progress->new();
my $progress_bar = Biodiverse::Progress->new(
no_gui_progress => $args{no_gui_progress},
);
my $total_to_do = $elements_ref->get_element_count;
print "[BASEDATA] Transferring properties for $total_to_do $type\n";

Expand Down
36 changes: 28 additions & 8 deletions lib/Biodiverse/Randomise.pm
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ sub run_randomisation {

my $progress_bar = Biodiverse::Progress->new(text => 'Randomisation');

my %progress_timers;
# arbitrary threshold but should be OK
my $no_gui_progress_thresh = 1;

# do stuff here
ITERATION:
foreach my $i (1 .. $iterations) {
Expand All @@ -530,22 +534,26 @@ sub run_randomisation {
"Randomisation iteration $i of $iterations this run",
($i / $iterations),
);

my $start_time_get_rand_bd = [gettimeofday];

my $rand_bd = eval {
$self->get_randomised_basedata (
%args,
rand_object => $rand_object,
rand_iter => $$total_iterations,
rand_function => $function,
rand_object => $rand_object,
rand_iter => $$total_iterations,
rand_function => $function,
no_gui_progress => (($progress_timers{gen_bd} // 1000) / $i < $no_gui_progress_thresh),
);
};
croak $EVAL_ERROR if $EVAL_ERROR || ! defined $rand_bd;

my $time_taken = sprintf "%.3f", tv_interval ($start_time_get_rand_bd);

my $t_diff = tv_interval ($start_time_get_rand_bd);
my $time_taken = sprintf "%.3f", $t_diff;
say "[RANDOMISE] Time taken to randomise basedata: $time_taken seconds";

$progress_timers{gen_bd} += $t_diff;

$rand_bd->rename (
name => join ('_', $bd->get_param ('NAME'), $function, $$total_iterations),
);
Expand Down Expand Up @@ -603,13 +611,19 @@ sub run_randomisation {
}

if ($generate_rand_analysis) {
my $start_time_analysis = [gettimeofday()];
my %prog_args = (
no_gui_progress => (($progress_timers{$target} // 1000) / $i < $no_gui_progress_thresh)
);

eval {
$self->override_object_analysis_args (
%args,
randomised_arg_object_cache => \%randomised_arg_object_cache,
object => $rand_analysis,
rand_object => $rand_object,
iteration => $$total_iterations,
%prog_args,
);
};
croak $EVAL_ERROR if $EVAL_ERROR;
Expand All @@ -618,6 +632,7 @@ sub run_randomisation {
$rand_analysis->run_analysis (
progress_text => $progress_text,
use_nbrs_from => $target,
%prog_args,
);
};
croak $EVAL_ERROR if $EVAL_ERROR;
Expand All @@ -626,9 +641,12 @@ sub run_randomisation {
$target->compare (
comparison => $rand_analysis,
result_list_name => $results_list_name,
%prog_args,
)
};
croak $EVAL_ERROR if $EVAL_ERROR;

$progress_timers{$target} += tv_interval ($start_time_analysis);
}

# Does nothing if not a cluster type analysis
Expand Down Expand Up @@ -1249,7 +1267,9 @@ sub rand_csr_by_group {

my $bd = $args{basedata_ref} || $self->get_param ('BASEDATA_REF');

my $progress_bar = Biodiverse::Progress->new();
my $progress_bar = Biodiverse::Progress->new(
no_gui_progress => $args{no_gui_progress},
);

# can't store MRMA objects to all output formats and then recreate
my $rand = delete $args{rand_object};
Expand Down Expand Up @@ -1614,7 +1634,7 @@ sub rand_structured {
);


my $progress_bar = Biodiverse::Progress->new();
my $progress_bar = Biodiverse::Progress->new(no_gui_progress => $args{no_gui_progress});

# need to get these from the ARGS param if available
# - should also croak if negative
Expand Down
2 changes: 1 addition & 1 deletion lib/Biodiverse/Randomise/CurveBall.pm
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ sub rand_curveball {
# calculated, and their ratio can be used as estimation for
# the proportion of the successful trials."

my $progress_bar = Biodiverse::Progress->new();
my $progress_bar = Biodiverse::Progress->new(no_gui_progress => $args{no_gui_progress});

my $progress_text =<<"END_PROGRESS_TEXT"
$name
Expand Down
4 changes: 2 additions & 2 deletions lib/Biodiverse/Randomise/IndependentSwaps.pm
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ sub rand_independent_swaps_modified {
# calculated, and their ratio can be used as estimation for
# the proportion of the successful trials."

my $progress_bar = Biodiverse::Progress->new();
my $progress_bar = Biodiverse::Progress->new(no_gui_progress => $args{no_gui_progress});

my $progress_text =<<"END_PROGRESS_TEXT"
$name
Expand Down Expand Up @@ -411,7 +411,7 @@ sub rand_independent_swaps {
# calculated, and their ratio can be used as estimation for
# the proportion of the successful trials."

my $progress_bar = Biodiverse::Progress->new();
my $progress_bar = Biodiverse::Progress->new(no_gui_progress => $args{no_gui_progress});

my $progress_text =<<"END_PROGRESS_TEXT"
$name
Expand Down
18 changes: 12 additions & 6 deletions lib/Biodiverse/Spatial.pm
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ sub compare {
my $e_list = $self->get_element_list;
return 1 if not scalar @$e_list;

my $progress = Biodiverse::Progress->new();
my $progress = Biodiverse::Progress->new(no_gui_progress => $args{no_gui_progress});
my $progress_text
= sprintf "Comparing %s with %s\n",
$self->get_param ('NAME'),
Expand Down Expand Up @@ -680,7 +680,9 @@ sub sp_calc {

# don't store this arg if specified
my $use_nbrs_from = $args{use_nbrs_from};
delete $args{use_nbrs_from};
delete $args{use_nbrs_from};

my $no_gui_progress = delete $args{no_gui_progress};

# flag for use if we drop out. Set to 1 on completion.
$self->set_param (COMPLETED => 0);
Expand Down Expand Up @@ -912,7 +914,10 @@ sub sp_calc {

my $progress_text_create
= $progress_text_base . "\nCreating target groups";
my $progress = Biodiverse::Progress->new(text => $progress_text_create);
my $progress = Biodiverse::Progress->new(
text => $progress_text_create,
no_gui_progress => $no_gui_progress,
);

my $failed_def_query_sp_res_hash = {};
my $elt_count = -1;
Expand Down Expand Up @@ -964,9 +969,10 @@ sub sp_calc {
"Spatial analysis\n$progress_text_base\n"
. "(0 / $to_do)"
. $using_index_text;
$progress = Biodiverse::Progress->new(text => $progress_text);

#$progress->update ($progress_text, 0);
$progress = Biodiverse::Progress->new(
text => $progress_text,
no_gui_progress => $no_gui_progress,
);

my ($count, $printed_progress) = (0, -1);
print "[SPATIAL] Progress (% of $to_do elements): ";
Expand Down

0 comments on commit 900dc75

Please sign in to comment.