Skip to content

Commit

Permalink
ENH: Add check if optimizer supports scales
Browse files Browse the repository at this point in the history
Not all optimizers support scale weights.  The
itkImageRegistrationMethodv4 was unconditionally
setting the scales and causing a confusing warning
message to be presented (identified in python, but
also present in c++).  Providing an overridable member
function that returns the support for weighting scales
allow suppression of the warnings.
  • Loading branch information
hjmjohnson committed Nov 19, 2022
1 parent ce57f30 commit 1fefa3f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Modules/Numerics/Optimizers/include/itkLBFGSBOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ class ITKOptimizers_EXPORT LBFGSBOptimizer : public SingleValuedNonLinearVnlOpti
const std::string
GetStopConditionDescription() const override;

/** Returns false unconditionally because LBFGSBOptimizer does not support using scales. */
bool
CanUseScales() const override
{
return false;
}

protected:
LBFGSBOptimizer();
~LBFGSBOptimizer() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ class ITKOptimizers_EXPORT SingleValuedNonLinearVnlOptimizer : public SingleValu
itkGetConstReferenceMacro(CachedDerivative, DerivativeType);
itkGetConstReferenceMacro(CachedCurrentPosition, ParametersType);

/** Returns true if derived optimizer supports using scales.
* For optimizers that do not support scaling, this
* default function is overriden to return false.*/
virtual bool
CanUseScales() const
{
return true;
}

protected:
SingleValuedNonLinearVnlOptimizer();
~SingleValuedNonLinearVnlOptimizer() override;
Expand Down
7 changes: 7 additions & 0 deletions Modules/Numerics/Optimizersv4/include/itkLBFGSBOptimizerv4.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ class ITKOptimizersv4_EXPORT LBFGSBOptimizerv4 : public LBFGSOptimizerBasev4<vnl
* function. */
itkGetConstReferenceMacro(InfinityNormOfProjectedGradient, double);

/** Returns false unconditionally because LBFGSBOptimizerv4 does not support using scales. */
bool
CanUseScales() const override
{
return false;
}

protected:
LBFGSBOptimizerv4();
~LBFGSBOptimizerv4() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ class ITK_TEMPLATE_EXPORT ObjectToObjectOptimizerBaseTemplate : public Object
virtual const StopConditionReturnStringType
GetStopConditionDescription() const = 0;

/** Returns true if derived optimizer supports using scales.
* For optimizers that do not support scaling, this
* default function is overriden to return false.*/
virtual bool
CanUseScales() const
{
return true;
}

protected:
/** Default constructor */
ObjectToObjectOptimizerBaseTemplate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,8 @@ ImageRegistrationMethodv4<TFixedImage, TMovingImage, TTransform, TVirtualImage,

this->m_Optimizer->SetMetric(this->m_Metric);

if ((this->m_Optimizer->GetScales()).Size() != this->m_OutputTransform->GetNumberOfLocalParameters())
if (this->m_Optimizer->CanUseScales() &&
((this->m_Optimizer->GetScales()).Size() != this->m_OutputTransform->GetNumberOfLocalParameters()))
{
using ScalesType = typename OptimizerType::ScalesType;
ScalesType scales;
Expand Down

0 comments on commit 1fefa3f

Please sign in to comment.