gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 0dd7bcc0 1/2: Statistics: does not crash when


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 0dd7bcc0 1/2: Statistics: does not crash when all inputs are NaN
Date: Tue, 19 Mar 2024 13:46:46 -0400 (EDT)

branch: master
commit 0dd7bcc0f3a3b2709eac20bb4f89acaa167e13da
Author: Thorsten Alteholz <thorsten@alteholz.dev>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    Statistics: does not crash when all inputs are NaN
    
    Until now, when input data for the Statistics program is all NAN, it would
    abort with an error of not having any data. This is not a desired behaviour
    because complicates processing within complex pipelines. When there is no
    input, the output should be NaN.
    
    With this commit, an all NAN (or empty) input will no longer result in an
    error, but just shows a warning which the user can avoid by using the
    '--quiet' option. Any resulting statistic (like '--median' or '--std') will
    also be returned as a NaN.
    
    This bug was reported by Raul Infante-Sainz.
    
    This fixes bug #63064.
---
 bin/statistics/statistics.c | 26 +++++++++++++++-----------
 bin/statistics/ui.c         | 16 +++++++---------
 lib/blank.c                 |  6 ++++++
 lib/statistics.c            |  9 +++------
 4 files changed, 31 insertions(+), 26 deletions(-)

diff --git a/bin/statistics/statistics.c b/bin/statistics/statistics.c
index 59843bae..f01db3c0 100644
--- a/bin/statistics/statistics.c
+++ b/bin/statistics/statistics.c
@@ -1151,17 +1151,21 @@ print_basics(struct statisticsparams *p)
      range shown in the basic information section applies to that, not the
      range of the histogram. In that case, we want to print the histogram
      information. */
-  printf("-------");
-  range=set_bin_range_params(p, 1);
-  p->asciiheight = p->asciiheight ? p->asciiheight : 10;
-  p->numasciibins = p->numasciibins ? p->numasciibins : 70;
-  bins=gal_statistics_regular_bins(p->input, range, p->numasciibins, NAN);
-  hist=gal_statistics_histogram(p->input, bins, 0, 0);
-  printf("\nHistogram:\n");
-  print_ascii_plot(p, hist, bins, 1, 0);
-  gal_data_free(bins);
-  gal_data_free(hist);
-  gal_data_free(range);
+  printf("-------\nHistogram:\n");
+  if(p->input->size)
+    {
+      range=set_bin_range_params(p, 1);
+      p->asciiheight = p->asciiheight ? p->asciiheight : 10;
+      p->numasciibins = p->numasciibins ? p->numasciibins : 70;
+      bins=gal_statistics_regular_bins(p->input, range, p->numasciibins,
+                                       NAN);
+      hist=gal_statistics_histogram(p->input, bins, 0, 0);
+      print_ascii_plot(p, hist, bins, 1, 0);
+      gal_data_free(bins);
+      gal_data_free(hist);
+      gal_data_free(range);
+    }
+  else printf("  No data");
 }
 
 
diff --git a/bin/statistics/ui.c b/bin/statistics/ui.c
index e5c88f47..ac07a1b0 100644
--- a/bin/statistics/ui.c
+++ b/bin/statistics/ui.c
@@ -1233,17 +1233,15 @@ ui_preparations(struct statisticsparams *p)
       if(p->input->next) gal_blank_remove_rows(p->input, NULL, 0);
       else               gal_blank_remove(p->input);
 
-      /* Make sure there actually are any (non-blank) elements left. */
-      if(p->input->size==0)
-        error(EXIT_FAILURE, 0, "%s: all elements are blank",
+      /* If all elements are blank and the user has not asked for
+         Statistics to be quiet, then let the user know. */
+      if(!p->cp.quiet && p->input->size==0)
+        error(EXIT_SUCCESS, 0, "WARNING: %s: no usable (non-blank) data. "
+              "If there is data in the input, maybe the '--greaterequal' "
+              "or '--lessthan' options need to be adjusted. To remove "
+              "this warning, use the '--quiet' (or '-q') option",
               gal_fits_name_save_as_string(p->inputname, cp->hdu));
 
-      /* Make sure there is data remaining: */
-      if(p->input->size==0)
-        error(EXIT_FAILURE, 0, "%s: no data, maybe the '--greaterequal' "
-              "or '--lessthan' options need to be adjusted",
-              gal_fits_name_save_as_string(p->inputname, cp->hdu) );
-
       /* Make the sorted array if necessary. */
       ui_make_sorted_if_necessary(p);
 
diff --git a/lib/blank.c b/lib/blank.c
index d7386567..dbf1c115 100644
--- a/lib/blank.c
+++ b/lib/blank.c
@@ -1041,6 +1041,9 @@ gal_blank_flag_remove(gal_data_t *input, gal_data_t *flag)
   /* Adjust the size elements of the dataset. */
   input->ndim=1;
   input->dsize[0]=input->size=num;
+
+  /* When there are no non-blank elements, free the array. */
+  if(num==0 && input->array) { free(input->array); input->array=NULL; }
 }
 
 
@@ -1189,6 +1192,9 @@ gal_blank_remove(gal_data_t *input)
   input->ndim=1;
   input->dsize[0]=input->size=num;
 
+  /* When there are no non-blank elements, free the array. */
+  if(num==0 && input->array) { free(input->array); input->array=NULL; }
+
   /* Set the flags to mark that there is no blanks. */
   input->flag |=  GAL_DATA_FLAG_BLANK_CH;
   input->flag &= ~GAL_DATA_FLAG_HASBLANK;
diff --git a/lib/statistics.c b/lib/statistics.c
index 3593ad58..73ac061d 100644
--- a/lib/statistics.c
+++ b/lib/statistics.c
@@ -1557,11 +1557,9 @@ gal_statistics_is_sorted(gal_data_t *input, int 
updateflags)
   /* Parse the array (if necessary). */
   switch(input->size)
     {
+    /* A 0 or one-element dataset can be considered, sorted, so we'll say
+       its increasing. */
     case 0:
-      error(EXIT_FAILURE, 0, "%s: input dataset has 0 elements", __func__);
-
-    /* A one-element dataset can be considered, sorted, so we'll say its
-       increasing. */
     case 1:
       out=STATISTICS_IS_SORTED_INCREASING;
       break;
@@ -1876,8 +1874,7 @@ gal_statistics_regular_bins(gal_data_t *input, gal_data_t 
*inrange,
   if(numbins==0)
     error(EXIT_FAILURE, 0, "%s: 'numbins' cannot be given a value of 0",
           __func__);
-  if(input->size==0)
-    error(EXIT_FAILURE, 0, "%s: input's size is 0", __func__);
+  if(input->size==0) return NULL;
 
 
   /* Set the minimum and maximum values. */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]