The tda4j Python package is the MATLAB and Java interface with NumPy in and out: the same library, running in a JVM that the package starts for you. It needs Python 3.10 or newer, and Java 21 or newer on the machine (java on the PATH, or JAVA_HOME).

Install

The package is not on PyPI yet, so pip install tda4j does not find it. Take tda4j-VERSION-py3-none-any.whl from the releases and install that:

pip install tda4j-VERSION-py3-none-any.whl

The wheel carries the library's jar, so there is nothing else to install. For the development version, build the wheel from a source checkout (Java 21 and sbt 2):

sbt tda4j/assembly              # the fat jar
python/scripts/bundle-jar.sh    # copies it into the package
pip install python/

Without bundling, TDA4J_JAR=/path/to/tda4j-VERSION-assembly.jar points the package at a jar. A first call:

import numpy as np
import tda4j

square = np.array([[0, 0], [1, 0], [1, 1], [0, 1]])
result = tda4j.persistence(square, max_dimension=1)

result.bars                # rows (dimension, birth, death); death is inf for a class that never dies
result.diagram(1)          # [[1.0, 1.414...]]: the loop, as (birth, death)
result.representative(4)   # the loop of the last bar: its edges and their coefficients
result.bar_cells(4)        # (2, 3) and (0, 1, 2): the edge that opens that loop and the triangle that closes it
result.plot_barcode()      # a figure; in a notebook it shows itself, `.save("barcode.svg")` writes it

Conventions

  • Options are keyword arguments in snake_case, and they are the options of the MATLAB and Java interface: max_dimension=1 is maxDimension, max_filtration_value, prime=3 (the default is 17), complex="alpha", engine, field="Q" (exact rationals; "R" for the reals), representative_type="cocycles", include_zero_length=True, min_persistence. A value can be a number, a string or a boolean; None leaves the option unset. A name that is not an option raises ValueError and lists the accepted ones. The defaults are the interface's: degrees 0 to 2, a bar shorter than 1% of the scale of the data is hidden (result.all_bars has every bar, result.hidden_count says how many were left out).
  • Arrays can be anything NumPy reads as numbers: arrays of any layout and type, lists, tuples. A point cloud has one row per point (np.loadtxt("points.csv", delimiter=",") reads a CSV, frame.to_numpy() a pandas frame); one-dimensional data is a column (x.reshape(-1, 1)).
  • Numbering is from 0 everywhere: bars, points, cells, cocycle indices. A bar is its row in result.bars.
  • Errors from the library are Python errors: ValueError for a bad argument, IndexError, NotImplementedError, and tda4j.Tda4jError for the rest.
  • The JVM starts when the first function needs it. To give it a bigger heap, which is the limit on how large a computation can be, start it yourself before that: tda4j.start_jvm(["-Xmx16g"]). JAVA_HOME chooses the Java.

Functions

