Skip to content

Commit b1694b9

Browse files
authored
Merge pull request #171 from MattScicluna/fix_sgd_mds
add warning for degenerate diffusion operator
2 parents ec7d6ea + cdfa109 commit b1694b9

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Python/phate/mds.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ def embed_MDS(
223223
else:
224224
X_dist = squareform(pdist(X, distance_metric))
225225

226+
# Check for degenerate distance matrix before calling classic MDS
227+
# This happens with extreme hyperparameters (e.g., KNN close to dataset size)
228+
# causing complete diffusion homogeneity
229+
if X_dist.std() < 1e-10 or len(np.unique(X_dist)) <= 1:
230+
import warnings
231+
warnings.warn(
232+
f"Degenerate distance matrix detected (std={X_dist.std():.2e}, "
233+
f"unique_values={len(np.unique(X_dist))}). "
234+
"This typically occurs when hyperparameters cause complete diffusion homogeneity "
235+
"(e.g., KNN close to dataset size). "
236+
"Returning zero embedding.",
237+
RuntimeWarning
238+
)
239+
# Return all zeros to indicate complete collapse
240+
return np.zeros((X_dist.shape[0], ndim))
241+
226242
# initialize all by CMDS
227243
Y_classic = classic(X_dist, n_components=ndim, random_state=seed)
228244
if how == "classic":
@@ -244,6 +260,7 @@ def embed_MDS(
244260
)
245261
else:
246262
raise RuntimeError
263+
247264
if how == "metric":
248265
# re-orient to classic
249266
_, Y, _ = scipy.spatial.procrustes(Y_classic, Y)

0 commit comments

Comments
 (0)