From 900dc7590f799dd793a8b0bc0c6111bcafc06b7a Mon Sep 17 00:00:00 2001 From: shawnlaffan Date: Tue, 13 Feb 2024 12:31:53 +1100 Subject: [PATCH] Randomisations: Disable GUI progress under "fast" processing 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. --- lib/Biodiverse/BaseData.pm | 4 ++- lib/Biodiverse/Randomise.pm | 36 +++++++++++++++----- lib/Biodiverse/Randomise/CurveBall.pm | 2 +- lib/Biodiverse/Randomise/IndependentSwaps.pm | 4 +-- lib/Biodiverse/Spatial.pm | 18 ++++++---- 5 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lib/Biodiverse/BaseData.pm b/lib/Biodiverse/BaseData.pm index b0c140eef..de52dae31 100644 --- a/lib/Biodiverse/BaseData.pm +++ b/lib/Biodiverse/BaseData.pm @@ -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"; diff --git a/lib/Biodiverse/Randomise.pm b/lib/Biodiverse/Randomise.pm index bd6a5e96c..36cab80f4 100644 --- a/lib/Biodiverse/Randomise.pm +++ b/lib/Biodiverse/Randomise.pm @@ -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) { @@ -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), ); @@ -603,6 +611,11 @@ 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, @@ -610,6 +623,7 @@ sub run_randomisation { object => $rand_analysis, rand_object => $rand_object, iteration => $$total_iterations, + %prog_args, ); }; croak $EVAL_ERROR if $EVAL_ERROR; @@ -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; @@ -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 @@ -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}; @@ -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 diff --git a/lib/Biodiverse/Randomise/CurveBall.pm b/lib/Biodiverse/Randomise/CurveBall.pm index 021469ed4..1063dd160 100644 --- a/lib/Biodiverse/Randomise/CurveBall.pm +++ b/lib/Biodiverse/Randomise/CurveBall.pm @@ -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 diff --git a/lib/Biodiverse/Randomise/IndependentSwaps.pm b/lib/Biodiverse/Randomise/IndependentSwaps.pm index 09a472b2f..f8f280ada 100644 --- a/lib/Biodiverse/Randomise/IndependentSwaps.pm +++ b/lib/Biodiverse/Randomise/IndependentSwaps.pm @@ -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 @@ -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 diff --git a/lib/Biodiverse/Spatial.pm b/lib/Biodiverse/Spatial.pm index 11a9121b9..f9fb567ed 100644 --- a/lib/Biodiverse/Spatial.pm +++ b/lib/Biodiverse/Spatial.pm @@ -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'), @@ -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); @@ -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; @@ -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): ";