-
Notifications
You must be signed in to change notification settings - Fork 761
Fixes #5170: Improve msd_type parsing #5173
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
base: develop
Are you sure you want to change the base?
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #5173 +/- ##
========================================
Coverage 92.72% 92.72%
========================================
Files 180 180
Lines 22472 22477 +5
Branches 3188 3189 +1
========================================
+ Hits 20837 20842 +5
Misses 1177 1177
Partials 458 458 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
package/MDAnalysis/analysis/msd.py
Outdated
| # Validate type first | ||
| if not isinstance(self.msd_type, str): | ||
| raise TypeError("msd_type must be a string") | ||
|
|
||
| # strip whitespace + lowercase | ||
| self.msd_type = self.msd_type.strip().lower() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added these lines in msd.py , they do -
- Explicitly checks that
msd_typeis a string to avoid unexpected AttributeErrors. - Uses
.strip().lower()to handle inputs with leading/trailing whitespace
(e.g. " xy ", " Xz "), while preserving existing case-insensitive behavior.
| def test_msd_type_whitespace(self, u, SELECTION): | ||
| m = MSD(u, SELECTION, msd_type=" xy ", fft=False) | ||
| assert m.dim_fac == 2 | ||
| assert m._dim == [0, 1] | ||
|
|
||
| def test_msd_type_uppercase(self, u, SELECTION): | ||
| m = MSD(u, SELECTION, msd_type=" Xz ", fft=False) | ||
| assert m.dim_fac == 2 | ||
| assert m._dim == [0, 2] | ||
|
|
||
| def test_msd_type_nonstring(self, u, SELECTION): | ||
| with pytest.raises(TypeError): | ||
| MSD(u, SELECTION, msd_type=123, fft=False) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added tests covering msd_type input handling in EinsteinMSD:
- whitespace handling (e.g. " xy ")
- case-insensitive parsing (existing behavior)
- non-string inputs raising
TypeError
|
@Dreamstick9 thank you for pointing this out ! Since the use of
|
|
One more thing I noticed in test_msd.py: test_msd_type_whitespace_around_valid_value expects a ValueError for " xy ". Since the PR goal is to support whitespace, shouldn't this test expect success instead of an error? Currently, this test will likely fail because the code raises a KeyError (due to the unstripped string) rather than the expected ValueError. |
|
This is intentional. As discussed in #5170, we decided not to silently normalize whitespace. Inputs like |

This PR implements enhancement proposed in issue #5170
Summary
EinsteinMSD currently lowercases the msd_type string but does not:
What This PR Does?
.strip()before.lower()to handle inputs like " xy ".All tests pass locally
📚 Documentation preview 📚: https://mdanalysis--5173.org.readthedocs.build/en/5173/