Conditions | 4 |
Total Lines | 70 |
Code Lines | 32 |
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.""" |
||
116 | def nearest_neighbours( |
||
117 | motif: np.ndarray, |
||
118 | cell: np.ndarray, |
||
119 | k: int, |
||
120 | asymmetric_unit: Optional[np.ndarray] = None): |
||
121 | """ |
||
122 | Given a periodic set represented by (motif, cell) and an integer k, find |
||
123 | the k nearest neighbours of the motif points in the periodic set. |
||
124 | |||
125 | Note that cloud and inds are not used yet but may be in the future. |
||
126 | |||
127 | Parameters |
||
128 | ---------- |
||
129 | motif : ndarray |
||
130 | Cartesian coords of the full motif, shape (no points, dims). |
||
131 | cell : ndarray |
||
132 | Cartesian coords of the unit cell, shape (dims, dims). |
||
133 | k : int |
||
134 | Number of nearest neighbours to find for each motif point. |
||
135 | asymmetric_unit : ndarray, optional |
||
136 | Indices pointing to an asymmetric unit in motif. |
||
137 | |||
138 | Returns |
||
139 | ------- |
||
140 | pdd : ndarray |
||
141 | An array shape (motif.shape[0], k) of distances from each motif |
||
142 | point to its k nearest neighbours in order. Points do not count |
||
143 | as their own nearest neighbour. E.g. the distance to the n-th |
||
144 | nearest neighbour of the m-th motif point is pdd[m][n]. |
||
145 | cloud : ndarray |
||
146 | The collection of points in the periodic set that were generated |
||
147 | during the nearest neighbour search. |
||
148 | inds : ndarray |
||
149 | An array shape (motif.shape[0], k) containing the indices of |
||
150 | nearest neighbours in cloud. E.g. the n-th nearest neighbour to |
||
151 | the m-th motif point is cloud[inds[m][n]]. |
||
152 | """ |
||
153 | |||
154 | if asymmetric_unit is not None: |
||
155 | asym_unit = motif[asymmetric_unit] |
||
156 | else: |
||
157 | asym_unit = motif |
||
158 | |||
159 | cloud_generator = generate_concentric_cloud(motif, cell) |
||
160 | n_points = 0 |
||
161 | cloud = [] |
||
162 | while n_points <= k: |
||
163 | l = next(cloud_generator) |
||
164 | n_points += l.shape[0] |
||
165 | cloud.append(l) |
||
166 | cloud.append(next(cloud_generator)) |
||
167 | cloud = np.concatenate(cloud) |
||
168 | |||
169 | tree = scipy.spatial.KDTree(cloud, |
||
170 | compact_nodes=False, |
||
171 | balanced_tree=False) |
||
172 | pdd_, inds = tree.query(asym_unit, k=k+1, workers=-1) |
||
173 | pdd = np.zeros_like(pdd_) |
||
174 | |||
175 | while not np.allclose(pdd, pdd_, atol=1e-12, rtol=0): |
||
176 | pdd = pdd_ |
||
177 | cloud = np.vstack((cloud, |
||
178 | next(cloud_generator), |
||
179 | next(cloud_generator))) |
||
180 | tree = scipy.spatial.KDTree(cloud, |
||
181 | compact_nodes=False, |
||
182 | balanced_tree=False) |
||
183 | pdd_, inds = tree.query(asym_unit, k=k+1, workers=-1) |
||
184 | |||
185 | return pdd_[:, 1:], cloud, inds[:, 1:] |
||
186 | |||
223 |