DARTsort 0.3.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. dartsort/__init__.py +68 -0
  2. dartsort/cli.py +105 -0
  3. dartsort/clustering/__init__.py +3 -0
  4. dartsort/clustering/cluster_util.py +392 -0
  5. dartsort/clustering/clustering.py +765 -0
  6. dartsort/clustering/clustering_features.py +202 -0
  7. dartsort/clustering/density.py +1299 -0
  8. dartsort/clustering/forward_backward.py +219 -0
  9. dartsort/clustering/gmm/mixture.py +5790 -0
  10. dartsort/clustering/gmm/stable_features.py +993 -0
  11. dartsort/clustering/kmeans.py +448 -0
  12. dartsort/clustering/merge.py +501 -0
  13. dartsort/clustering/modes.py +214 -0
  14. dartsort/clustering/refine_util.py +191 -0
  15. dartsort/clustering/relocate.py +109 -0
  16. dartsort/clustering/split.py +1285 -0
  17. dartsort/config.py +288 -0
  18. dartsort/detect/__init__.py +2 -0
  19. dartsort/detect/detect.py +449 -0
  20. dartsort/detect/detect_filters.py +37 -0
  21. dartsort/evaluate/__init__.py +1 -0
  22. dartsort/evaluate/analysis.py +594 -0
  23. dartsort/evaluate/comparison.py +534 -0
  24. dartsort/evaluate/hybrid_util.py +599 -0
  25. dartsort/evaluate/noise_recording_tools.py +184 -0
  26. dartsort/evaluate/sim_template_tools.py +842 -0
  27. dartsort/evaluate/simkit.py +840 -0
  28. dartsort/evaluate/simlib.py +270 -0
  29. dartsort/localize/__init__.py +0 -0
  30. dartsort/localize/localize_torch.py +291 -0
  31. dartsort/localize/localize_util.py +313 -0
  32. dartsort/main.py +657 -0
  33. dartsort/peel/__init__.py +6 -0
  34. dartsort/peel/grab.py +208 -0
  35. dartsort/peel/matching.py +628 -0
  36. dartsort/peel/matching_util/__init__.py +3 -0
  37. dartsort/peel/matching_util/compressed_upsampled.py +718 -0
  38. dartsort/peel/matching_util/drifty.py +791 -0
  39. dartsort/peel/matching_util/matching_base.py +616 -0
  40. dartsort/peel/matching_util/pairwise.py +359 -0
  41. dartsort/peel/matching_util/pairwise_util.py +1317 -0
  42. dartsort/peel/peel_base.py +1000 -0
  43. dartsort/peel/running_template.py +1498 -0
  44. dartsort/peel/subtract.py +1123 -0
  45. dartsort/peel/threshold.py +471 -0
  46. dartsort/peel/universal.py +191 -0
  47. dartsort/pretrained/default_temporal_kernel.npy +0 -0
  48. dartsort/pretrained/detect_np1.pt +0 -0
  49. dartsort/pretrained/detect_np2.pt +0 -0
  50. dartsort/pretrained/glasbey1024.npz +0 -0
  51. dartsort/pretrained/offset_trained_denoiser.pt +0 -0
  52. dartsort/pretrained/offset_trained_denoiser_added_temp.pt +0 -0
  53. dartsort/pretrained/offset_trained_denoiser_added_temp_boost.pt +0 -0
  54. dartsort/pretrained/original_single_chan_denoiser.pt +0 -0
  55. dartsort/pretrained/single_chan_denoiser.pt +0 -0
  56. dartsort/templates/__init__.py +11 -0
  57. dartsort/templates/get_templates.py +771 -0
  58. dartsort/templates/postprocess_util.py +555 -0
  59. dartsort/templates/realignment.py +466 -0
  60. dartsort/templates/superres_util.py +204 -0
  61. dartsort/templates/template_util.py +518 -0
  62. dartsort/templates/templates.py +528 -0
  63. dartsort/transform/__init__.py +2 -0
  64. dartsort/transform/_multichan_denoiser_kit.py +650 -0
  65. dartsort/transform/all_transformers.py +58 -0
  66. dartsort/transform/amortized_localization.py +503 -0
  67. dartsort/transform/amplitudes.py +197 -0
  68. dartsort/transform/decollider.py +635 -0
  69. dartsort/transform/enforce_decrease.py +158 -0
  70. dartsort/transform/localize.py +86 -0
  71. dartsort/transform/matching_denoiser.py +44 -0
  72. dartsort/transform/pipeline.py +391 -0
  73. dartsort/transform/single_channel_denoiser.py +153 -0
  74. dartsort/transform/single_channel_templates.py +98 -0
  75. dartsort/transform/supervised_denoiser.py +486 -0
  76. dartsort/transform/temporal_pca.py +413 -0
  77. dartsort/transform/transform_base.py +255 -0
  78. dartsort/util/__init__.py +0 -0
  79. dartsort/util/cli_util.py +179 -0
  80. dartsort/util/data_util.py +1128 -0
  81. dartsort/util/drift_util.py +1054 -0
  82. dartsort/util/internal_config.py +1074 -0
  83. dartsort/util/interpolation_util.py +1012 -0
  84. dartsort/util/job_util.py +19 -0
  85. dartsort/util/logging_util.py +91 -0
  86. dartsort/util/main_util.py +235 -0
  87. dartsort/util/more_operators.py +269 -0
  88. dartsort/util/multiprocessing_util.py +211 -0
  89. dartsort/util/nn_util.py +461 -0
  90. dartsort/util/noise_util.py +1461 -0
  91. dartsort/util/peel_util.py +223 -0
  92. dartsort/util/py_util.py +213 -0
  93. dartsort/util/registration_util.py +128 -0
  94. dartsort/util/residual_util.py +30 -0
  95. dartsort/util/sparse_util.py +81 -0
  96. dartsort/util/spikeio.py +227 -0
  97. dartsort/util/spiketorch.py +1198 -0
  98. dartsort/util/testing_util/__init__.py +0 -0
  99. dartsort/util/testing_util/matching_debug_util.py +517 -0
  100. dartsort/util/testing_util/mixture_testing_util.py +291 -0
  101. dartsort/util/torch_optimization_util.py +156 -0
  102. dartsort/util/torch_util.py +75 -0
  103. dartsort/util/universal_util.py +269 -0
  104. dartsort/util/waveform_util.py +809 -0
  105. dartsort/vis/__init__.py +19 -0
  106. dartsort/vis/analysis_plots.py +392 -0
  107. dartsort/vis/colors.py +36 -0
  108. dartsort/vis/gt.py +392 -0
  109. dartsort/vis/layout.py +149 -0
  110. dartsort/vis/metrics_util.py +23 -0
  111. dartsort/vis/mixture.py +1489 -0
  112. dartsort/vis/over_time.py +514 -0
  113. dartsort/vis/recanim.py +429 -0
  114. dartsort/vis/scatterplots.py +779 -0
  115. dartsort/vis/sorting.py +163 -0
  116. dartsort/vis/unit.py +917 -0
  117. dartsort/vis/unit_comparison.py +1017 -0
  118. dartsort/vis/versus.py +300 -0
  119. dartsort/vis/vismain.py +537 -0
  120. dartsort/vis/waveforms.py +357 -0
  121. dartsort-0.3.5.dist-info/METADATA +93 -0
  122. dartsort-0.3.5.dist-info/RECORD +125 -0
  123. dartsort-0.3.5.dist-info/WHEEL +5 -0
  124. dartsort-0.3.5.dist-info/entry_points.txt +2 -0
  125. dartsort-0.3.5.dist-info/top_level.txt +1 -0
