Skip to content

Commit bcd115b

Browse files
committed
add warning for degenerate diffusion operator
1 parent ec7d6ea commit bcd115b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Python/phate/mds.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ def embed_MDS(
200200
low dimensional embedding of X using MDS
201201
"""
202202

203+
print('here')
204+
203205
if how not in ["classic", "metric", "nonmetric"]:
204206
raise ValueError(
205207
"Allowable 'how' values for MDS: 'classic', "
@@ -223,6 +225,22 @@ def embed_MDS(
223225
else:
224226
X_dist = squareform(pdist(X, distance_metric))
225227

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

0 commit comments

Comments
 (0)