Package: cripser
CubicalRipser : Persistent homology for 2D image and 3D voxel data (and 1D scalar timeseries)copyright by Takeki Sudo and Kazushi Ahara, Meiji University, 2018modified by Shizuo Kaji, Kyushu University, 2019DescriptionCubicalRipser is an adaptation ofRipserby Ulrich Bauer to computation of persistent homology of weighted cubical complexes.For 2 and 3 dimensional cubical complexes, CubicalRipser is among the fastest programs for computing persistent homologyCubical Ripser implements both the V- and the T- constructions for the filtration of cubical complexes (seeV and T constructions).The coefficients are taken in the field with two elements.SeeOther software for persistent homology of cubical complexesFor details, please look at our paperCubical Ripser: Software for computing persistent homology of image and volume databy Shizuo Kaji, Takeki Sudo, Kazushi Ahara.LicenseCubicalRipser is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.Get StartedYou can try Cubical Ripser onGoogle ColaboratoryYou may also want to look atA guide through TDA toolsgiving a hands-on tutorial for various tools for topological data analysis including Cubical RipserHow Deep-learning and Persistent homology can be combined is demonstrated:Example 1andExample 2InstallationRecommended: pipInstall the Python module only:% pip install -U cripserBuild from sourceThe command-line executable should be easily build with C++11 compilers such as G++, Clang, or Microsoft C++.
To build the command-line executable from source:% cd build
% cmake ..
% makeThe executable is "cubicalripser".If cmake is not available on your system, you can also do% cd src
% make allbut perhaps you have to manually modify "Makefile".To install Python module,% pip install .Windows specificsOn Windows, you may get an error like "Python config failure: Python is 64-bit, chosen compiler is 32-bit".
Then, you have to specify your compiler; for example% cmake .. -G"Visual Studio 15 2017 Win64"
% cmake --build . --target ALL_BUILD --config ReleaseAlso, due to the non-standard type used in pybind11, you may encounter an error
saying "the type ssize_t is undefined". This error may be resolved by addingtypedef SSIZE_T ssize_t;right after the first appearance of#if defined(_MSC_VER)in pybind11/include/pybind11/numpy.hHow to usePython moduleTo use from python,import cripser
pd = cripser.computePH(arr,maxdim=2)where arr is a 2D or 3D numpy array of type numpy.float64.
The result is stored in (n,9)-array, where n is the number of cycles.
Each row consists ofdim birth   death   x1  y1  z1  x2  y2  z2where (x1,y1,z1) is the location of the creator cell of the cycle and (x2,y2,z2) is the location of the destroyer cell of the cycle.
See alsoCreator and Destroyer cells.If you want to compute with the T-construction instead of the V-construction,import tcripser
pd = tcripser.computePH(arr,maxdim=2)Look at the Jupyter notebook demo/cubicalripser.ipynb andhttps://github.com/shizuo-kaji/HomologyCNNfor practical usage.Command-line executable(See also2D Image filefor a Python-based command-line utility.)To see the command-line options:% ./cubicalripserExample:% ./cubicalripser --print --maxdim 2 --output out.csv demo/3dimsample.txtThe results are recorded inresult.csv.
Each line in the outputresult.csvconsists 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).Cubical Ripser accepts 1D/2D/3D Numpy arrays% ./cubicalripser --output result.csv input.npyInput file formatThe python version accepts NUMPY arrays as input.
A small utility is included that converts images in various formats into NUMPU arrays.The command-line version of CubicalRipser accepts three types of input files: NUMPY (.npy), Perseus TEXT (.txt), CSV (.csv), DIPHA (.complex).2D Image fileGiven a JPEG imageinput.jpg, we can compute its persistent homology by% python demo/cr.py input.jpg -o output.csvThe result is saved in the CSV fileoutput.csvwhose rows lookdim birth   death   x1  y1  z1  x2  y2  z2where (x1,y1,z1) is the location of the creator cell of the cycle and (x2,y2,z2) is the location of the destroyer cell of the cycle.Alternatively, we can first convert the image into a 2D Numpy arrayinput.npyby% python demo/img2npy.py input.jpg input.npyand compute its persistent homology by the python module:import numpy as np                                      # import the Numpy module
import cripser                                          # import the Cubical Ripser python module
arr = np.load("input.npy").astype(np.float64)           # load the image in the numpy array format
result = cripser.computePH(arr,maxdim=1)   # compute the persistent homology up to degree 1Here,resultis another 2D Numpy array of shape (M,9), where M is the number of cycles.
The none numbers of each row 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.3D Volume fileGiven a series of DICOM files namedinput00.dcm,input01.dcm,input02.dcm... under the directorydicom,
we can compute its persistent homology by% python demo/cr.py dicom  --sort -it dcm -o output.csvby reading .dcm files from the directrydicomin a sorted order.Alternatively, we can first convert the DICOM files to a single 3D Numpy arrayvolume.npythat is compatible with Cubical Ripser by% python demo/img2npy.py dicom/input*.dcm volume.npyA 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.npyNote 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.npy1D time seriesA 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 demonstratedhere.Deep Learning X Persistent homologyLifetime enhanced imageis a way to feed the topological features obtained by persistent homology
into convolutional neural networks (CNNs).% ./cubicalripser --output result.npy input.npy
% python demo/stackPH.py result.npy -o lifetime_image.npy -i input.npyInlifetime_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 ofour paperand thedemonstrationfor details.Similarly, thepersistent histogram imagecan be obtained by% python demo/stackPH.py result.npy -o lifetime_image.npy -t hist -i input.npyCSV file (only for 2D image)The filename should end with ".csv".Text file (Perseus)The filename should end with ".txt".
Please look atPerseus Dense Cubical Grids formatfor specification.3
max_x
max_y
max_z
val[1,1,1]
val[2,1,1]
...
val[max_x,max_y,max_z]DIPHA fileThe filename should end with ".complex".
Look atDIPHA binary formatfor specification.We can convert input and output files between Cubical Ripser and DIPHA.to convert an Numpy arrayimg.npyinto DIPHA's formatimg.complex% python dipha2npy.py img.npy img.complexthe other way around% python dipha2npy.py img.complex img.npyconvert DIPHA's outputresult.outputinto an Numpy arrayresult.npy% python dipha2npy.py result.output result.npyV and T constructionsThere are two major ways to build a filtred cubical complex from an image (that is, a function over a grid).In the V-construction, each pixel in the image corresponds to the 0 cell.In the T-construction, each pixel in the image corresponds to the top cell.In the 2D setting, the V-construction amounts to considering 4-neighbour pixel connectivity,
whereas the T-construction amounts to considering 8-neighbour pixel connectivity.Cubical Ripser provides two versions of executables:for the V-construction: cubicalripser, cripser (python module)for the T-construction: tcubicalripser (no python module provided)By the Alexander duality, the following two give essentially the same results:./cubicalripser input.npy
./tcubicalripser --embedded input.npyThe 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.Look at the following paper for details:Duality in Persistent Homology of Images by
Adélie Garin, Teresa Heiss, Kelly Maggs, Bea Bleile, Vanessa RobinsCreator and Destroyer cellsThe 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 = lifetimewhere 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 = lifetimeThe authors thank Nicholas Byrne for suggesting the convention and providing a test code.Other software for persistent homology of cubical complexesWe 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.Cubicleby Hubert WagnerIt 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.HomcCubeBy Ippei Obayashi.It computes for the V-construction of the image.
It is integrated into Homcloud developed by the same author.DIPHAby Ulrich Bauer and Michael KerberIt 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.GUDHIdeveloped at INRIAIt computes for the T-construction of the image.
It is well-documented and offers a well-organised and easy to use interface.
It focuses more on usability than performance.diamorsedeveloped at The Australian National University.It computes for the V-construction of the image.Perseusby Vidit NandaIt computes for the V-construction of the image.Release Notes(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 thedefinition of birth/death location(suggested by Nicholas Byrne)(up to v0.0.5, difference from theoriginal versionoptimised 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 controloption to use the Alexander duality for the highest degree persistent homologyV and T construction for building cubical complexes from an imageoutput birth/death location