Conditions | 19 |
Total Lines | 180 |
Code Lines | 104 |
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._emd.network_simplex() 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 | """An implementation of the Wasserstein metric (Earth Mover's distance) |
||
18 | @numba.njit(cache=True) |
||
19 | def network_simplex( |
||
20 | source_demands: np.ndarray, |
||
21 | sink_demands: np.ndarray, |
||
22 | network_costs: np.ndarray |
||
23 | ) -> Tuple[float, np.ndarray]: |
||
24 | """Calculate the Earth mover's distance (Wasserstien metric) between |
||
25 | two weighted distributions given by two sets of weights and a cost |
||
26 | matrix. |
||
27 | |||
28 | This is a port of the network simplex algorithm implented by Loïc |
||
29 | Séguin-C for the networkx package to allow acceleration with numba. |
||
30 | Copyright (C) 2010 Loïc Séguin-C. [email protected]. All rights |
||
31 | reserved. BSD license. |
||
32 | |||
33 | Parameters |
||
34 | ---------- |
||
35 | source_demands : :class:`numpy.ndarray` |
||
36 | Weights of the first distribution. |
||
37 | sink_demands : :class:`numpy.ndarray` |
||
38 | Weights of the second distribution. |
||
39 | network_costs : :class:`numpy.ndarray` |
||
40 | Cost matrix of distances between elements of the two |
||
41 | distributions. Shape (len(source_demands), len(sink_demands)). |
||
42 | |||
43 | Returns |
||
44 | ------- |
||
45 | (emd, plan) : Tuple[float, :class:`numpy.ndarray`] |
||
46 | A tuple of the Earth mover's distance and the optimal matching. |
||
47 | |||
48 | References |
||
49 | ---------- |
||
50 | [1] Z. Kiraly, P. Kovacs. |
||
51 | Efficient implementation of minimum-cost flow algorithms. |
||
52 | Acta Universitatis Sapientiae, Informatica 4(1), 67--118 |
||
53 | (2012). |
||
54 | [2] R. Barr, F. Glover, D. Klingman. |
||
55 | Enhancement of spanning tree labeling procedures for network |
||
56 | optimization. INFOR 17(1), 16--34 (1979). |
||
57 | """ |
||
58 | |||
59 | n_sources, n_sinks = source_demands.shape[0], sink_demands.shape[0] |
||
60 | n = n_sources + n_sinks |
||
61 | e = n_sources * n_sinks |
||
62 | B = np.int64(np.ceil(np.sqrt(e))) |
||
63 | fp_multiplier = np.float64(1_000_000) |
||
64 | |||
65 | # Add one additional node for a dummy source and sink |
||
66 | source_d_int = (source_demands * fp_multiplier).astype(np.int64) |
||
67 | sink_d_int = (sink_demands * fp_multiplier).astype(np.int64) |
||
68 | sink_source_sum_diff = np.sum(sink_d_int) - np.sum(source_d_int) |
||
69 | |||
70 | if sink_source_sum_diff > 0: |
||
71 | source_d_int[np.argmax(source_d_int)] += sink_source_sum_diff |
||
72 | elif sink_source_sum_diff < 0: |
||
73 | sink_d_int[np.argmax(sink_d_int)] -= sink_source_sum_diff |
||
74 | |||
75 | demands = np.empty(n, dtype=np.int64) |
||
76 | demands[:n_sources] = -source_d_int |
||
77 | demands[n_sources:] = sink_d_int |
||
78 | tails = np.empty(e + n, dtype=np.int64) |
||
79 | heads = np.empty(e + n, dtype=np.int64) |
||
80 | |||
81 | ind = 0 |
||
82 | for i in range(n_sources): |
||
83 | for j in range(n_sinks): |
||
84 | tails[ind] = i |
||
85 | heads[ind] = n_sources + j |
||
86 | ind += 1 |
||
87 | |||
88 | for i, demand in enumerate(demands): |
||
89 | ind = e + i |
||
90 | if demand > 0: |
||
91 | tails[ind] = -1 |
||
92 | heads[ind] = -1 |
||
93 | else: |
||
94 | tails[ind] = i |
||
95 | heads[ind] = i |
||
96 | |||
97 | # Create costs and capacities for the arcs between nodes |
||
98 | network_costs = (network_costs.ravel() * fp_multiplier).astype(np.int64) |
||
99 | network_capac = np.empty(shape=(e, ), dtype=np.int64) |
||
100 | ind = 0 |
||
101 | for i in range(n_sources): |
||
102 | for j in range(n_sinks): |
||
103 | network_capac[ind] = np.int64( |
||
104 | min(source_demands[i], sink_demands[j]) * fp_multiplier |
||
105 | ) |
||
106 | ind += 1 |
||
107 | |||
108 | # In amd network_costs are always positive, otherwise take abs here |
||
109 | faux_inf = np.int64(3 * max( |
||
110 | np.sum(network_costs), |
||
111 | np.sum(network_capac), |
||
112 | np.amax(source_d_int), |
||
113 | np.amax(sink_d_int) |
||
114 | )) |
||
115 | |||
116 | costs = np.empty(e + n, dtype=np.int64) |
||
117 | costs[:e] = network_costs |
||
118 | costs[e:] = faux_inf |
||
119 | |||
120 | capac = np.empty(e + n, dtype=np.int64) |
||
121 | capac[:e] = network_capac |
||
122 | capac[e:] = fp_multiplier |
||
123 | |||
124 | flows = np.empty(e + n, dtype=np.int64) |
||
125 | flows[:e] = 0 |
||
126 | flows[e:e+n_sources] = source_d_int |
||
127 | flows[e+n_sources:] = sink_d_int |
||
128 | |||
129 | potentials = np.empty(n, dtype=np.int64) |
||
130 | demands_neg_mask = demands <= 0 |
||
131 | potentials[demands_neg_mask] = faux_inf |
||
132 | potentials[~demands_neg_mask] = -faux_inf |
||
133 | |||
134 | parent = np.full(shape=(n + 1, ), fill_value=-1, dtype=np.int64) |
||
135 | parent[-1] = -2 |
||
136 | |||
137 | size = np.full(shape=(n + 1, ), fill_value=1, dtype=np.int64) |
||
138 | size[-1] = n + 1 |
||
139 | |||
140 | next_node = np.arange(1, n + 2, dtype=np.int64) |
||
141 | next_node[-2] = -1 |
||
142 | next_node[-1] = 0 |
||
143 | |||
144 | last_node = np.arange(n + 1, dtype=np.int64) |
||
145 | last_node[-1] = n - 1 |
||
146 | |||
147 | prev_node = np.arange(-1, n, dtype=np.int64) |
||
148 | edge = np.arange(e, e + n, dtype=np.int64) |
||
149 | |||
150 | # Pivot loop |
||
151 | f = 0 |
||
152 | while True: |
||
153 | i, p, q, f = _find_entering_edges( |
||
154 | B, e, f, tails, heads, costs, potentials, flows |
||
155 | ) |
||
156 | # If no entering edges then the optimal score is found |
||
157 | if p == -1: |
||
158 | break |
||
159 | |||
160 | cycle_nodes, cycle_edges = _find_cycle(i, p, q, size, edge, parent) |
||
161 | j, s, t = _find_leaving_edge( |
||
162 | cycle_nodes, cycle_edges, capac, flows, tails, heads |
||
163 | ) |
||
164 | res_cap = capac[j] - flows[j] if tails[j] == s else flows[j] |
||
165 | |||
166 | # Augment flows |
||
167 | for i_, p_ in zip(cycle_edges, cycle_nodes): |
||
168 | if tails[i_] == p_: |
||
169 | flows[i_] += res_cap |
||
170 | else: |
||
171 | flows[i_] -= res_cap |
||
172 | |||
173 | # Do nothing more if the entering edge is the same as the leaving edge. |
||
174 | if i != j: |
||
175 | if parent[t] != s: |
||
176 | # Ensure that s is the parent of t. |
||
177 | s, t = t, s |
||
178 | |||
179 | # Ensure that q is in the subtree rooted at t. |
||
180 | for val in cycle_edges: |
||
181 | if val == j: |
||
182 | p, q = q, p |
||
183 | break |
||
184 | if val == i: |
||
185 | break |
||
186 | |||
187 | _remove_edge(s, t, size, prev_node, last_node, next_node, parent, edge) |
||
188 | _make_root(q, parent, size, last_node, prev_node, next_node, edge) |
||
189 | _add_edge(i, p, q, next_node, prev_node, last_node, size, parent, edge) |
||
190 | _update_potentials(i, p, q, heads, potentials, costs, last_node, next_node) |
||
191 | |||
192 | final_flows = flows[:e] / fp_multiplier |
||
193 | edge_costs = costs[:e] / fp_multiplier |
||
194 | emd = final_flows.dot(edge_costs) |
||
195 | |||
196 | return emd, final_flows.reshape((n_sources, n_sinks)) |
||
197 | |||
198 | |||
470 |