Skip to content

Conversation

@ycexiao
Copy link
Collaborator

@ycexiao ycexiao commented Aug 18, 2025

What problem does this PR address?

Address the first item in #241.

raise ValueError if x_squeezed is not strictly increasing.

What should the reviewer(s) do?

Please check the implementation.

if instead of try and except is used since it is more specific to the failed case. Please see which one do we want to use.

@codecov
Copy link

codecov bot commented Aug 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.92%. Comparing base (dce54cf) to head (add0413).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #248   +/-   ##
=======================================
  Coverage   99.92%   99.92%           
=======================================
  Files          23       23           
  Lines        1355     1355           
=======================================
  Hits         1354     1354           
  Misses          1        1           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ycexiao ycexiao marked this pull request as ready for review August 18, 2025 20:25
@ycexiao
Copy link
Collaborator Author

ycexiao commented Aug 18, 2025

@sbillinge, it's ready for review.

if not strictly_increasing_x:
raise ValueError(
"Computed squeezed x is not strictly increasing. "
"Please change the input x_morph or the squeeze "
Copy link
Contributor

Choose a reason for hiding this comment

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

If possible let's have a more helpfule message here. I am a user. OK, I need to change those things but what do I change them to? How do I change them?

Copy link
Collaborator Author

@ycexiao ycexiao Aug 19, 2025

Choose a reason for hiding this comment

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

@sbillinge What about

                "Computed squeezed x is not strictly increasing. "
                "Please change the input x_morph or the coefficients  "
                "so that the polynomial a0 + (a1+1) * x_morph "
                "+ a2 * x_morph**2 + ...  is strictly increasing."

?

Copy link
Contributor

Choose a reason for hiding this comment

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

how about
"The squeeze parameters resulted in an x-array that is not strictly increasing which is nonsensical. Please consider squeezing with a lower-order polynomial or over a narrowing range"

Please chech that what I wrote actually makes sense. @Sparks29032 ?

Copy link
Collaborator

@Sparks29032 Sparks29032 Sep 12, 2025

Choose a reason for hiding this comment

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

@sbillinge I agree with lowering the polynomial order, but narrowing the range may not always help resolve this.

One thing I found works empirically (no theory to back this up, and I doubt there will be) is first using stretch and hshift (since they are monotonic) to obtain a decent fit and using that as a0, a1.

Another thing that definitely helps is ensuring that your two functions are relatively close after applying your initial guess.

Perhaps we change "narrowing range" to "over a region of closer agreement". Need to reword but that kind of idea.

@Sparks29032
Copy link
Collaborator

Sparks29032 commented Aug 20, 2025

See my comment on #249.

Also, the error message should indicate that this is a refinement issue, not a user issue. Maybe something along the lines of squeeze only works for small coefficients or something and suggest morphfuncx as an alternative?

@Sparks29032
Copy link
Collaborator

Ideally, we don't ever have to throw this error out in the first place. Is there an alternative to CubicSpline that lets us interpolate onto a non-increasing grid? What if we actually sort the grid before applying squeeze, does that mess up the morph?

@ycexiao
Copy link
Collaborator Author

ycexiao commented Aug 25, 2025

Is there an alternative to CubicSpline that lets us interpolate onto a non-increasing grid?

@Sparks29032
I think the motive to use a strictly increasing grid is to make sure there is only one function value per input. Non-monotonicity of a continuous function itself implies there are duplicated function values, which is x_squeezed in here. And duplicated x_squeezed should be handled as an Error.

@Sparks29032
Copy link
Collaborator

Sparks29032 commented Aug 25, 2025

Non-monotonicity does not necessarily imply duplicated values on a discrete grid, though the issue of duplicated values is definitely important; thanks for pointing that out.

My main concern is that this morph often gives this error during refinement on a dense grid since switching two adjacent function values around does not greatly change the Rw, hence giving non-monotonic squeeze values.

Is there a way that we can enforce monotonicity during refinement? Or can we correct for non-monotonicity somehow?

@Sparks29032
Copy link
Collaborator

Hmm, techniques to do so seem a bit complex for the purposes of diffpy.morph. If we want to just throw this error, we can tell the user that this morph is only intended for small polynomial stretches.

Copy link
Collaborator Author

@ycexiao ycexiao left a comment

Choose a reason for hiding this comment

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

@Sparks29032 @sbillinge , it's ready for review.

strictly_increasing_x = (np.diff(x_squeezed) > 0).all()
if not strictly_increasing_x:
raise ValueError(
"Computed squeezed x is not strictly increasing. "
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The error message is updated.

Copy link
Contributor

Choose a reason for hiding this comment

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

I added a suggested error message on the main comment thread. I think the non-strictly increasing problem probably emerges after the squeeze regression runs, so it may not matter what starting parameters the user gives (if the regression is working as hoped).

raise ValueError(
"Computed squeezed x is not strictly increasing. "
"The squeezed morph is only intended for small polynomial "
"stretches. Please decrease the magnitude of the polynomial "
Copy link
Contributor

Choose a reason for hiding this comment

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

this still seems pretty cryptic to me. If I got this message as a user I wouldn't know what to change if I ran again. there is agreat discussion on the comment thread which is way clearer. Why don't we capture some of that here. It is ok if the message is long. @Sparks29032 please could you draft something. Tell the whole story, including the "trick" of getting good starting values with squeeze and hshift and using for a0 and a1.

Btw, the fact that this works seems to suggest that it is often a convergence issue and not, in those cases, a fundamental issue, i.e., the regression is stuck in a local minimum far from the right solution when this error happens, so we can mention that too?

Copy link
Collaborator

@Sparks29032 Sparks29032 Sep 13, 2025

Choose a reason for hiding this comment

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

@sbillinge The story I would tell is only empirical from the limited times I've used this morph:

Error: The polynomial applied by the squeeze morph has resulted in a grid that is no longer strictly increasing, likely due to a convergence issue. A strictly increasing grid is required for diffpy.morph to compute the morphed function through cubic spline interpolation. Here are some suggested methods to resolve this:
(1) Please decrease the order of your polynomial and try again.
(2) If you are using initial guesses of all 0, please ensure your objective function only requires a small polynomial squeeze to match your reference. (In other words, there is good agreement between the two functions.)
(3) If you expect a large polynomial squeeze to be needed, please ensure your initial parameters for the polynomial morph result in good agreement between your reference and objective functions. One way to obtain such parameters is to first apply a --hshift and --stretch morph. Then, use the hshift parameter for a0 and stretch parameter for a1.

However, I think we should allow squeeze to obtain non-monotonic values. See #256 for implementation.

@ycexiao
Copy link
Collaborator Author

ycexiao commented Sep 15, 2025

Closed, as we will handle the not-strictly-increasing x_squeezed case in #256

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