gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master fc65b4e9: Convolve: correctly accounts for sin


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master fc65b4e9: Convolve: correctly accounts for single element kernels
Date: Tue, 4 Jun 2024 07:01:28 -0400 (EDT)

branch: master
commit fc65b4e9e67b94c2bf4b183c3ad995781d7e8c81
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    Convolve: correctly accounts for single element kernels
    
    Until now, when a single-pixel kernel was given to Convolve, it would
    crash by complaining about non-equal dimensions of inputs, even though the
    single-pixel kernel was actually 2 dimensional!
    
    With this commit, the root of the problem was fixed in the library function
    'gal_dimension_remove_extra'. Also, the error message in Convolve has been
    edited to be more clear.
    
    This fixes bug #65833.
---
 NEWS              |  2 ++
 bin/convolve/ui.c |  5 +++--
 doc/gnuastro.texi |  2 ++
 lib/dimension.c   | 41 ++++++++++++++++++++++++++---------------
 4 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/NEWS b/NEWS
index ebc9ca7c..391c4d26 100644
--- a/NEWS
+++ b/NEWS
@@ -203,6 +203,8 @@ See the end of the file for license conditions.
 
   - bug #65822: MakeProfiles does not write negative values with --replace.
 
+  - bug #65833: Convolve crashes when the kernel only has a single element.
+
 
 
 
diff --git a/bin/convolve/ui.c b/bin/convolve/ui.c
index cc12690c..6852f8ad 100644
--- a/bin/convolve/ui.c
+++ b/bin/convolve/ui.c
@@ -493,8 +493,9 @@ ui_read_kernel(struct convolveparams *p)
   /* Make sure that the kernel and input have the same number of
      dimensions. */
   if(p->kernel->ndim!=p->input->ndim)
-    error(EXIT_FAILURE, 0, "input datasets must have the same number of "
-          "dimensions");
+    error(EXIT_FAILURE, 0, "input and kernel must have the same number of "
+          "dimensions, but they have %zu and %zu dimensions respectively",
+          p->input->ndim, p->kernel->ndim);
 }
 
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 530de36a..c5754b8c 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -38612,6 +38612,8 @@ Similar to @code{gal_dimension_collapse_mclip_number}, 
but with filled re-clippi
 
 @deftypefun size_t gal_dimension_remove_extra (size_t @code{ndim}, size_t 
@code{*dsize}, struct wcsprm @code{*wcs})
 Remove extra dimensions (those that only have a length of 1) from the basic 
size information of a dataset.
+If the total number of elements (in all dimensions) is 1, this function will 
not remove anything (because an ``extra'' is only meaningful when some 
dimensions have more than one element length).
+
 @code{ndim} is the number of dimensions and @code{dsize} is an array with 
@code{ndim} elements containing the size along each dimension in the C 
dimension order.
 When @code{wcs!=NULL}, the respective dimension will also be removed from the 
WCS.
 
diff --git a/lib/dimension.c b/lib/dimension.c
index 1eb36862..c711a6ff 100644
--- a/lib/dimension.c
+++ b/lib/dimension.c
@@ -1802,21 +1802,32 @@ gal_dimension_collapse_sclip_fill_number(gal_data_t 
*in, size_t c_dim,
 size_t
 gal_dimension_remove_extra(size_t ndim, size_t *dsize, struct wcsprm *wcs)
 {
-  size_t i, j;
-
-  for(i=0;i<ndim;++i)
-    if(dsize[i]==1)
-      {
-        /* Correct the WCS. */
-        if(wcs) gal_wcs_remove_dimension(wcs, ndim-i);
-
-        /* Shift all subsequent dimensions to replace this one. */
-        for(j=i;j<ndim-1;++j) dsize[j]=dsize[j+1];
-
-        /* Decrement the 'i' and the total number of dimension. */
-        --i;
-        --ndim;
-      }
+  size_t i, j, size=1;
+
+  /* When there is only one element in the dataset, "extra" dimensions are
+     not defined. This is because we define "extra" by dimensions of length
+     1, so when there is only a single element everything will be "extra"
+     and the function will return 0 dimensions!  See
+     https://savannah.gnu.org/bugs/index.php?65833 for example.  Therefore,
+     the dimensions should be preserved and this function should not do
+     anything. */
+  for(i=0;i<ndim;++i) size*=dsize[i];
+  if(size>1)
+    {
+      for(i=0;i<ndim;++i)
+        if(dsize[i]==1)
+          {
+            /* Correct the WCS. */
+            if(wcs) gal_wcs_remove_dimension(wcs, ndim-i);
+
+            /* Shift all subsequent dimensions to replace this one. */
+            for(j=i;j<ndim-1;++j) dsize[j]=dsize[j+1];
+
+            /* Decrement the 'i' and the total number of dimension. */
+            --i;
+            --ndim;
+          }
+    }
 
   /* Return the number of dimensions. */
   return ndim;



reply via email to

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