Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changes/954.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Improved Atoms.index usability

Now one gets an empty list if no atoms are found.
One can also get the combined indices of several
atom types, e.g., `atoms.index("C", "H")`.
36 changes: 34 additions & 2 deletions src/sisl/_core/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,40 @@
return uZ[self.species]

def index(self, atom):
"""Return the indices of the atom object"""
return (self._species == self.species_index(atom)).nonzero()[0]
"""Return indices of atoms matching the specified atom identifier(s).

Parameters
----------
atom : str, Atom, int, or lists hereof
Atom identifier (element string, Atom instance, or species_index).
It can also be a list of identifiers for matching against any of the entries.

Returns
-------
numpy.ndarray
Indices of all matching atoms. Returns an empty array if no match is found.

Notes
-----
Unlike Python's ``list.index``, this method does **not** raise a ``ValueError``
when no match is found. Instead, it returns an empty array.
To check for existence, use e.g. ``if idx.size > 0: ...``.
"""

if not isinstance(atom, (list, tuple, np.ndarray)):
atom = [atom]

idx = []
for a in atom:
try:
idx.append((self._species == self.species_index(a)).nonzero()[0])
except KeyError:
pass

if len(idx) > 0:
idx = np.unique(idx)

return idx

def species_index(self, atom):
"""Return the species index of the atom object"""
Expand Down
9 changes: 7 additions & 2 deletions src/sisl/_core/tests/test_atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,10 @@ def test_charge_diff():

def test_index1():
atom = Atoms(["C", "Au"])
with pytest.raises(KeyError):
atom.index(Atom("B"))
assert atom.index("B") == []
assert atom.index("C") == [0]
assert atom.index(Atom(79)) == [1]
assert atom.index(1) == [1]
assert atom.index(["C", "C"]) == [0]
assert atom.index(["B", "C", "C"]) == [0]
assert np.allclose(atom.index(["Au", "C"]), [0, 1])
Loading