Source code for sknano.core.atoms._atoms
# -*- coding: utf-8 -*-
"""
==============================================================================
Base class for structure data atoms (:mod:`sknano.core.atoms._atoms`)
==============================================================================
.. currentmodule:: sknano.core.atoms._atoms
"""
from __future__ import absolute_import, division, print_function
__docformat__ = 'restructuredtext en'
from operator import attrgetter
import numpy as np
from sknano.core import UserList
__all__ = ['Atoms']
[docs]class Atoms(UserList):
"""Base class for collection of `Atom` objects.
Parameters
----------
atoms : {None, sequence, `Atoms`}, optional
if not `None`, then a list of `Atom` instance objects or an
existing `Atoms` instance object.
copylist : bool, optional
perform shallow copy of atoms list
deepcopy : bool, optional
perform deepcopy of atoms list
"""
_atomattrs = ['symbol', 'Z', 'm']
def __init__(self, atoms=None, copylist=True, deepcopy=False):
super(Atoms, self).__init__(initlist=atoms,
copylist=copylist,
deepcopy=deepcopy)
def __str__(self):
return repr(self)
def __repr__(self):
"""Return canonical string representation of `Atoms`."""
return "Atoms(atoms={!r})".format(self.data)
[docs] def sort(self, key=None, reverse=False):
if key is None:
self.data.sort(key=attrgetter('element', 'Z'),
reverse=reverse)
else:
self.data.sort(key=key, reverse=reverse)
@property
def Natoms(self):
"""Number of atoms in `Atoms`."""
return len(self)
@property
def M(self):
"""Total mass of `Atoms`."""
#return math.fsum(self.masses)
return self.masses.sum()
@property
def masses(self):
"""Return list of `Atom` masses."""
return np.asarray([atom.m for atom in self])
@property
def symbols(self):
"""Return list of `Atom` symbols."""
return np.asarray([atom.symbol for atom in self])
[docs] def filter(self, condition, invert=False):
"""Filter `Atoms` by `condition`.
Parameters
----------
condition : array_like, bool
Boolean index array having same shape as the initial dimensions
of the list of `Atoms` being indexed.
invert : bool, optional
If `True`, the boolean array `condition` is inverted element-wise.
Returns
-------
filtered_atoms : `Atoms`
If `invert` is `False`, return the elements where `condition`
is `True`.
If `invert` is `True`, return the elements where `~condition`
(i.e., numpy.invert(condition)) is `True`.
"""
if invert:
condition = ~condition
return self.__class__(atoms=np.asarray(self)[condition].tolist())
[docs] def get_atoms(self, asarray=False):
"""Return list of `Atoms`.
Parameters
----------
asarray : bool, optional
Returns
-------
sequence or ndarray
"""
if asarray:
return np.asarray(self)
else:
return self