Skip to content

Commit cfde752

Browse files
authored
Merge pull request #137 from lucasimi/develop
Using numba instead of cython for specialized metrics
2 parents 159257d + 084795c commit cfde752

File tree

6 files changed

+54
-126
lines changed

6 files changed

+54
-126
lines changed

.github/workflows/deploy.yml

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ on:
66
- 'v[0-9]+.[0-9]+.[0-9]+'
77

88
jobs:
9-
build_locally:
10-
name: Build locally
9+
deploy-job:
10+
name: Build and deploy
1111
runs-on: ubuntu-latest
12+
environment: pypi
1213
steps:
1314
- name: Check out repository code
1415
uses: actions/checkout@v4
@@ -24,51 +25,6 @@ jobs:
2425
run: |
2526
python -m build
2627
python -m twine check dist/*
27-
28-
build_wheels:
29-
name: Build wheels on ${{ matrix.os }}
30-
runs-on: ${{ matrix.os }}
31-
strategy:
32-
matrix:
33-
os: [ubuntu-latest, windows-latest, macos-13, macos-14]
34-
steps:
35-
- uses: actions/checkout@v4
36-
- name: Build wheels
37-
uses: pypa/cibuildwheel@v2.19.2
38-
- uses: actions/upload-artifact@v4
39-
with:
40-
name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
41-
path: ./wheelhouse/*.whl
42-
43-
build_sdist:
44-
name: Build source distribution
45-
runs-on: ubuntu-latest
46-
steps:
47-
- uses: actions/checkout@v4
48-
49-
- name: Build sdist
50-
run: pipx run build --sdist
51-
52-
- uses: actions/upload-artifact@v4
53-
with:
54-
name: cibw-sdist
55-
path: dist/*.tar.gz
56-
57-
upload_pypi:
58-
needs: [build_locally, build_wheels, build_sdist]
59-
runs-on: ubuntu-latest
60-
environment: pypi
61-
permissions:
62-
id-token: write
63-
steps:
64-
- uses: actions/download-artifact@v4
65-
with:
66-
pattern: cibw-*
67-
path: dist
68-
merge-multiple: true
69-
70-
- uses: pypa/gh-action-pypi-publish@release/v1
71-
with:
72-
repository-url: https://test.pypi.org/legacy/
73-
74-
- uses: pypa/gh-action-pypi-publish@release/v1
28+
- name: Upload package to PyPI
29+
run: |
30+
python -m twine upload -r pypi dist/* --verbose

pyproject.toml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[build-system]
2-
requires = ["setuptools", "wheel", "Cython"]
2+
requires = ["setuptools>=42", "wheel"]
33
build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "tda-mapper"
7-
version = "0.7.1"
7+
version = "0.7.2"
88
description = "A simple and efficient Python implementation of Mapper algorithm for Topological Data Analysis"
99
readme = "README.md"
1010
authors = [{ name = "Luca Simi", email = "lucasimi90@gmail.com" }]
@@ -19,21 +19,19 @@ keywords = ["tda", "mapper", "topology", "topological data analysis"]
1919
dependencies = [
2020
"matplotlib>=3.3.4",
2121
"networkx>=2.5",
22+
"numba>=0.54",
2223
"numpy>=1.20.1, <2.0.0",
2324
"plotly>=4.14.3"
2425
]
25-
requires-python = ">=3.7"
26+
requires-python = ">=3.6"
2627

2728
[project.optional-dependencies]
28-
dev = ["coverage", "pandas", "scikit-learn"]
29+
dev = ["coverage[toml]", "pandas", "scikit-learn"]
2930

3031
[project.urls]
3132
Homepage = "https://github.com/lucasimi/tda-mapper-python"
3233
Documentation = "https://tda-mapper.readthedocs.io"
3334
Issues = "https://github.com/lucasimi/tda-mapper-python/issues"
3435

35-
[tool.setuptools.package-data]
36-
"tdamapper.utils" = ["_metrics.c", "_metrics.pyx"]
37-
38-
[tool.cibuildwheel]
39-
skip = "cp36-*"
36+
[tool.coverage.run]
37+
omit = ["**/_*.py"]

setup.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/tdamapper/utils/_metrics.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import numpy as np
2+
from numba import njit
3+
4+
5+
@njit(fastmath=True)
6+
def euclidean(x, y):
7+
return np.linalg.norm(x - y)
8+
9+
10+
@njit(fastmath=True)
11+
def manhattan(x, y):
12+
return np.linalg.norm(x - y, ord=1)
13+
14+
15+
@njit(fastmath=True)
16+
def chebyshev(x, y):
17+
return np.linalg.norm(x - y, ord=np.inf)
18+
19+
20+
@njit(fastmath=True)
21+
def minkowski(p, x, y):
22+
return np.linalg.norm(x - y, ord=p)
23+
24+
25+
@njit(fastmath=True)
26+
def cosine(x, y):
27+
xy = np.dot(x, y)
28+
xx = np.linalg.norm(x)
29+
yy = np.linalg.norm(y)
30+
similarity = xy / (xx * yy)
31+
return np.sqrt(2.0 * (1.0 - similarity))

src/tdamapper/utils/_metrics.pyx

Lines changed: 0 additions & 49 deletions
This file was deleted.

tests/test_bench_metrics.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,37 @@
44
import pandas as pd
55
import numpy as np
66

7+
import numba
8+
79
import tdamapper.utils.metrics as metrics
810

911

12+
@numba.njit(fastmath=True)
1013
def euclidean_numpy(a, b):
1114
return np.sqrt(np.sum((a - b) ** 2))
1215

1316

17+
@numba.njit(fastmath=True)
1418
def euclidean_numpy_linalg(a, b):
1519
return np.linalg.norm(a - b)
1620

1721

22+
@numba.njit(fastmath=True)
1823
def manhattan_numpy(a, b):
1924
return np.sum(np.abs(a - b))
2025

2126

27+
@numba.njit(fastmath=True)
2228
def manhattan_numpy_linalg(a, b):
2329
return np.linalg.norm(a - b, ord=1)
2430

2531

32+
@numba.njit(fastmath=True)
2633
def chebyshev_numpy(a, b):
2734
return np.max(np.abs(a - b))
2835

2936

37+
@numba.njit(fastmath=True)
3038
def chebyshev_numpy_linalg(a, b):
3139
return np.linalg.norm(a - b, ord=np.inf)
3240

@@ -37,7 +45,8 @@ def eval_dist(X, d):
3745

3846

3947
def run_dist_bench(X, d):
40-
return timeit.timeit(lambda: eval_dist(X, d), number=30)
48+
eval_dist(X, d)
49+
return timeit.timeit(lambda: eval_dist(X, d), number=100)
4150

4251

4352
def run_euclidean_bench(X):

0 commit comments

Comments
 (0)