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

Indexing Atoms broken on __getitem__ #1343

Open
pmrv opened this issue Feb 26, 2024 · 3 comments
Open

Indexing Atoms broken on __getitem__ #1343

pmrv opened this issue Feb 26, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@pmrv
Copy link
Contributor

pmrv commented Feb 26, 2024

s = Atoms(...)
s[0]

triggers the exception below. Setting values is unaffected.

File ~/software/pyiron_atomistics/pyiron_atomistics/atomistics/structure/atoms.py:2122, in Atoms.__getitem__(self, item)
   2112     return Atom(
   2113         element=element,
   2114         position=position,
   (...)
   2118         **new_dict,
   2119     )
   2121 new_array = super(Atoms, self).__getitem__(item)
-> 2122 new_array.dimension = self.dimension
   2123 if isinstance(item, tuple):
   2124     item = list(item)

AttributeError: 'Atom' object has no attribute 'dimension'
@pmrv pmrv added the bug Something isn't working label Feb 26, 2024
@pmrv pmrv self-assigned this Feb 26, 2024
@skatnagallu
Copy link
Contributor

I am using a pyiron structure for atom probe simulations. I am also running into same error.

However, strangely I am unable to reproduce the error when I just do this.

from pyiron_atomistics import Atoms
s = Atoms('N2', [(0, 0, 0), (0, 0, 1.5)])
s[0]

The above code block runs perfectly fine in my simulation environment. But, when I am trying to run my simulations I get the error. afaik the only difference is that I have a simulation class instance, whose attribute is pyiron structure.

a = MySimulation()
print(a.structure[0].number)

This gives me error.

@skatnagallu
Copy link
Contributor

skatnagallu commented Oct 11, 2024

I was able to reproduce the error now. strangely, when index is of dtype np.int64 it gives an error.

from pyiron_atomistics import Atoms
import numpy as np
s = Atoms('N2', [(0, 0, 0), (0, 0, 1.5)])
a = 0
print(type(a))
print(s[a])
a = np.int64(a)
print(type(a))
print(s[a])

This gives the following output

<class 'int'>
Atom('N', [0.0, 0.0, 0.0], index=0)
<class 'numpy.int64'>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[24], line 9
      7 a = np.int64(a)
      8 print(type(a))
----> 9 print(s[a])

File ~/.venv/lib/python3.11/site-packages/pyiron_atomistics/atomistics/structure/atoms.py:2122, in Atoms.__getitem__(self, item)
   2112     return Atom(
   2113         element=element,
   2114         position=position,
   (...)
   2118         **new_dict,
   2119     )
   2121 new_array = super(Atoms, self).__getitem__(item)
-> 2122 new_array.dimension = self.dimension
   2123 if isinstance(item, tuple):
   2124     item = list(item)

AttributeError: 'Atom' object has no attribute 'dimension'

Not sure if this helps.

@pmrv
Copy link
Contributor Author

pmrv commented Oct 11, 2024

Zhenyu and I figured out a hint for this (on a different problem). Can you check whether anywhere in Atoms.__getitem__ there is a check of the form isinstance(..., int)? If so replace int by numbers.Integral and it might work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants