Closed shoubhikraj closed 1 year ago
I can reproduce this – absolutely wild. no idea why this happens. thoughts appreciated!
@t-young31 Just found that the same behaviour can be reproduced by using inspect.
>>> inspect.getmembers(mol.hessian)
>>> mol.hessian.units
Unit(J ang^-2)
The code that getmembers is running is here: https://github.com/python/cpython/blob/498598e8c2d64232d26c075de87c513415176bbf/Lib/inspect.py#L555
So there is something there which is changing the units.
I reckon getmembers
is evaluating the _mass_weighted
cached_property, which converts to "Unit(J ang^-2)". It should probably take a copy right?
@t-young31 Yes, I think the issue is coming from the conversion procedure itself here: https://github.com/duartegroup/autodE/blob/6813afd7523091352634f37339e75c9cf95f232f/autode/values.py#L74-L77
I think that assigning to the original object's slice causes the original array object to be modified, instead of creating a new object.
I think that assigning to the original object's slice causes the original array object to be modified, instead of creating a new object.
I think that's the desired behaviour of to
but a method that modifies an array in place should really put it back right!
@t-young31 I don't understand why to
has to modify the underlying object, why not send back a modified copy:
new_value = value.__class__(np.array(value, copy=True) * c, units=units)
return new_value
Since to
is returning something, there is no need to modify inplace.
It doesn't have to modify the object but in general unnecessary copies are bad.. pytorch/pandas offers both. I'm not sure either are 'better' (but definitely a method that modifies in-place to do something should revert it)
Only seen from interactive python shell.
I have looked at the values in the Hessian and the units really do change. (i.e. the numbers change as well) I am not quite sure why this is happening. But it would be problematic if the units changed randomly in the middle of calculations.
Environment