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

Adding new attributes to Atom class #47

Open
pcpatfire opened this issue Apr 4, 2021 · 12 comments
Open

Adding new attributes to Atom class #47

pcpatfire opened this issue Apr 4, 2021 · 12 comments

Comments

@pcpatfire
Copy link

Hi,
I need to add some more attributes to the Atom class. How can I do it, without changing your Atom and Structures classes?
For instance, I need to add the atomic mass information. I was using settattr to add it to each Atom instance, as follows:

for i in range(atom_num):
    # some code here...
    setattr(atoms[i], 'mass', mass_value)

s = Structure(atoms, lattice=lattice, title=title)

This solution is partially working, because the information is saved in the Atom instance, and I can get it as, for instance, s[5].mass (where 5 is the index of the 6th atom in the list), but s[5:7].mass generates the following error:

AttributeError: 'Structure' object has no attribute 'mass'

Is there a better to fix this issue?
Thank you

@hakonanes
Copy link
Contributor

Hi @pcpatfire,

the reason is that s[5] returns an Atom, while s[5:7] returns a Structure

>>> type(s[5])
diffpy.structure.atom.Atom
>>> type(s[5:7])
diffpy.structure.structure.Structure

So, to retrieve the mass attribute, you must target each Atom separately, not the Structure (which does not have a mass attribute)

>>> from diffpy.structure import Atom, Structure
>>> atoms = []
>>> for i in range(10):
...     a = Atom()
...     a.mass = i
...     atoms.append(a)
>>> s = Structure(atoms=atoms)
>>> [a.mass for a in s]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Does this help?

@hakonanes
Copy link
Contributor

Or

>>> [a.mass for a in s[5:7]]
[5, 6]

@pcpatfire
Copy link
Author

pcpatfire commented Apr 5, 2021 via email

@pcpatfire
Copy link
Author

Is there a way to link the mass attribute to the Structure class as it was done with the original Atom attributes, via _linkAtomAttribute? As, for instance:

    occupancy = _linkAtomAttribute('occupancy',
        '''Array of atom occupancies.  Assignment updates the
        occupancy attribute of all atoms.''')

@hakonanes
Copy link
Contributor

Glad it helped!

Is there a way to link the mass attribute to the Structure class as it was done with the original Atom attributes, via _linkAtomAttribute? As, for instance:

Yes, by creating your own MyStructure class inheriting from Structure, but you won't get s[5:7].mass to work unless you override the __getitem__() method as well:

>>> from diffpy.structure import Atom, Structure
>>> from diffpy.structure.utils import _linkAtomAttribute
>>> class MyStructure(Structure):
...     mass = _linkAtomAttribute("mass", doc="Atom mass")
...     def __getitem__(self, idx):
...         new = super().__getitem__(idx)
...         return self.__class__(new)
>>> atoms = []
>>> for i in range(10):
...     a = Atom()
...     a.mass = i
...     atoms.append(a)
>>> s = MyStructure(atoms=atoms)
>>> s.mass
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> s[5].mass
5
>>> s[5:7].mass
array([5, 6])

This overriding is not 100% waterproof, and there must be many use cases where you will get errors.

@pcpatfire
Copy link
Author

Thank you!

@sbillinge
Copy link
Contributor

sbillinge commented Apr 5, 2021 via email

@pcpatfire
Copy link
Author

pcpatfire commented Apr 5, 2021 via email

@sbillinge
Copy link
Contributor

sbillinge commented Apr 5, 2021 via email

@pcpatfire
Copy link
Author

pcpatfire commented Apr 5, 2021 via email

@sbillinge
Copy link
Contributor

sbillinge commented Apr 6, 2021 via email

@pcpatfire
Copy link
Author

Hi Simon, I was sending you an email to your official university email address.
I'm not sure you received it.
Pacifico

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

No branches or pull requests

3 participants