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 JSON serialization for Molecule #745

Merged
merged 5 commits into from Oct 27, 2020
Merged

Fix JSON serialization for Molecule #745

merged 5 commits into from Oct 27, 2020

Conversation

mattwthompson
Copy link
Member

Resolves #547

This tries to patch the dict inside of Serializable.to_json() to convert the conformers from bytes to lists, which surprisingly seem to survive round-trips. This should fix Molecule serialization but is almost guaranteed to not work for other classes class that need to serialize array data (Topology.box_vectors?). Improvements to the _prep_numpy_data_for_json function should enable this, it's currently a half-baked attempt at the general solution.

@codecov
Copy link

codecov bot commented Oct 13, 2020

Codecov Report

Merging #745 into master will increase coverage by 0.02%.
The diff coverage is 95.45%.

@j-wags j-wags self-requested a review October 26, 2020 16:45
Comment on lines +123 to +125
# TODO: More generally check for bytes in dict
if "conformers" in d.keys():
d = _prep_numpy_data_for_json(d)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is only a stopgap solution. It may be better to more generally check for the type of the data, but this field is the only one in the toolkit that currently causes this issue.

@mattwthompson
Copy link
Member Author

Passing locally after the version bump

@mattwthompson mattwthompson marked this pull request as ready for review October 26, 2020 19:13
Copy link
Member

@j-wags j-wags left a comment

Choose a reason for hiding this comment

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

Updated json serialization test to use minidrugbank (since it was already pytest.parametrized for it) and added the changes to the releasenotes. This should be ready to merge if tests pass!

@@ -416,6 +417,18 @@ def test_serialization_no_conformers(self):
pickle_copy = pickle.loads(pickle.dumps(mol))
assert mol == pickle_copy

def test_json_numpy_roundtrips(self):
Copy link
Member

Choose a reason for hiding this comment

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

👍 This is a cool test. I've always been vaguely concerned about precision loss and compounding rounding errors, so it's really neat to see this.

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 was pleasantly surprised to see it work

openforcefield/utils/serialization.py Outdated Show resolved Hide resolved
Co-authored-by: Jeff Wagner <jwagnerjpl@gmail.com>
@mattwthompson
Copy link
Member Author

LGTM is hanging but the changes here should be small enough in scope to not produce huge warning lights

@mattwthompson mattwthompson merged commit e1a20b9 into master Oct 27, 2020
@mattwthompson mattwthompson deleted the dump-json branch May 25, 2021 19:39
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.

JSON serialization of Molecule broken
2 participants