You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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'
Hi @pcpatfire <https://github.com/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?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#47 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMDYOGFXEDWE3LUDJBOTHULTHF4Y7ANCNFSM42LCFP6A>
.
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.''')
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:
Thanks Hakon.
@pcpatfire, I would advise against overriding __getitem__ in Structure
because it does a lot of work that you will lose and I don't know the
consequences. Also, you gain very little.
you can use the syntax s[5:7] to get an array of atom atom objects, but
then you have to iterate over the list to get the atom attributes. Instead
you could just iterate directly over the atom object returned by structure,
e.g.,
[s[i].mass for i in range(len(atom_list))]
to get the same thing without using the slicing functionality
S
On Mon, Apr 5, 2021 at 10:56 AM Håkon Wiik Ånes ***@***.***> wrote:
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.massarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> s[5].mass5>>> s[5:7].massarray([5, 6])
This overriding is *not* 100% waterproof, and there must be many use
cases where you will get errors.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#47 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABAOWUIRQ6LYJQD7ABFF3U3THHFRBANCNFSM42LCFP6A>
.
--
Simon Billinge
Professor, Columbia University
Physicist, Brookhaven National Laboratory
Thanks Hakon.
@pcpatfire, I would advise against overriding __getitem__ in Structure
because it does a lot of work that you will lose and I don't know the
consequences. Also, you gain very little.
you can use the syntax s[5:7] to get an array of atom atom objects, but
then you have to iterate over the list to get the atom attributes. Instead
you could just iterate directly over the atom object returned by structure,
e.g.,
[s[i].mass for i in range(len(atom_list))]
to get the same thing without using the slicing functionality
S
On Mon, Apr 5, 2021 at 10:56 AM Håkon Wiik Ånes ***@***.***>
wrote:
> 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.massarray([0,
1, 2, 3, 4, 5, 6, 7, 8, 9])>>> s[5].mass5>>> s[5:7].massarray([5, 6])
>
> This overriding is *not* 100% waterproof, and there must be many use
> cases where you will get errors.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <
#47 (comment)
>,
> or unsubscribe
> <
https://github.com/notifications/unsubscribe-auth/ABAOWUIRQ6LYJQD7ABFF3U3THHFRBANCNFSM42LCFP6A
>
> .
>
--
Simon Billinge
Professor, Columbia University
Physicist, Brookhaven National Laboratory
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#47 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMDYOGEHBB6WETCRSFIJAZLTHHHYRANCNFSM42LCFP6A>
.
if there is enough interest we could add mass as an atom attribute in the
base code. I think it is available in hte PeriodicTable data that we are
pulling the information from....
....as always, PRs from friendly members of the community are welcome 😉
On Mon, Apr 5, 2021 at 11:20 AM pcpatfire ***@***.***> wrote:
Thanks Simon. I think I will do as you suggest.
Il Lun 5 Apr 2021, 17:15 Simon Billinge ***@***.***> ha
scritto:
> Thanks Hakon.
>
> @pcpatfire, I would advise against overriding __getitem__ in Structure
> because it does a lot of work that you will lose and I don't know the
> consequences. Also, you gain very little.
>
> you can use the syntax s[5:7] to get an array of atom atom objects, but
> then you have to iterate over the list to get the atom attributes.
Instead
> you could just iterate directly over the atom object returned by
structure,
> e.g.,
>
> [s[i].mass for i in range(len(atom_list))]
>
> to get the same thing without using the slicing functionality
>
> S
>
> On Mon, Apr 5, 2021 at 10:56 AM Håkon Wiik Ånes ***@***.***>
> wrote:
>
> > 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.massarray([0,
> 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> s[5].mass5>>> s[5:7].massarray([5, 6])
> >
> > This overriding is *not* 100% waterproof, and there must be many use
> > cases where you will get errors.
> >
> > —
> > You are receiving this because you are subscribed to this thread.
> > Reply to this email directly, view it on GitHub
> > <
>
#47 (comment)
> >,
> > or unsubscribe
> > <
>
https://github.com/notifications/unsubscribe-auth/ABAOWUIRQ6LYJQD7ABFF3U3THHFRBANCNFSM42LCFP6A
> >
> > .
> >
>
>
> --
> Simon Billinge
> Professor, Columbia University
> Physicist, Brookhaven National Laboratory
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <
#47 (comment)
>,
> or unsubscribe
> <
https://github.com/notifications/unsubscribe-auth/AMDYOGEHBB6WETCRSFIJAZLTHHHYRANCNFSM42LCFP6A
>
> .
>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#47 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABAOWUKXTBA6FHLYJCUI2UDTHHINFANCNFSM42LCFP6A>
.
--
Simon Billinge
Professor, Columbia University
Physicist, Brookhaven National Laboratory
Yes, I think it could be useful. I was using the atom mass as an example. I
found the diffpy.structure package very useful to create a parser for the
HISTORY output file of the DLPOLY application - a huge file (some GB) with
thousands of crystal structures with thousands of atoms each. There are
several more attributes such as mass and charge, but also some other
information as atom velocity and force components. This information is
loaded from the HISTORY file itself. In this case I need to add more
attributes to the Atom instances to store those data too. Another
information I need in general is the atomic radius, because the final aim
of my application will be to represent the crystal structure in 3D, and I
am displaying the atoms as spheres whose radius is proportional to the real
atomic radius. My intention is to use the atom_data.py from the crystals
package, into which I added my own radius information - that can be
retrieved using the 'element' attribute defined in the Atom class. By the
way, the atom_data.py file contains many interesting information about all
the elements, among which the atomic masses too.
Thank you again
Pacifico Cofrancesco
Chemistry Department - University of Pavia (Italy)
Il giorno lun 5 apr 2021 alle ore 17:30 Simon Billinge <
***@***.***> ha scritto:
if there is enough interest we could add mass as an atom attribute in the
base code. I think it is available in hte PeriodicTable data that we are
pulling the information from....
....as always, PRs from friendly members of the community are welcome 😉
On Mon, Apr 5, 2021 at 11:20 AM pcpatfire ***@***.***> wrote:
> Thanks Simon. I think I will do as you suggest.
>
> Il Lun 5 Apr 2021, 17:15 Simon Billinge ***@***.***> ha
> scritto:
>
> > Thanks Hakon.
> >
> > @pcpatfire, I would advise against overriding __getitem__ in Structure
> > because it does a lot of work that you will lose and I don't know the
> > consequences. Also, you gain very little.
> >
> > you can use the syntax s[5:7] to get an array of atom atom objects, but
> > then you have to iterate over the list to get the atom attributes.
> Instead
> > you could just iterate directly over the atom object returned by
> structure,
> > e.g.,
> >
> > [s[i].mass for i in range(len(atom_list))]
> >
> > to get the same thing without using the slicing functionality
> >
> > S
> >
> > On Mon, Apr 5, 2021 at 10:56 AM Håkon Wiik Ånes ***@***.***>
> > wrote:
> >
> > > 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.massarray([0,
> > 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> s[5].mass5>>> s[5:7].massarray([5, 6])
> > >
> > > This overriding is *not* 100% waterproof, and there must be many use
> > > cases where you will get errors.
> > >
> > > —
> > > You are receiving this because you are subscribed to this thread.
> > > Reply to this email directly, view it on GitHub
> > > <
> >
>
#47 (comment)
> > >,
> > > or unsubscribe
> > > <
> >
>
https://github.com/notifications/unsubscribe-auth/ABAOWUIRQ6LYJQD7ABFF3U3THHFRBANCNFSM42LCFP6A
> > >
> > > .
> > >
> >
> >
> > --
> > Simon Billinge
> > Professor, Columbia University
> > Physicist, Brookhaven National Laboratory
> >
> > —
> > You are receiving this because you were mentioned.
> > Reply to this email directly, view it on GitHub
> > <
>
#47 (comment)
> >,
> > or unsubscribe
> > <
>
https://github.com/notifications/unsubscribe-auth/AMDYOGEHBB6WETCRSFIJAZLTHHHYRANCNFSM42LCFP6A
> >
> > .
> >
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <
#47 (comment)
>,
> or unsubscribe
> <
https://github.com/notifications/unsubscribe-auth/ABAOWUKXTBA6FHLYJCUI2UDTHHINFANCNFSM42LCFP6A
>
> .
>
--
Simon Billinge
Professor, Columbia University
Physicist, Brookhaven National Laboratory
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#47 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMDYOGAI4DSAG2LOEVXCKMLTHHJRBANCNFSM42LCFP6A>
.
Thanks Pacifico, This is a very interesting application. In general we
want to be able to parse MD trajectories so your parser itself would be a
nice addition to the diffpy package, and we could work in updates to the
Atom class attributes, if you would be interested in working with us to
get it incorporated. Can you reach out off the main list and we can
discuss it?
S
On Mon, Apr 5, 2021 at 11:59 AM pcpatfire ***@***.***> wrote:
Yes, I think it could be useful. I was using the atom mass as an example. I
found the diffpy.structure package very useful to create a parser for the
HISTORY output file of the DLPOLY application - a huge file (some GB) with
thousands of crystal structures with thousands of atoms each. There are
several more attributes such as mass and charge, but also some other
information as atom velocity and force components. This information is
loaded from the HISTORY file itself. In this case I need to add more
attributes to the Atom instances to store those data too. Another
information I need in general is the atomic radius, because the final aim
of my application will be to represent the crystal structure in 3D, and I
am displaying the atoms as spheres whose radius is proportional to the real
atomic radius. My intention is to use the atom_data.py from the crystals
package, into which I added my own radius information - that can be
retrieved using the 'element' attribute defined in the Atom class. By the
way, the atom_data.py file contains many interesting information about all
the elements, among which the atomic masses too.
Thank you again
Pacifico Cofrancesco
Chemistry Department - University of Pavia (Italy)
Il giorno lun 5 apr 2021 alle ore 17:30 Simon Billinge <
***@***.***> ha scritto:
> if there is enough interest we could add mass as an atom attribute in the
> base code. I think it is available in hte PeriodicTable data that we are
> pulling the information from....
>
> ....as always, PRs from friendly members of the community are welcome 😉
>
> On Mon, Apr 5, 2021 at 11:20 AM pcpatfire ***@***.***> wrote:
>
> > Thanks Simon. I think I will do as you suggest.
> >
> > Il Lun 5 Apr 2021, 17:15 Simon Billinge ***@***.***> ha
> > scritto:
> >
> > > Thanks Hakon.
> > >
> > > @pcpatfire, I would advise against overriding __getitem__ in
Structure
> > > because it does a lot of work that you will lose and I don't know the
> > > consequences. Also, you gain very little.
> > >
> > > you can use the syntax s[5:7] to get an array of atom atom objects,
but
> > > then you have to iterate over the list to get the atom attributes.
> > Instead
> > > you could just iterate directly over the atom object returned by
> > structure,
> > > e.g.,
> > >
> > > [s[i].mass for i in range(len(atom_list))]
> > >
> > > to get the same thing without using the slicing functionality
> > >
> > > S
> > >
> > > On Mon, Apr 5, 2021 at 10:56 AM Håkon Wiik Ånes ***@***.***>
> > > wrote:
> > >
> > > > 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.massarray([0,
> > > 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> s[5].mass5>>> s[5:7].massarray([5, 6])
> > > >
> > > > This overriding is *not* 100% waterproof, and there must be many
use
> > > > cases where you will get errors.
> > > >
> > > > —
> > > > You are receiving this because you are subscribed to this thread.
> > > > Reply to this email directly, view it on GitHub
> > > > <
> > >
> >
>
#47 (comment)
> > > >,
> > > > or unsubscribe
> > > > <
> > >
> >
>
https://github.com/notifications/unsubscribe-auth/ABAOWUIRQ6LYJQD7ABFF3U3THHFRBANCNFSM42LCFP6A
> > > >
> > > > .
> > > >
> > >
> > >
> > > --
> > > Simon Billinge
> > > Professor, Columbia University
> > > Physicist, Brookhaven National Laboratory
> > >
> > > —
> > > You are receiving this because you were mentioned.
> > > Reply to this email directly, view it on GitHub
> > > <
> >
>
#47 (comment)
> > >,
> > > or unsubscribe
> > > <
> >
>
https://github.com/notifications/unsubscribe-auth/AMDYOGEHBB6WETCRSFIJAZLTHHHYRANCNFSM42LCFP6A
> > >
> > > .
> > >
> >
> > —
> > You are receiving this because you commented.
> > Reply to this email directly, view it on GitHub
> > <
>
#47 (comment)
> >,
> > or unsubscribe
> > <
>
https://github.com/notifications/unsubscribe-auth/ABAOWUKXTBA6FHLYJCUI2UDTHHINFANCNFSM42LCFP6A
> >
> > .
> >
>
>
> --
> Simon Billinge
> Professor, Columbia University
> Physicist, Brookhaven National Laboratory
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <
#47 (comment)
>,
> or unsubscribe
> <
https://github.com/notifications/unsubscribe-auth/AMDYOGAI4DSAG2LOEVXCKMLTHHJRBANCNFSM42LCFP6A
>
> .
>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#47 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABAOWUOSND7XVAL6OMNQ3CLTHHM4TANCNFSM42LCFP6A>
.
--
Simon Billinge
Professor, Columbia University
Physicist, Brookhaven National Laboratory
Activity
hakonanes commentedon Apr 5, 2021
Hi @pcpatfire,
the reason is that
s[5]returns anAtom, whiles[5:7]returns aStructureSo, to retrieve the
massattribute, you must target eachAtomseparately, not theStructure(which does not have amassattribute)Does this help?
hakonanes commentedon Apr 5, 2021
Or
pcpatfire commentedon Apr 5, 2021
pcpatfire commentedon Apr 5, 2021
Is there a way to link the
massattribute to theStructureclass as it was done with the originalAtomattributes, via_linkAtomAttribute? As, for instance:hakonanes commentedon Apr 5, 2021
Glad it helped!
Yes, by creating your own
MyStructureclass inheriting fromStructure, but you won't gets[5:7].massto work unless you override the__getitem__()method as well:This overriding is not 100% waterproof, and there must be many use cases where you will get errors.
pcpatfire commentedon Apr 5, 2021
Thank you!
sbillinge commentedon Apr 5, 2021
pcpatfire commentedon Apr 5, 2021
sbillinge commentedon Apr 5, 2021
pcpatfire commentedon Apr 5, 2021
sbillinge commentedon Apr 6, 2021
pcpatfire commentedon Apr 7, 2021
Hi Simon, I was sending you an email to your official university email address.
I'm not sure you received it.
Pacifico