feat: optimize stretch using a Hessian matrix by john-halloran · Pull Request #204 · diffpy/diffpy.stretched-nmf · GitHub
Skip to content

feat: optimize stretch using a Hessian matrix#204

Merged
sbillinge merged 4 commits into
diffpy:mainfrom
john-halloran:hessian-fix
Jun 28, 2026
Merged

feat: optimize stretch using a Hessian matrix#204
sbillinge merged 4 commits into
diffpy:mainfrom
john-halloran:hessian-fix

Conversation

@john-halloran

Copy link
Copy Markdown
Contributor

This is another PR bringing us closer to parity with MATLAB. Proper use of a Hessian was key to the speed and quality of convergence they were able to obtain. I had tried this a few times before, but it never worked even though the Hessian had the right shape. By properly regularizing, it now works.

@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

@sbillinge sbillinge left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

pls see comment

gra = gra.flatten()
return fun, gra

def hessian(stretch_vec):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need a test for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea. I have added a test checking for some of the expected properties (right shape, symmetric, correct ordering/cross-coupling).

@sbillinge sbillinge left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please see comment

return fun, gra

def _regularize_function_hessian(self, stretch):
residuals, d_stretch_comps, dd_stretch_comps = (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this could use some dicumentation and also maybe a more descriptive variable name. Stretch is a verb usually and I would expect a noun here.

We were pretty lazy about documenting code in the early part of the project because we wanted it to get going from a low place, but we are at the point of the project where we can take the time to make the code more maintainable moving forward.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, I have added a numpy style docstring as used for some of the other functions.

As for renaming stretch, I am open to it, but that would be a broader refactor that I'd be more comfortable carrying out once the current PRs are merged. The word "stretch" is used for this variable name to be consistent with the class variable stretch, which is broadly used throughout the entire code. It, along with components and weights are some of the most-used words in the entire file. Replacing it with a longer term would also have the side effect of visually bloating certain lines of code that use the variable repeatedly in calculations.
That being said, we could try something like stretch_factors which makes it more clear we are dealing with a noun. I'd just prefer to hold off on it for now.

@sbillinge sbillinge Jun 28, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok. The code is really not very readable ATM which would make it harder to maintain in the future, but I am ok to put this off for now.

@sbillinge sbillinge left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please see my comment. I will merge like this but we really need to refactor to make the code much more readable now it is working

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Always start descriptions of parameters with "The"

@sbillinge sbillinge merged commit 3916f48 into diffpy:main Jun 28, 2026
7 checks passed
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.

2 participants