| Conditions | 4 |
| Total Lines | 64 |
| Code Lines | 26 |
| 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 | k: int, |
||
| 16 | asymmetric_unit: Optional[np.ndarray] = None): |
||
| 17 | """ |
||
| 18 | Given a periodic set represented by (motif, cell) and an integer k, find |
||
| 19 | the k nearest neighbours of the motif points in the periodic set. |
||
| 20 | |||
| 21 | Note that cloud and inds are not used yet but may be in the future. |
||
| 22 | |||
| 23 | Parameters |
||
| 24 | ---------- |
||
| 25 | motif : numpy.ndarray |
||
| 26 | Cartesian coords of the full motif, shape (no points, dims). |
||
| 27 | cell : numpy.ndarray |
||
| 28 | Cartesian coords of the unit cell, shape (dims, dims). |
||
| 29 | k : int |
||
| 30 | Number of nearest neighbours to find for each motif point. |
||
| 31 | asymmetric_unit : numpy.ndarray, optional |
||
| 32 | Indices pointing to an asymmetric unit in motif. |
||
| 33 | |||
| 34 | Returns |
||
| 35 | ------- |
||
| 36 | pdd : numpy.ndarray |
||
| 37 | An array shape (motif.shape[0], k) of distances from each motif |
||
| 38 | point to its k nearest neighbours in order. Points do not count |
||
| 39 | as their own nearest neighbour. E.g. the distance to the n-th |
||
| 40 | nearest neighbour of the m-th motif point is pdd[m][n]. |
||
| 41 | cloud : numpy.ndarray |
||
| 42 | The collection of points in the periodic set that were generated |
||
| 43 | during the nearest neighbour search. |
||
| 44 | inds : numpy.ndarray |
||
| 45 | An array shape (motif.shape[0], k) containing the indices of |
||
| 46 | nearest neighbours in cloud. E.g. the n-th nearest neighbour to |
||
| 47 | the m-th motif point is cloud[inds[m][n]]. |
||
| 48 | """ |
||
| 49 | |||
| 50 | if asymmetric_unit is not None: |
||
| 51 | asym_unit = motif[asymmetric_unit] |
||
| 52 | else: |
||
| 53 | asym_unit = motif |
||
| 54 | |||
| 55 | cloud_generator = generate_concentric_cloud(motif, cell) |
||
| 56 | n_points = 0 |
||
| 57 | cloud = [] |
||
| 58 | while n_points <= k: |
||
| 59 | l = next(cloud_generator) |
||
| 60 | n_points += l.shape[0] |
||
| 61 | cloud.append(l) |
||
| 62 | cloud.append(next(cloud_generator)) |
||
| 63 | cloud = np.concatenate(cloud) |
||
| 64 | |||
| 65 | tree = KDTree(cloud, compact_nodes=False, balanced_tree=False) |
||
| 66 | pdd_, inds = tree.query(asym_unit, k=k+1, workers=-1) |
||
| 67 | pdd = np.zeros_like(pdd_) |
||
| 68 | |||
| 69 | while not np.allclose(pdd, pdd_, atol=1e-12, rtol=0): |
||
| 70 | pdd = pdd_ |
||
| 71 | cloud = np.vstack((cloud, next(cloud_generator))) |
||
| 72 | tree = KDTree(cloud, compact_nodes=False, balanced_tree=False) |
||
| 73 | pdd_, inds = tree.query(asym_unit, k=k+1, workers=-1) |
||
| 74 | |||
| 75 | return pdd_[:, 1:], cloud, inds[:, 1:] |
||
| 76 | |||
| 239 |