automatic-writing / projection_axes.py
jejunepixels's picture
Add optional projected original-dimension axis guides with opacity and origin controls
9d3e0c7 verified
Raw History Blame Contribute Delete
1.38 kB
"""Directions of original feature axes under the actual three-dimensional PCA map."""
import math
import torch
def projected_dimension_axes(basis, center, scale, space):
basis=basis.detach().float().cpu()
center=center.detach().float().cpu()
scale=float(scale)
if basis.ndim!=2 or not basis.shape[0] or basis.shape[1]!=3 or center.shape!=(basis.shape[0],):
raise ValueError("Axis geometry must use a D-by-3 basis and its D-dimensional centre.")
if not math.isfinite(scale) or scale<=0 or not torch.isfinite(basis).all() or not torch.isfinite(center).all():
raise ValueError("Axis projection must be finite with a positive scale.")
unit_directions=basis*(2.4/scale)
# One common extent preserves the relative projected lengths of all dimensions.
extent=3.2/float(torch.linalg.vector_norm(unit_directions,dim=1).quantile(.95).clamp_min(1e-8))
directions=unit_directions*extent
raw_origin=(-center@basis)*(2.4/scale)
rounded=lambda row:[round(float(value),6) for value in row]
return {"count":len(basis),"vectors":[rounded(row) for row in directions],
"raw_origin":rounded(raw_origin),"coordinate_extent":extent,
"guide_radius":3.2,"space":space,
"description":"Original feature directions through the chosen origin; a common extent is used for visibility"}