Skip to content

Adding new attributes to Atom class #47

Open

Description

@pcpatfire

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

Activity

  1. hakonanes commented on Apr 5, 2021

    @hakonanes
    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?

  2. hakonanes commented on Apr 5, 2021

    @hakonanes
    Contributor

    Or

    >>> [a.mass for a in s[5:7]]
    [5, 6]
  3. pcpatfire commented on Apr 5, 2021

    @pcpatfire
    Author
  4. pcpatfire commented on Apr 5, 2021

    @pcpatfire
    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.''')
    
  5. hakonanes commented on Apr 5, 2021

    @hakonanes
    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.

  6. pcpatfire commented on Apr 5, 2021

    @pcpatfire
    Author

    Thank you!

  7. sbillinge commented on Apr 5, 2021

    @sbillinge
    Contributor
  8. pcpatfire commented on Apr 5, 2021

    @pcpatfire
    Author
  9. sbillinge commented on Apr 5, 2021

    @sbillinge
    Contributor
  10. pcpatfire commented on Apr 5, 2021

    @pcpatfire
    Author
  11. sbillinge commented on Apr 6, 2021

    @sbillinge
    Contributor
  12. pcpatfire commented on Apr 7, 2021

    @pcpatfire
    Author

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

  13. added this to the 3.4.0-rc.0 release milestone on Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions