multipers 1.0__cp311-cp311-manylinux_2_34_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of multipers might be problematic. Click here for more details.
- multipers/__init__.py +4 -0
- multipers/_old_rank_invariant.pyx +328 -0
- multipers/_signed_measure_meta.py +72 -0
- multipers/data/MOL2.py +350 -0
- multipers/data/UCR.py +18 -0
- multipers/data/__init__.py +1 -0
- multipers/data/graphs.py +272 -0
- multipers/data/immuno_regions.py +27 -0
- multipers/data/minimal_presentation_to_st_bf.py +0 -0
- multipers/data/pytorch2simplextree.py +91 -0
- multipers/data/shape3d.py +101 -0
- multipers/data/synthetic.py +68 -0
- multipers/distances.py +100 -0
- multipers/euler_characteristic.cpython-311-x86_64-linux-gnu.so +0 -0
- multipers/euler_characteristic.pyx +132 -0
- multipers/function_rips.cpython-311-x86_64-linux-gnu.so +0 -0
- multipers/function_rips.pyx +101 -0
- multipers/hilbert_function.cpython-311-x86_64-linux-gnu.so +0 -0
- multipers/hilbert_function.pyi +46 -0
- multipers/hilbert_function.pyx +145 -0
- multipers/ml/__init__.py +0 -0
- multipers/ml/accuracies.py +61 -0
- multipers/ml/convolutions.py +384 -0
- multipers/ml/invariants_with_persistable.py +79 -0
- multipers/ml/kernels.py +128 -0
- multipers/ml/mma.py +422 -0
- multipers/ml/one.py +472 -0
- multipers/ml/point_clouds.py +191 -0
- multipers/ml/signed_betti.py +50 -0
- multipers/ml/signed_measures.py +1046 -0
- multipers/ml/sliced_wasserstein.py +313 -0
- multipers/ml/tools.py +99 -0
- multipers/multiparameter_edge_collapse.py +29 -0
- multipers/multiparameter_module_approximation.cpython-311-x86_64-linux-gnu.so +0 -0
- multipers/multiparameter_module_approximation.pxd +147 -0
- multipers/multiparameter_module_approximation.pyi +439 -0
- multipers/multiparameter_module_approximation.pyx +931 -0
- multipers/pickle.py +53 -0
- multipers/plots.py +207 -0
- multipers/point_measure_integration.cpython-311-x86_64-linux-gnu.so +0 -0
- multipers/point_measure_integration.pyx +59 -0
- multipers/rank_invariant.cpython-311-x86_64-linux-gnu.so +0 -0
- multipers/rank_invariant.pyx +154 -0
- multipers/simplex_tree_multi.cpython-311-x86_64-linux-gnu.so +0 -0
- multipers/simplex_tree_multi.pxd +121 -0
- multipers/simplex_tree_multi.pyi +715 -0
- multipers/simplex_tree_multi.pyx +1284 -0
- multipers/tensor.pxd +13 -0
- multipers/test.pyx +44 -0
- multipers-1.0.dist-info/LICENSE +21 -0
- multipers-1.0.dist-info/METADATA +9 -0
- multipers-1.0.dist-info/RECORD +56 -0
- multipers-1.0.dist-info/WHEEL +5 -0
- multipers-1.0.dist-info/top_level.txt +1 -0
- multipers.libs/libtbb-5d1cde94.so.12.10 +0 -0
- multipers.libs/libtbbmalloc-5e0a3d4c.so.2.10 +0 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
from tqdm import tqdm
|
|
2
|
+
import numpy as np
|
|
3
|
+
from torch_geometric.data.data import Data
|
|
4
|
+
import networkx as nx
|
|
5
|
+
from sklearn.base import BaseEstimator, TransformerMixin
|
|
6
|
+
from typing import Iterable
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def modelnet2pts2gs(train_dataset, test_dataset , nbr_size = 8, exp_flag = True, labels_only = False,n=100, n_jobs=1, random=False):
|
|
10
|
+
from sklearn.neighbors import kneighbors_graph
|
|
11
|
+
"""
|
|
12
|
+
sample points and create neighborhoold graph
|
|
13
|
+
"""
|
|
14
|
+
dataset = train_dataset + test_dataset
|
|
15
|
+
indices = np.random.choice(range(len(dataset)),replace=False, size=n) if random else range(n)
|
|
16
|
+
|
|
17
|
+
dataset:list[Data] = [dataset[i] for i in indices]
|
|
18
|
+
_,labels = torch_geometric_2nx(dataset, labels_only=True)
|
|
19
|
+
if labels_only: return labels
|
|
20
|
+
|
|
21
|
+
def data2graph(data:Data):
|
|
22
|
+
pos = data.pos.numpy()
|
|
23
|
+
adj = kneighbors_graph(pos, nbr_size, mode='distance', n_jobs=n_jobs)
|
|
24
|
+
g = nx.from_scipy_sparse_array(adj, edge_attribute= 'weight')
|
|
25
|
+
if exp_flag:
|
|
26
|
+
for u, v in g.edges(): # TODO optimize
|
|
27
|
+
g[u][v]['weight'] = np.exp(-g[u][v]['weight'])
|
|
28
|
+
return g
|
|
29
|
+
#TODO : nx.set_edge_attributes()
|
|
30
|
+
|
|
31
|
+
return [data2graph(data) for data in dataset], labels
|
|
32
|
+
def torch_geometric_2nx(dataset, labels_only = False, print_flag = False, weight_flag = False):
|
|
33
|
+
"""
|
|
34
|
+
:param dataset:
|
|
35
|
+
:param labels_only: return labels only
|
|
36
|
+
:param print_flag:
|
|
37
|
+
:param weight_flag: whether computing distance as weights or not
|
|
38
|
+
:return:
|
|
39
|
+
"""
|
|
40
|
+
if labels_only:
|
|
41
|
+
return None, [int(data.y) for data in dataset]
|
|
42
|
+
def data2graph(data:Data):
|
|
43
|
+
edges = np.unique(data.edge_index.numpy().T, axis=0)
|
|
44
|
+
g = nx.from_edgelist(edges)
|
|
45
|
+
edge_filtration = {(u,v):np.linalg.norm(data.pos[u] - data.pos[v]) for u,v in g.edges}
|
|
46
|
+
nx.set_node_attributes(g,{node:0 for node in g.nodes}, "geodesic")
|
|
47
|
+
nx.set_edge_attributes(g, edge_filtration, "geodesic")
|
|
48
|
+
return g
|
|
49
|
+
return [data2graph(data) for data in tqdm(dataset, desc="Turning Data to graphs")], [int(data.y) for data in dataset]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def modelnet2graphs(version = '10', print_flag = False, labels_only = False, a = 0, b = 10, weight_flag = False):
|
|
53
|
+
""" load modelnet 10 or 40 and convert to graphs"""
|
|
54
|
+
from torch_geometric.transforms import FaceToEdge
|
|
55
|
+
from .shape3d import load_modelnet
|
|
56
|
+
train_dataset, test_dataset = load_modelnet(version, point_flag = False)
|
|
57
|
+
dataset = train_dataset + test_dataset
|
|
58
|
+
if b>0: dataset = [dataset[i] for i in range(a,b)]
|
|
59
|
+
if labels_only:
|
|
60
|
+
return torch_geometric_2nx(dataset, labels_only=True)
|
|
61
|
+
dataset = [FaceToEdge(remove_faces=False)(data) for data in dataset]
|
|
62
|
+
graphs, labels = torch_geometric_2nx(dataset, print_flag=print_flag, weight_flag= weight_flag)
|
|
63
|
+
return graphs, labels
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class Torch2SimplexTree(BaseEstimator,TransformerMixin):
|
|
69
|
+
"""
|
|
70
|
+
WARNING : build in progress
|
|
71
|
+
PyTorch Data-like to simplextree.
|
|
72
|
+
|
|
73
|
+
Input
|
|
74
|
+
-----
|
|
75
|
+
Class having `pos`, `edges`, `faces` methods
|
|
76
|
+
|
|
77
|
+
Filtrations
|
|
78
|
+
-----------
|
|
79
|
+
- Geodesic (geodesic rips)
|
|
80
|
+
- eccentricity
|
|
81
|
+
"""
|
|
82
|
+
import multipers as mp
|
|
83
|
+
|
|
84
|
+
def __init__(self, filtrations:Iterable[str]=[]):
|
|
85
|
+
super().__init__()
|
|
86
|
+
|
|
87
|
+
def fit(self, X, y=None):
|
|
88
|
+
return self
|
|
89
|
+
|
|
90
|
+
def transform(self,X:list[nx.Graph]):
|
|
91
|
+
return
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from os.path import expanduser
|
|
3
|
+
from torch_geometric.datasets import ModelNet
|
|
4
|
+
|
|
5
|
+
DATASET_PATH = expanduser("~/Datasets/")
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
####################### MODELNET
|
|
10
|
+
def load_modelnet(version='10', sample_points = False, reset:bool=False, remove_faces=False):
|
|
11
|
+
from torch_geometric.transforms import FaceToEdge, SamplePoints
|
|
12
|
+
"""
|
|
13
|
+
:param point_flag: Sample points if point_flag true. Otherwise load mesh
|
|
14
|
+
:return: train_dataset, test_dataset
|
|
15
|
+
"""
|
|
16
|
+
assert version in ['10', '40']
|
|
17
|
+
if sample_points:
|
|
18
|
+
pre_transform, transform = FaceToEdge(remove_faces=remove_faces), SamplePoints(num=sample_points)
|
|
19
|
+
else:
|
|
20
|
+
pre_transform, transform = FaceToEdge(remove_faces=remove_faces), None
|
|
21
|
+
path = f"{DATASET_PATH}/ModelNet{version}"
|
|
22
|
+
if reset:
|
|
23
|
+
# print(f"rm -rf {path}")
|
|
24
|
+
os.system(f"rm -rf {path+'/processed/'}")
|
|
25
|
+
train_dataset = ModelNet(path, name=version, train=True, transform=transform, pre_transform=pre_transform)
|
|
26
|
+
test_dataset = ModelNet(path, name=version, train=False, transform=transform, pre_transform=pre_transform)
|
|
27
|
+
return train_dataset, test_dataset
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def get_ModelNet(dataset, num_graph, seed):
|
|
31
|
+
train,test = load_modelnet(version=dataset[8:])
|
|
32
|
+
test_size = len(test) / len(train)
|
|
33
|
+
if num_graph >0:
|
|
34
|
+
np.random.seed(seed)
|
|
35
|
+
indices = np.random.choice(len(train), num_graph, replace=False)
|
|
36
|
+
train = train[indices]
|
|
37
|
+
indices = np.random.choice(len(test), int(num_graph*test_size), replace=False)
|
|
38
|
+
test = test[indices]
|
|
39
|
+
np.random.seed() # resets seed
|
|
40
|
+
return train, test
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def get(dataset:str, num_graph=0, seed=0, node_per_graph=0):
|
|
44
|
+
if dataset.startswith("ModelNet"):
|
|
45
|
+
return get_ModelNet(dataset=dataset, num_graph=num_graph, seed=seed)
|
|
46
|
+
datasets = get_(dataset=dataset, num_sample=num_graph)
|
|
47
|
+
graphs = []
|
|
48
|
+
labels = []
|
|
49
|
+
np.random.seed(seed)
|
|
50
|
+
for data, ls in datasets:
|
|
51
|
+
nodes = np.random.choice(range(len(data.pos)), replace=False, size=node_per_graph)
|
|
52
|
+
for i,node in enumerate(nodes):
|
|
53
|
+
data_ = data # if i == 0 else None # prevents doing copies
|
|
54
|
+
graphs.append([data_, node])
|
|
55
|
+
labels.append(ls[node])
|
|
56
|
+
return graphs, labels
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def get_(dataset:str, dataset_num:int|None=None, num_sample:int=0, DATASET_PATH = expanduser("~/Datasets/")):
|
|
60
|
+
from torch_geometric.io import read_off
|
|
61
|
+
if dataset.startswith("3dshapes/"):
|
|
62
|
+
dataset_ = dataset[len("3dshapes/"):]
|
|
63
|
+
else:
|
|
64
|
+
dataset_ = dataset
|
|
65
|
+
if dataset_num is None and "/" in dataset_:
|
|
66
|
+
position = dataset_.rfind("/")
|
|
67
|
+
dataset_num = int(dataset_[position+1:-4]) # cuts the "<dataset>/" and the ".off"
|
|
68
|
+
dataset_ = dataset_[:position]
|
|
69
|
+
|
|
70
|
+
if dataset_num is None: # gets a random (available) number for this dataset
|
|
71
|
+
from os import listdir
|
|
72
|
+
from random import choice
|
|
73
|
+
files = listdir(DATASET_PATH+f"3dshapes/{dataset_}")
|
|
74
|
+
if num_sample <= 0:
|
|
75
|
+
files = [file for file in files if "label" not in file]
|
|
76
|
+
else:
|
|
77
|
+
files = np.random.choice([file for file in files if "label" not in file], replace=False, size=num_sample)
|
|
78
|
+
dataset_nums = np.sort([int("".join([char for char in file if char.isnumeric()])) for file in files])
|
|
79
|
+
|
|
80
|
+
print("Dataset nums : ", *dataset_nums)
|
|
81
|
+
out = [get_(dataset_, dataset_num=num) for num in dataset_nums]
|
|
82
|
+
return out
|
|
83
|
+
|
|
84
|
+
path = DATASET_PATH+f"3dshapes/{dataset_}/{dataset_num}.off"
|
|
85
|
+
data = read_off(path)
|
|
86
|
+
faces = data.face.numpy().T
|
|
87
|
+
# data = FaceToEdge(remove_faces=remove_faces)(data)
|
|
88
|
+
#labels
|
|
89
|
+
label_path = path.split(".")[0] + "_labels.txt"
|
|
90
|
+
f = open(label_path, "r")
|
|
91
|
+
labels = np.zeros(len(data.pos), dtype="<U10") # Assumes labels are of size at most 10 chars
|
|
92
|
+
current_label=""
|
|
93
|
+
for i, line in enumerate(f.readlines()):
|
|
94
|
+
if i % 2 == 0:
|
|
95
|
+
current_label = line.strip()
|
|
96
|
+
continue
|
|
97
|
+
faces_of_label = np.array(line.strip().split(" "), dtype=int) -1 # this starts at 1, python starts at 0
|
|
98
|
+
# print(faces_of_label.min())
|
|
99
|
+
nodes_of_label = np.unique(faces[faces_of_label].flatten())
|
|
100
|
+
labels[nodes_of_label] = current_label # les labels sont sur les faces
|
|
101
|
+
return data, labels
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from sklearn.preprocessing import LabelEncoder
|
|
3
|
+
def noisy_annulus(n1:int=1000,n2:int=200, r1:float=1, r2:float=2, dim:int=2, center:np.ndarray|list|None=None, **kwargs)->np.ndarray:
|
|
4
|
+
"""Generates a noisy annulus dataset.
|
|
5
|
+
|
|
6
|
+
Parameters
|
|
7
|
+
----------
|
|
8
|
+
r1 : float.
|
|
9
|
+
Lower radius of the annulus.
|
|
10
|
+
r2 : float.
|
|
11
|
+
Upper radius of the annulus.
|
|
12
|
+
n1 : int
|
|
13
|
+
Number of points in the annulus.
|
|
14
|
+
n2 : int
|
|
15
|
+
Number of points in the square.
|
|
16
|
+
dim : int
|
|
17
|
+
Dimension of the annulus.
|
|
18
|
+
center: list or array
|
|
19
|
+
center of the annulus.
|
|
20
|
+
|
|
21
|
+
Returns
|
|
22
|
+
-------
|
|
23
|
+
numpy array
|
|
24
|
+
Dataset. size : (n1+n2) x dim
|
|
25
|
+
|
|
26
|
+
"""
|
|
27
|
+
from numpy.random import uniform
|
|
28
|
+
from numpy.linalg import norm
|
|
29
|
+
|
|
30
|
+
set =[]
|
|
31
|
+
while len(set)<n1:
|
|
32
|
+
draw=uniform(low=-r2, high=r2, size=dim)
|
|
33
|
+
if norm(draw) > r1 and norm(draw) < r2:
|
|
34
|
+
set.append(draw)
|
|
35
|
+
annulus = np.array(set) if center == None else np.array(set) + np.array(center)
|
|
36
|
+
diffuse_noise = uniform(size=(n2,dim), low=-1.1*r2,high=1.1*r2)
|
|
37
|
+
if center is not None: diffuse_noise += np.array(center)
|
|
38
|
+
return np.vstack([annulus, diffuse_noise])
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def three_annulus(num_pts:int=500,num_outliers:int=500):
|
|
42
|
+
X = np.block([
|
|
43
|
+
[np.random.uniform(low=-2,high=2,size=(num_outliers,2))],
|
|
44
|
+
[np.array(noisy_annulus(r1=0.6,r2=0.9,n1=(int)(num_pts*1/3), n2=0, center = [1,-0.2]))],
|
|
45
|
+
[np.array(noisy_annulus(r1=0.4,r2=0.55,n1=(int)(num_pts*1/3), n2=0, center = [-1.2,-1]))],
|
|
46
|
+
[np.array(noisy_annulus(r1=0.3,r2=0.4,n1=(int)(num_pts*1/3), n2=0, center = [-0.7,1.1]))],
|
|
47
|
+
])
|
|
48
|
+
return X
|
|
49
|
+
|
|
50
|
+
def orbit(n:int=1000, r:float=1., x0=[]):
|
|
51
|
+
point_list=[]
|
|
52
|
+
if len(x0) != 2:
|
|
53
|
+
x,y=np.random.uniform(size=2)
|
|
54
|
+
else:
|
|
55
|
+
x,y = x0
|
|
56
|
+
point_list.append([x,y])
|
|
57
|
+
for _ in range(n-1):
|
|
58
|
+
x = (x + r*y*(1-y)) %1
|
|
59
|
+
y = (y + r*x*(1-x)) %1
|
|
60
|
+
point_list.append([x,y])
|
|
61
|
+
return np.asarray(point_list, dtype=float)
|
|
62
|
+
|
|
63
|
+
def get_orbit5k(num_pts = 1000, num_data=5000):
|
|
64
|
+
rs = [2.5, 3.5, 4, 4.1, 4.3]
|
|
65
|
+
labels = np.random.choice(rs, size=num_data, replace=True)
|
|
66
|
+
X = [orbit(n=num_pts, r=r) for r in labels]
|
|
67
|
+
labels = LabelEncoder().fit_transform(labels)
|
|
68
|
+
return X, labels
|
multipers/distances.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import ot
|
|
3
|
+
import numpy as np
|
|
4
|
+
from multipers.simplex_tree_multi import SimplexTreeMulti
|
|
5
|
+
from multipers.multiparameter_module_approximation import PyMultiDiagrams, PyModule
|
|
6
|
+
|
|
7
|
+
def sm2diff(sm1,sm2):
|
|
8
|
+
if isinstance(sm1[0],np.ndarray):
|
|
9
|
+
backend_concatenate = lambda a,b : np.concatenate([a,b], axis=0)
|
|
10
|
+
backend_tensor = lambda x : np.asarray(x, dtype=int)
|
|
11
|
+
elif isinstance(sm1[0],torch.Tensor):
|
|
12
|
+
backend_concatenate = lambda a,b : torch.concatenate([a,b], dim=0)
|
|
13
|
+
backend_tensor = lambda x :torch.tensor(x).type(torch.int)
|
|
14
|
+
else:
|
|
15
|
+
raise Exception("Invalid backend. Numpy or torch.")
|
|
16
|
+
pts1,w1 = sm1
|
|
17
|
+
pts2,w2 = sm2
|
|
18
|
+
pos_indices1 = backend_tensor([i for i,w in enumerate(w1) for _ in range(w) if w>0])
|
|
19
|
+
pos_indices2 = backend_tensor([i for i,w in enumerate(w2) for _ in range(w) if w>0])
|
|
20
|
+
neg_indices1 = backend_tensor([i for i,w in enumerate(w1) for _ in range(-w) if w<0])
|
|
21
|
+
neg_indices2 = backend_tensor([i for i,w in enumerate(w2) for _ in range(-w) if w<0])
|
|
22
|
+
x = backend_concatenate(pts1[pos_indices1],pts2[neg_indices2])
|
|
23
|
+
y = backend_concatenate(pts1[neg_indices1],pts2[pos_indices2])
|
|
24
|
+
return x,y
|
|
25
|
+
|
|
26
|
+
def sm_distance(sm1,sm2, reg=0,reg_m=0, numItermax=10000, p=1):
|
|
27
|
+
x,y = sm2diff(sm1,sm2)
|
|
28
|
+
loss = ot.dist(x,y, metric='sqeuclidean', p=2) # only euc + sqeuclidian are implemented in pot for the moment with torch backend # TODO : check later
|
|
29
|
+
if isinstance(x,np.ndarray):
|
|
30
|
+
empty_tensor = np.array([]) # uniform weights
|
|
31
|
+
elif isinstance(x,torch.Tensor):
|
|
32
|
+
empty_tensor = torch.tensor([]) # uniform weights
|
|
33
|
+
|
|
34
|
+
if reg == 0:
|
|
35
|
+
return ot.lp.emd2(empty_tensor,empty_tensor,M=loss)*len(x)
|
|
36
|
+
if reg_m == 0:
|
|
37
|
+
return ot.sinkhorn2(a=empty_tensor,b=empty_tensor,M=loss,reg=reg, numItermax=numItermax)
|
|
38
|
+
return ot.sinkhorn_unbalanced2(a=empty_tensor,b=empty_tensor,M=loss,reg=reg, reg_m=reg_m, numItermax=numItermax)
|
|
39
|
+
# return ot.sinkhorn2(a=onesx,b=onesy,M=loss,reg=reg, numItermax=numItermax)
|
|
40
|
+
# return ot.bregman.empirical_sinkhorn2(x,y,reg=reg)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def estimate_matching(b1:PyMultiDiagrams, b2:PyMultiDiagrams):
|
|
49
|
+
assert(len(b1) == len(b2))
|
|
50
|
+
from gudhi.bottleneck import bottleneck_distance
|
|
51
|
+
def get_bc(b:PyMultiDiagrams, i:int)->np.ndarray:
|
|
52
|
+
temp = b[i].get_points()
|
|
53
|
+
out = np.array(temp)[:,:,0] if len(temp) >0 else np.empty((0,2)) # GUDHI FIX
|
|
54
|
+
return out
|
|
55
|
+
return max((bottleneck_distance(get_bc(b1,i), get_bc(b2,i)) for i in range(len(b1))))
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
#### Functions to estimate precision
|
|
59
|
+
def estimate_error(st:SimplexTreeMulti, module:PyModule, degree:int, nlines:int = 100, verbose:bool =False):
|
|
60
|
+
"""
|
|
61
|
+
Given an MMA SimplexTree and PyModule, estimates the bottleneck distance using barcodes given by gudhi.
|
|
62
|
+
|
|
63
|
+
Parameters
|
|
64
|
+
----------
|
|
65
|
+
st:SimplexTree
|
|
66
|
+
The simplextree representing the n-filtered complex. Used to define the gudhi simplextrees on different lines.
|
|
67
|
+
module:PyModule
|
|
68
|
+
The module on which to estimate approximation error, w.r.t. the original simplextree st.
|
|
69
|
+
degree: The homology degree to consider
|
|
70
|
+
|
|
71
|
+
Returns
|
|
72
|
+
-------
|
|
73
|
+
The estimation of the matching distance, i.e., the maximum of the sampled bottleneck distances.
|
|
74
|
+
|
|
75
|
+
"""
|
|
76
|
+
from time import perf_counter
|
|
77
|
+
parameter = 0
|
|
78
|
+
|
|
79
|
+
def _get_bc_ST(st, basepoint, degree:int):
|
|
80
|
+
"""
|
|
81
|
+
Slices an mma simplextree to a gudhi simplextree, and compute its persistence on the diagonal line crossing the given basepoint.
|
|
82
|
+
"""
|
|
83
|
+
gst = st.project_on_line(basepoint=basepoint, parameter=parameter) # we consider only the 1rst coordinate (as )
|
|
84
|
+
gst.compute_persistence()
|
|
85
|
+
return gst.persistence_intervals_in_dimension(degree)
|
|
86
|
+
from gudhi.bottleneck import bottleneck_distance
|
|
87
|
+
low, high = module.get_box()
|
|
88
|
+
nfiltration = len(low)
|
|
89
|
+
basepoints = np.random.uniform(low=low, high=high, size=(nlines,nfiltration))
|
|
90
|
+
# barcodes from module
|
|
91
|
+
print("Computing mma barcodes...", flush=1, end="") if verbose else None
|
|
92
|
+
time = perf_counter()
|
|
93
|
+
bcs_from_mod = module.barcodes(degree=degree, basepoints = basepoints).get_points()
|
|
94
|
+
print(f"Done. {perf_counter() - time}s.") if verbose else None
|
|
95
|
+
clean = lambda dgm : np.array([[birth[parameter], death[parameter]] for birth,death in dgm if len(birth) > 0 and birth[parameter] != np.inf])
|
|
96
|
+
bcs_from_mod = [clean(dgm) for dgm in bcs_from_mod] # we only consider the 1st coordinate of the barcode
|
|
97
|
+
# Computes gudhi barcodes
|
|
98
|
+
from tqdm import tqdm
|
|
99
|
+
bcs_from_gudhi = [_get_bc_ST(st,basepoint=basepoint, degree=degree) for basepoint in tqdm(basepoints, disable= not verbose, desc = "Computing gudhi barcodes")]
|
|
100
|
+
return max((bottleneck_distance(a,b) for a,b in tqdm(zip(bcs_from_mod, bcs_from_gudhi), disable = not verbose, total=nlines, desc="Computing bottleneck distances")))
|
|
Binary file
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# cimport multipers.tensor as mt
|
|
2
|
+
from libc.stdint cimport intptr_t, uint16_t, uint32_t, int32_t
|
|
3
|
+
from libcpp.vector cimport vector
|
|
4
|
+
from libcpp cimport bool, int, float
|
|
5
|
+
from libcpp.utility cimport pair
|
|
6
|
+
from typing import Optional,Iterable,Callable
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
cimport numpy as cnp
|
|
10
|
+
cnp.import_array()
|
|
11
|
+
|
|
12
|
+
ctypedef float value_type
|
|
13
|
+
python_value_type=np.float32
|
|
14
|
+
|
|
15
|
+
ctypedef int32_t indices_type # uint fails for some reason
|
|
16
|
+
python_indices_type=np.int32
|
|
17
|
+
|
|
18
|
+
ctypedef int32_t tensor_dtype
|
|
19
|
+
python_tensor_dtype = np.int32
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
ctypedef pair[vector[vector[indices_type]], vector[tensor_dtype]] signed_measure_type
|
|
23
|
+
|
|
24
|
+
cdef extern from "multi_parameter_rank_invariant/euler_characteristic.h" namespace "Gudhi::multiparameter::euler_characteristic":
|
|
25
|
+
void get_euler_surface_python(const intptr_t, tensor_dtype*, const vector[indices_type], bool, bool, bool) except + nogil
|
|
26
|
+
signed_measure_type get_euler_signed_measure(const intptr_t, tensor_dtype* , const vector[indices_type], bool, bool) except + nogil
|
|
27
|
+
|
|
28
|
+
def euler_signed_measure(simplextree, mass_default=None, bool verbose=False, bool plot=False):
|
|
29
|
+
"""
|
|
30
|
+
Computes the signed measures given by the decomposition of the hilbert function.
|
|
31
|
+
|
|
32
|
+
Input
|
|
33
|
+
-----
|
|
34
|
+
- simplextree:SimplexTreeMulti, the multifiltered simplicial complex
|
|
35
|
+
- mass_default: Either None, or 'auto' or 'inf', or array-like of floats. Where to put the default mass to get a zero-mass measure.
|
|
36
|
+
- plot:bool, plots the computed measures if true.
|
|
37
|
+
- n_jobs:int, number of jobs. Defaults to #cpu, but when doing parallel computations of signed measures, we recommend setting this to 1.
|
|
38
|
+
- verbose:bool, prints c++ logs.
|
|
39
|
+
|
|
40
|
+
Output
|
|
41
|
+
------
|
|
42
|
+
`[signed_measure_of_degree for degree in degrees]`
|
|
43
|
+
with `signed_measure_of_degree` of the form `(dirac location, dirac weights)`.
|
|
44
|
+
"""
|
|
45
|
+
assert len(simplextree.filtration_grid[0]) > 0, "Squeeze grid first."
|
|
46
|
+
cdef bool zero_pad = mass_default is not None
|
|
47
|
+
grid_conversion = [np.asarray(f) for f in simplextree.filtration_grid]
|
|
48
|
+
# assert simplextree.num_parameters == 2
|
|
49
|
+
grid_shape = np.array([len(f) for f in grid_conversion])
|
|
50
|
+
|
|
51
|
+
# match mass_default: ## Cython bug
|
|
52
|
+
# case None:
|
|
53
|
+
# pass
|
|
54
|
+
# case "inf":
|
|
55
|
+
# mass_default = np.array([np.inf]*simplextree.num_parameters)
|
|
56
|
+
# case "auto":
|
|
57
|
+
# mass_default = np.array([1.1*np.max(f) - 0.1*np.min(f) for f in grid_conversion])
|
|
58
|
+
# case _:
|
|
59
|
+
# mass_default = np.asarray(mass_default)
|
|
60
|
+
# assert mass_default.ndim == 1 and mass_default.shape[0] == simplextree.num_parameters
|
|
61
|
+
if mass_default is None:
|
|
62
|
+
mass_default = mass_default
|
|
63
|
+
else:
|
|
64
|
+
mass_default = np.asarray(mass_default)
|
|
65
|
+
assert mass_default.ndim == 1 and mass_default.shape[0] == simplextree.num_parameters
|
|
66
|
+
if zero_pad:
|
|
67
|
+
for i, _ in enumerate(grid_shape):
|
|
68
|
+
grid_shape[i] += 1 # adds a 0
|
|
69
|
+
for i,f in enumerate(grid_conversion):
|
|
70
|
+
grid_conversion[i] = np.concatenate([f, [mass_default[i]]])
|
|
71
|
+
assert len(grid_shape) == simplextree.num_parameters, "Grid shape size has to be the number of parameters."
|
|
72
|
+
container_array = np.ascontiguousarray(np.zeros(grid_shape, dtype=python_tensor_dtype).flatten())
|
|
73
|
+
assert len(container_array) < np.iinfo(python_indices_type).max, "Too large container. Raise an issue on github if you encounter this issue. (Due to tensor's operator[])"
|
|
74
|
+
cdef intptr_t simplextree_ptr = simplextree.thisptr
|
|
75
|
+
cdef vector[indices_type] c_grid_shape = grid_shape
|
|
76
|
+
cdef tensor_dtype[::1] container = container_array
|
|
77
|
+
cdef tensor_dtype* container_ptr = &container[0]
|
|
78
|
+
cdef signed_measure_type out
|
|
79
|
+
with nogil:
|
|
80
|
+
out = get_euler_signed_measure(simplextree_ptr, container_ptr, c_grid_shape, zero_pad, verbose)
|
|
81
|
+
pts, weights = np.asarray(out.first, dtype=int).reshape(-1, simplextree.num_parameters), np.asarray(out.second, dtype=int)
|
|
82
|
+
# return pts, weights
|
|
83
|
+
|
|
84
|
+
coords = np.empty(shape=pts.shape, dtype=float)
|
|
85
|
+
for i in range(coords.shape[1]):
|
|
86
|
+
coords[:,i] = grid_conversion[i][pts[:,i]]
|
|
87
|
+
sm =(coords, weights)
|
|
88
|
+
if plot:
|
|
89
|
+
from multipers.plots import plot_signed_measures
|
|
90
|
+
plot_signed_measures([sm])
|
|
91
|
+
return sm
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def euler_surface(simplextree, bool mobius_inversion=False, bool zero_pad=False, plot=False, bool verbose=False):
|
|
95
|
+
"""
|
|
96
|
+
Computes the hilbert function.
|
|
97
|
+
|
|
98
|
+
Input
|
|
99
|
+
-----
|
|
100
|
+
- simplextree:SimplexTreeMulti, the multifiltered simplicial complex
|
|
101
|
+
- degrees:array-like of ints, the degrees to compute
|
|
102
|
+
- mass_default: Either None, or 'auto' or 'inf', or array-like of floats. Where to put the default mass to get a zero-mass measure.
|
|
103
|
+
- plot:bool, plots the computed measures if true.
|
|
104
|
+
- n_jobs:int, number of jobs. Defaults to #cpu, but when doing parallel computations of signed measures, we recommend setting this to 1.
|
|
105
|
+
- verbose:bool, prints c++ logs.
|
|
106
|
+
|
|
107
|
+
Output
|
|
108
|
+
------
|
|
109
|
+
Integer array of the form `(num_degrees, num_filtration_values_of_parameter 1, ..., num_filtration_values_of_parameter n)`
|
|
110
|
+
"""
|
|
111
|
+
assert len(simplextree.filtration_grid[0]) > 0, "Squeeze grid first."
|
|
112
|
+
grid_conversion = [np.asarray(f) for f in simplextree.filtration_grid] if len(simplextree.filtration_grid[0]) > 0 else None
|
|
113
|
+
# assert simplextree.num_parameters == 2
|
|
114
|
+
grid_shape = [len(f) for f in grid_conversion]
|
|
115
|
+
assert len(grid_shape) == simplextree.num_parameters
|
|
116
|
+
container_array = np.ascontiguousarray(np.zeros(grid_shape, dtype=python_tensor_dtype).flatten())
|
|
117
|
+
cdef intptr_t simplextree_ptr = simplextree.thisptr
|
|
118
|
+
cdef vector[indices_type] c_grid_shape = grid_shape
|
|
119
|
+
cdef tensor_dtype[::1] container = container_array
|
|
120
|
+
cdef tensor_dtype* container_ptr = &container[0]
|
|
121
|
+
# cdef signed_measure_type out
|
|
122
|
+
# cdef indices_type i = 0
|
|
123
|
+
# cdef indices_type j = 1
|
|
124
|
+
# cdef vector[indices_type] fixed_values = np.asarray([0,0], dtype=int)
|
|
125
|
+
with nogil:
|
|
126
|
+
get_euler_surface_python(simplextree_ptr, container_ptr, c_grid_shape, mobius_inversion, zero_pad, verbose)
|
|
127
|
+
out = (grid_conversion, container_array.reshape(grid_shape))
|
|
128
|
+
if plot:
|
|
129
|
+
from multipers.plots import plot_surface
|
|
130
|
+
plot_surface(*out)
|
|
131
|
+
return out
|
|
132
|
+
|
|
Binary file
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# cimport multipers.tensor as mt
|
|
2
|
+
from libc.stdint cimport intptr_t, uint16_t, uint32_t, int32_t
|
|
3
|
+
from libcpp.vector cimport vector
|
|
4
|
+
from libcpp cimport bool, int, float
|
|
5
|
+
from libcpp.utility cimport pair, tuple
|
|
6
|
+
from typing import Optional,Iterable,Callable
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
cimport numpy as cnp
|
|
10
|
+
cnp.import_array()
|
|
11
|
+
|
|
12
|
+
ctypedef float value_type
|
|
13
|
+
python_value_type=np.float32
|
|
14
|
+
|
|
15
|
+
ctypedef int32_t indices_type # uint fails for some reason
|
|
16
|
+
python_indices_type=np.int32
|
|
17
|
+
|
|
18
|
+
ctypedef int32_t tensor_dtype
|
|
19
|
+
python_tensor_dtype = np.int32
|
|
20
|
+
|
|
21
|
+
ctypedef pair[vector[vector[indices_type]], vector[tensor_dtype]] signed_measure_type
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
from multipers.simplex_tree_multi import SimplexTreeMulti
|
|
25
|
+
|
|
26
|
+
cdef extern from "multi_parameter_rank_invariant/function_rips.h" namespace "Gudhi::multiparameter::function_rips":
|
|
27
|
+
void compute_function_rips_surface_python(const intptr_t, tensor_dtype* , const vector[indices_type], indices_type,indices_type, bool, bool, indices_type) except + nogil
|
|
28
|
+
signed_measure_type compute_function_rips_signed_measure_python(const intptr_t, tensor_dtype* , const vector[indices_type], indices_type,indices_type, bool, bool, indices_type) except + nogil
|
|
29
|
+
pair[vector[value_type],int] get_degree_rips_st_python(const intptr_t,const intptr_t, const vector[int]) except + nogil
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def get_degree_rips(st, vector[int] degrees, grid_strategy="exact", resolution=0):
|
|
36
|
+
assert st.dimension() == 1
|
|
37
|
+
degree_rips_st = SimplexTreeMulti(num_parameters=degrees.size())
|
|
38
|
+
cdef intptr_t simplextree_ptr = st.thisptr
|
|
39
|
+
cdef intptr_t st_multi_ptr = degree_rips_st.thisptr
|
|
40
|
+
cdef pair[vector[value_type],int] out
|
|
41
|
+
with nogil:
|
|
42
|
+
out = get_degree_rips_st_python(simplextree_ptr, st_multi_ptr, degrees)
|
|
43
|
+
filtrations = np.asarray(out.first)
|
|
44
|
+
cdef int max_degree = out.second
|
|
45
|
+
cdef bool inf_flag = filtrations[-1] == np.inf
|
|
46
|
+
if inf_flag:
|
|
47
|
+
filtrations = filtrations[:-1]
|
|
48
|
+
filtrations, = degree_rips_st._reduce_grid([filtrations],strategy=grid_strategy,resolutions=resolution)
|
|
49
|
+
if inf_flag:
|
|
50
|
+
filtrations = np.concatenate([filtrations, [np.inf]])
|
|
51
|
+
degree_rips_st.grid_squeeze([filtrations]*degree_rips_st.num_parameters)
|
|
52
|
+
degree_rips_st.filtration_grid = [filtrations, np.asarray(degrees)[::-1]]
|
|
53
|
+
return degree_rips_st,max_degree
|
|
54
|
+
|
|
55
|
+
def function_rips_surface(st_multi, vector[indices_type] homological_degrees, bool mobius_inversion=True, bool zero_pad=False, indices_type n_jobs=0):
|
|
56
|
+
assert st_multi._is_squeezed, "Squeeze first !"
|
|
57
|
+
cdef intptr_t st_multi_ptr = st_multi.thisptr
|
|
58
|
+
cdef indices_type I = len(st_multi.filtration_grid[0])
|
|
59
|
+
cdef indices_type J = st_multi.num_parameters
|
|
60
|
+
container_shape = (homological_degrees.size(),I,J)
|
|
61
|
+
container_array = np.ascontiguousarray(np.zeros(container_shape, dtype=python_tensor_dtype).flatten())
|
|
62
|
+
assert len(container_array) < np.iinfo(np.uint32).max, "Too large container. Raise an issue on github if you encounter this issue. (Due to tensor's operator[])"
|
|
63
|
+
cdef tensor_dtype[::1] container = container_array
|
|
64
|
+
cdef tensor_dtype* container_ptr = &container[0]
|
|
65
|
+
with nogil:
|
|
66
|
+
compute_function_rips_surface_python(st_multi_ptr,container_ptr, homological_degrees, I,J, mobius_inversion, zero_pad, n_jobs)
|
|
67
|
+
filtration_grid = st_multi.filtration_grid
|
|
68
|
+
if filtration_grid[0][-1] == np.inf:
|
|
69
|
+
filtration_grid[0][-1] = filtration_grid[0][-2]
|
|
70
|
+
return filtration_grid, container_array.reshape(container_shape)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def function_rips_signed_measure(st_multi, vector[indices_type] homological_degrees, bool mobius_inversion=True, bool zero_pad=False, indices_type n_jobs=0, bool reconvert = True):
|
|
75
|
+
assert st_multi._is_squeezed
|
|
76
|
+
cdef intptr_t st_multi_ptr = st_multi.thisptr
|
|
77
|
+
cdef indices_type I = len(st_multi.filtration_grid[0])
|
|
78
|
+
cdef indices_type J = st_multi.num_parameters
|
|
79
|
+
container_shape = (homological_degrees.size(),I,J)
|
|
80
|
+
container_array = np.ascontiguousarray(np.zeros(container_shape, dtype=python_tensor_dtype).flatten())
|
|
81
|
+
assert len(container_array) < np.iinfo(np.uint32).max, "Too large container. Raise an issue on github if you encounter this issue. (Due to tensor's operator[])"
|
|
82
|
+
cdef tensor_dtype[::1] container = container_array
|
|
83
|
+
cdef tensor_dtype* container_ptr = &container[0]
|
|
84
|
+
cdef signed_measure_type out
|
|
85
|
+
# TODO nogil
|
|
86
|
+
with nogil:
|
|
87
|
+
out = compute_function_rips_signed_measure_python(st_multi_ptr,container_ptr, homological_degrees, I,J, mobius_inversion, zero_pad, n_jobs)
|
|
88
|
+
pts, weights = np.asarray(out.first, dtype=int).reshape(-1, 3), np.asarray(out.second, dtype=int)
|
|
89
|
+
|
|
90
|
+
degree_indices = [np.argwhere(pts[:,0] == degree_index).flatten() for degree_index, degree in enumerate(homological_degrees)] ## TODO : maybe optimize
|
|
91
|
+
sms = [(pts[id,1:],weights[id]) for id in degree_indices]
|
|
92
|
+
if not reconvert: return sms
|
|
93
|
+
|
|
94
|
+
grid_conversion = st_multi.filtration_grid
|
|
95
|
+
for degree_index,(pts,weights) in enumerate(sms):
|
|
96
|
+
coords = np.empty(shape=pts.shape, dtype=float)
|
|
97
|
+
for i in range(coords.shape[1]):
|
|
98
|
+
coords[:,i] = np.asarray(grid_conversion[i])[pts[:,i]]
|
|
99
|
+
sms[degree_index]=(coords, weights)
|
|
100
|
+
|
|
101
|
+
return sms
|
|
Binary file
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# cimport multipers.tensor as mt
|
|
2
|
+
from libc.stdint cimport intptr_t, uint16_t, uint32_t, int32_t
|
|
3
|
+
from libcpp.vector cimport vector
|
|
4
|
+
from libcpp cimport bool, int, float
|
|
5
|
+
from libcpp.utility cimport pair
|
|
6
|
+
from typing import Optional,Iterable,Callable
|
|
7
|
+
|
|
8
|
+
def hilbert_signed_measure(simplextree, degrees, mass_default=None, plot=False, n_jobs=0, verbose=False):
|
|
9
|
+
"""
|
|
10
|
+
Computes the signed measures given by the decomposition of the hilbert function.
|
|
11
|
+
|
|
12
|
+
Input
|
|
13
|
+
-----
|
|
14
|
+
- simplextree:SimplexTreeMulti, the multifiltered simplicial complex
|
|
15
|
+
- degrees:array-like of ints, the degrees to compute
|
|
16
|
+
- mass_default: Either None, or 'auto' or 'inf', or array-like of floats. Where to put the default mass to get a zero-mass measure.
|
|
17
|
+
- plot:bool, plots the computed measures if true.
|
|
18
|
+
- n_jobs:int, number of jobs. Defaults to #cpu, but when doing parallel computations of signed measures, we recommend setting this to 1.
|
|
19
|
+
- verbose:bool, prints c++ logs.
|
|
20
|
+
|
|
21
|
+
Output
|
|
22
|
+
------
|
|
23
|
+
`[signed_measure_of_degree for degree in degrees]`
|
|
24
|
+
with `signed_measure_of_degree` of the form `(dirac location, dirac weights)`.
|
|
25
|
+
"""
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def hilbert_function(simplextree, degrees, zero_pad=False, plot=False, n_jobs=0):
|
|
30
|
+
"""
|
|
31
|
+
Computes the hilbert function.
|
|
32
|
+
|
|
33
|
+
Input
|
|
34
|
+
-----
|
|
35
|
+
- simplextree:SimplexTreeMulti, the multifiltered simplicial complex
|
|
36
|
+
- degrees:array-like of ints, the degrees to compute
|
|
37
|
+
- mass_default: Either None, or 'auto' or 'inf', or array-like of floats. Where to put the default mass to get a zero-mass measure.
|
|
38
|
+
- plot:bool, plots the computed measures if true.
|
|
39
|
+
- n_jobs:int, number of jobs. Defaults to #cpu, but when doing parallel computations of signed measures, we recommend setting this to 1.
|
|
40
|
+
- verbose:bool, prints c++ logs.
|
|
41
|
+
|
|
42
|
+
Output
|
|
43
|
+
------
|
|
44
|
+
Integer array of the form `(num_degrees, num_filtration_values_of_parameter 1, ..., num_filtration_values_of_parameter n)`
|
|
45
|
+
"""
|
|
46
|
+
pass
|