aimsalgo  5.0.5
Neuroimaging image processing
regularBinnedHisto.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 #ifndef AIMS_HISTOGRAM_REGULARBINNEDHISTO_H
36 #define AIMS_HISTOGRAM_REGULARBINNEDHISTO_H
37 
39 #include <vector>
40 #include <map>
41 
42 // #include <time.h> // FIXME DEBUG
43 
44 namespace aims
45 {
46 
49  template< typename T>
51  {
52  public:
53 
54  RegularBinnedHistogram( unsigned bins = 0 );
57 
60  void doit( const AimsData<T>& thing );
61  // same but specify min/max to avoid seaching extrema
62  void doit( const AimsData<T>& thing, T mindataval, T maxdataval );
63  unsigned bins() const { return _bins; }
64  void setBins( unsigned bins );
65  T minDataValue() const { return _minvalue; }
66  T maxDataValue() const { return _maxvalue; }
67  std::vector<T> *unique( const AimsData<T>& thing,
68  size_t abort_max = 0 ) const;
69 
70  private:
71  unsigned _bins;
72  T _minvalue;
73  T _maxvalue;
74  };
75 
76 
77  template< typename T> inline
79  : Histogram<T>(), _bins( bins ), _minvalue( 0 ), _maxvalue( 0 )
80  {
81  }
82 
83 
84  template< typename T> inline
86  const RegularBinnedHistogram<T>& other )
87  : Histogram< T >( other ), _bins( other._bins ),
88  _minvalue( other._minvalue ), _maxvalue( other._maxvalue )
89  {
90  }
91 
92 
93  template< typename T> inline
95  {
96  _bins = bins;
97  this->_data = AimsData<int32_t>();
98  }
99 
100 
101  template< typename T > inline
103  {
104  doit( thing, thing.minimum(), thing.maximum() );
105  }
106 
107 
108  template< typename T > inline
109  void RegularBinnedHistogram<T>::doit( const AimsData<T>& thing, T mini,
110  T maxi )
111  {
112  _minvalue = mini;
113  _maxvalue = maxi;
114 
115  if( _bins == 0 )
116  {
117  _bins = 256;
118  }
119 
120  this->_data = AimsData<int32_t>( _bins );
121  this->_data = 0;
122  typename AimsData<T>::const_iterator iv, fv=thing.end();
123  double scl = (double) _bins / (double) ( maxi - mini );
124  double x;
125  int y;
126 
127  int iy, iz, it, nx = thing.dimX(), ny = thing.dimY(), nz = thing.dimZ(),
128  nt = thing.dimT();
129  for( it=0; it<nt; ++it )
130  for( iz=0; iz<nz; ++iz )
131  for( iy=0; iy<ny; ++iy )
132  for( iv=&thing( 0, iy, iz, it ), fv=iv+nx; iv!=fv; ++iv )
133  {
134 // std::cout << "iv: " << iv << ", nx: " << nx << ", ny: " << ny << ", nz: " << nz << ", nt: " << nt << ", iv+1: " << &thing( 1, iy, iz, it ) << std::endl;
135  x = (double) ( (double) (*iv) - mini ) * scl;
136  y = (int) x;
137  if( y < 0 )
138  {}
139  else if( y >= (int) _bins )
140  {
141  if( x == (int) _bins )
142  ++this->_data( _bins-1 );
143  }
144  else
145  ++this->_data( y );
146  }
147  }
148 
149 
150  template< typename T > inline
151  std::vector<T> *
153  size_t abort_max ) const
154  {
155  std::map<T, unsigned> vals;
156  typename AimsData<T>::const_iterator iv, fv=thing.end();
157  // std::cout << "unique...\n";
158  // clock_t t0 = clock();
159  size_t n = 0;
160 
161  for( iv=thing.begin(); iv!=fv; ++iv )
162  {
163  ++vals[*iv];
164  if( abort_max != 0 )
165  {
166  ++n;
167  if( n % 1000 == 0 && vals.size() >= abort_max )
168  throw std::runtime_error( "too many values" );
169  }
170  }
171  // std::cout << "unique map done in " << float(clock() - t0) / CLOCKS_PER_SEC << "s: " << thing.dimX() * thing.dimY() * thing.dimZ() * thing.dimT() * CLOCKS_PER_SEC / float(clock() - t0) << " vox/s.\n";
172  std::vector<T> *res = new std::vector<T>( vals.size() );
173  typename std::map<T, unsigned>::iterator im, e = vals.end();
174  typename std::vector<T>::iterator i = res->begin();
175  for( im=vals.begin(); im!=e; ++im, ++i )
176  *i = im->first;
177  // std::cout << "unique vector done.\n";
178  return res;
179  }
180 
181 }
182 
183 #endif
std::vector< T > * unique(const AimsData< T > &thing, size_t abort_max=0) const
const T * const_iterator
int dimZ() const
int nt
iterator begin()
int dimY() const
Histogram container class, with a specified number of regular bins.
RegularBinnedHistogram(unsigned bins=0)
void doit(const AimsData< T > &thing)
classical histogram computation function.
Base class of histogram container class.
Definition: histogram.h:42
AimsData< int32_t > _data
histogram datas
Definition: histogram.h:117
int dimT() const
T minimum() const
iterator end()
T maximum() const
int dimX() const