Conditions | 13 |
Total Lines | 55 |
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:
Complex classes like amd._nearest_neighbours.generate_integer_lattice() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """Implements core function nearest_neighbours used for AMD and PDD calculations.""" |
||
27 | def generate_integer_lattice(dims: int) -> Iterable[np.ndarray]: |
||
28 | """Generates batches of integer lattice points. |
||
29 | |||
30 | Each yield gives all points (that have not already been yielded) |
||
31 | inside a sphere centered at the origin with radius d. d starts at 0 |
||
32 | and increments by 1 on each loop. |
||
33 | |||
34 | Parameters |
||
35 | ---------- |
||
36 | dims : int |
||
37 | The dimension of Euclidean space the lattice is in. |
||
38 | |||
39 | Yields |
||
40 | ------- |
||
41 | ndarray |
||
42 | Yields arrays of integer points in dims dimensional Euclidean space. |
||
43 | """ |
||
44 | |||
45 | ymax = collections.defaultdict(int) |
||
46 | d = 0 |
||
47 | |||
48 | if dims == 1: |
||
49 | yield np.array([[0]]) |
||
50 | while True: |
||
51 | d += 1 |
||
52 | yield np.array([[-d], [d]]) |
||
53 | |||
54 | while True: |
||
|
|||
55 | # get integer lattice points in +ve directions |
||
56 | positive_int_lattice = [] |
||
57 | while True: |
||
58 | batch = [] |
||
59 | for xy in itertools.product(range(d+1), repeat=dims-1): |
||
60 | if _dist(xy, ymax[xy]) <= d**2: |
||
61 | batch.append((*xy, ymax[xy])) |
||
62 | ymax[xy] += 1 |
||
63 | if not batch: |
||
64 | break |
||
65 | positive_int_lattice += batch |
||
66 | positive_int_lattice.sort(key=_distkey) |
||
67 | |||
68 | # expand +ve integer lattice to full lattice with reflections |
||
69 | int_lattice = [] |
||
70 | for p in positive_int_lattice: |
||
71 | int_lattice.append(p) |
||
72 | for n_reflections in range(1, dims+1): |
||
73 | for indexes in itertools.combinations(range(dims), n_reflections): |
||
74 | if all((p[i] for i in indexes)): |
||
75 | p_ = list(p) |
||
76 | for i in indexes: |
||
77 | p_[i] *= -1 |
||
78 | int_lattice.append(p_) |
||
79 | |||
80 | yield np.array(int_lattice) |
||
81 | d += 1 |
||
82 | |||
223 |