@@ -0,0 +1,448 @@
1
+ import numpy as np
2
+ import torch
3
+ import torch.nn.functional as F
4
+ from tqdm.auto import trange
5
+
6
+ try:
7
+ import cupy # type: ignore[reportMissingImports]
8
+
9
+ del cupy
10
+
11
+ HAVE_CUPY = True
12
+ except ImportError:
13
+ HAVE_CUPY = False
14
+
15
+
16
+ from ..util.logging_util import DARTSORTDEBUG, get_logger
17
+ from ..util.sparse_util import (
18
+ coo_to_cupy,
19
+ coo_to_scipy,
20
+ distsq_to_lik_coo,
21
+ logsumexp_coo,
22
+ sparse_centroid_distsq,
23
+ )
24
+ from ..util.spiketorch import spawn_torch_rg
25
+ from .density import guess_mode
26
+
27
+ logger = get_logger(__name__)
28
+
29
+
30
+ def kmeanspp(
31
+ X,
32
+ n_components=10,
33
+ random_state: np.random.Generator | torch.Generator | int = 0,
34
+ kmeanspp_initial="random",
35
+ mode_dim=2,
36
+ skip_assignment=False,
37
+ min_distance=None,
38
+ show_progress=False,
39
+ initial_distances=None,
40
+ ):
41
+ """K-means++ initialization
42
+
43
+ Start at a random point (kmeanspp_initial=='random') or at the point
44
+ farthest from the mean (kmeanspp_initial=='mean').
45
+ """
46
+ X = torch.asarray(X)
47
+ n, p = X.shape
48
+ n_components = min(n, n_components)
49
+
50
+ if isinstance(random_state, torch.Generator):
51
+ gen = random_state
52
+ else:
53
+ rg = np.random.default_rng(random_state)
54
+ gen = spawn_torch_rg(rg, device=X.device)
55
+
56
+ has_initial_dists = initial_distances is not None
57
+
58
+ centroid_ixs = torch.full((n_components,), n, dtype=torch.long, device=X.device)
59
+ dists = None
60
+ if has_initial_dists:
61
+ idists = torch.asarray(initial_distances, dtype=X.dtype, device=X.device)
62
+ else:
63
+ idists = None
64
+
65
+ if kmeanspp_initial == "random":
66
+ if dists is None:
67
+ centroid_ixs[0] = torch.randint(n, size=(), device=X.device, generator=gen)
68
+ else:
69
+ assert idists is not None
70
+ centroid_ixs[0] = torch.multinomial(idists, 1, generator=gen)
71
+ elif kmeanspp_initial == "mean":
72
+ closest = torch.cdist(X, X.mean(0, keepdim=True)).argmax()
73
+ centroid_ixs[0] = closest.item()
74
+ elif kmeanspp_initial == "mode":
75
+ Xm = X
76
+ if Xm.shape[1] > mode_dim:
77
+ q = min(mode_dim + 10, *Xm.shape)
78
+ u, s, v = torch.pca_lowrank(Xm, q=q, niter=7)
79
+ Xm = u[:, :mode_dim].mul_(s[:mode_dim])
80
+ cixs = guess_mode(Xm.numpy(force=True))
81
+ cixs = torch.asarray(cixs, dtype=centroid_ixs.dtype, device=centroid_ixs.device)
82
+ centroid_ixs.copy_(cixs)
83
+ else:
84
+ assert False
85
+
86
+ diff_buffer = X.clone()
87
+
88
+ dists = torch.subtract(X, X[centroid_ixs[0]], out=diff_buffer).square_().sum(1)
89
+ assignments = None
90
+ if not skip_assignment:
91
+ assignments = torch.zeros((n,), dtype=torch.long, device=X.device)
92
+
93
+ p = dists.clone()
94
+ xrange = trange if show_progress else range
95
+ for j in xrange(1, n_components):
96
+ if has_initial_dists:
97
+ assert idists is not None
98
+ torch.minimum(dists, idists, out=p)
99
+ else:
100
+ p.copy_(dists)
101
+ if min_distance:
102
+ invalid = dists < min_distance
103
+ if invalid.all():
104
+ logger.dartsortdebug(f"kmeanspp: All close, stop at iteration {j}.")
105
+ break
106
+ p.masked_fill_(invalid, 0.0)
107
+ centroid_ixs[j] = torch.multinomial(p, 1, generator=gen)
108
+
109
+ newdists = torch.subtract(X, X[centroid_ixs[j]], out=diff_buffer).square_()
110
+ newdists = torch.sum(newdists, dim=1, out=p)
111
+ if not skip_assignment:
112
+ closer = newdists < dists
113
+ assert assignments is not None
114
+ assignments.masked_fill_(closer, j)
115
+ torch.minimum(dists, newdists, out=dists)
116
+ else:
117
+ j += 1 # type: ignore
118
+
119
+ centroid_ixs = centroid_ixs[:j]
120
+ if not skip_assignment:
121
+ assignments = assignments
122
+ centroid_ixs = centroid_ixs.to(assignments)
123
+ phi = dists.mean()
124
+
125
+ return centroid_ixs, assignments, dists, phi
126
+
127
+
128
+ def truncated_kmeans(
129
+ X,
130
+ max_sigma=5.0,
131
+ n_components=10,
132
+ n_initializations=10,
133
+ random_state: int | np.random.Generator = 0,
134
+ n_iter=100,
135
+ min_log_prop=-25.0,
136
+ dirichlet_alpha=1.0,
137
+ kmeanspp_min_dist=0.0,
138
+ sigma_atol=1e-3,
139
+ batch_size=2048,
140
+ dcc_batch_size=64,
141
+ noise_const_dims=None,
142
+ with_log_likelihoods=False,
143
+ device=None,
144
+ show_progress=False,
145
+ ):
146
+ rg = np.random.default_rng(random_state)
147
+
148
+ if torch.is_tensor(X):
149
+ device = X.device
150
+ elif device is None:
151
+ device = "cuda" if torch.cuda.is_available() else "cpu"
152
+ device = torch.device(device)
153
+ is_gpu = device.type == "cuda"
154
+ X = torch.asarray(X, device=device)
155
+ gen = spawn_torch_rg(rg, device=X.device)
156
+ n, p = X.shape
157
+
158
+ # initialize...
159
+ sigmasq = torch.tensor(torch.inf)
160
+ nearest_distsq = centroid_ixs = labels = None
161
+ if show_progress:
162
+ it = trange(n_initializations, desc="kmeans++")
163
+ else:
164
+ it = range(n_initializations)
165
+ initial_distances = None
166
+ if noise_const_dims is not None:
167
+ noise_dims = np.setdiff1d(np.arange(X.shape[1]), noise_const_dims)
168
+ initial_distances = X[:, noise_dims].square().sum(1)
169
+ for _ in it:
170
+ _c, _l, _d, _p = kmeanspp(
171
+ X,
172
+ n_components=n_components,
173
+ random_state=gen,
174
+ kmeanspp_initial="random",
175
+ min_distance=kmeanspp_min_dist,
176
+ initial_distances=initial_distances,
177
+ )
178
+ if _p < sigmasq:
179
+ sigmasq = _p
180
+ centroid_ixs = _c
181
+ labels = _l
182
+ nearest_distsq = _d
183
+ assert torch.isfinite(sigmasq)
184
+ assert centroid_ixs is not None
185
+ assert labels is not None
186
+ assert nearest_distsq is not None
187
+
188
+ if logger.isEnabledFor(DARTSORTDEBUG):
189
+ logger.dartsortdebug(
190
+ f"truncated_kmeans: Max dist {nearest_distsq.max().sqrt().item()} for "
191
+ f"{len(centroid_ixs)} centroids. phi={sigmasq.sqrt().item()}."
192
+ )
193
+ del nearest_distsq, _d # type: ignore
194
+
195
+ # initialize parameters
196
+ n_components = len(centroid_ixs)
197
+ neg_nc_log = -torch.log(torch.tensor(float(n_components)))
198
+ log_proportions = X.new_full((n_components,), neg_nc_log) # type: ignore
199
+ sigmasq = sigmasq / p
200
+ centroids = X[centroid_ixs]
201
+
202
+ # scratch buffers
203
+ new_centroids = torch.zeros_like(centroids)
204
+ N = X.new_zeros(log_proportions.shape, dtype=torch.double)
205
+ prev_sigma = torch.inf
206
+ dcc = X.new_zeros((n_components, n_components))
207
+ dccbuf = X.new_zeros((dcc_batch_size, n_components, p))
208
+ distsq_buf = torch.zeros_like(X[: 20 * batch_size])
209
+ distsq_buf = (distsq_buf, distsq_buf.clone())
210
+ sigma = sigmasq.sqrt().numpy(force=True).item()
211
+ if with_log_likelihoods:
212
+ log_likelihoods = X.new_full((n,), -torch.inf)
213
+ else:
214
+ log_likelihoods = None
215
+
216
+ if show_progress:
217
+ it = trange(n_iter, desc=f"kmeans σ={sigma:0.4f}")
218
+ else:
219
+ it = range(n_iter)
220
+
221
+ done = False
222
+ for j in it:
223
+ done = done or j == n_iter - 1
224
+ max_distance_sq = max_sigma * sigmasq * p
225
+
226
+ new_centroids.fill_(0.0)
227
+ N.fill_(0.0)
228
+ new_sigmasq = 0.0
229
+ weight = 0.0
230
+
231
+ # update centroid dists
232
+ for i0 in range(0, n_components, dcc_batch_size):
233
+ i1 = min(n_components, i0 + dcc_batch_size)
234
+ torch.subtract(
235
+ centroids[None], centroids[i0:i1, None], out=dccbuf[: i1 - i0]
236
+ ).square_()
237
+ torch.sum(dccbuf[: i1 - i0], dim=2, out=dcc[i0:i1])
238
+ dccmask = dcc < max_distance_sq
239
+
240
+ for i0 in range(0, n, batch_size):
241
+ i1 = min(n, i0 + batch_size)
242
+ distsq_coo, distsq_buf = sparse_centroid_distsq(
243
+ X[i0:i1],
244
+ centroids,
245
+ labels=labels[i0:i1],
246
+ centroid_mask=dccmask,
247
+ dbufs=distsq_buf,
248
+ )
249
+ assert distsq_coo.shape == (i1 - i0, n_components)
250
+ distsq_values = distsq_coo.values().clone()
251
+ liks = distsq_to_lik_coo(
252
+ distsq_coo, sigmasq, log_proportions, in_place=True
253
+ )
254
+ del distsq_coo
255
+
256
+ if done and with_log_likelihoods:
257
+ assert log_likelihoods is not None
258
+ batch_liks = logsumexp_coo(liks)
259
+ log_likelihoods[i0:i1] = batch_liks
260
+ if done:
261
+ continue
262
+
263
+ resps = torch.sparse.softmax(liks, dim=1)
264
+ # update labels... torch sparse has no argmax(), so need scipy
265
+ # or cupy. scipy is a big slowdown here, so cupy if possible.
266
+ if is_gpu and HAVE_CUPY:
267
+ resps_cupy = coo_to_cupy(resps).tocsc()
268
+ batch_labels = resps_cupy.argmax(axis=1)
269
+ else:
270
+ resps_scipy = coo_to_scipy(resps)
271
+ batch_labels = resps_scipy.argmax(axis=1, explicit=True)
272
+ labels[i0:i1] = torch.as_tensor(batch_labels).to(labels).squeeze()
273
+
274
+ # get sigmasq
275
+ w = resps.values().clone()
276
+ batch_w = w.sum()
277
+ w /= batch_w
278
+ batch_sigmasq = torch.sum(distsq_values.mul_(w)) / p
279
+
280
+ # get N and centroids
281
+ batch_N = resps.sum(dim=0).to_dense()
282
+ resps.values().div_(batch_N[resps.indices()[1]])
283
+ batch_centroids = resps.T @ X[i0:i1]
284
+
285
+ # update counts
286
+ N += batch_N
287
+ weight += batch_w
288
+
289
+ # update Welford running means
290
+ n1_n01 = batch_N.div_(N.clip(min=1e-5))[:, None]
291
+ w1_w01 = batch_w / weight
292
+ new_centroids += batch_centroids.sub_(new_centroids).mul_(n1_n01)
293
+ new_sigmasq += batch_sigmasq.sub_(new_sigmasq).mul_(w1_w01)
294
+
295
+ if done:
296
+ break
297
+
298
+ # update state
299
+ logN = N.log_() + dirichlet_alpha
300
+ log_proportions = F.log_softmax(logN, dim=0).to(X)
301
+ log_proportions = log_proportions.clamp_(min=min_log_prop)
302
+ centroids, new_centroids = new_centroids, centroids
303
+ sigmasq = new_sigmasq
304
+
305
+ # check convergence
306
+ sigma = torch.sqrt(sigmasq).numpy(force=True).item() # type: ignore
307
+ if abs(sigma - prev_sigma) < sigma_atol:
308
+ done = True
309
+ if not with_log_likelihoods:
310
+ break
311
+ prev_sigma = sigma
312
+ if show_progress:
313
+ it.set_description(f"kmeans σ={sigma:0.4f}") # type: ignore
314
+
315
+ return dict(
316
+ centroid_ixs=centroid_ixs,
317
+ centroids=centroids,
318
+ sigmasq=sigmasq,
319
+ sigma=sigma,
320
+ log_proportions=log_proportions,
321
+ labels=labels,
322
+ log_likelihoods=log_likelihoods,
323
+ )
324
+
325
+
326
+ def kmeans_inner(
327
+ X,
328
+ n_kmeanspp_tries=5,
329
+ n_iter=100,
330
+ n_components=10,
331
+ random_state: np.random.Generator | torch.Generator | int = 0,
332
+ kmeanspp_initial="random",
333
+ with_proportions=False,
334
+ drop_prop=0.025,
335
+ drop_sum=5.0,
336
+ test_convergence=True,
337
+ atol=1e-5,
338
+ ):
339
+ """A bit more than K-means
340
+
341
+ Supports a proportion vector, as well as automatically dropping tiny clusters.
342
+ """
343
+ best_phi = torch.inf
344
+ centroid_ixs = labels = None
345
+ if isinstance(random_state, int):
346
+ random_state = np.random.default_rng(random_state)
347
+ for _ in range(n_kmeanspp_tries):
348
+ _centroid_ixs, _labels, _, phi = kmeanspp(
349
+ X,
350
+ n_components=n_components,
351
+ random_state=random_state,
352
+ kmeanspp_initial=kmeanspp_initial,
353
+ )
354
+ if phi < best_phi:
355
+ centroid_ixs = _centroid_ixs
356
+ labels = _labels
357
+ best_phi = phi
358
+ assert labels is not None
359
+
360
+ centroids = X[centroid_ixs]
361
+ dists = torch.cdist(X, centroids).square_()
362
+ # responsibilities, sum to 1 over centroids
363
+ e = F.softmax(-0.5 * dists, dim=1)
364
+ if not n_iter:
365
+ return labels, e, centroids, dists
366
+
367
+ proportions = e.mean(0)
368
+ phi = None
369
+
370
+ for j in range(n_iter):
371
+ keep = None
372
+ if drop_prop:
373
+ keep = proportions > drop_prop
374
+ e = e[:, keep]
375
+ proportions = proportions[keep]
376
+ if drop_sum:
377
+ totals = e.sum(0)
378
+ keep = totals > drop_sum
379
+ e = e[:, keep]
380
+ proportions = proportions[keep]
381
+ if keep is not None and (not keep.numel() or not keep.any()):
382
+ return torch.full_like(labels, 0), None, None, None
383
+
384
+ # update centroids
385
+ w = e / e.sum(0)
386
+ centroids = w.T @ X
387
+
388
+ # e step
389
+ dists = torch.cdist(X, centroids).square_()
390
+ if with_proportions:
391
+ e = F.softmax(-0.5 * dists + proportions.log(), dim=1)
392
+ else:
393
+ e = F.softmax(-0.5 * dists, dim=1)
394
+ proportions = e.mean(0)
395
+
396
+ if test_convergence:
397
+ phi_ = (e @ dists.T).mean()
398
+ if phi is not None and torch.isclose(phi, phi_, atol=atol):
399
+ break
400
+ phi = phi_
401
+
402
+ assignments = torch.argmin(dists, 1)
403
+ return assignments, e, centroids, dists
404
+
405
+
406
+ def kmeans(
407
+ X,
408
+ n_kmeans_tries=5,
409
+ n_kmeanspp_tries=5,
410
+ n_iter=100,
411
+ n_components=10,
412
+ random_state: np.random.Generator | torch.Generator | int = 0,
413
+ kmeanspp_initial="random",
414
+ with_proportions=False,
415
+ drop_prop=0.025,
416
+ drop_sum=5.0,
417
+ test_convergence=True,
418
+ ):
419
+ best_phi = np.inf
420
+ if isinstance(random_state, int):
421
+ random_state = np.random.default_rng(random_state)
422
+ assignments = torch.zeros(len(X), dtype=torch.long)
423
+ e = centroids = dists = None
424
+ for j in range(n_kmeans_tries):
425
+ aa, ee, cc, dists = kmeans_inner(
426
+ X,
427
+ n_kmeanspp_tries=n_kmeanspp_tries,
428
+ n_iter=n_iter,
429
+ n_components=n_components,
430
+ random_state=random_state,
431
+ kmeanspp_initial=kmeanspp_initial,
432
+ with_proportions=with_proportions,
433
+ drop_prop=drop_prop,
434
+ drop_sum=drop_sum,
435
+ test_convergence=test_convergence,
436
+ )
437
+ if dists is None:
438
+ continue
439
+ assert ee is not None
440
+ phi = (ee * dists).sum(1).mean().numpy(force=True)
441
+ if phi < best_phi:
442
+ best_phi = phi
443
+ assignments = aa
444
+ e = ee
445
+ centroids = cc
446
+ return dict(
447
+ labels=assignments, responsibilities=e, centroids=centroids, dists=dists
448
+ )