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