Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Topological data analysis: persistent homology, Mapper, and TDA tools
.claude/skills/brycewang-stanford-topology-data-analysis/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-07 | ✗→✓ | ▲ Improved | 50% | 0% |
| case-16 | ✗→✓ | ▲ Improved | 127% | 0% |
| case-09 | ✓→✗ | ▼ Worse | 98% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 61% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 72% | 0% |
A skill for applying topological data analysis (TDA) methods to research data. Covers persistent homology, Vietoris-Rips complexes, persistence diagrams, the Mapper algorithm, and vectorization methods for integrating topological features into machine learning pipelines.
TDA extracts topological features (connected components, loops, voids) from data by building simplicial complexes at multiple scales:
| Complex | Construction | Computational Cost | |---------|-------------|-------------------| | Vietoris-Rips | Edge if distance < epsilon | O(n^d) for d-simplices | | Cech | Ball intersection (exact) | Computationally expensive | | Alpha | Delaunay-based (exact in low dim) | Efficient in R^2, R^3 | | Cubical | Grid-based (for images) | Linear in pixels |
Scale epsilon: 0.1 0.3 0.5 0.7 1.0
|------|------|------|------|------|
Components: 10 6 3 2 1
(H0 features born at 0, die at merging scale)
Loops: 0 0 1 2 0
(H1 features born when loop forms, die when filled)A feature that persists across many scales is a genuine topological signal; short-lived features are noise.
pythonimport numpy as np from ripser import ripser from persim import plot_diagrams def compute_persistence(point_cloud: np.ndarray, max_dim: int = 2, max_edge: float = 2.0) -> dict: """ Compute persistent homology of a point cloud. point_cloud: (n_points, n_dimensions) array max_dim: maximum homology dimension to compute max_edge: maximum edge length in Rips complex Returns persistence diagrams for each dimension. """ result = ripser( point_cloud, maxdim=max_dim, thresh=max_edge, ) diagrams = result["dgms"] summary = {} for dim, dgm in enumerate(diagrams): # Filter out infinite death times for H0 finite = dgm[dgm[:, 1] < np.inf] if len(dgm) > 0 else dgm lifetimes = finite[:, 1] - finite[:, 0] if len(finite) > 0 else np.array([]) summary[f"H{dim}"] = { "n_features": len(finite), "max_persistence": float(lifetimes.max()) if len(lifetimes) > 0 else 0, "mean_persistence": float(lifetimes.mean()) if len(lifetimes) > 0 else 0, "birth_death_pairs": finite.tolist(), } return summary # Example: torus point cloud def sample_torus(n=1000, R=3.0, r=1.0, noise=0.1): """Sample points from a torus in R^3.""" theta = np.random.uniform(0, 2 * np.pi, n) phi = np.random.uniform(0, 2 * np.pi, n) x = (R + r * np.cos(phi)) * np.cos(theta) + np.random.normal(0, noise, n) y = (R + r * np.cos(phi)) * np.sin(theta) + np.random.normal(0, noise, n) z = r * np.sin(phi) + np.random.normal(0, noise, n) return np.column_stack([x, y, z]) torus = sample_torus(500) persistence = compute_persistence(torus, max_dim=2) # Expected: H0 has 1 long-lived component, # H1 has 2 prominent loops (the two fundamental cycles), # H2 has 1 prominent void (the cavity)
To use topological features in machine learning, persistence diagrams must be vectorized:
pythonfrom sklearn.base import BaseEstimator, TransformerMixin class PersistenceStatistics(BaseEstimator, TransformerMixin): """ Extract statistical features from persistence diagrams. Produces a fixed-length feature vector from variable-length diagrams. """ def __init__(self, max_dim: int = 1): self.max_dim = max_dim def fit(self, X, y=None): return self def transform(self, diagrams_list: list) -> np.ndarray: features = [] for diagrams in diagrams_list: row = [] for dim in range(self.max_dim + 1): dgm = diagrams[dim] lifetimes = dgm[:, 1] - dgm[:, 0] lifetimes = lifetimes[np.isfinite(lifetimes)] if len(lifetimes) == 0: row.extend([0, 0, 0, 0, 0, 0]) else: row.extend([ len(lifetimes), # count np.sum(lifetimes), # total persistence np.max(lifetimes), # max persistence np.mean(lifetimes), # mean persistence np.std(lifetimes), # std persistence np.sum(lifetimes ** 2), # persistence entropy proxy ]) features.append(row) return np.array(features)
pythondef persistence_image(diagram: np.ndarray, resolution: int = 20, sigma: float = 0.1, weight_fn=None) -> np.ndarray: """ Compute a persistence image from a persistence diagram. Transforms birth-death pairs into a stable, fixed-size representation. """ if weight_fn is None: weight_fn = lambda birth, persistence: persistence # Transform to birth-persistence coordinates births = diagram[:, 0] persistences = diagram[:, 1] - diagram[:, 0] # Create grid x_range = np.linspace(births.min() - sigma, births.max() + sigma, resolution) y_range = np.linspace(0, persistences.max() + sigma, resolution) xx, yy = np.meshgrid(x_range, y_range) image = np.zeros((resolution, resolution)) for b, p in zip(births, persistences): if not np.isfinite(p): continue w = weight_fn(b, p) gaussian = w * np.exp(-((xx - b)**2 + (yy - p)**2) / (2 * sigma**2)) image += gaussian return image
Mapper provides a compressed topological summary of high-dimensional data:
pythonimport kmapper as km from sklearn.cluster import DBSCAN def run_mapper(data: np.ndarray, lens_fn=None, n_cubes: int = 10, overlap: float = 0.3) -> dict: """ Run the Mapper algorithm to produce a simplicial complex summarizing the shape of the data. data: (n_samples, n_features) array lens_fn: filter function (default: first two PCA components) """ mapper = km.KeplerMapper(verbose=0) # Compute lens (filter function) if lens_fn is None: from sklearn.decomposition import PCA lens = mapper.fit_transform(data, projection=PCA(n_components=2)) else: lens = lens_fn(data) # Build the Mapper graph graph = mapper.map( lens, data, cover=km.Cover(n_cubes=n_cubes, perc_overlap=overlap), clusterer=DBSCAN(eps=0.5, min_samples=3), ) # Summary statistics n_nodes = len(graph["nodes"]) n_edges = sum(len(v) for v in graph["links"].values()) // 2 return { "n_nodes": n_nodes, "n_edges": n_edges, "node_sizes": [len(v) for v in graph["nodes"].values()], "graph": graph, }
| Parameter | Effect | Guidance | |-----------|--------|---------| | Filter function | Projects data to low dimensions | PCA, eccentricity, density | | Number of intervals | Controls resolution of cover | 10-30 typical | | Overlap percentage | Controls connectivity | 20-50%, higher = more edges | | Clustering algorithm | Groups points within intervals | DBSCAN, single-linkage |
pythonfrom persim import bottleneck, wasserstein def compare_persistence_diagrams(dgm1: np.ndarray, dgm2: np.ndarray) -> dict: """ Compare two persistence diagrams using standard TDA distances. """ bn_dist = bottleneck(dgm1, dgm2) ws_dist = wasserstein(dgm1, dgm2, order=2) return { "bottleneck_distance": round(bn_dist, 6), "wasserstein_2_distance": round(ws_dist, 6), }
pythondef permutation_test_persistence(data1: np.ndarray, data2: np.ndarray, n_permutations: int = 1000, dim: int = 1) -> dict: """ Test whether two point clouds have significantly different topological features using a permutation test on Wasserstein distance. """ from persim import wasserstein # Observed distance dgm1 = ripser(data1, maxdim=dim)["dgms"][dim] dgm2 = ripser(data2, maxdim=dim)["dgms"][dim] observed = wasserstein(dgm1, dgm2) # Permutation distribution combined = np.vstack([data1, data2]) n1 = len(data1) perm_distances = [] for _ in range(n_permutations): perm = np.random.permutation(len(combined)) perm_d1 = combined[perm[:n1]] perm_d2 = combined[perm[n1:]] perm_dgm1 = ripser(perm_d1, maxdim=dim)["dgms"][dim] perm_dgm2 = ripser(perm_d2, maxdim=dim)["dgms"][dim] perm_distances.append(wasserstein(perm_dgm1, perm_dgm2)) p_value = np.mean(np.array(perm_distances) >= observed) return { "observed_distance": round(observed, 6), "p_value": round(p_value, 4), "significant_at_005": p_value < 0.05, }
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→fail | 15,645 | 18,652 | +19% | 1 | 1 | 0% | 2,944 | 5,425 | +84% | 0 | 0 | — |
case-02 | pass→pass | 17,764 | 12,977 | -27% | 1 | 1 | 0% | 3,050 | 4,912 | +61% | 0 | 0 | — |
case-03 | pass→pass | 14,392 | 8,253 | -43% | 1 | 1 | 0% | 2,476 | 4,265 | +72% | 0 | 0 | — |
case-04 | pass→pass | 17,062 | 19,100 | +12% | 1 | 1 | 0% | 2,942 | 6,194 | +111% | 0 | 0 | — |
case-17 | pass→pass | 3,658 | 11,316 | +209% | 1 | 1 | 0% | 558 | 3,220 | +477% | 0 | 0 | — |
case-05 | pass→pass | 11,248 | 10,611 | -6% | 1 | 1 | 0% | 1,847 | 4,560 | +147% | 0 | 0 | — |
case-06 | fail→fail | 20,207 | 17,857 | -12% | 1 | 1 | 0% | 3,878 | 6,221 | +60% | 0 | 0 | — |
case-07 | fail→pass | 17,998 | 8,268 | -54% | 1 | 1 | 0% | 2,788 | 4,173 | +50% | 0 | 0 | — |
case-08 | pass→pass | 10,215 | 9,833 | -4% | 1 | 1 | 0% | 1,532 | 4,412 | +188% | 0 | 0 | — |
case-09 | pass→fail | 16,239 | 15,987 | -2% | 1 | 1 | 0% | 2,904 | 5,757 | +98% | 0 | 0 | — |
case-10 | pass→pass | 17,391 | 20,136 | +16% | 1 | 1 | 0% | 2,740 | 5,951 | +117% | 0 | 0 | — |
case-11 | pass→pass | 10,446 | 6,996 | -33% | 1 | 1 | 0% | 1,833 | 4,002 | +118% | 0 | 0 | — |
case-12 | pass→pass | 17,377 | 23,534 | +35% | 1 | 1 | 0% | 2,944 | 6,886 | +134% | 0 | 0 | — |
case-13 | pass→pass | 17,607 | 14,456 | -18% | 1 | 1 | 0% | 2,725 | 5,014 | +84% | 0 | 0 | — |
case-14 | pass→pass | 12,740 | 5,940 | -53% | 1 | 1 | 0% | 1,937 | 3,695 | +91% | 0 | 0 | — |
case-15 | pass→pass | 9,237 | 2,453 | -73% | 1 | 1 | 0% | 1,425 | 3,158 | +122% | 0 | 0 | — |
case-16 | fail→pass | 16,792 | 17,729 | +6% | 1 | 1 | 0% | 2,500 | 5,666 | +127% | 0 | 0 | — |
case-18 | pass→pass | 12,318 | 12,342 | +0% | 1 | 1 | 0% | 1,865 | 4,623 | +148% | 0 | 0 | — |
case-19 | pass→pass | 7,798 | 10,272 | +32% | 1 | 1 | 0% | 1,254 | 4,307 | +243% | 0 | 0 | — |
case-20 | pass→pass | 17,083 | 17,178 | +1% | 1 | 1 | 0% | 2,579 | 5,592 | +117% | 0 | 0 | — |
case-21 | pass→pass | 12,950 | 15,556 | +20% | 1 | 1 | 0% | 2,013 | 5,302 | +163% | 0 | 0 | — |
case-22 | fail→fail | 27,538 | 34,895 | +27% | 1 | 1 | 0% | 4,889 | 9,405 | +92% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +5 percentage points is the difference between those two pass rates over the 22 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.