function input
persistence(points, **options) a point cloud, one row per point; any complex
persistence_from_distances(distances, **options) a square distance matrix
persistence_from_image(image, **options) an image or voxel grid of any dimension, as a cubical complex; nan is missing data
persistence_from_relation(relation, **options) the Dowker complex of a relation, a matrix that need not be square; dual=True builds it on the columns, representatives_on="columns" puts the representatives there
persistence_from_landmarks(points, landmarks, **options) the witness complex on the landmarks; persistence_from_distances_and_landmarks for a distance matrix
relative_persistence(points, subcomplex, **options) the Vietoris-Rips complex relative to the subcomplex on the points subcomplex; relative_persistence_from_distances for a distance matrix
harmonic_chain_barcode(points, **options), harmonic_representatives(points, **options) a Result of the harmonic chains of the Vietoris-Rips complex: the harmonic chain barcode, or the ordinary bars with harmonic representatives (representative(i) is the harmonic cycle); harmonic_chain_barcode_from_distances and harmonic_representatives_from_distances for a distance matrix. field is "R" (the default here) or "Q"; representative_type="cocycles" gives harmonic_representatives the harmonic cocycles
inclusion_map(points, subsample, **options), simplicial_map(source_points, target_points, vertex_map, **options) the map on persistent homology a subsample or a simplicial map induces: a MapResult with, in each degree, source_bars, target_bars, entries, image, kernel, cokernel; inclusion_map_from_distances, simplicial_map_from_distances for distance matrices
select_landmarks(points, num_landmarks), covering_radius(points, landmarks) landmarks by farthest-point sampling, and the covering radius of any landmarks; ..._from_distances for distance matrices
euler_curve(points, **options) the Euler characteristic curve of the Vietoris-Rips complex: call it, curve(t), or curve.sample(t_min, t_max, n)
bootstrap_band(points, **options) the resampling band of the bars of one degree
tomato_clusters(points, k, tau), tomato_diagram(points, k) ToMATo clustering: the cluster of every point, and the valleys between peaks
takens_embedding(series, dimension, delay=1, stride=1), delay_by_autocorrelation(series, max_lag) the sliding-window embedding of a time series (or of a series of vectors) as a point cloud, and a first guess for its delay
persistent_laplacian(points, dimension, start, stop), persistent_laplacian_from_distances(distances, dimension, start, stop) the persistent Laplacian of the Vietoris-Rips complex: matrix, eigenvalues (ascending), betti_number, spectral_gap (None if every eigenvalue is zero) and cells
persistent_laplacian_of_map(source_points, target_points, vertex_map, dimension, start, stop), persistent_laplacian_of_map_from_distances(source_distances, target_distances, vertex_map, dimension, start, stop) the persistent Laplacian of a simplicial map between the Vietoris-Rips complexes of two spaces: vertex_map[i] is the index of the target point that source point i goes to; the same result, with weights, and betti_number the rank of the map on homology
morse_reduction(points, **options), morse_reduction_from_distances(distances, **options) the discrete Morse reduction of the Vietoris-Rips complex: cell_counts and critical_counts by dimension, pair_count, critical_cells and bars (computed on the critical cells); options max_dimension, max_filtration_value, strategy ("reduction" or "coreduction"), prime, threads
zigzag_from_steps(initial, simplices, additions, **options), zigzag_from_complexes(complexes, **options) zigzag persistence of a sequence of simplicial complexes (simplices as lists of vertex numbers), given as steps that add or remove one simplex or as a list of complexes: bars (rows (dimension, birth, death)), representatives (a cycle at each bar's birth) and last_position; options max_dimension, prime and, for complexes, link ("none", "union" or "intersection")
product_bars(first, second, filtration="max"), hom_bars(first, second), hom_dimension(first, second), dual_bars(bars) the algebra of barcodes: the bars of a product of two filtered complexes from the bars of the factors, the internal Hom, the dimension of the space of maps, the dual; each barcode is a result (all of its bars) or an array of rows (dimension, birth, death)
h1_bars(points), circular_coordinates(points, r), toroidal_coordinates(points, r, cocycle_indices) circular and toroidal coordinates; num_landmarks=n on all three gives Perea's sparse coordinates over n landmarks; complex="cech" or "alpha" on all three takes the loops of that complex (radii, not diameters; no landmarks)
load_result(path) a result saved by result.save(path), by the other interfaces, or by --save-result

What a result has

  • bars, all_bars, diagram(dimension), hidden_count, persistence_threshold, and len(result).
  • representative(i): a Representative with cells (tuples of vertex numbers) and coefficients. For an image a cell is a cube in doubled coordinates (see MATLAB and Java).
  • bar_cells(i): the cell that created the bar and the cell that destroyed it (None for a bar that never dies); see the quickstart for which engines record them.
  • bottleneck_distance, wasserstein_distance, sliced_wasserstein_distance, bottleneck_matching, wasserstein_matching, each against another result and a dimension.
  • landscape, betti_curve, silhouette, persistence_entropy, persistence_image (see vectorization).
  • boundary() and cells(): the filtered complex as a boundary matrix in coordinate form, and its cells.
  • plot_barcode(), plot_diagram(), plot_landscape(dim), plot_persistence_image(dim), plot_input(), plot_complex(at), plot_filtration(max), plot_representative(i) and the _3d variants return a Plot: svg() and html() give its text, save(path) writes an .svg or .html file, view() opens it in the library's live viewer, and a notebook shows it. The tweaks that take plain arguments are methods in snake_case that return a new plot: plot.with_title("A circle").with_size(800, 600). tda4j.side(a, b) puts plots in one row, and tda4j.set_plot_defaults("palette=print") changes the look of the next ones. plot_filtration(max) is a page with a slider for the scale beside the persistence diagram (save("page.html"); save_frames(directory, count) writes a still picture at each of count scales, for a video or the slides of a talk, see a slider through the filtration). plot_explore() and plot_linked_filtration(max) are pages whose pictures are linked, zoom and choose classes (see exploring a barcode). A notebook shows these pages, and plot_filtration's, as the live page (a Jupyter notebook has to be trusted to run its scripts); every other plot shows as its SVG.
  • save(path), to be read back by load_result.

Tutorials

Most tutorials show their code in a Python tab beside the Scala and MATLAB ones, starting with finding a loop, and All the ways to call TDA4j does three small tasks in each language.

Coming from ripser.py or GUDHI

tda4j.persistence(X, max_dimension=1, prime=2).diagram(1) is ripser(X, maxdim=1)['dgms'][1]; the defaults that differ (the coefficients, and the top degree of GUDHI) are in Coming from JavaPlex, Ripser or GUDHI.

What is not in the package

The package is the interface of MATLAB and Java, so what that interface does not have, Python does not: the typed Scala API, with its streams, engines and cursors, and the simplicial sets and group nerves of the sset add-on. The tutorials on Truchet tiles, cup products and group cohomology are in that part, and have no Python tab. The tweaks of a plot that take Scala values (withPoints(size = 3.5), a Palette) and a board of plots with a free layout (TDA4j.layout) are not wrapped; tda4j.set_plot_defaults changes the look of every plot, and tda4j.side puts plots in a row.