Written by
- Takeki Sudo and Kazushi Ahara, Meiji University
- Shizuo Kaji, Kyushu University.
CubicalRipser is an extension of Ripser by Ulrich Bauer, tailored for the efficient computation of persistent homology of cubical complexes.
- High Performance: Among the fastest tools for computing persistent homology of 2D and 3D cubical complexes.
- Flexible Filtrations: Supports both V-construction and T-construction for cubical complexes (details).
- Binary Coefficients: Computations are performed over the field with two elements.
- Cross-Platform: Python module and standalone command-line executable available.
For description, refer to the paper:
Cubical Ripser: Software for Computing Persistent Homology of Image and Volume Data
by Shizuo Kaji, Takeki Sudo, and Kazushi Ahara.
CubicalRipser is open-source software licensed under the GNU Lesser General Public License v3.0 or later.
Refer to the LICENSE file for more details.
- Google Colab Demo: CubicalRipser in Action
- Topological Data Analysis (TDA) Tutorial: Hands-On Guide
- Applications in Deep Learning:
Install the Python module directly:
pip install -U cripser
If you encounter architecture compatibility issues, try:
pip uninstall cripser
pip install --no-binary cripser cripser
Requires a C++11-compatible compiler (e.g., GCC, Clang, MSVC).
-
Build the command-line executable:
cd build cmake .. make
The executable
cubicalripser
will be created. -
Alternatively, without
cmake
:cd src make all
Modify the
Makefile
if needed. -
Install the Python module:
pip install .
- Use a 64-bit compiler to match Python's architecture, e.g.:
cmake .. -G"Visual Studio 15 2017 Win64" cmake --build . --target ALL_BUILD --config Release
- Fix potential
ssize_t
issues inpybind11
by adding:intypedef SSIZE_T ssize_t;
pybind11/include/pybind11/numpy.h
after#if defined(_MSC_VER)
.
Cubical Ripser accepts 1D/2D/3D Numpy arrays.
import cripser
import numpy as np
arr = np.load("input.npy").astype(np.float64)
pd = cripser.computePH(arr, maxdim=2)
Result: A NumPy array of shape (n, 9)
where each row contains:
dim, birth, death, x1, y1, z1, x2, y2, z2
.
They indicate the dimension of the cycle, birth-time, death-time, location (x1,y1,z1) of the cell giving birth to the cycle, and location (x2,y2,z2) of the cell destroying the cycle.
- To use the T-construction:
import tcripser pd = tcripser.computePH(arr, maxdim=2)
./cubicalripser --print --maxdim 2 --output out.csv demo/3dimsample.txt
Result: out.csv
with rows formatted as:
dim, birth, death, x1, y1, z1, x2, y2, z2
.
Each line consists of nine numbers indicating the dimension of the cycle, birth-time, death-time, the creator location (x,y,z), and the destroyer location (x,y,z).
For Numpy arrays:
./cubicalripser --output result.csv input.npy
- NUMPY (.npy): Native format for both Python and CLI.
- Perseus Text (.txt): Specification.
- CSV (.csv): Simplified input for 2D images.
- DIPHA (.complex): Specification.
A small utility is included that converts images in various formats into NUMPY arrays.
-
Convert images to
.npy
:python demo/img2npy.py input.jpg output.npy
A series of image files such as JPEG and PNG files (as long as the Pillow library can handle them) can also be made into a volume in a similar way:
python demo/img2npy.py input*.jpg volume.npy
Note that here we rely on the shell's path expansion. If your shell does not support it, you can manually specify file names as in the following:
python demo/img2npy.py input00.dcm input01.dcm input02.dcm volume.npy
-
Handle DICOM volumes: Given a series of DICOM files named input00.dcm, input01.dcm, input02.dcm... under the directory dicom, we can convert the DICOM files to a single 3D Numpy array volume.npy that is compatible with Cubical Ripser by
python demo/img2npy.py dicom/*.dcm output.npy
Or, we can compute persistent homology directly by
python demo/cr.py dicom --sort -it dcm -o output.csv
by reading .dcm files from the directry dicom in a sorted order.
The filename should end with ".complex". Look at DIPHA binary format for specification.
We can convert input and output files between Cubical Ripser and DIPHA.
- to convert an Numpy array img.npy into DIPHA's format img.complex
python dipha2npy.py img.npy img.complex
- the other way around
python dipha2npy.py img.complex img.npy
- convert DIPHA's output result.output into an Numpy array result.npy
python dipha2npy.py result.output result.npy
A scalar time-series can be considered as a 1D image, so Cubical Ripser can compute its persistent homology. Note that other software would be more efficient for this purpose.
An example of regressing the frequency of noisy sine curves is demonstrated here.
- V-Construction: Pixels represent 0-cells (4-neighbor connectivity in 2D).
- T-Construction: Pixels represent top-cells (8-neighbor connectivity in 2D).
Use the appropriate executable for your needs:
- V-construction:
cubicalripser
(Python module:cripser
). - T-construction:
tcubicalripser
(Python module:tcripser
).
By the Alexander duality, the following two give essentially the same results:
./cubicalripser input.npy
./tcubicalripser --embedded input.npy
The difference is in the sign of the filtration and the permanent cycle. Here, (--embedded) converts the input I to -I^\infty described in the paper below.
For more details, see Duality in Persistent Homology of Images by Adélie Garin et al.
The creator of a cycle is the cell which gives birth to the cycle. For example, the voxel in a connected component with the lowest filtration value creates a 0-dimensional cycle, and the voxel which connects two separate connected components destroys the component with a higher birth time. The creator and the destroyer cells are not uniquely determined, but they provide useful information to localise the cycle. Cubical Ripser adopts the following convention on the location of these cells: when the lifetime of a cycle is finte,
arr[x2,y2,z2] - arr[x1,y1,z1] = death - birth = lifetime
where arr is the image, (x1,y1,z1) is the location of the creator cell, and (x2,y2,z2) is the location of the destroyer cell. Note that when computed with the (--embedded) option, the roles of creator and destroyer are switched:
arr[x1,y1,z1] - arr[x2,y2,z2] = death - birth = lifetime
The authors thank Nicholas Byrne for suggesting the convention and providing a test code.
-
Lifetime Enhanced Image: Adds topological features as additional channels for CNNs.
./cubicalripser --output result.npy input.npy python demo/stackPH.py result.npy -o lifetime_image.npy -i input.npy
In lifetime_image.npy, persistent homology is encoded as the extra channels so that it can be used as input for CNNs.
Please look at the example section of our paper.
-
Persistent Histogram Image:
Similarly, the persistent histogram image can be obtained bypython demo/stackPH.py result.npy -o hist_image.npy -t hist -i input.npy
For practical examples, see HomologyCNN.
We give a referece to various software for persistent homology of images. The comments are based on our limited understanding and tests, and hence, could be wrong.
- Cubicle by Hubert Wagner
It computes for the V-construction of the image. Its parallelised algorithm offers faster computation on multi-core machines. Also, it reads the input image in small chunks so that it requires much less memory footprint.
- HomcCube By Ippei Obayashi.
It computes for the V-construction of the image. It is integrated into Homcloud developed by the same author.
- DIPHA by Ulrich Bauer and Michael Kerber
It computes for the V-construction of the image. It is parallelised with MPI so it works on a cluster. The software has been used in various projects. The memory footprint is relatively large.
- GUDHI developed at INRIA
It computes for the V- and T-construction of an array of any dimension. It is well-documented and offers a well-organised and easy to use interface. It focuses more on usability than performance.
- diamorse developed at The Australian National University.
It computes for the V-construction of the image.
- Perseus by Vidit Nanda
It computes for the V-construction of the image.
- (v0.0.8) fixed memory leak in Python bindings (pointed out by Nicholas Byrne)
- (v0.0.7) slight speed up
- (v0.0.6) changes in the definition of birth/death location (suggested by Nicholas Byrne)
- (up to v0.0.5, difference from the original version
- optimised codes (much less memory footprint, much faster for certain data; sometimes more than 100 times.)
- Python friendly: see the Jupyter Notebook example found under the demo directory.
- virtually infinite input size (compared to 510x510x510)
- cache control
- option to use the Alexander duality for the highest degree persistent homology
- V and T construction for building cubical complexes from an image
- output birth/death location