Skip to main content

Data science for topological data analysis

Project description

tdads

License: GPL v3 JOSS DOI Zenodo DOI

Data science (ds) for topological data analysis (tda) (i.e. tdads = tda+ds).

Installation

$ pip install tdads

API

tdads has two major modules:

  1. Machine learning. The classes diagram_mds and diagram_kpca and can be used to project a group of diagrams into a low dimensional space (i.e. dimension reduction).
  2. Statistics. The permutation_test class can carry out ANOVA-like tests for identifying group differences of persistence diagrams. The diagram_bootstrap class can be used to identify statistically significant topological features in a dataset.

Usage

As an example we will

  1. create 10 persistence diagrams from two distinct groups,
  2. describe the significant topological features in each diagram,
  3. resolve the two groups with MDS, and
  4. capture the group difference using a permutation test.
from tdads.machine_learning import *
from tdads.inference import *
from numpy.random import uniform
from numpy import array
from math import cos, sin, pi
from ripser import ripser
import matplotlib.pyplot as plt

# function to create a circle dataset and
# compute its diagram
def circle_diagram():
    # sample 100 points from the unit circle
    theta = uniform(low = 0, high = 2*pi, size = 100)
    data = array([[cos(theta[i]), sin(theta[i])] for i in range(100)])
    # compute persistence diagram
    diag = ripser(data, maxdim = 2)
    return [data, diag]

# function to create a sphere dataset and
# compute its diagram
def sphere_diagram():
    # sample 100 points from the unit sphere
    phi = uniform(low = 0, high = 2*pi, size = 100)
    theta = uniform(low = 0, high = pi, size = 100)
    data = array([[sin(theta[i])*cos(phi[i]), sin(theta[i])*sin(phi[i]), cos(theta[i])] for i in range(100)])
    # compute persistence diagram
    diag = ripser(data, maxdim = 2)
    return [data, diag]

# create 10 diagrams, five from circle datasets and
# five from sphere datasets
result = [circle_diagram(), circle_diagram(), circle_diagram(), circle_diagram(), circle_diagram(),
          sphere_diagram(), sphere_diagram(), sphere_diagram(), sphere_diagram(), sphere_diagram()]
data = [r[0] for r in result]
diagrams = [r[1] for r in result]

# use the bootstrap procedure to determine the significant
# topological features in each diagram
def diag_fun(X, thresh):
    return ripser(X = X, thresh = thresh, maxdim = 2)
boot = diagram_bootstrap(diag_fun = diag_fun, dims = [0,1,2], alpha = 0.01)
boot_diagrams = [boot.compute(X = d, thresh = 2) for d in data]

# the subsetted diagrams show that only the first five diagrams have 
# one loop and only the last five diagrams have one void:
[print('Num clusters:' + str(len(d['subsetted_diagram'][0])) + ', num loops: ' + str(len(d['subsetted_diagram'][1])) + ', num voids: ' + str(len(d['subsetted_diagram'][2]))) for d in boot_diagrams]

# a 2D MDS projection of the 10 diagrams resolves the two groups:
mds = diagram_mds(p = float('inf'), dim = 2) # for 2-dimensional homology
emb = mds.fit_transform(diagrams)
plt.scatter(emb[:,0], emb[:,1], color = ['red','red','red','red','red','blue','blue','blue','blue','blue'])
plt.xlabel('Embedding dim 1')
plt.ylabel('Embedding dim 2')
plt.show()

# a permutation test captures the group differences in all dimensions
pt = perm_test(p = float('inf'), iterations = 50, dims = [0,1,2])
res = pt.test([[d for d in diagrams[0:5]], [d for d in diagrams[5:10]]])
res['p_values']

Citation

If you use tdads, please consider citing as:

  • Brown et al., (2024). TDApplied: An R package for machine learning and inference with persistence diagrams. Journal of Open Source Software, 9(95), 6321, https://doi.org/10.21105/joss.06321

Contributing

Interested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

tdads was created by Shael Brown. It is licensed under the terms of the GNU General Public License v3.0 license.

Credits

tdads was created with cookiecutter and the py-pkgs-cookiecutter template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tdads-0.1.0.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tdads-0.1.0-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

Details for the file tdads-0.1.0.tar.gz.

File metadata

  • Download URL: tdads-0.1.0.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/21.6.0

File hashes

Hashes for tdads-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cebe91f71af97bf465b8bcbb37137b9e0f713f8c55f579864dceeea9abadae0e
MD5 da4546f5172e590b0eef070a7c92480a
BLAKE2b-256 b8148f867daf09cdfcce9581ad952743d19ca50fc593fd455b266a66ec7e8505

See more details on using hashes here.

File details

Details for the file tdads-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tdads-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/21.6.0

File hashes

Hashes for tdads-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb5a76c5f4c000c764ddb7ec7516f3c2c71158cf0237f5263b30f2b8a1ffdccd
MD5 b54b4ec96a88b7150f0ac62c18116440
BLAKE2b-256 6a2b23c9b1036bb7559d6d83a05d62f8c8acb0840e1e1667f0001add3f57e324

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page