DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
DWISingleVoxelSimulator.cxx
Go to the documentation of this file.
1 
19 #include "DWISingleVoxelSimulatorCLP.h"
20 #include "itkDWIWriter.h"
21 
26 int
27 main (int argc, char const* argv[])
28 {
29  // GenerateCLP
30  PARSE_ARGS;
31 
32  typedef itk::VectorImage<float, 3> OutputImageType;
33  OutputImageType::Pointer outputImage;
34  typedef itk::Image<float, 3> B0ImageType;
35 
37  GeneratorType::Pointer dwiGenerator = GeneratorType::New();
38 
39  utlGlobalException(_DiffusionParameters.size()==0, "need to set --params");
40  dwiGenerator->SetDiffusionParameterValues(_DiffusionParameters);
41 
42  dwiGenerator->SetDebug(_DebugArg.isSet());
43  utlGlobalException(_OutputDWIFile=="" && _OutputODFFile=="" && _OutputEAPFile==""
44  && _OutputMSDFile=="" && _OutputRTOFile=="", "need to set output dwi/odf/eap/peak/rto/msd");
45  dwiGenerator->SetIsOutputDWI(_OutputDWIFile!="");
46  dwiGenerator->SetIsOutputEAP(_OutputEAPFile!="");
47  dwiGenerator->SetIsOutputODF(_OutputODFFile!="");
48  dwiGenerator->SetIsOutputRTO(_OutputRTOFile!="");
49  dwiGenerator->SetIsOutputMSD(_OutputMSDFile!="");
50 
51  if ( _SNRArg.isSet() )
52  dwiGenerator->SetSNR( _SNR );
53  if (_NoiseSigmaArg.isSet())
54  dwiGenerator->SetNoiseSigma( _NoiseSigma );
55  utlGlobalException(_SNRArg.isSet() && _NoiseSigmaArg.isSet(), "Only one of these is allowed: --snr or --noisesigma");
56 
57  if (_TauArg.isSet())
58  {
59  dwiGenerator->GetSamplingSchemeQSpace()->SetTau(_Tau);
60  dwiGenerator->GetSamplingSchemeRSpace()->SetTau(_Tau);
61  }
62 
63  utlGlobalException(!_QSpaceOrientationsArg.isSet() && !_RSpaceOrientationsArg.isSet(), "no gradient files in Q space and R space");
64 
65  GeneratorType::STDVectorPointer bvec(new GeneratorType::STDVectorType());
66  GeneratorType::STDVectorPointer rvec(new GeneratorType::STDVectorType());
67 
68  if (_QSpaceOrientationsArg.isSet())
69  {
70  utlGlobalException(!_BValuesArg.isSet() && !_BFileArg.isSet(), "need to set --bvalues or --bfile");
71  utlGlobalException(_BValuesArg.isSet() && _BFileArg.isSet(), "Only one of these is allowed: --bvalues or --bfile");
72 
73  if (_BValuesArg.isSet())
74  *bvec = _BValues;
75  if (_BFileArg.isSet())
76  utl::ReadVector(_BFile,*bvec);
77 
78  GeneratorType::MatrixPointer qMatrix = utl::ReadGrad<double>(_QSpaceOrientations,DIRECTION_NODUPLICATE, CARTESIAN_TO_CARTESIAN);
79 
80  if (bvec->size()>0 && qMatrix->Rows()>0 && bvec->size()!=qMatrix->Rows())
81  utl::MatchBVectorAndGradientMatrix(*bvec, *qMatrix);
82  dwiGenerator->GetSamplingSchemeQSpace()->SetOrientationsCartesian( qMatrix );
83  dwiGenerator->GetSamplingSchemeQSpace()->SetBVector(bvec);
84  }
85 
86  if (_RSpaceOrientationsArg.isSet())
87  {
88  utlGlobalException(!_RValuesArg.isSet() && !_RFileArg.isSet(), "need to set --rvalues or --rfile");
89  utlGlobalException(_RValuesArg.isSet() && _RFileArg.isSet(), "Only one of these is allowed: --rvalues or --rfile");
90 
91  if (_RValuesArg.isSet())
92  *rvec = _RValues;
93  if (_RFileArg.isSet())
94  utl::ReadVector(_RFile,*rvec);
95 
96  GeneratorType::MatrixPointer rMatrix = utl::ReadGrad<double>(_RSpaceOrientations,DIRECTION_NODUPLICATE, CARTESIAN_TO_CARTESIAN);
97 
98  if (rvec->size()>0 && rMatrix->Rows()>0 && rvec->size()!=rMatrix->Rows())
99  utl::MatchBVectorAndGradientMatrix(*rvec, *rMatrix);
100  dwiGenerator->GetSamplingSchemeRSpace()->SetOrientationsCartesian( rMatrix );
101  dwiGenerator->GetSamplingSchemeRSpace()->SetRadiusVector(rvec);
102  }
103 
104  if (_B0ScaleArg.isSet())
105  dwiGenerator->SetB0Scale(_B0Scale);
106 
107  if (_ModelType=="TENSOR_SPHERICAL")
108  dwiGenerator->SetModelType(GeneratorType::SYMMETRICAL_TENSOR_IN_SPHERICAL_COORDS);
109  else if (_ModelType=="TENSOR_CARTESIAN")
110  dwiGenerator->SetModelType(GeneratorType::SYMMETRICAL_TENSOR_IN_CARTESIAN_COORDS);
111  else if (_ModelType=="CYLINDER_SPHERICAL")
112  dwiGenerator->SetModelType(GeneratorType::CYLINDER_SPHERICAL_MODEL);
113 
114  if (_RandomType=="FIXED")
115  dwiGenerator->SetRandomType(GeneratorType::FIXED);
116  else if (_RandomType=="UNIFORM")
117  {
118  dwiGenerator->SetRandomType(GeneratorType::UNIFORM);
119  utlSAGlobalException(_TessellationOrder>0 && _ElecNumber>0).msg("only one of --elecnumber and --tessorder can be set");
120  if (_TessellationOrder>0)
121  dwiGenerator->SetUniformFromTessellation(_TessellationOrder);
122  if (_ElecNumber>0)
123  dwiGenerator->SetUniformFromElectrostaticEnergy(_ElecNumber);
124  }
125 
126  utlGlobalException(_Size.size()!=3, "--size should have 3 dimension");
127  OutputImageType::SizeType size;
128  for ( int i = 0; i < 3; i += 1 )
129  size[i] = _Size[i];
130  dwiGenerator->SetOutputSize(size);
131 
132  if (_MaxNumberOfPeaks>0)
133  {
134  dwiGenerator->SetMaxNumberOfPeaks(_MaxNumberOfPeaks);
135  dwiGenerator->SetPeakType(itk::PeakContainerHelper::GetPeakType(_PeakType));
136  }
137 
138  dwiGenerator->Update();
139 
140 
141  // Write Output
142 
143  if (_OutputDWIFile!="")
144  {
145  std::cout << "Writing DWI data ... " << std::endl;
146  outputImage = dwiGenerator->GetDWIImage();
147  if (_OutputDWIType!="4D")
148  {
149  typedef itk::DWIWriter<float> DWIWriterType;
150  DWIWriterType::Pointer writer = DWIWriterType::New();
151  writer->SetInput(outputImage);
152  if (_OutputDWIType=="EACHSHELL")
153  writer->SetOutputEachShell(true);
154  writer->SetSamplingSchemeQSpace(dwiGenerator->GetSamplingSchemeQSpace());
155 
156  std::string outputFile_noext, outputFile_ext;
157  utl::GetFileExtension(_OutputDWIFile, outputFile_ext, outputFile_noext);
158  writer->SetConfigurationFile(outputFile_noext + ".txt");
159  writer->SetBFile(outputFile_noext + "_b.txt");
160  writer->SetOrientationFile(outputFile_noext+"_grad.txt");
161  writer->SetDWIFile(outputFile_noext + ".nii.gz");
162  writer->Update();
163  }
164  else
165  {
166  itk::SaveImage<OutputImageType>(outputImage, _OutputDWIFile);
167  }
168 
169  if ( _B0FileArg.isSet() )
170  itk::SaveImage<B0ImageType>(dwiGenerator->GetB0Image(), _B0File);
171  }
172 
173  if (_OutputEAPFile!="")
174  {
175  std::cout << "Writing EAP data ... " << std::endl;
176  outputImage = dwiGenerator->GetEAPImage();
177  utlException(itk::IsImageEmpty(outputImage), "logical error! eap image does not exist. Need to set --rorientations --rvalues");
178  itk::SaveImage<OutputImageType>(outputImage, _OutputEAPFile);
179  }
180 
181  if (_OutputODFFile!="")
182  {
183  std::cout << "Writing ODF data ... " << std::endl;
184  outputImage = dwiGenerator->GetODFImage();
185  utlException(itk::IsImageEmpty(outputImage), "logical error! odf image does not exist. Need to set --rorientations --rvalues");
186  itk::SaveImage<OutputImageType>(outputImage, _OutputODFFile);
187  }
188 
189  if (_OutputPeakFile!="")
190  {
191  std::cout << "Writing Peak data ... " << std::endl;
192  utlSAGlobalException(_MaxNumberOfPeaks<=0).msg("need to set _MaxNumberOfPeaks in --peaks");
193  outputImage = dwiGenerator->GetPeakImage();
194  utlException(itk::IsImageEmpty(outputImage), "logical error! peak image does not exist");
195  itk::SaveImage<OutputImageType>(outputImage, _OutputPeakFile);
196  }
197 
198  return EXIT_SUCCESS;
199 
200 }
201 
bool IsImageEmpty(const SmartPointer< ImageType > &image)
Definition: utlITK.h:435
void MatchBVectorAndGradientMatrix(const T &br, std::vector< T > &vec, const NDArray< T, 2 > &grad)
Definition: utlDMRI.h:480
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
#define utlSAGlobalException(expr)
Definition: utlCoreMacro.h:362
static PeakType GetPeakType(const std::string &str)
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
write gradient file, b values and DWI files (with optional index file)
Definition: itkDWIWriter.h:60
Generate DWI data with the same diffusion parameter set for all voxels. If random mode is used...
void ReadVector(const std::string &vectorStr, std::vector< T > &vec, const char *cc=" ")
Definition: utlCore.h:1159
void GetFileExtension(const std::string &fileNameAbsolute, std::string &ext, std::string &fileNoExt)
Definition: utlCore.h:559
int main(int argc, char const *argv[])
Simulate DWI data with fixed single voxel configuration.