From bcd115b768e3b36c83c165a0e786d1583287cd2f Mon Sep 17 00:00:00 2001 From: Matt Scicluna Date: Fri, 7 Nov 2025 16:59:46 -0500 Subject: [PATCH 1/2] add warning for degenerate diffusion operator --- Python/phate/mds.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Python/phate/mds.py b/Python/phate/mds.py index fdb3d45..16794d0 100644 --- a/Python/phate/mds.py +++ b/Python/phate/mds.py @@ -200,6 +200,8 @@ def embed_MDS( low dimensional embedding of X using MDS """ + print('here') + if how not in ["classic", "metric", "nonmetric"]: raise ValueError( "Allowable 'how' values for MDS: 'classic', " @@ -223,6 +225,22 @@ def embed_MDS( else: X_dist = squareform(pdist(X, distance_metric)) + # Check for degenerate distance matrix before calling classic MDS + # This happens with extreme hyperparameters (e.g., KNN close to dataset size) + # causing complete diffusion homogeneity + if X_dist.std() < 1e-10 or len(np.unique(X_dist)) <= 1: + import warnings + warnings.warn( + f"Degenerate distance matrix detected (std={X_dist.std():.2e}, " + f"unique_values={len(np.unique(X_dist))}). " + "This typically occurs when hyperparameters cause complete diffusion homogeneity " + "(e.g., KNN close to dataset size). " + "Returning zero embedding.", + RuntimeWarning + ) + # Return all zeros to indicate complete collapse + return np.zeros((X_dist.shape[0], ndim)) + # initialize all by CMDS Y_classic = classic(X_dist, n_components=ndim, random_state=seed) if how == "classic": @@ -244,6 +262,7 @@ def embed_MDS( ) else: raise RuntimeError + if how == "metric": # re-orient to classic _, Y, _ = scipy.spatial.procrustes(Y_classic, Y) From cdfa109f253ced745d8766d934c8b1e812931cf6 Mon Sep 17 00:00:00 2001 From: Matthew Scicluna Date: Fri, 14 Nov 2025 12:06:32 -0500 Subject: [PATCH 2/2] Remove debug print from MDS function Removed debug print statement from MDS function. --- Python/phate/mds.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Python/phate/mds.py b/Python/phate/mds.py index 16794d0..555e5b5 100644 --- a/Python/phate/mds.py +++ b/Python/phate/mds.py @@ -200,8 +200,6 @@ def embed_MDS( low dimensional embedding of X using MDS """ - print('here') - if how not in ["classic", "metric", "nonmetric"]: raise ValueError( "Allowable 'how' values for MDS: 'classic', "