gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 92e2f36 1/2: Warning (and blank output) for qu


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 92e2f36 1/2: Warning (and blank output) for quantile with size of zero
Date: Tue, 6 Jun 2017 08:53:20 -0400 (EDT)

branch: master
commit 92e2f36711c0ddadfdf576eaf86ae04cb26169fb
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Warning (and blank output) for quantile with size of zero
    
    When the size of an input dataset is 0 (can happen in automatic
    conditions), the quantile is not defined. With this commit, in such cases,
    the `gal_statistics_quantile_index' function will print a warning and
    `gal_statistics_quantile' (which will call `gal_statistics_quantile_index'
    and thus print its warning) will return a blank value of the given dataset.
---
 lib/statistics.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/lib/statistics.c b/lib/statistics.c
index 8db333e..7e77964 100644
--- a/lib/statistics.c
+++ b/lib/statistics.c
@@ -289,6 +289,13 @@ gal_statistics_quantile_index(size_t size, double quantile)
 {
   double floatindex;
 
+  /* Some sanity checks. */
+  if(size==0)
+    {
+      error(0, 0, "%s: `size' is 0. The quantile is not defined for "
+              "a zero-sized array\n", __func__);
+      return GAL_BLANK_SIZE_T;
+    }
   if(quantile<0.0f || quantile>1.0f)
     error(EXIT_FAILURE, 0, "%s: the input quantile should be between 0.0 "
           "and 1.0 (inclusive). You have asked for %g", __func__, quantile);
@@ -316,6 +323,7 @@ gal_statistics_quantile_index(size_t size, double quantile)
 gal_data_t *
 gal_statistics_quantile(gal_data_t *input, double quantile, int inplace)
 {
+  void *blank;
   size_t dsize=1, index;
   gal_data_t *nbs=gal_statistics_no_blank_sorted(input, inplace);
   gal_data_t *out=gal_data_alloc(NULL, nbs->type, 1, &dsize,
@@ -325,8 +333,15 @@ gal_statistics_quantile(gal_data_t *input, double 
quantile, int inplace)
   index=gal_statistics_quantile_index(nbs->size, quantile);
 
   /* Write the value at this index into the output. */
-  memcpy(out->array, gal_data_ptr_increment(nbs->array, index, nbs->type),
-         gal_type_sizeof(nbs->type));
+  if(index==GAL_BLANK_SIZE_T)
+    {
+      blank=gal_data_malloc_array(nbs->type, 1, __func__, "blank");
+      memcpy(out->array, blank, gal_type_sizeof(nbs->type));
+      free(blank);
+    }
+  else
+    memcpy(out->array, gal_data_ptr_increment(nbs->array, index, nbs->type),
+           gal_type_sizeof(nbs->type));
 
   /* Clean up and return. */
   if(nbs!=input) gal_data_free(nbs);



reply via email to

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