Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: ensure np.interp gets an increasing sequence in LUTs #1282

Merged
merged 12 commits into from
Oct 16, 2024

Conversation

ZLLentz
Copy link
Member

@ZLLentz ZLLentz commented Sep 3, 2024

Description

  • Fix an issue where the LookupTablePositioner would fail silently if the lookup table was not strictly increasing in both axes.
  • Lookup tables that are strictly decreasing in either axis will now be supported.
  • Lookup tables that have inconsistent ordering in either axis will not log a warning when the errant conversion is done.
  • Refactor the LookupTablePosition class slightly to avoid code duplication

Motivation and Context

lxe in XCS didn't work on master because the look up table was decreasing instead of increasing.
np.interp expects the first array to be strictly increasing.

https://numpy.org/doc/stable/reference/generated/numpy.interp.html

How Has This Been Tested?

  • Interactively in XCS
  • Added unit tests

Where Has This Been Documented?

Pre-release notes and docstrings only

Pre-merge checklist

  • Code works interactively
  • Code contains descriptive docstrings, including context and API
  • New/changed functions and methods are covered in the test suite where possible
  • Test suite passes locally
  • Test suite passes on GitHub Actions
  • Ran docs/pre-release-notes.sh and created a pre-release documentation page
  • Pre-release docs include context, functional descriptions, and contributors as appropriate

fp = self._table_data_by_name[real_field]

# xp must be increasing
if not xp[1] > xp[0]:
Copy link
Contributor

Choose a reason for hiding this comment

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

This is very intolerant of noise in the signal, are we confident we won't jitter enough to trigger this by accident?

Copy link
Member Author

Choose a reason for hiding this comment

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

These come from fixed values in a text file that can look something like this:

50        71
55        41.5
60        18.8
65        6
70        1.3
75        0.5

Since they are fixed values there won't be any jitter, but I think it's worth adding a more thorough check so we can throw a warning if the values aren't monotonic

@ZLLentz ZLLentz marked this pull request as ready for review October 16, 2024 22:08
@ZLLentz ZLLentz requested a review from tangkong October 16, 2024 22:08
Copy link
Contributor

@tangkong tangkong left a comment

Choose a reason for hiding this comment

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

this seems right to me. Nice pseudopositioner tests. I looked through the commit history and the spirit of stayed true throughout the various iterations


assert lut.real.limits == (0, 9)
assert lut.pseudo.limits == (40, 400)
np.testing.assert_allclose(lut.forward(60 * ps)[0], 2 * rs)
Copy link
Contributor

Choose a reason for hiding this comment

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

how did I not know np.testing existed...

Copy link
Member Author

Choose a reason for hiding this comment

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

You know, I didn't actually read the functions I was adding the sign swaps to, I was only looking for numbers and figuring out whether they were the real positions or the pseudo positions, so I also didn't realize np.testing existed.

My test here is just using the pre-existing test from Ken but adding the cases where either side is decreasing instead of increasing.

Returns True if axis 1 of arr is strictly increasing and False otherwise.
"""
# see numpy.interp docs https://numpy.org/doc/stable/reference/generated/numpy.interp.html
return np.all(np.diff(arr) > 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess monotonically increasing does mean strictly increasing (never decreasing or staying the same). We just had the wrong idea of "monotonically increasing" before.

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 actually re-read the docs and found that they specified strictly increasing instead of monotonically increasing, the difference being that a monotonic sequence is allowed to repeat adjacent numbers or have a zero slope.

@ZLLentz ZLLentz merged commit fb7775d into pcdshub:master Oct 16, 2024
11 checks passed
@ZLLentz ZLLentz deleted the fix_decr_lut branch October 16, 2024 22:58
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