-
Notifications
You must be signed in to change notification settings - Fork 94
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
Document that Molecule.from_dict accepts a list of lists but not a numpy array #1998
Comments
You probably already figured it out, but the fulcrum of the issue is what types are handled by the little helper >>> from openff.toolkit.utils.utils import deserialize_numpy
/Users/mattthompson/micromamba/envs/openff-interchange-env/lib/python3.11/site-packages/openff/utilities/provenance.py:23: CondaExecutableNotFoundWarning: No conda/mamba/micromamba executable found. Unable to determine package versions.
warnings.warn(
>>> import numpy
>>> deserialize_numpy(numpy.eye(3).tolist(), shape=(3, 3))
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> deserialize_numpy(numpy.eye(3), shape=(3, 3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mattthompson/software/openff-toolkit/openff/toolkit/utils/utils.py", line 433, in deserialize_numpy
np_array = np_array.reshape(shape)
^^^^^^^^
UnboundLocalError: cannot access local variable 'np_array' where it is not associated with a value
>>> Something like this should smooth that over:
diff --git a/openff/toolkit/utils/utils.py b/openff/toolkit/utils/utils.py
index db599d0a..1f64e80a 100644
--- a/openff/toolkit/utils/utils.py
+++ b/openff/toolkit/utils/utils.py
@@ -406,7 +406,7 @@ def serialize_numpy(np_array) -> tuple[bytes, tuple[int]]:
def deserialize_numpy(
- serialized_np: Union[bytes, list],
+ serialized_np: Union[NDArray, bytes, list],
shape: tuple[int, ...],
) -> NDArray:
"""
@@ -425,11 +425,19 @@ def deserialize_numpy(
np_array
The deserialized numpy array
"""
- if isinstance(serialized_np, list):
+ if isinstance(serialized_np, np.ndarray):
+ np_array = serialized_np
+ elif isinstance(serialized_np, list):
np_array = np.array(serialized_np)
- if isinstance(serialized_np, bytes):
+ elif isinstance(serialized_np, bytes):
dt = np.dtype("float").newbyteorder(">")
np_array = np.frombuffer(serialized_np, dtype=dt)
+ else:
+ raise ValueError(
+ f"Unexpected type ({type(serialized_np)}) found when attempting to deserialize from the "
+ "serialized form of a numpy array."
+ )
+
np_array = np_array.reshape(shape)
return np_array
>>> deserialize_numpy(numpy.eye(3).tolist(), shape=(3, 3))
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> deserialize_numpy(numpy.eye(3), shape=(3, 3))
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> Arguably this is unsupported since the dict model of a |
Yeah, Matt's right here - This isn't intended to be a supported use case. This might be resolved by better docs (like, "the exact format of the input dict for this method is determined by the output of Molecule.to_dict") |
Describe the bug
When using the
Molecule.from_dict
method,molecule_dict["conformers"][0]
cannot be a numpy array.To Reproduce
Output
Computing environment (please complete the following information):
conda list
:Additional context
removing np.array() from the conformers key (so a list of lists) will allow the example to run as expected.
The text was updated successfully, but these errors were encountered: