Spectral layout improvements - #437
Draft
karlmoresco wants to merge 3 commits into
Draft
Conversation
karlmoresco
marked this pull request as draft
August 7, 2026 14:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Diffusion map
Overview
This is an experimental added option to the spectral layout algorithm that returns the diffusion map coordinates as opposed to the standard spectral layout coordinates. This should theoretically produce layouts where nodes that are truly (via "heat flow") proximate are placed near closely in the layout. If
diffusion_timeisn't passed specifically, the function behaves the same as before.Diffusion map (bottom equation):

The "time" parameter
There's a variable parameter
diffusion_time("t" in the equations) that govern the behavior of this coordinate mapping, and refining this layout step includes finding the appropriate range for this parameter. At too highdiffusion_time, the coordinates collapse to the origin (as can be seen from the equations). Note that the t-scales fornormalize=Trueandnormalize=Falseare not the same, as the lower spectrum of the normalized laplacian usually has smaller eigenvalues compared to the non-normalized. Also, the heat kernel correspond to different things for the normalized and non-normalized Laplacian:"Though, it is important to note, that the underlying dynamics of the degree-normalized Laplacians correspond more closely to stochastic diffusion, i.e., modelling the transition probabilities of discrete ’walkers’, rather than the physical diffusion of ’heat’ governed by the discrete ∇2 operator". (source)
Approximation
Keep in mind this method is based on an approximation of the heat kernel:
Heat kernel:

Where N=4 in the approximation, as opposed to the exact definition where N is the number of eigenvectors of the Laplacian. Observing the diffusion distance definition ("heat distance" above), it's clear that the approximation is justified as the terms of the sum decay exponentially. Still, this diffusion map approach might not create meaningful results because of its approximate nature.
Docstring
This PR also updates the docstring of
spectral_layoutwith more details and a full Google style docstring signature. There's a separate PR for only the docstring change.Type of change
Please delete options that are not relevant.
PR checklist:
Note
Medium Risk
Changes numerical behavior of the psvd spectral path and adds a new layout mode; public return type is unchanged but coordinates can differ when diffusion_time is set or via the svds fix.
Overview
Adds an optional
diffusion_timeargument tospectral_layout, so embeddings can use diffusion-map coordinates (eigenvector axes scaled byexp(-λ t)) instead of plain Laplacian eigenvectors._spectral_layout_eigenand_spectral_layout_psvdnow return the selected eigenvalues alongside raw coordinates to support that step; negativediffusion_timeis rejected.For the bipartite
psvdpath,svdsis called withwhich="LM", and Laplacian eigenvalues are derived as1.0 - sfrom the retained singular values. Thespectral_layoutdocstring is expanded to a full Google-style description of behavior, parameters, and errors. Default behavior whendiffusion_timeis omitted is unchanged.Reviewed by Cursor Bugbot for commit 8f244f0. Bugbot is set up for automated code reviews on this repo. Configure here.