Skip to content

Commit

Permalink
Merge pull request #3742 from hjmjohnson/add-supports-scales
Browse files Browse the repository at this point in the history
ENH: Add check if optimizer supports scales
  • Loading branch information
thewtex committed Nov 23, 2022
2 parents 7c19c4a + 1fefa3f commit d19e774
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 d19e774

Please sign in to comment.