ANIMA  4.0
animaLowMemPatientToGroupODFComparisonBridge.cxx
Go to the documentation of this file.
3 
4 namespace anima
5 {
6 
8 {
9  m_NbSplits = 2;
10  m_NumThreads = itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads();
11 
12  m_DataODFImages = new ImageSplitterODFType;
13  m_TestODFImage = new ImageSplitterODFType;
14 
15  m_ComputationMask = NULL;
16 
17  m_StatisticalTestType = MainFilterType::FISHER;
18  m_ExplainedRatio = 0.9;
19  m_NumEigenValuesPCA = 6;
20 
21  m_SampleDirections.clear();
22 }
23 
25 {
26  if (m_DataODFImages)
27  delete m_DataODFImages;
28 
29  if (m_TestODFImage)
30  delete m_TestODFImage;
31 }
32 
34 {
35  m_ComputationMask = anima::readImage <MaskImageType> (cMask);
36 
37  m_DataODFImages->SetComputationMask(m_ComputationMask);
38  m_TestODFImage->SetComputationMask(m_ComputationMask);
39 }
40 
41 void LowMemoryPatientToGroupODFComparisonBridge::Update(int specificSplitToDo, bool genOutputDescriptionData)
42 {
43  if (!m_ComputationMask)
44  itkExceptionMacro("No computation mask... Exiting...");
45 
47  for (unsigned int i = 0;i < MaskImageType::GetImageDimension();++i)
48  tmpInd[i] = m_NbSplits;
49 
50  m_DataODFImages->SetNumberOfBlocks(tmpInd);
51  m_TestODFImage->SetNumberOfBlocks(tmpInd);
52 
53  std::vector < ImageSplitterODFType::TInputIndexType > splitIndexesToProcess;
54 
55  if (specificSplitToDo != -1)
56  {
57  tmpInd[0] = (unsigned int)floor((double)(specificSplitToDo/(m_NbSplits*m_NbSplits)));
58  unsigned int tmpVal = specificSplitToDo - tmpInd[0]*m_NbSplits*m_NbSplits;
59  tmpInd[1] = (unsigned int)floor((double)(tmpVal/m_NbSplits));
60  tmpInd[2] = tmpVal - tmpInd[1]*m_NbSplits;
61 
62  if (!m_DataODFImages->EmptyMask(tmpInd))
63  splitIndexesToProcess.push_back(tmpInd);
64  }
65  else
66  {
67  for (unsigned int i = 0;i < m_NbSplits;++i)
68  {
69  tmpInd[0] = i;
70  for (unsigned int j = 0;j < m_NbSplits;++j)
71  {
72  tmpInd[1] = j;
73  for (unsigned int k = 0;k < m_NbSplits;++k)
74  {
75  tmpInd[2] = k;
76 
77  if (!m_DataODFImages->EmptyMask(tmpInd))
78  splitIndexesToProcess.push_back(tmpInd);
79  }
80  }
81  }
82  }
83 
84  for (unsigned int i = 0;i < splitIndexesToProcess.size();++i)
85  {
86  std::cout << "Processing block : " << splitIndexesToProcess[i][0] << " "
87  << splitIndexesToProcess[i][1] << " " << splitIndexesToProcess[i][2] << std::endl;
88 
89  m_DataODFImages->SetBlockIndex(splitIndexesToProcess[i]);
90  m_DataODFImages->Update();
91 
92  m_TestODFImage->SetBlockIndex(splitIndexesToProcess[i]);
93  m_TestODFImage->Update();
94 
96 
97  for (unsigned int j = 0;j < m_DataODFImages->GetNbImages();++j)
98  mainFilter->AddDatabaseInput(m_DataODFImages->GetOutput(j));
99 
100  mainFilter->SetComputationMask(m_DataODFImages->GetSmallMaskWithMargin());
101  mainFilter->SetNumberOfWorkUnits(m_NumThreads);
102 
103  mainFilter->SetStatisticalTestType(m_StatisticalTestType);
104  mainFilter->SetExplainedRatio(m_ExplainedRatio);
105  mainFilter->SetNumEigenValuesPCA(m_NumEigenValuesPCA);
106 
107  mainFilter->SetSampleDirections(m_SampleDirections);
108 
109  mainFilter->SetInput(0,m_TestODFImage->GetOutput(0));
110 
111  mainFilter->Update();
112 
113  std::cout << "Results computed... Writing output parcel..." << std::endl;
114 
115  char numSplit[2048];
116  sprintf(numSplit,"_%ld_%ld_%ld.nrrd",splitIndexesToProcess[i][0],splitIndexesToProcess[i][1],splitIndexesToProcess[i][2]);
117 
118  //Write outputs
119  std::string outputName = m_OutputName + numSplit;
120  std::string outputPValName = m_OutputPValName + numSplit;
121  this->BuildAndWrite(mainFilter->GetOutput(0),outputName,m_DataODFImages->GetBlockRegionInsideMargin());
122  this->BuildAndWrite(mainFilter->GetOutput(1),outputPValName,m_DataODFImages->GetBlockRegionInsideMargin());
123  }
124 
125  if (genOutputDescriptionData)
126  {
127  std::string tmpOutName = m_OutputName + ".txt";
128  std::ofstream tmpFileOut(tmpOutName.c_str());
129 
130  std::string tmpOutPValName = m_OutputPValName + ".txt";
131  std::ofstream tmpFilePValOut(tmpOutPValName.c_str());
132 
133  for (unsigned int i = 0;i < m_NbSplits;++i)
134  {
135  tmpInd[0] = i;
136  for (unsigned int j = 0;j < m_NbSplits;++j)
137  {
138  tmpInd[1] = j;
139  for (unsigned int k = 0;k < m_NbSplits;++k)
140  {
141  tmpInd[2] = k;
142 
143  if (!m_DataODFImages->EmptyMask(tmpInd))
144  {
145  OutputImageType::RegionType tmpBlRegion = m_DataODFImages->GetSpecificBlockRegion(tmpInd);
146  char numSplit[2048];
147  sprintf(numSplit,"_%ld_%ld_%ld.nrrd",tmpInd[0],tmpInd[1],tmpInd[2]);
148 
149  tmpFileOut << "<BLOCK>" << std::endl;
150  tmpFileOut << "BLOCK_FILE=" << m_OutputName + numSplit << std::endl;
151  tmpFileOut << "STARTING_INDEX=" << tmpBlRegion.GetIndex()[0] << " "
152  << tmpBlRegion.GetIndex()[1] << " " << tmpBlRegion.GetIndex()[2] << std::endl;
153  tmpFileOut << "</BLOCK>" << std::endl;
154 
155  tmpFilePValOut << "<BLOCK>" << std::endl;
156  tmpFilePValOut << "BLOCK_FILE=" << m_OutputPValName + numSplit << std::endl;
157  tmpFilePValOut << "STARTING_INDEX=" << tmpBlRegion.GetIndex()[0] << " "
158  << tmpBlRegion.GetIndex()[1] << " " << tmpBlRegion.GetIndex()[2] << std::endl;
159  tmpFilePValOut << "</BLOCK>" << std::endl;
160  }
161  }
162  }
163  }
164 
165  tmpFileOut.close();
166  tmpFilePValOut.close();
167  }
168 }
169 
171  OutputImageType::RegionType finalROI)
172 {
173  OutputImageType::RegionType tmpRegion = finalROI;
174  for (unsigned int i = 0;i < OutputImageType::GetImageDimension();++i)
175  tmpRegion.SetIndex(i,0);
176 
177  OutputImageType::Pointer tmpRes = OutputImageType::New();
178  tmpRes->Initialize();
179 
180  tmpRes->SetOrigin(m_ComputationMask->GetOrigin());
181  tmpRes->SetRegions(tmpRegion);
182  tmpRes->SetDirection(m_ComputationMask->GetDirection());
183  tmpRes->SetSpacing(m_ComputationMask->GetSpacing());
184 
185  tmpRes->Allocate();
186 
187  itk::ImageRegionIterator <OutputImageType> tmpImIt (tmpIm,finalROI);
188  itk::ImageRegionIterator <OutputImageType> tmpResIt (tmpRes,tmpRegion);
189 
190  while (!tmpImIt.IsAtEnd())
191  {
192  tmpResIt.Set(tmpImIt.Get());
193 
194  ++tmpImIt;
195  ++tmpResIt;
196  }
197 
198  anima::writeImage <OutputImageType> (resName,tmpRes);
199 }
200 
201 } // end namespace anima
void Update(int specificSplitToDo=-1, bool genOutputDescriptionData=false)
TInputRegionType GetBlockRegionInsideMargin()
MaskImageType * GetSmallMaskWithMargin()
PatientToGroupODFComparisonImageFilter< double >::OutputImageType OutputImageType
void SetComputationMask(MaskImageType::Pointer &maskIm)
void SetBlockIndex(TInputIndexType &bIndex)
void SetNumberOfBlocks(TInputIndexType &bNumBlocks)
TInputImage * GetOutput(unsigned int i)
TInputRegionType GetSpecificBlockRegion(TInputIndexType &block)
bool EmptyMask(TInputIndexType &bIndex)
void BuildAndWrite(OutputImageType *tmpIm, std::string resName, OutputImageType::RegionType finalROI)
TInputImage::IndexType TInputIndexType