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.1.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.1-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tdads-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 676406806b9b67073412134d704f85a0f26c2987221ed3003017c7df031430ee
MD5 d6aab48c5a37c8dec5e8264d0af3f232
BLAKE2b-256 a0a6d9ce4753095d32fa71f0e22bbd24dec1cb40722ce78d7e43cb0f74f70542

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tdads-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d0f6e74b695844e5c52d1e7262e68866537b2555f7f880e0e288666ea9a628f3
MD5 dbc71e89aa3c5e732cc29e0645f3d44d
BLAKE2b-256 2ba56cc33b94504c1226b0ec1e49e02f9200a1967c8056c4ef31491080246286

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