Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weighted Delaunay triangulation #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 58 additions & 6 deletions cechmate/filtrations/alpha.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import numpy.linalg as linalg
from scipy import spatial
from scipy.spatial import ConvexHull

from .base import BaseFiltration

Expand All @@ -26,16 +27,21 @@ class Alpha(BaseFiltration):

"""

def build(self, X):
def build(self, X, weights=[]):
"""
Do the Alpha filtration of a Euclidean point set (requires scipy)

Parameters
===========
X: Nxd array
Array of N Euclidean vectors in d dimensions
weights : Nx1 array
Array of weights (default is non-weighted)

"""

self.weights = weights


if X.shape[0] < X.shape[1]:
warnings.warn(
"The input point cloud has more columns than rows; "
Expand All @@ -47,14 +53,17 @@ def build(self, X):

## Step 1: Figure out the filtration
if self.verbose:
print("Doing spatial.Delaunay triangulation...")
print("Doing Delaunay triangulation...")
tic = time.time()

delaunay_faces = spatial.Delaunay(X).simplices

if not np.shape(self.weights)[0]==0:
delaunay_faces = self._weighted_delaunay(X)
else:
delaunay_faces = spatial.Delaunay(X).simplices

if self.verbose:
print(
"Finished spatial.Delaunay triangulation (Elapsed Time %.3g)"
"Finished Delaunay triangulation (Elapsed Time %.3g)"
% (time.time() - tic)
)
print("Building alpha filtration...")
Expand Down Expand Up @@ -169,3 +178,46 @@ def _get_circumcenter(self, X):
x = x.dot(V.T) + muV
return (x, rSqr)
return (np.inf, np.inf) # SC2 (Points not in general position)

def _weighted_delaunay(self, X):
"""
Computes a weighted delaunay triangulation using the duality of delaunay triangulation with a convex hull of a dimension higher
This function is triggered if a weight function is input to build the Alpha filtration
and replaces the delaunay triangulation of scipy.

Returns
-------
Triangular faces from a weighted delaunay triangulation in the form of a numpy array.
Similar to the output from scipy.spatial.Delaunay

"""
num, dim = np.shape(X)

lifted = np.zeros((num,dim+1))

for i in range(num):
p = X[i,:]
lifted[i,:] = np.append(p,np.sum(p**2) - self.weights[i]**2)
pinf = np.append(np.zeros((1,dim)),1e10);
lifted = np.vstack((lifted, pinf))
hull = ConvexHull(lifted)
delaunay = []
for simplex in hull.simplices:
if num not in simplex:
delaunay.append(simplex.tolist())

return np.asarray(delaunay)














23 changes: 22 additions & 1 deletion docs/notebooks/BasicUsage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,27 @@
"print(\"H1:\\n\", dgms[1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Weighted Alpha filtrations\n",
"He we showcase an usage of Alpha filtrations when the points have some weights. \n",
"This is done by computing a weighted delaunay triangulation and changing the circum radius accordingly for Alpha filtratiion computation."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"num_points = 10\n",
"points = np.random.rand(num_points, 2)\n",
"weights = np.random.rand(num_points,1)\n",
"filtration = alpha.build(points, weights=weights)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -355,7 +376,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
"version": "3.7.3"
}
},
"nbformat": 4,
Expand Down