Skip to content

Conversation

@N-Dekker
Copy link
Member

@N-Dekker N-Dekker commented Jun 7, 2023

Allow the file name specified by the "InitialTransformParameterFileName" parameter in a transform parameter file to be relative to that transform parameter file.

@N-Dekker N-Dekker requested review from mstaring and stefanklein June 7, 2023 18:14

/** We can safely read the initial transform. */
this->ReadInitialTransformFromFile(fileName.c_str());
const auto lastConfigurationParameterFilePathSeparator = configurationParameterFileName.find_last_of("\\/");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does itksys not have a robust function for what you are trying to do here?

Copy link
Member Author

@N-Dekker N-Dekker Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does itksys not have a robust function for what you are trying to do here?

Good question 😃 I found some itksys functions that do similar things, by splitting a path to its components, and then joining them again:

  static std::vector<std::string> SplitString(const std::string& s,
                                              char separator = '/',
                                              bool isPath = false);
  static std::string Join(const std::vector<std::string>& list,
                          const std::string& separator);

But as far as I can see, it involves extra manual programming, it doesn't get much easier than what it does now: Just find the last separator (slash or backslash) in the current transform file path (by std::string::find_last_of), and then concatenate the current path up to that last separator with the string specified by "InitialTransformParameterFileName".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, just at least add a comment explaining what you do here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks! I just added some more comment, and also made the code clearer by introducing a local variable, isAbsoluteFilePath. Hope it's clear enough now!

Comment on lines 407 to 418
if (lastConfigurationParameterFilePathSeparator != std::string::npos && !startsWithFilePathSeparator &&
!hasDriveLetter && !itksys::SystemTools::FileExists(fileName))
{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could reverse if and else, and then do:

if (lastConfigurationParameterFilePathSeparator == std::string::npos 
    || startsWithFilePathSeparator
    || hasDriveLetter
    || itksys::SystemTools::FileExists(fileName))

To make the condition slightly simpler, maybe.

Could even just test on itksys::SystemTools::FileExists(fileName) and forget about the other three conditions. Might make the code slightly slower sometimes, but slightly easier to read.

Allow the file name specified by the "InitialTransformParameterFileName" parameter in a transform parameter file to be relative to that transform parameter file.
@N-Dekker N-Dekker force-pushed the Support-relative-InitialTransformParameterFileName branch from 0e96c2f to 088ec11 Compare June 8, 2023 10:34
@N-Dekker
Copy link
Member Author

N-Dekker commented Jun 8, 2023

Note that the pull request still supports "InitialTransformParameterFileName" file paths relative to the current working directory as well (rather than relative to the directory of the current transform parameter file). The current working directory is typically where the executable is started. This is important for backward compatibility, but still for the current executables as well. For example, suppose elastix is started in an input data directory, "C:\InputDir":

C:\InputDir>elastix -out D:\OutputDir -f fixed.mhd -m moving.mhd -t0 InitialTransformParameters.txt -p RegistrationParameters.txt

Creates a file "D:\OutputDir\TransformParameters.0.txt" having:

(InitialTransformParametersFileName "InitialTransformParameters.txt")

While the file InitialTransformParameters.txt could be located in "C:\InputDir". This still works fine after this pull request.

@N-Dekker N-Dekker merged commit a7ae46e into main Jun 8, 2023
@N-Dekker N-Dekker deleted the Support-relative-InitialTransformParameterFileName branch June 8, 2023 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants