@@ -67,9 +67,9 @@ def __init__(
67
67
mat = np .array (matrix , dtype = np .float64 ).reshape ((3 , 3 ))
68
68
mat .setflags (write = False )
69
69
self ._matrix : NDArray [np .float64 ] = mat
70
- self ._inv_matrix : np .ndarray | None = None
70
+ self ._inv_matrix : NDArray [ np .float64 ] | None = None
71
71
self ._diags = None
72
- self ._lll_matrix_mappings : dict [float , tuple [np .ndarray , np .ndarray ]] = {}
72
+ self ._lll_matrix_mappings : dict [float , tuple [NDArray [ np .float64 ], NDArray [ np .float64 ] ]] = {}
73
73
self ._lll_inverse = None
74
74
75
75
self .pbc = pbc
@@ -178,7 +178,7 @@ def is_orthogonal(self) -> bool:
178
178
return all (abs (a - 90 ) < 1e-5 for a in self .angles )
179
179
180
180
@property
181
- def matrix (self ) -> np .ndarray :
181
+ def matrix (self ) -> NDArray [ np .float64 ] :
182
182
"""Copy of matrix representing the Lattice."""
183
183
return self ._matrix
184
184
@@ -188,23 +188,23 @@ def is_3d_periodic(self) -> bool:
188
188
return all (self .pbc )
189
189
190
190
@property
191
- def inv_matrix (self ) -> np .ndarray :
191
+ def inv_matrix (self ) -> NDArray [ np .float64 ] :
192
192
"""Inverse of lattice matrix."""
193
193
if self ._inv_matrix is None :
194
- self ._inv_matrix = np .linalg .inv (self ._matrix )
194
+ self ._inv_matrix = np .linalg .inv (self ._matrix ) # type: ignore[assignment]
195
195
self ._inv_matrix .setflags (write = False )
196
- return self ._inv_matrix
196
+ return self ._inv_matrix # type: ignore[return-value]
197
197
198
198
@property
199
- def metric_tensor (self ) -> np .ndarray :
199
+ def metric_tensor (self ) -> NDArray [ np .float64 ] :
200
200
"""The metric tensor of the lattice."""
201
201
return np .dot (self ._matrix , self ._matrix .T )
202
202
203
203
def copy (self ) -> Self :
204
204
"""Make a copy of this lattice."""
205
205
return type (self )(self .matrix .copy (), pbc = self .pbc )
206
206
207
- def get_cartesian_coords (self , fractional_coords : ArrayLike ) -> np .ndarray :
207
+ def get_cartesian_coords (self , fractional_coords : ArrayLike ) -> NDArray [ np .float64 ] :
208
208
"""Get the Cartesian coordinates given fractional coordinates.
209
209
210
210
Args:
@@ -215,7 +215,7 @@ def get_cartesian_coords(self, fractional_coords: ArrayLike) -> np.ndarray:
215
215
"""
216
216
return np .dot (fractional_coords , self ._matrix )
217
217
218
- def get_fractional_coords (self , cart_coords : ArrayLike ) -> np .ndarray :
218
+ def get_fractional_coords (self , cart_coords : ArrayLike ) -> NDArray [ np .float64 ] :
219
219
"""Get the fractional coordinates given Cartesian coordinates.
220
220
221
221
Args:
@@ -229,7 +229,7 @@ def get_fractional_coords(self, cart_coords: ArrayLike) -> np.ndarray:
229
229
def get_vector_along_lattice_directions (
230
230
self ,
231
231
cart_coords : ArrayLike ,
232
- ) -> np .ndarray :
232
+ ) -> NDArray [ np .float64 ] :
233
233
"""Get the coordinates along lattice directions given Cartesian coordinates.
234
234
235
235
Note, this is different than a projection of the Cartesian vector along the
@@ -540,23 +540,23 @@ def reciprocal_lattice_crystallographic(self) -> Self:
540
540
return type (self )(self .reciprocal_lattice .matrix / (2 * np .pi ))
541
541
542
542
@property
543
- def lll_matrix (self ) -> np .ndarray :
543
+ def lll_matrix (self ) -> NDArray [ np .float64 ] :
544
544
"""The matrix for LLL reduction."""
545
545
if 0.75 not in self ._lll_matrix_mappings :
546
546
self ._lll_matrix_mappings [0.75 ] = self ._calculate_lll ()
547
547
return self ._lll_matrix_mappings [0.75 ][0 ]
548
548
549
549
@property
550
- def lll_mapping (self ) -> np .ndarray :
550
+ def lll_mapping (self ) -> NDArray [ np .float64 ] :
551
551
"""The mapping between the LLL reduced lattice and the original lattice."""
552
552
if 0.75 not in self ._lll_matrix_mappings :
553
553
self ._lll_matrix_mappings [0.75 ] = self ._calculate_lll ()
554
554
return self ._lll_matrix_mappings [0.75 ][1 ]
555
555
556
556
@property
557
- def lll_inverse (self ) -> np .ndarray :
557
+ def lll_inverse (self ) -> NDArray [ np .float64 ] :
558
558
"""Inverse of self.lll_mapping."""
559
- return np .linalg .inv (self .lll_mapping )
559
+ return np .linalg .inv (self .lll_mapping ) # type: ignore[return-value]
560
560
561
561
@property
562
562
def selling_vector (self ) -> NDArray [np .float64 ]:
@@ -922,7 +922,7 @@ def find_all_mappings(
922
922
ltol : float = 1e-5 ,
923
923
atol : float = 1 ,
924
924
skip_rotation_matrix : bool = False ,
925
- ) -> Iterator [tuple [Lattice , np .ndarray | None , np .ndarray ]]:
925
+ ) -> Iterator [tuple [Lattice , NDArray [ np .float64 ] | None , NDArray [ np .float64 ] ]]:
926
926
"""Find all mappings between current lattice and another lattice.
927
927
928
928
Args:
@@ -991,7 +991,7 @@ def find_mapping(
991
991
ltol : float = 1e-5 ,
992
992
atol : float = 1 ,
993
993
skip_rotation_matrix : bool = False ,
994
- ) -> tuple [Lattice , np .ndarray | None , np .ndarray ] | None :
994
+ ) -> tuple [Lattice , NDArray [ np .float64 ] | None , NDArray [ np .float64 ] ] | None :
995
995
"""Find a mapping between current lattice and another lattice. There
996
996
are an infinite number of choices of basis vectors for two entirely
997
997
equivalent lattices. This method returns a mapping that maps
@@ -1006,7 +1006,7 @@ def find_mapping(
1006
1006
Defaults to False.
1007
1007
1008
1008
Returns:
1009
- tuple[Lattice, np.ndarray, np.ndarray ]: (aligned_lattice, rotation_matrix, scale_matrix)
1009
+ tuple[Lattice, NDArray[ np.float_], NDArray[ np.float_] ]: (aligned_lattice, rotation_matrix, scale_matrix)
1010
1010
if a mapping is found. aligned_lattice is a rotated version of other_lattice that
1011
1011
has the same lattice parameters, but which is aligned in the
1012
1012
coordinate system of this lattice so that translational points
@@ -1039,7 +1039,7 @@ def get_lll_reduced_lattice(self, delta: float = 0.75) -> Self:
1039
1039
self ._lll_matrix_mappings [delta ] = self ._calculate_lll ()
1040
1040
return type (self )(self ._lll_matrix_mappings [delta ][0 ])
1041
1041
1042
- def _calculate_lll (self , delta : float = 0.75 ) -> tuple [np .ndarray , np .ndarray ]:
1042
+ def _calculate_lll (self , delta : float = 0.75 ) -> tuple [NDArray [ np .float64 ], NDArray [ np .float64 ] ]:
1043
1043
"""Perform a Lenstra-Lenstra-Lovasz lattice basis reduction to obtain a
1044
1044
c-reduced basis. This method returns a basis which is as "good" as
1045
1045
possible, with "good" defined by orthogonality of the lattice vectors.
@@ -1116,15 +1116,15 @@ def _calculate_lll(self, delta: float = 0.75) -> tuple[np.ndarray, np.ndarray]:
1116
1116
result = np .linalg .lstsq (q .T , p .T , rcond = None )[0 ].T
1117
1117
u [k :3 , (k - 2 ) : k ] = result
1118
1118
1119
- return a .T , mapping .T
1119
+ return a .T , mapping .T # type: ignore[return-value]
1120
1120
1121
- def get_lll_frac_coords (self , frac_coords : ArrayLike ) -> np .ndarray :
1121
+ def get_lll_frac_coords (self , frac_coords : ArrayLike ) -> NDArray [ np .float64 ] :
1122
1122
"""Given fractional coordinates in the lattice basis, returns corresponding
1123
1123
fractional coordinates in the lll basis.
1124
1124
"""
1125
1125
return np .dot (frac_coords , self .lll_inverse )
1126
1126
1127
- def get_frac_coords_from_lll (self , lll_frac_coords : ArrayLike ) -> np .ndarray :
1127
+ def get_frac_coords_from_lll (self , lll_frac_coords : ArrayLike ) -> NDArray [ np .float64 ] :
1128
1128
"""Given fractional coordinates in the lll basis, returns corresponding
1129
1129
fractional coordinates in the lattice basis.
1130
1130
"""
@@ -1292,7 +1292,7 @@ def scale(self, new_volume: float) -> Self:
1292
1292
1293
1293
return type (self )(versors * (new_c * ratios ), pbc = self .pbc )
1294
1294
1295
- def get_wigner_seitz_cell (self ) -> list [list [np .ndarray ]]:
1295
+ def get_wigner_seitz_cell (self ) -> list [list [NDArray [ np .float64 ] ]]:
1296
1296
"""Get the Wigner-Seitz cell for the given lattice.
1297
1297
1298
1298
Returns:
@@ -1316,7 +1316,7 @@ def get_wigner_seitz_cell(self) -> list[list[np.ndarray]]:
1316
1316
1317
1317
return out
1318
1318
1319
- def get_brillouin_zone (self ) -> list [list [np .ndarray ]]:
1319
+ def get_brillouin_zone (self ) -> list [list [NDArray [ np .float64 ] ]]:
1320
1320
"""Get the Wigner-Seitz cell for the reciprocal lattice, aka the
1321
1321
Brillouin Zone.
1322
1322
@@ -1333,7 +1333,7 @@ def dot(
1333
1333
coords_a : ArrayLike ,
1334
1334
coords_b : ArrayLike ,
1335
1335
frac_coords : bool = False ,
1336
- ) -> np .ndarray :
1336
+ ) -> NDArray [ np .float64 ] :
1337
1337
"""Compute the scalar product of vector(s).
1338
1338
1339
1339
Args:
@@ -1361,7 +1361,7 @@ def dot(
1361
1361
1362
1362
return np .array (list (itertools .starmap (np .dot , zip (cart_a , cart_b , strict = True ))))
1363
1363
1364
- def norm (self , coords : ArrayLike , frac_coords : bool = True ) -> np .ndarray :
1364
+ def norm (self , coords : ArrayLike , frac_coords : bool = True ) -> NDArray [ np .float64 ] :
1365
1365
"""Compute the norm of vector(s).
1366
1366
1367
1367
Args:
@@ -1448,7 +1448,7 @@ def get_points_in_sphere_py(
1448
1448
center : ArrayLike ,
1449
1449
r : float ,
1450
1450
zip_results : bool = True ,
1451
- ) -> list [tuple [np .ndarray , float , int , np .ndarray ]] | list [np .ndarray ]:
1451
+ ) -> list [tuple [NDArray [ np .float64 ] , float , int , NDArray [ np .float64 ]]] | list [NDArray [ np .float64 ] ]:
1452
1452
"""Find all points within a sphere from the point taking into account
1453
1453
periodic boundary conditions. This includes sites in other periodic
1454
1454
images.
@@ -1503,8 +1503,8 @@ def get_points_in_sphere_old(
1503
1503
r : float ,
1504
1504
zip_results = True ,
1505
1505
) -> (
1506
- list [tuple [np .ndarray , float , int , np .ndarray ]]
1507
- | tuple [list [np .ndarray ] , list [float ], list [int ], list [np .ndarray ]]
1506
+ list [tuple [NDArray [ np .float64 ] , float , int , NDArray [ np .float64 ] ]]
1507
+ | tuple [list [NDArray [ np .float64 ]] , list [float ], list [int ], list [NDArray [ np .float64 ] ]]
1508
1508
):
1509
1509
"""Find all points within a sphere from the point taking into account
1510
1510
periodic boundary conditions. This includes sites in other periodic
@@ -1603,7 +1603,7 @@ def get_all_distances(
1603
1603
self ,
1604
1604
frac_coords1 : ArrayLike ,
1605
1605
frac_coords2 : ArrayLike ,
1606
- ) -> np .ndarray :
1606
+ ) -> NDArray [ np .float64 ] :
1607
1607
"""Get the distances between two lists of coordinates taking into
1608
1608
account periodic boundary conditions and the lattice. Note that this
1609
1609
computes an MxN array of distances (i.e. the distance between each
@@ -1657,7 +1657,7 @@ def get_distance_and_image(
1657
1657
frac_coords1 : ArrayLike ,
1658
1658
frac_coords2 : ArrayLike ,
1659
1659
jimage : ArrayLike | None = None ,
1660
- ) -> tuple [float , np .ndarray ]:
1660
+ ) -> tuple [float , NDArray [ np .int_ ] ]:
1661
1661
"""Get distance between two frac_coords assuming periodic boundary
1662
1662
conditions. If the index jimage is not specified it selects the j
1663
1663
image nearest to the i atom and returns the distance and jimage
@@ -1675,7 +1675,7 @@ def get_distance_and_image(
1675
1675
the image that is nearest to the site is found.
1676
1676
1677
1677
Returns:
1678
- tuple[float, np.ndarray ]: distance and periodic lattice translations (jimage)
1678
+ tuple[float, NDArray[ np.int_] ]: distance and periodic lattice translations (jimage)
1679
1679
of the other site for which the distance applies. This means that
1680
1680
the distance between frac_coords1 and (jimage + frac_coords2) is
1681
1681
equal to distance.
@@ -1819,7 +1819,7 @@ def get_points_in_spheres(
1819
1819
numerical_tol : float = 1e-8 ,
1820
1820
lattice : Lattice | None = None ,
1821
1821
return_fcoords : bool = False ,
1822
- ) -> list [list [tuple [NDArray [np .float64 ], float , int , NDArray [np .int_ ]]]]:
1822
+ ) -> list [list [tuple [NDArray [np .float64 ], float , int , NDArray [np .float64 ]]]]:
1823
1823
"""For each point in `center_coords`, get all the neighboring points
1824
1824
in `all_coords` that are within the cutoff radius `r`.
1825
1825
@@ -1893,12 +1893,19 @@ def get_points_in_spheres(
1893
1893
if not valid_coords :
1894
1894
return [[]] * len (center_coords )
1895
1895
valid_coords = np .concatenate (valid_coords , axis = 0 )
1896
- valid_images = np .concatenate (valid_images , axis = 0 )
1896
+ return get_points_in_spheres (
1897
+ valid_coords , # type:ignore[arg-type]
1898
+ center_coords ,
1899
+ r ,
1900
+ pbc ,
1901
+ numerical_tol ,
1902
+ lattice ,
1903
+ return_fcoords = return_fcoords ,
1904
+ )
1897
1905
1898
- else :
1899
- valid_coords = all_coords # type: ignore[assignment]
1900
- valid_images = [[0 , 0 , 0 ]] * len (valid_coords )
1901
- valid_indices = np .arange (len (valid_coords )) # type: ignore[assignment]
1906
+ valid_coords = all_coords # type: ignore[assignment]
1907
+ valid_images = [[0 , 0 , 0 ]] * len (valid_coords ) # type: ignore[list-item]
1908
+ valid_indices = np .arange (len (valid_coords )) # type: ignore[assignment]
1902
1909
1903
1910
# Divide the valid 3D space into cubes and compute the cube ids
1904
1911
all_cube_index = _compute_cube_index (valid_coords , global_min , r ) # type: ignore[arg-type]
@@ -1910,16 +1917,16 @@ def get_points_in_spheres(
1910
1917
cube_to_coords : dict [int , list ] = defaultdict (list )
1911
1918
cube_to_images : dict [int , list ] = defaultdict (list )
1912
1919
cube_to_indices : dict [int , list ] = defaultdict (list )
1913
- for ii , jj , kk , ll in zip (all_cube_index .ravel (), valid_coords , valid_images , valid_indices , strict = True ):
1914
- cube_to_coords [ii ].append (jj )
1915
- cube_to_images [ii ].append (kk )
1916
- cube_to_indices [ii ].append (ll )
1920
+ for ii , jj , kk , ll in zip (all_cube_index .ravel (), valid_coords , valid_images , valid_indices , strict = True ): # type: ignore[assignment]
1921
+ cube_to_coords [ii ].append (jj ) # type: ignore[index]
1922
+ cube_to_images [ii ].append (kk ) # type: ignore[index]
1923
+ cube_to_indices [ii ].append (ll ) # type: ignore[index]
1917
1924
1918
1925
# Find all neighboring cubes for each atom in the lattice cell
1919
1926
site_neighbors = find_neighbors (site_cube_index , nx , ny , nz )
1920
- neighbors : list [list [tuple [np .ndarray , float , int , np .ndarray ]]] = []
1927
+ neighbors : list [list [tuple [NDArray [ np .float64 ] , float , int , NDArray [ np .float64 ] ]]] = []
1921
1928
1922
- for ii , jj in zip (center_coords , site_neighbors , strict = True ):
1929
+ for cc , jj in zip (center_coords , site_neighbors , strict = True ):
1923
1930
l1 = np .array (_three_to_one (jj , ny , nz ), dtype = np .int64 ).ravel ()
1924
1931
# Use the cube index map to find the all the neighboring
1925
1932
# coords, images, and indices
@@ -1930,8 +1937,8 @@ def get_points_in_spheres(
1930
1937
nn_coords = np .concatenate ([cube_to_coords [k ] for k in ks ], axis = 0 ) # type:ignore[index]
1931
1938
nn_images = itertools .chain (* (cube_to_images [k ] for k in ks )) # type:ignore[index]
1932
1939
nn_indices = itertools .chain (* (cube_to_indices [k ] for k in ks )) # type:ignore[index]
1933
- distances = np .linalg .norm (nn_coords - ii [None , :], axis = 1 )
1934
- nns : list [tuple [np .ndarray , float , int , np .ndarray ]] = []
1940
+ distances = np .linalg .norm (nn_coords - cc [None , :], axis = 1 ) # type:ignore[index]
1941
+ nns : list [tuple [NDArray [ np .float64 ] , float , int , NDArray [ np .float64 ] ]] = []
1935
1942
for coord , index , image , dist in zip (nn_coords , nn_indices , nn_images , distances , strict = True ):
1936
1943
# Filtering out all sites that are beyond the cutoff
1937
1944
# Here there is no filtering of overlapping sites
@@ -1946,23 +1953,23 @@ def get_points_in_spheres(
1946
1953
1947
1954
# The following internal functions are used in the get_points_in_sphere method
1948
1955
def _compute_cube_index (
1949
- coords : np .ndarray ,
1956
+ coords : NDArray [ np .float64 ] ,
1950
1957
global_min : float ,
1951
1958
radius : float ,
1952
- ) -> np .ndarray :
1959
+ ) -> NDArray [ np .int_ ] :
1953
1960
"""Compute the cube index from coordinates
1954
1961
Args:
1955
1962
coords: (nx3 array) atom coordinates
1956
1963
global_min: (float) lower boundary of coordinates
1957
1964
radius: (float) cutoff radius.
1958
1965
1959
1966
Returns:
1960
- np.ndarray : nx3 array int indices
1967
+ NDArray[ np.float_] : nx3 array int indices
1961
1968
"""
1962
- return np .array (np .floor ((coords - global_min ) / radius ), dtype = np .int64 )
1969
+ return np .array (np .floor ((coords - global_min ) / radius ), dtype = np .int_ )
1963
1970
1964
1971
1965
- def _one_to_three (label1d : np .ndarray , ny : int , nz : int ) -> np .ndarray :
1972
+ def _one_to_three (label1d : NDArray [ np .int_ ] , ny : int , nz : int ) -> NDArray [ np .int_ ] :
1966
1973
"""Convert a 1D index array to 3D index array.
1967
1974
1968
1975
Args:
@@ -1971,20 +1978,20 @@ def _one_to_three(label1d: np.ndarray, ny: int, nz: int) -> np.ndarray:
1971
1978
nz: (int) number of cells in z direction
1972
1979
1973
1980
Returns:
1974
- np.ndarray : nx3 array int indices
1981
+ NDArray[ np.float_] : nx3 array int indices
1975
1982
"""
1976
1983
last = np .mod (label1d , nz )
1977
1984
second = np .mod ((label1d - last ) / nz , ny )
1978
1985
first = (label1d - last - second * nz ) / (ny * nz )
1979
1986
return np .concatenate ([first , second , last ], axis = 1 )
1980
1987
1981
1988
1982
- def _three_to_one (label3d : np .ndarray , ny : int , nz : int ) -> np .ndarray :
1989
+ def _three_to_one (label3d : NDArray [ np .int_ ] , ny : int , nz : int ) -> NDArray [ np .int_ ] :
1983
1990
"""The reverse of _one_to_three."""
1984
1991
return np .array (label3d [:, 0 ] * ny * nz + label3d [:, 1 ] * nz + label3d [:, 2 ]).reshape ((- 1 , 1 ))
1985
1992
1986
1993
1987
- def find_neighbors (label : np .ndarray , nx : int , ny : int , nz : int ) -> list [np .ndarray ]:
1994
+ def find_neighbors (label : NDArray [ np .int_ ] , nx : int , ny : int , nz : int ) -> list [NDArray [ np .int_ ] ]:
1988
1995
"""Given a cube index, find the neighbor cube indices.
1989
1996
1990
1997
Args:
0 commit comments