-
Notifications
You must be signed in to change notification settings - Fork 663
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
Improve missing AtomGroup property error messages #2565
Comments
Hey @zemanj ! I'll get onto it. Thanks! |
Hi @Naimish240, thank you for taking this on! Please see also the discussion in #2536, there are some hints regarding how this can be handled properly. |
Hi @zemanj, I have submitted a PR, please check it out. Thank you! |
Hi @zemanj, I would like to work on this. I want to know whether the error should be generalised to cover many attributes or just raise a NoDataError every time it comes across an unknown attribute with a better error message? |
@shfrz @AnshulAngaria I appreciate your efforts. However, I have to say that I don't think it's good practice to "highjack" an issue another person is already working on (@Naimish240 in this case, see above). Nevertheless, I'll review your PRs but keep in mind that if @Naimish240 comes up with a proper solution in a timely manner, this is the one that will be merged. If you think you have a good solution for an issue someone else is already about to work on, you should discuss this with him/her before moving ahead. |
I edited my initial issue comment and added a detailed description of the problem. Furthermore, I sketched what I think a possible solution might look like. |
@zemanj this sounds very good. The dictionaries don't need to be created by hand, (and shouldn't), instead they can get registered when the class is defined. This sort of magic is already in use with https://github.com/MDAnalysis/mdanalysis/blob/develop/package/MDAnalysis/core/universe.py#L805 So what would need doing is here:
You would need to add a scan through the _TOPOLOGY_ERRORS = {
'center_of_mass': 'This requires masses',
} That can be used for making nice |
Hi @richardjgowers, this seems an elegant approach. I would love to implement that. But let's see what @Naimish240 comes up with first. |
@richardjgowers That sounds exactly like the solution I was looking for! As far as I can see, the only downside is that this machinery won't be on-demand (cached) but executed on import. |
Ping @Naimish240 |
As @Naimish240 seems to be unavailable, someone else may give this a go. |
Expected behavior
When I try to access an
AtomGroup
property that is generally valid but the corresponding data is missing from the underlying topology/trajectory, I expect aNoDataError
with a message stating that the data I am requesting is not available from the topology/trajectory.Actual behavior
When I load a topology that doesn't contain information about, e.g., charges, and try to access that
AtomGroup
property, anAttributeError: AtomGroup has no attribute charges
is raised.Code to reproduce the behavior
Version of MDAnalysis
Current 0.20.1 dev branch with Python 3.6 under Fedora 31
Should be version-independent though.
Detailed problem description
The
AttributeError
still makes sense if one tries to access an attribute that is generally unknown toAtom
orAtomGroup
. Thus, one needs to distinguish between attributes which could be there if the respective topology/trajectory info was there, and others that generally don't exist.Furthermore, there are derived attributes such as
fragindices
, which are constructed from the topology'sbond
information. Thus, the error message for such derived attributes should state the missing information from which they are derived. The same is true for methods that require certain data, so that trying to accessAtomGroup.center_of_mass()
should raise aNoDataError
due to missing information about masses.Some examples: in the code example given above
u.atoms.boogiewoogie
should raise anAttributeError
u.atoms.charges
should raise aNoDataError
due to missing charge informationu.atoms.fragindices
should raise aNoDataError
due to missing bond informationu.atoms.unwrap()
should raise aNoDataError
due to missing bond informationu.atoms.total_charge()
should raise aNoDataError
due to missing charge informationProposed solution
AtomGroup
attributes as keys and the corresponding topology/trajectory information which is required for the attribute to exists.Atom
attributes.__getattr__()
methods ofAtom
andAtomGroup
, check whether the requested attribute is present in the respective dictionary's keys. If (and only if) that is the case, raise aNoDataError
with an error message clarifying the problem. Otherwise, do nothing so that anAttributeError
is automatically raised whenever a truly unknown attribute is accessed.Regarding points 1 and 2, this involves parsing the
transplants
ofAtomGroup
and probably also those ofAtom
. Thanks to @richardjgowers we know how to automate the process so that the bookkeeping is done automatically, see his comment below.The text was updated successfully, but these errors were encountered: