PCL
WinsorizedSigmaClippingRejection.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/WinsorizedSigmaClippingRejection.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_WinsorizedSigmaClippingRejection_h
53 #define __PCL_WinsorizedSigmaClippingRejection_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Array.h>
61 #include <pcl/Math.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
81 {
82 public:
83 
88 
93 
98  {
99  }
100 
105 
111  bool ClipLow() const
112  {
113  return m_clipLow;
114  }
115 
121  bool ClipHigh() const
122  {
123  return m_clipHigh;
124  }
125 
130  void EnableLowClipping( bool enable = true )
131  {
132  m_clipLow = enable;
133  }
134 
139  void DisableLowClipping( bool disable = true )
140  {
141  EnableLowClipping( !disable );
142  }
143 
148  void EnableHighClipping( bool enable = true )
149  {
150  m_clipHigh = enable;
151  }
152 
157  void DisableHighClipping( bool disable = true )
158  {
159  EnableHighClipping( !disable );
160  }
161 
172  float LowSigma() const
173  {
174  return m_sigmaLow;
175  }
176 
187  float HighSigma() const
188  {
189  return m_sigmaHigh;
190  }
191 
196  void SetLowSigma( float sigma )
197  {
198  PCL_PRECONDITION( sigma >= 0 )
199  m_sigmaLow = Max( 0.0F, sigma );
200  }
201 
206  void SetHighSigma( float sigma )
207  {
208  PCL_PRECONDITION( sigma >= 0 )
209  m_sigmaHigh = Max( 0.0F, sigma );
210  }
211 
224  float WinsorizationCutoff() const
225  {
226  return m_cutoff;
227  }
228 
234  void SetWinsorizationCutoff( float cutoff )
235  {
236  PCL_PRECONDITION( cutoff >= 1 )
237  m_cutoff = Max( 1.0F, cutoff );
238  }
239 
289  template <class C, typename I>
290  void PerformRejection( I& i, I& j, double& mean, double& sigma, C& data ) const
291  {
292  data.Sort();
293  i = I( 0 );
294  j = I( data.Length() );
295  for ( int it = 0; ; ++it )
296  {
297  GetWinsorizedParameters( mean, sigma, i, j, data, (it > 0) ? .0F : m_cutoff );
298  if ( 1 + sigma == 1 )
299  return;
300  if ( I( j - i ) < I( 3 ) )
301  return;
302 
303  int c = 0;
304  if ( m_clipLow )
305  for ( ; i < j; ++i, ++c )
306  if ( (mean - data[i])/sigma <= m_sigmaLow )
307  break;
308  if ( m_clipHigh )
309  for ( ; i < j; --j, ++c )
310  if ( (data[j-1] - mean)/sigma <= m_sigmaHigh )
311  break;
312  if ( c == 0 )
313  return;
314  }
315  }
316 
325  template <class C, typename I>
326  void operator()( I& i, I& j, double& mean, double& sigma, C& data ) const
327  {
328  return PerformRejection( i, j, mean, sigma, data );
329  }
330 
331 private:
332 
333  bool m_clipLow = true; // perform rejection of low values
334  bool m_clipHigh = true; // perform rejection of high values
335  float m_sigmaLow = 3; // rejection sigma, low values
336  float m_sigmaHigh = 3; // rejection sigma, high values
337  float m_cutoff = 5; // Winsorization cutoff point in sigma units
338 
359  template <class C, typename I>
360  static void GetWinsorizedParameters( double& mean, double& sigma, const I i, const I j, const C& data, float cutoff )
361  {
362  size_type n = size_type( j - i );
363  if ( n < 2 )
364  {
365  mean = sigma = 0;
366  return;
367  }
368 
369  Array<double> v( n );
370  {
371  size_type l = 0;
372  for ( I k = i; k < j; ++k, ++l )
373  v[l] = double( data[k] );
374  }
375 
376  mean = MedianDestructive( v.Begin(), v.End() );
377  sigma = 1.1926*Sn( v.Begin(), v.End() );
378  v.Sort();
379 
380  for ( int it = 0;; )
381  {
382  if ( 1 + sigma == 1 )
383  break;
384 
385  double t0 = mean - 1.5*sigma;
386  double t1 = mean + 1.5*sigma;
387 
388  if ( cutoff > 0 )
389  {
390  double c0 = mean - cutoff*sigma;
391  double c1 = mean + cutoff*sigma;
392  for ( size_type i = 0; i < n; ++i )
393  if ( v[i] < t0 )
394  v[i] = (v[i] > c0) ? t0 : mean;
395  else if ( v[i] > t1 )
396  v[i] = (v[i] < c1) ? t1 : mean;
397  }
398  else
399  {
400  for ( size_type i = 0; i < n; ++i )
401  if ( v[i] < t0 )
402  v[i] = t0;
403  else if ( v[i] > t1 )
404  v[i] = t1;
405  }
406 
407  double s0 = sigma;
408  sigma = 1.134*StdDev( v.Begin(), v.End() );
409  mean = Mean( v.Begin(), v.End() );
410  if ( ++it > 1 )
411  if ( Abs( s0 - sigma )/s0 < 0.0005 )
412  break;
413  }
414  }
415 };
416 
417 // ----------------------------------------------------------------------------
418 
419 } // pcl
420 
421 #endif // __PCL_WinsorizedSigmaClippingRejection_h
422 
423 // ----------------------------------------------------------------------------
424 // EOF pcl/WinsorizedSigmaClippingRejection.h - Released 2024-06-18T15:48:54Z
Winsorized sigma clipping outlier rejection algorithm.
void operator()(I &i, I &j, double &mean, double &sigma, C &data) const
void PerformRejection(I &i, I &j, double &mean, double &sigma, C &data) const
WinsorizedSigmaClippingRejection(const WinsorizedSigmaClippingRejection &)=default
T Abs(const Complex< T > &c) noexcept
Definition: Complex.h:429
size_t size_type
Definition: Defs.h:609
double MedianDestructive(T *__restrict__ i, T *__restrict__ j) noexcept
Definition: Math.h:2960
double StdDev(const T *__restrict__ i, const T *__restrict__ j, double center) noexcept
Definition: Math.h:2813
double Mean(const T *__restrict__ i, const T *__restrict__ j) noexcept
Definition: Math.h:2709
double Sn(T *__restrict__ x, T *__restrict__ xn)
Definition: Math.h:3924
constexpr const T & Max(const T &a, const T &b) noexcept
Definition: Utility.h:119
PCL root namespace.
Definition: AbstractImage.h:77