aimsalgo  5.1.2
Neuroimaging image processing
roistat.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_ROI_ROISTAT_H
36 #define AIMS_ROI_ROISTAT_H
37 
39 #include <aims/roi/roi.h>
40 #include <aims/roi/roistat.h>
41 #include <aims/roi/roiselector.h>
42 #include <aims/roi/roigtm.h>
46 #include <aims/math/mathelem.h>
47 #include <graph/graph/graph.h>
48 #include <float.h>
49 
50 namespace aims
51 {
52  class RoiSelector;
53  class RoiGtm;
54 
55  class AIMSALGO_API RoiStats : public Graph
56  {
57  public:
58 
59  RoiStats( Resampler< float >* interpolator , RoiSelector* roiSel );
60  virtual ~RoiStats( );
61 
62  template <typename T>
63  void populate( carto::rc_ptr<carto::Volume<T> > &image, AimsRoi &roi,
64  const AffineTransformation3d &motion );
65  std::vector<float> tacByStructName(std::string s);
66 
67  void applyGtm( RoiGtm& rgtm );
68  void streamout(char *out=NULL);
69  bool isGtmCorrected() { return _isGtmCorrected; }
70  void setGtmCorrected() { _isGtmCorrected = true; }
71 
72  protected:
76 
77  };
78 
79 
80  template <typename T>
82  AimsRoi &roi,
83  const AffineTransformation3d &motion )
84  {
85  std::set< Vertex*> currentRoiStatVert; //tmp var for current set vert of this
86  Vertex* v; //tmp var for vertice of the object "this"
87  std::vector< int > roi_st, roi_dt;
88 
89  // Tatoo the roi object AND the RoiStat object with the image name
90  roi.setProperty( "last_image_stated", image);
91  setProperty("last_image_stated", image);
92 
93  // get the maximum bouding box
94  std::vector< int > bbmaxTO;
95  std::vector< float > vsTO;
96  roi.getProperty( "boundingbox_max", bbmaxTO);
97  roi.getProperty( "voxel_size", vsTO );
98 
99  // Gestion de start et duration compte tenu d'appel monoframe ou multi frame
100  std::vector< int > i_st, i_dt;
101  carto::PropertySet & hd = image->header();
102  if( hd.hasProperty("start_time") )
103  {
104  hd.getProperty("start_time", i_st);
105  }
106  else
107  {
108  ASSERT(image->getSizeT() == 1); // dynamic series with no start/dur time
109  i_st.push_back(0); // Force val for "static" series
110  }
111  if ( hd.hasProperty("duration_time") )
112  {
113  hd.getProperty("duration_time", i_dt);
114  }
115  else
116  {
117  ASSERT(image->getSizeT() == 1); // dynamic series with no start/dur time
118  i_dt.push_back(1); // Force val for "static" series
119  }
120 
121  // Start gathering data.
122  carto::VolumeRef< float > tmpin( image->getSizeX(), image->getSizeY(),
123  image->getSizeZ() );
124  tmpin.setVoxelSize( image->getVoxelSize() );
125  tmpin.copyHeaderFrom(image->header());
126  const std::list<BaseTree*> & metaRoiList = _roiSel->children();
129 
130  // Level 1 : Start loop on time dimension
131  for (int t = 0; t < image->getSizeT(); ++t) // populate may be invoked in byframe
132  { // or allframe mode
133  // 1-Init des fields start et duration time;
134  // 2-Re Copy of one frame in tem image
135  // 3-Set in order the interpolation process
136  getProperty( "start_time", roi_st );
137  roi_st.push_back( i_st[ t ] );
138  setProperty("start_time",roi_st);
139  getProperty( "duration_time", roi_dt );
140  roi_dt.push_back( i_dt[ t ] );
141  setProperty("duration_time",roi_dt);
142 
143  int x,y,z;
144  std::vector<int> dim = tmpin->getSize();
145  for( z=0; z<dim[2]; ++z )
146  for( y=0; y<dim[1]; ++y )
147  for( x=0; x<dim[0]; ++x )
148  tmpin(x, y, z) = image->at(x, y, z, t);
149 
150  _interpolator->setRef( tmpin );
151  carto::VolumeRef< float > tmpout = _interpolator->doit( motion,
152  bbmaxTO[0]+1, bbmaxTO[1]+1, bbmaxTO[2]+1,
153  Point3df( vsTO[0],vsTO[1],vsTO[2] ) );
154 
155  // Level 2 : Loop on the different MetaRoi of selectionSet
156  for (BaseTree::const_iterator metaRoi = metaRoiList.begin();
157  metaRoi != metaRoiList.end();
158  metaRoi++)
159  {
160  std::map<int,carto::rc_ptr<BucketMap<Void> > > corresBuckets;
161  std::map<int,carto::rc_ptr<BucketMap<Void> > >::iterator
162  ib, eb = corresBuckets.end();
163  Tree* tmpTree = dynamic_cast< Tree *>( *metaRoi ); // Test
164  std::string surname;(*tmpTree).getProperty("surname", surname);
165 
166  // Construct the corresBucket
167  const std::list<BaseTree*> & metaRoiChild = (*metaRoi)->children();
168  for( BaseTree::const_iterator metaRoiContent = metaRoiChild.begin();
169  metaRoiContent != metaRoiChild.end();
170  ++metaRoiContent )
171  {
172  tmpTree = dynamic_cast< Tree *>( *metaRoiContent ); // Test
173  std::string tmp;(*tmpTree).getProperty("nomenName", tmp);
174  //std::cout << "Debug nomenName>> " << tmp << std::endl;
175 
176  std::set<Vertex*> roisv = roi.VertByNameAndDescendantName( tmp );
177  for( std::set< Vertex* >::const_iterator j = roisv.begin();
178  j != roisv.end(); ++j )
179  {
180  int l; (*j)->getProperty("roi_label", l);
181  if( (*j)->getProperty( "aims_roi", bck ) )
182  corresBuckets[ l ] = bck;
183  else
184  std::cout << "Bucket not found. Label = " << l << std::endl ;
185  }
186  }
187 
188  if (corresBuckets.size() == 0) continue;
189 
190  // Lets process the current corresBuckets
191  // and construct the vertices of RoiStat.
192  currentRoiStatVert = getVerticesWith("surname", surname);
193 
194  ASSERT (currentRoiStatVert.size() < 2 ); // only 0 first loop
195  //or 1 -other loops
196  if (currentRoiStatVert.size() == 0)
197  {
198  v = addVertex();
199  v->setProperty("surname", surname);
200  }
201  else v = *( currentRoiStatVert.begin() );
202 
203  // Level 3: loop on the corresBuckets in the arg roi and accumulate
204  float mean = 0.0;
205  float std = 0.0;
206  float min = FLT_MAX;
207  float max = FLT_MIN;
208  int pn2 = 0 ;
209  for( ib=corresBuckets.begin(); ib!=eb; ++ib )
210  {
211  bck = ib->second;
212  // 0- Init
213  // 1-Retrieve one bucket : (*v)
214  // 2-Cumul stat
215  if( !bck->empty() )
216  for( ibk=bck->begin()->second.begin(),
217  ebk=bck->begin()->second.end(); ibk!=ebk; ++ibk )
218  {
219  float pixel = tmpout( ibk->first );
220  mean += pixel;
221  std += square( pixel);
222  min = (min > pixel) ? pixel : min;
223  max = (max < pixel) ? pixel : max;
224  ++pn2 ;
225  }
226  } // end of Level 3 loop
227 
228  //Update the current vertice in the RoiStat object
229  mean /= (float) pn2;
230  std=(float)sqrt( (double)(std-square(mean)*pn2) / (float)(pn2-1) );
231 
232  v->setProperty("voxel_number", pn2 );
233  std::vector< float > tac, sac, mac, Mac;
234  v->getProperty( "mean_ac", tac);
235  tac.push_back( mean );
236  v->setProperty("mean_ac", tac);
237  v->getProperty( "std_ac", sac);
238  sac.push_back( std );
239  v->setProperty( "std_ac", sac);
240  v->getProperty( "min_ac", mac);
241  mac.push_back( min );
242  v->setProperty( "min_ac", mac);
243  v->getProperty( "max_ac", Mac);
244  Mac.push_back( max );
245  v->setProperty( "max_ac", Mac);
246  } //end of level 2 (loop on different set in the selectionSet)
247  } // end level 1 (loop on time)
248 
249  setProperty("voxel_volume",vsTO[0]*vsTO[1]*vsTO[2]);
250  }
251 }
252 
253 #endif
#define AIMSALGO_API
#define ASSERT(EX)
const std::list< BaseTree * > & children() const
std::list< BaseTree * >::const_iterator const_iterator
Vertex * addVertex(const std::string &s="")
VSet::iterator iterator
std::set< Vertex * > getVerticesWith(const std::string &s) const
std::set< Vertex * > VertByNameAndDescendantName(std::string &name)
void setRef(const carto::rc_ptr< carto::Volume< T > > &ref)
Set the input data to be resampled by the doit() methods.
Definition: resampler_d.h:311
void doit(const aims::AffineTransformation3d &transform, carto::Volume< T > &output_data) const
Resample the input volume set with setRef() into an existing volume.
Definition: resampler_d.h:226
void populate(carto::rc_ptr< carto::Volume< T > > &image, AimsRoi &roi, const AffineTransformation3d &motion)
Definition: roistat.h:81
bool _isGtmCorrected
Definition: roistat.h:75
void applyGtm(RoiGtm &rgtm)
RoiStats(Resampler< float > *interpolator, RoiSelector *roiSel)
Resampler< float > * _interpolator
Definition: roistat.h:73
bool isGtmCorrected()
Definition: roistat.h:69
RoiSelector * _roiSel
Definition: roistat.h:74
void setGtmCorrected()
Definition: roistat.h:70
virtual ~RoiStats()
void streamout(char *out=NULL)
std::vector< float > tacByStructName(std::string s)
virtual bool hasProperty(const std::string &) const
bool getProperty(const std::string &, T &) const
void setVoxelSize(float vx, float vy=1., float vz=1., float vt=1.)
virtual void copyHeaderFrom(const PropertySet &other)
std::vector< int > getSize() const
const T & at(long x, long y=0, long z=0, long t=0) const
T square(const T &val)
float min(float x, float y)
Definition: thickness.h:106
float max(float x, float y)
Definition: thickness.h:98