-
Notifications
You must be signed in to change notification settings - Fork 35
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
Comments
Hi @pcpatfire, the reason is that >>> type(s[5])
diffpy.structure.atom.Atom
>>> type(s[5:7])
diffpy.structure.structure.Structure So, to retrieve the >>> 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? |
Or >>> [a.mass for a in s[5:7]]
[5, 6] |
Yes. It helped. Thank you so much!
Il Lun 5 Apr 2021, 11:08 Håkon Wiik Ånes ***@***.***> ha
scritto:
… 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
|
Glad it helped!
Yes, by creating your own >>> 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. |
Thank you! |
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 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>
.
|
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
|
Hi Simon, I was sending you an email to your official university email address. |
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: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), buts[5:7].mass
generates the following error:AttributeError: 'Structure' object has no attribute 'mass'
Is there a better to fix this issue?
Thank you
The text was updated successfully, but these errors were encountered: