Skip to content

Commit

Permalink
BUG: Library should write to output directory whenever it is specified
Browse files Browse the repository at this point in the history
Whenever the output directory is specified, the library will now write files to this directory, just like the executable does.

(Before this commit, the elastix library would only create the result images in memory, even when the WriteResultImage parameter would be "true". This was an inconsistency between the library and the elastix executable.)

With this commit, an empty string (`""`) is used internally to indicate that the output directory is unspecified. The use of a magical "output_path_not_set" string is abandoned.
  • Loading branch information
N-Dekker committed May 31, 2023
1 parent 691c358 commit 99902e5
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ MissingStructurePenalty<TElastix>::AfterEachIteration()
this->m_Configuration->ReadParameter(
writeResultMeshThisIteration, "WriteResultMeshAfterEachIteration", "", level, 0, false);

const std::string outputDirectoryPath = this->m_Configuration->GetCommandLineArgument("-out");

/** Writing result mesh. */
if (writeResultMeshThisIteration)
if (writeResultMeshThisIteration && !outputDirectoryPath.empty())
{
std::string componentLabel(this->GetComponentLabel());
std::string metricNumber = componentLabel.substr(6, 2); // strip "Metric" keep number
Expand All @@ -204,7 +206,7 @@ MissingStructurePenalty<TElastix>::AfterEachIteration()
{

std::ostringstream makeFileName;
makeFileName << this->m_Configuration->GetCommandLineArgument("-out") << "resultmesh" << ch << metricNumber << "."
makeFileName << outputDirectoryPath << "resultmesh" << ch << metricNumber << "."
<< this->m_Configuration->GetElastixLevel() << ".R" << level << ".It" << std::setfill('0')
<< std::setw(7) << iter << "." << resultMeshFormat;

Expand Down Expand Up @@ -238,8 +240,10 @@ MissingStructurePenalty<TElastix>::AfterEachResolution()
this->m_Configuration->ReadParameter(
writeResultMeshThisResolution, "WriteResultMeshAfterEachResolution", "", level, 0, false);

const std::string outputDirectoryPath = this->m_Configuration->GetCommandLineArgument("-out");

/** Writing result mesh. */
if (writeResultMeshThisResolution)
if (writeResultMeshThisResolution && !outputDirectoryPath.empty())
{
std::string componentLabel(this->GetComponentLabel());
std::string metricNumber = componentLabel.substr(6, 2); // strip "Metric" keep number
Expand All @@ -252,7 +256,7 @@ MissingStructurePenalty<TElastix>::AfterEachResolution()
{

std::ostringstream makeFileName;
makeFileName << this->m_Configuration->GetCommandLineArgument("-out") << "resultmesh" << ch << metricNumber << "."
makeFileName << outputDirectoryPath << "resultmesh" << ch << metricNumber << "."
<< this->m_Configuration->GetElastixLevel() << ".R" << level << "." << resultMeshFormat;

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ PolydataDummyPenalty<TElastix>::AfterEachIteration()
this->m_Configuration->ReadParameter(
writeResultMeshThisIteration, "WriteResultMeshAfterEachIteration", "", level, 0, false);

const std::string outputDirectoryPath = this->m_Configuration->GetCommandLineArgument("-out");

/** Writing result mesh. */
if (writeResultMeshThisIteration)
if (writeResultMeshThisIteration && !outputDirectoryPath.empty())
{
std::string componentLabel(this->GetComponentLabel());
std::string metricNumber = componentLabel.substr(6, 2); // strip "Metric" keep number
Expand All @@ -193,7 +195,7 @@ PolydataDummyPenalty<TElastix>::AfterEachIteration()
{

std::ostringstream makeFileName;
makeFileName << this->m_Configuration->GetCommandLineArgument("-out") << "resultmesh" << ch << metricNumber << "."
makeFileName << outputDirectoryPath << "resultmesh" << ch << metricNumber << "."
<< this->m_Configuration->GetElastixLevel() << ".R" << level << ".It" << std::setfill('0')
<< std::setw(7) << iter << "." << resultMeshFormat;

Expand Down Expand Up @@ -227,8 +229,10 @@ PolydataDummyPenalty<TElastix>::AfterEachResolution()
this->m_Configuration->ReadParameter(
writeResultMeshThisResolution, "WriteResultMeshAfterEachResolution", "", level, 0, false);

const std::string outputDirectoryPath = this->m_Configuration->GetCommandLineArgument("-out");

/** Writing result mesh. */
if (writeResultMeshThisResolution)
if (writeResultMeshThisResolution && !outputDirectoryPath.empty())
{
std::string componentLabel(this->GetComponentLabel());
std::string metricNumber = componentLabel.substr(6, 2); // strip "Metric" keep number
Expand All @@ -241,7 +245,7 @@ PolydataDummyPenalty<TElastix>::AfterEachResolution()
{

std::ostringstream makeFileName;
makeFileName << this->m_Configuration->GetCommandLineArgument("-out") << "resultmesh" << ch << metricNumber << "."
makeFileName << outputDirectoryPath << "resultmesh" << ch << metricNumber << "."
<< this->m_Configuration->GetElastixLevel() << ".R" << level << "." << resultMeshFormat;

try
Expand Down
22 changes: 14 additions & 8 deletions Components/Optimizers/FullSearch/elxFullSearchOptimizer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,18 @@ FullSearch<TElastix>::BeforeEachResolution()
this->m_OptimizationSurface->Allocate();
/** \todo try/catch block around Allocate? */

/** Set the name of this image on disk. */
std::string resultImageFormat = "mhd";
configuration.ReadParameter(resultImageFormat, "ResultImageFormat", 0, false);
makeString.str("");
makeString << configuration.GetCommandLineArgument("-out") << "OptimizationSurface."
<< configuration.GetElastixLevel() << ".R" << level << "." << resultImageFormat;
this->m_OptimizationSurface->SetOutputFileName(makeString.str().c_str());
if (const std::string outputDirectoryPath = configuration.GetCommandLineArgument("-out");
!outputDirectoryPath.empty())
{
/** Set the name of this image on disk. */
std::string resultImageFormat = "mhd";
configuration.ReadParameter(resultImageFormat, "ResultImageFormat", 0, false);

makeString.str("");
makeString << outputDirectoryPath << "OptimizationSurface." << configuration.GetElastixLevel() << ".R" << level
<< "." << resultImageFormat;
this->m_OptimizationSurface->SetOutputFileName(makeString.str().c_str());
}

log::info(std::ostringstream{} << "Total number of iterations needed in this resolution: "
<< this->GetNumberOfIterations() << ".");
Expand Down Expand Up @@ -239,7 +244,8 @@ FullSearch<TElastix>::AfterEachResolution()
/** Write the optimization surface to disk */
bool writeSurfaceEachResolution = false;
configuration.ReadParameter(writeSurfaceEachResolution, "WriteOptimizationSurfaceEachResolution", 0, false);
if (writeSurfaceEachResolution)

if (writeSurfaceEachResolution && !configuration.GetCommandLineArgument("-out").empty())
{
try
{
Expand Down
6 changes: 4 additions & 2 deletions Core/ComponentBaseClasses/elxFixedImagePyramidBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ FixedImagePyramidBase<TElastix>::BeforeEachResolutionBase()
std::string resultImageFormat = "mhd";
configuration.ReadParameter(resultImageFormat, "ResultImageFormat", 0, false);

const std::string outputDirectoryPath = configuration.GetCommandLineArgument("-out");

/** Writing result image. */
if (writePyramidImage)
if (writePyramidImage && !outputDirectoryPath.empty())
{
/** Create a name for the final result. */
std::ostringstream makeFileName;
makeFileName << configuration.GetCommandLineArgument("-out");
makeFileName << outputDirectoryPath;
makeFileName << this->GetComponentLabel() << "." << configuration.GetElastixLevel() << ".R" << level << "."
<< resultImageFormat;

Expand Down
6 changes: 4 additions & 2 deletions Core/ComponentBaseClasses/elxMovingImagePyramidBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ MovingImagePyramidBase<TElastix>::BeforeEachResolutionBase()
std::string resultImageFormat = "mhd";
configuration.ReadParameter(resultImageFormat, "ResultImageFormat", 0, false);

const std::string outputDirectoryPath = configuration.GetCommandLineArgument("-out");

/** Writing result image. */
if (writePyramidImage)
if (writePyramidImage && !outputDirectoryPath.empty())
{
/** Create a name for the final result. */
std::ostringstream makeFileName;
makeFileName << configuration.GetCommandLineArgument("-out");
makeFileName << outputDirectoryPath;
makeFileName << this->GetComponentLabel() << "." << configuration.GetElastixLevel() << ".R" << level << "."
<< resultImageFormat;

Expand Down
54 changes: 28 additions & 26 deletions Core/ComponentBaseClasses/elxResamplerBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,18 @@ ResamplerBase<TElastix>::AfterEachResolutionBase()
configuration.ReadParameter(
writeResultImageThisResolution, "WriteResultImageAfterEachResolution", "", level, 0, false);

const std::string outputDirectoryPath = configuration.GetCommandLineArgument("-out");

/** Writing result image. */
if (writeResultImageThisResolution)
if (writeResultImageThisResolution && !outputDirectoryPath.empty())
{
/** Create a name for the final result. */
const auto resultImageName = configuration.RetrieveParameterStringValue("result", "ResultImageName", 0, false);
std::string resultImageFormat = "mhd";
configuration.ReadParameter(resultImageFormat, "ResultImageFormat", 0, false);
std::ostringstream makeFileName;
makeFileName << configuration.GetCommandLineArgument("-out") << resultImageName << '.'
<< configuration.GetElastixLevel() << ".R" << level << "." << resultImageFormat;
makeFileName << outputDirectoryPath << resultImageName << '.' << configuration.GetElastixLevel() << ".R" << level
<< "." << resultImageFormat;

/** Time the resampling. */
itk::TimeProbe timer;
Expand Down Expand Up @@ -147,8 +149,10 @@ ResamplerBase<TElastix>::AfterEachIterationBase()
bool writeResultImageThisIteration = false;
configuration.ReadParameter(writeResultImageThisIteration, "WriteResultImageAfterEachIteration", "", level, 0, false);

const std::string outputDirectoryPath = configuration.GetCommandLineArgument("-out");

/** Writing result image. */
if (writeResultImageThisIteration)
if (writeResultImageThisIteration && !outputDirectoryPath.empty())
{
/** Set the final transform parameters. */
this->GetElastix()->GetElxTransformBase()->SetFinalParameters();
Expand All @@ -158,9 +162,8 @@ ResamplerBase<TElastix>::AfterEachIterationBase()
std::string resultImageFormat = "mhd";
configuration.ReadParameter(resultImageFormat, "ResultImageFormat", 0, false);
std::ostringstream makeFileName;
makeFileName << configuration.GetCommandLineArgument("-out") << resultImageName << '.'
<< configuration.GetElastixLevel() << ".R" << level << ".It" << std::setfill('0') << std::setw(7)
<< iter << "." << resultImageFormat;
makeFileName << outputDirectoryPath << resultImageName << '.' << configuration.GetElastixLevel() << ".R" << level
<< ".It" << std::setfill('0') << std::setw(7) << iter << "." << resultImageFormat;

/** Apply the final transform, and save the result. */
try
Expand Down Expand Up @@ -210,29 +213,28 @@ ResamplerBase<TElastix>::AfterRegistrationBase()
this->ReleaseMemory();
}

/**
* Create the result image and put it in ResultImageContainer
* Only necessary when compiling elastix as a library!
*/
if (isElastixLibrary)
/** Writing result image. */
if (writeResultImage == "true")
{
if (writeResultImage == "true")
/**
* Create the result image and put it in ResultImageContainer
* Only necessary when compiling elastix as a library!
*/
if (isElastixLibrary)
{
this->CreateItkResultImage();
}
}
else
{
/** Writing result image. */
if (writeResultImage == "true")

if (const std::string outputDirectoryPath = configuration.GetCommandLineArgument("-out");
!outputDirectoryPath.empty())
{
/** Create a name for the final result. */
const auto resultImageName = configuration.RetrieveParameterStringValue("result", "ResultImageName", 0, false);
std::string resultImageFormat = "mhd";
configuration.ReadParameter(resultImageFormat, "ResultImageFormat", 0);
std::ostringstream makeFileName;
makeFileName << configuration.GetCommandLineArgument("-out") << resultImageName << '.'
<< configuration.GetElastixLevel() << "." << resultImageFormat;
makeFileName << outputDirectoryPath << resultImageName << '.' << configuration.GetElastixLevel() << "."
<< resultImageFormat;

/** Time the resampling. */
itk::TimeProbe timer;
Expand All @@ -256,13 +258,13 @@ ResamplerBase<TElastix>::AfterRegistrationBase()
log::info(std::ostringstream{} << " Applying final transform took "
<< Conversion::SecondsToDHMS(timer.GetMean(), 2));
}
else
{
/** Do not apply the final transform. */
log::info(std::ostringstream{} << '\n'
<< "Skipping applying final transform, no resulting output image generated.");
} // end if
}
else
{
/** Do not apply the final transform. */
log::info(std::ostringstream{} << '\n'
<< "Skipping applying final transform, no resulting output image generated.");
} // end if

} // end AfterRegistrationBase()

Expand Down
Loading

0 comments on commit 99902e5

Please sign in to comment.