aimsalgo  5.0.5
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>
43 #include <aims/data/pheader.h>
45 #include <aims/resampling/motion.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( AimsData<T> &image, AimsRoi &roi,
64  const Motion &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>
81  void RoiStats::populate( AimsData<T> &image, AimsRoi &roi,
82  const Motion &motion )
83  {
84  std::set< Vertex*> currentRoiStatVert; //tmp var for current set vert of this
85  Vertex* v; //tmp var for vertice of the object "this"
86  std::vector< int > roi_st, roi_dt;
87 
88  // Tatoo the roi object AND the RoiStat object with the image name
89  roi.setProperty( "last_image_stated", image);
90  setProperty("last_image_stated", image);
91 
92  // get the maximum bouding box
93  std::vector< int > bbmaxTO;
94  std::vector< float > vsTO;
95  roi.getProperty( "boundingbox_max", bbmaxTO);
96  roi.getProperty( "voxel_size", vsTO );
97 
98  // Gestion de start et duration compte tenu d'appel monoframe ou multi frame
99  std::vector< int > i_st, i_dt;
100  PythonHeader *hd = dynamic_cast<PythonHeader *>( image.header() );
101  if (! hd) {
102  for(int i= 0; i < image.dimT(); i++) {
103  i_st.push_back(i);
104  i_dt.push_back(1);
105  }
106  } else {
107  if ( hd->hasProperty("start_time") ) {
108  hd->getProperty("start_time", i_st);
109  } else {
110  ASSERT(image.dimT() == 1); // dynamic series with no start/dur time
111  i_st.push_back(0); // Force val for "static" series
112  }
113  if ( hd->hasProperty("duration_time") ) {
114  hd->getProperty("duration_time", i_dt);
115  } else {
116  ASSERT(image.dimT() == 1); // dynamic series with no start/dur time
117  i_dt.push_back(1); // Force val for "static" series
118  }
119  }
120 
121  // Start gathering data.
122  AimsData< float > tmpin( image.dimX(), image.dimY(), image.dimZ() );
123  tmpin.setSizeXYZT( image.sizeX(), image.sizeY(), image.sizeZ(), 1.0 );
124  if(image.header() != 0)
125  tmpin.setHeader(image.header()->cloneHeader()) ;
126  const std::list<BaseTree*> & metaRoiList = _roiSel->children();
129 
130  // Level 1 : Start loop on time dimension
131  for (int t = 0; t < image.dimT(); ++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  ForEach3d( tmpin, x, y, z )
145  tmpin(x, y, z) = image(x, y, z, t);
146 
147  _interpolator->setRef( tmpin );
148  AimsData< float > tmpout = _interpolator->doit( motion,
149  bbmaxTO[0]+1, bbmaxTO[1]+1, bbmaxTO[2]+1,
150  Point3df( vsTO[0],vsTO[1],vsTO[2] ) );
151 
152  // Level 2 : Loop on the different MetaRoi of selectionSet
153  for (BaseTree::const_iterator metaRoi = metaRoiList.begin();
154  metaRoi != metaRoiList.end();
155  metaRoi++)
156  {
157  std::map<int,carto::rc_ptr<BucketMap<Void> > > corresBuckets;
158  std::map<int,carto::rc_ptr<BucketMap<Void> > >::iterator
159  ib, eb = corresBuckets.end();
160  Tree* tmpTree = dynamic_cast< Tree *>( *metaRoi ); // Test
161  std::string surname;(*tmpTree).getProperty("surname", surname);
162 
163  // Construct the corresBucket
164  const std::list<BaseTree*> & metaRoiChild = (*metaRoi)->children();
165  for( BaseTree::const_iterator metaRoiContent = metaRoiChild.begin();
166  metaRoiContent != metaRoiChild.end();
167  ++metaRoiContent )
168  {
169  tmpTree = dynamic_cast< Tree *>( *metaRoiContent ); // Test
170  std::string tmp;(*tmpTree).getProperty("nomenName", tmp);
171  //std::cout << "Debug nomenName>> " << tmp << std::endl;
172 
173  std::set<Vertex*> roisv = roi.VertByNameAndDescendantName( tmp );
174  for( std::set< Vertex* >::const_iterator j = roisv.begin();
175  j != roisv.end(); ++j )
176  {
177  int l; (*j)->getProperty("roi_label", l);
178  if( (*j)->getProperty( "aims_roi", bck ) )
179  corresBuckets[ l ] = bck;
180  else
181  std::cout << "Bucket not found. Label = " << l << std::endl ;
182  }
183  }
184 
185  if (corresBuckets.size() == 0) continue;
186 
187  // Lets process the current corresBuckets
188  // and construct the vertices of RoiStat.
189  currentRoiStatVert = getVerticesWith("surname", surname);
190 
191  ASSERT (currentRoiStatVert.size() < 2 ); // only 0 first loop
192  //or 1 -other loops
193  if (currentRoiStatVert.size() == 0)
194  {
195  v = addVertex();
196  v->setProperty("surname", surname);
197  }
198  else v = *( currentRoiStatVert.begin() );
199 
200  // Level 3: loop on the corresBuckets in the arg roi and accumulate
201  float mean = 0.0;
202  float std = 0.0;
203  float min = FLT_MAX;
204  float max = FLT_MIN;
205  int pn2 = 0 ;
206  for( ib=corresBuckets.begin(); ib!=eb; ++ib )
207  {
208  bck = ib->second;
209  // 0- Init
210  // 1-Retrieve one bucket : (*v)
211  // 2-Cumul stat
212  if( !bck->empty() )
213  for( ibk=bck->begin()->second.begin(),
214  ebk=bck->begin()->second.end(); ibk!=ebk; ++ibk )
215  {
216  float pixel = tmpout( ibk->first );
217  mean += pixel;
218  std += square( pixel);
219  min = (min > pixel) ? pixel : min;
220  max = (max < pixel) ? pixel : max;
221  ++pn2 ;
222  }
223  } // end of Level 3 loop
224 
225  //Update the current vertice in the RoiStat object
226  mean /= (float) pn2;
227  std=(float)sqrt( (double)(std-square(mean)*pn2) / (float)(pn2-1) );
228 
229  v->setProperty("voxel_number", pn2 );
230  std::vector< float > tac, sac, mac, Mac;
231  v->getProperty( "mean_ac", tac);
232  tac.push_back( mean );
233  v->setProperty("mean_ac", tac);
234  v->getProperty( "std_ac", sac);
235  sac.push_back( std );
236  v->setProperty( "std_ac", sac);
237  v->getProperty( "min_ac", mac);
238  mac.push_back( min );
239  v->setProperty( "min_ac", mac);
240  v->getProperty( "max_ac", Mac);
241  Mac.push_back( max );
242  v->setProperty( "max_ac", Mac);
243  } //end of level 2 (loop on different set in the selectionSet)
244  } // end level 1 (loop on time)
245 
246  setProperty("voxel_volume",vsTO[0]*vsTO[1]*vsTO[2]);
247  }
248 }
249 
250 #endif
float min(float x, float y)
Definition: thickness.h:105
float max(float x, float y)
Definition: thickness.h:97
int dimZ() const
virtual bool getProperty(const std::string &, Object &) const
#define ForEach3d(thing, x, y, z)
float sizeZ() const
STL namespace.
int dimY() const
void setGtmCorrected()
Definition: roistat.h:70
std::list< BaseTree *>::const_iterator const_iterator
float sizeX() const
virtual Header * cloneHeader(bool keepUuid=false) const=0
std::set< Vertex *> VertByNameAndDescendantName(std::string &name)
Resampler< float > * _interpolator
Definition: roistat.h:73
const std::list< BaseTree *> & children() const
VSet::iterator iterator
const aims::Header * header() const
RoiSelector * _roiSel
Definition: roistat.h:74
float sizeY() const
bool isGtmCorrected()
Definition: roistat.h:69
int dimT() const
void setSizeXYZT(float sizex=1.0f, float sizey=1.0f, float sizez=1.0f, float sizet=1.0f)
void populate(AimsData< T > &image, AimsRoi &roi, const Motion &motion)
Definition: roistat.h:81
virtual bool hasProperty(const std::string &) const
#define AIMSALGO_API
#define ASSERT(EX)
T square(const T &val)
int dimX() const
bool _isGtmCorrected
Definition: roistat.h:75