"""This module defines the Fingerprint class."""from__future__importannotationsfromcollections.abcimportIterablefromcollections.abcimportIteratorfromcollections.abcimportMutableMappingimportaseimportnumpyasnp
[docs]classFingerprint(MutableMapping):"""A set of displacement vectors relative to a particular atom within an ase.Atoms object. The displacement vectors for atoms of a given chemical symbol can be accessed through the MutableMapping interface. For example: structure = ase.Atoms('CO', positions=[[0, 0, 0], [1, 0, 0]]) fp = Fingerprint(structure, 0, [0, 1]) fp['C'] Attributes: structure: The ase.Atoms instance to which the Fingerprint instance is related. reference: An int indicating the index of the reference atom used to construct the Fingerprint instance. indices: A tuple indicating the indices of the atoms within the structure used to construct the Fingerprint instance. """def__init__(self,structure:ase.Atoms,reference:int,indices:Iterable[int]|None=None,)->None:ifindicesisNone:indices=range(len(structure))histogram={}foratominstructure[indices]:displacement=atom.position-structure[reference].positionifatom.symbolnotinhistogram:histogram[atom.symbol]=np.array([displacement])else:histogram[atom.symbol]=np.vstack([histogram[atom.symbol],displacement])self._histogram:dict[str,np.ndarray]=histogramself.structure=structureself.reference=referenceself.indices=tuple(indices)def__getitem__(self,__k:str)->np.ndarray:returnself._histogram[__k]def__setitem__(self,__k:int,__v:np.ndarray):self._histogram[__k]=__vdef__delitem__(self,__k):delself._histogram[__k]def__iter__(self)->Iterator[str]:returniter(self._histogram)def__len__(self)->int:returnlen(self._histogram)
[docs]@classmethoddeffrom_structure(cls,structure:ase.Atoms)->list[Fingerprint]:"""Creates a list of Fingerprint objects corresponding to each atom within an ase.Atoms object. Args: structure: An ase.Atoms instance representing the structure from which to create the list of Fingerprints. Returns: A list of the Fingerprints for each atom. """fingerprints=[]fori,_inenumerate(structure):fingerprints.append(cls(structure,i))returnfingerprints