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