Conditions | 3 |
Total Lines | 58 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | """Implements core function nearest_neighbours used for AMD and PDD calculations.""" |
||
12 | def nearest_neighbours( |
||
13 | motif: np.ndarray, |
||
14 | cell: np.ndarray, |
||
15 | x: np.ndarray, |
||
16 | k: int): |
||
17 | """ |
||
18 | Given a periodic set represented by (motif, cell) and an integer k, find |
||
19 | the k nearest neighbours in the periodic set to points in x. |
||
20 | |||
21 | Parameters |
||
22 | ---------- |
||
23 | motif : numpy.ndarray |
||
24 | Orthogonal (Cartesian) coords of the motif, shape (no points, dims). |
||
25 | cell : numpy.ndarray |
||
26 | Orthogonal (Cartesian) coords of the unit cell, shape (dims, dims). |
||
27 | x : numpy.ndarray |
||
28 | Array of points to query for neighbours. For invariants of crystals |
||
29 | this is the asymmetric unit. |
||
30 | k : int |
||
31 | Number of nearest neighbours to find for each point in x. |
||
32 | |||
33 | Returns |
||
34 | ------- |
||
35 | pdd : numpy.ndarray |
||
36 | Array shape (motif.shape[0], k) of distances from points in x |
||
37 | to their k nearest neighbours in the periodic set, in order. |
||
38 | E.g. pdd[m][n] is the distance from x[m] to its n-th nearest |
||
39 | neighbour in the periodic set. |
||
40 | cloud : numpy.ndarray |
||
41 | Collection of points in the periodic set that was generated |
||
42 | during the nearest neighbour search. |
||
43 | inds : numpy.ndarray |
||
44 | Array shape (motif.shape[0], k) containing the indices of |
||
45 | nearest neighbours in cloud. E.g. the n-th nearest neighbour to |
||
46 | the m-th motif point is cloud[inds[m][n]]. |
||
47 | """ |
||
48 | |||
49 | cloud_generator = generate_concentric_cloud(motif, cell) |
||
50 | n_points = 0 |
||
51 | cloud = [] |
||
52 | while n_points <= k: |
||
53 | l = next(cloud_generator) |
||
54 | n_points += l.shape[0] |
||
55 | cloud.append(l) |
||
56 | cloud.append(next(cloud_generator)) |
||
57 | cloud = np.concatenate(cloud) |
||
58 | |||
59 | tree = KDTree(cloud, compact_nodes=False, balanced_tree=False) |
||
60 | pdd_, inds = tree.query(x, k=k+1, workers=-1) |
||
61 | pdd = np.zeros_like(pdd_) |
||
62 | |||
63 | while not np.allclose(pdd, pdd_, atol=1e-12, rtol=0): |
||
64 | pdd = pdd_ |
||
65 | cloud = np.vstack((cloud, next(cloud_generator))) |
||
66 | tree = KDTree(cloud, compact_nodes=False, balanced_tree=False) |
||
67 | pdd_, inds = tree.query(x, k=k+1, workers=-1) |
||
68 | |||
69 | return pdd_[:, 1:], cloud, inds[:, 1:] |
||
70 | |||
234 |