gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 062dab9: Arithmetic: new operator to only keep


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 062dab9: Arithmetic: new operator to only keep unique elements in dataset
Date: Tue, 28 May 2019 16:05:20 -0400 (EDT)

branch: master
commit 062dab9d5f58b98f0dac16790d0779694989326a
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Arithmetic: new operator to only keep unique elements in dataset
    
    With this commit, the Arithmetic program has a new `unique' operator which
    will remove the duplicate (and blank) elements from the dataset and output
    a 1D array containing only the unique values.
---
 NEWS                        |  5 ++++
 bin/arithmetic/arithmetic.c | 59 +++++++++++++++++++++++++++++++++++++++++++++
 bin/arithmetic/arithmetic.h |  1 +
 doc/gnuastro.texi           |  9 +++++++
 4 files changed, 74 insertions(+)

diff --git a/NEWS b/NEWS
index a06fd7e..b8dcd39 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,11 @@ See the end of the file for license conditions.
 
 ** New features
 
+  Arithmetic:
+   - `unique' operator removes all duplicate (and blank) elements from the
+     dataset and returns a single-dimention output, containing only the
+     unique values in the dataset.
+
   CosmicCalculator:
    --obsline: alternative way to set the used redshift. With this option
      instead of explicity giving the redshift, you can give a rest-frame
diff --git a/bin/arithmetic/arithmetic.c b/bin/arithmetic/arithmetic.c
index 0c60cdb..dc74502 100644
--- a/bin/arithmetic/arithmetic.c
+++ b/bin/arithmetic/arithmetic.c
@@ -877,6 +877,59 @@ arithmetic_tofile(struct arithmeticparams *p, char *token, 
int freeflag)
 
 
 
+/* Pull out unique elements */
+#define UNIQUE_BYTYPE(TYPE) {                                           \
+    size_t i, j;                                                        \
+    TYPE *a=input->array, b;                                            \
+                                                                        \
+    /* Write the blank value for this type into `b'. */                 \
+    gal_blank_write(&b, input->type);                                   \
+                                                                        \
+    /* Go over the elements, and set the duplicates to blank. */        \
+    /* Note that for integers and floats, the behavior of blank/NaN */  \
+    /* differs: for floats (NaN), we can identify a blank using the  */ \
+    /* fact that by definition, NaN!=NaN. */                            \
+    if(b==b)                                                            \
+      for(i=0;i<input->size;++i)                                        \
+        { if(a[i]!=b)    for(j=i+1;j<input->size;++j) if(a[i]==a[j]) a[j]=b;} \
+    else                                                                \
+      for(i=0;i<input->size;++i)                                        \
+        { if(a[i]==a[i]) for(j=i+1;j<input->size;++j) if(a[i]==a[j]) a[j]=b;} \
+  }
+
+void
+arithmetic_unique(struct arithmeticparams *p, char *token, int operator)
+{
+  gal_data_t *input = operands_pop(p, token);
+
+  /* Remove the duplicates based on size. */
+  switch(input->type)
+    {
+    case GAL_TYPE_UINT8:   UNIQUE_BYTYPE( uint8_t  ); break;
+    case GAL_TYPE_INT8:    UNIQUE_BYTYPE( int8_t   ); break;
+    case GAL_TYPE_UINT16:  UNIQUE_BYTYPE( uint16_t ); break;
+    case GAL_TYPE_INT16:   UNIQUE_BYTYPE( int16_t  ); break;
+    case GAL_TYPE_UINT32:  UNIQUE_BYTYPE( uint32_t ); break;
+    case GAL_TYPE_INT32:   UNIQUE_BYTYPE( int32_t  ); break;
+    case GAL_TYPE_UINT64:  UNIQUE_BYTYPE( uint64_t ); break;
+    case GAL_TYPE_INT64:   UNIQUE_BYTYPE( int64_t  ); break;
+    case GAL_TYPE_FLOAT32: UNIQUE_BYTYPE( float    ); break;
+    case GAL_TYPE_FLOAT64: UNIQUE_BYTYPE( double   ); break;
+    default:
+      error(EXIT_FAILURE, 0, "the `unique' operator doesn't support type "
+            "code `%u'", input->type);
+    }
+
+  /* Remove all blank elements. */
+  gal_blank_remove(input);
+
+  /* Clean up and add the collapsed dataset to the top of the operands. */
+  operands_add(p, NULL, input);
+}
+
+
+
+
 
 
 
@@ -1096,6 +1149,8 @@ reversepolish(struct arithmeticparams *p)
             { op=ARITHMETIC_OP_COLLAPSE_MEAN;         nop=0; }
           else if (!strcmp(token->v, "collapse-number"))
             { op=ARITHMETIC_OP_COLLAPSE_NUMBER;       nop=0; }
+          else if (!strcmp(token->v, "unique"))
+            { op=ARITHMETIC_OP_UNIQUE;                nop=0; }
           else
             error(EXIT_FAILURE, 0, "the argument \"%s\" could not be "
                   "interpretted as a file name, named dataset, number, or "
@@ -1199,6 +1254,10 @@ reversepolish(struct arithmeticparams *p)
                   arithmetic_collapse(p, token->v, op);
                   break;
 
+                case ARITHMETIC_OP_UNIQUE:
+                  arithmetic_unique(p, token->v, op);
+                  break;
+
                 default:
                   error(EXIT_FAILURE, 0, "%s: a bug! please contact us at "
                         "%s to fix the problem. The code %d is not "
diff --git a/bin/arithmetic/arithmetic.h b/bin/arithmetic/arithmetic.h
index 7132f41..d267a58 100644
--- a/bin/arithmetic/arithmetic.h
+++ b/bin/arithmetic/arithmetic.h
@@ -46,6 +46,7 @@ enum arithmetic_prog_operators
   ARITHMETIC_OP_COLLAPSE_MAX,
   ARITHMETIC_OP_COLLAPSE_MEAN,
   ARITHMETIC_OP_COLLAPSE_NUMBER,
+  ARITHMETIC_OP_UNIQUE,
 };
 
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 77080c4..137cfe1 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -13042,6 +13042,15 @@ Similar to @option{collapse-sum}, but the returned 
dataset will have the
 same numeric type as the input and will contain the maximum value for each
 pixel along the collapsed dimension.
 
address@hidden unique
+Remove all duplicate (and blank) elements from the first popped
+operand. The unique elements of the dataset will be stored in a
+single-dimensional dataset.
+
+Recall that by default, single-dimensional datasets are stored as a table
+column in the output. But you can use @option{--onedasimage} to store them
+as a single-dimensional FITS array/image.
+
 @item erode
 @cindex Erosion
 Erode the foreground pixels (with value @code{1}) of the input dataset



reply via email to

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