duartegroup / autodE

automated reaction profile generation
https://duartegroup.github.io/autodE/
MIT License
166 stars 51 forks source link

AutodE classes subclassed from ndarray cannot be serialized properly #221

Closed shoubhikraj closed 1 year ago

shoubhikraj commented 1 year ago

AutodE uses subclasses of ndarray (such as autode.values.Coordinates), with extra attributes such as units, other than the array. However, the attributes are not passed through when pickling. This means that the units etc. are lost when any of those objects are returned from parallel processes (as multiprocessing Queues use pickle to serialize from workers).

Minimal example:

>>> import autode as ade
>>> mol = ade.Molecule(smiles='CCO')
>>> x = mol.coordinates
>>> x
Coordinates([[ 0.8737  0.1872  0.0478]
 [-0.4674 -0.481  -0.0469]
 [-1.4902  0.3552 -0.417 ]
 [ 0.797   1.3     0.0437]
 [ 1.3353 -0.1674  1.0122]
 [ 1.5848 -0.1419 -0.747 ]
 [-0.7607 -0.9924  0.9124]
 [-0.3963 -1.3063 -0.793 ]
 [-1.4762  1.2465 -0.012 ]] Å)
>>> type(x)
<class 'autode.values.Coordinates'>
>>> pickle.loads(pickle.dumps(x,pickle.HIGHEST_PROTOCOL))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/cha17srm/miniconda3/envs/main/lib/python3.10/site-packages/autode/values.py", line 645, in __repr__
    return f'Coordinates({np.ndarray.__str__(self)} {self.units.name})'
AttributeError: 'Coordinates' object has no attribute 'units'

OS: Ubuntu 22.0.4. Also shows same error on Windows 10.

shoubhikraj commented 1 year ago

Will be fixed by #215