multipers 1.1.3__cp310-cp310-macosx_11_0_universal2.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/.dylibs/libtbb.12.12.dylib +0 -0
- multipers/.dylibs/libtbbmalloc.2.12.dylib +0 -0
- multipers/__init__.py +5 -0
- multipers/_old_rank_invariant.pyx +328 -0
- multipers/_signed_measure_meta.py +193 -0
- multipers/data/MOL2.py +350 -0
- multipers/data/UCR.py +18 -0
- multipers/data/__init__.py +1 -0
- multipers/data/graphs.py +466 -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 +172 -0
- multipers/euler_characteristic.cpython-310-darwin.so +0 -0
- multipers/euler_characteristic.pyx +137 -0
- multipers/function_rips.cpython-310-darwin.so +0 -0
- multipers/function_rips.pyx +102 -0
- multipers/hilbert_function.cpython-310-darwin.so +0 -0
- multipers/hilbert_function.pyi +46 -0
- multipers/hilbert_function.pyx +151 -0
- multipers/io.cpython-310-darwin.so +0 -0
- multipers/io.pyx +176 -0
- multipers/ml/__init__.py +0 -0
- multipers/ml/accuracies.py +61 -0
- multipers/ml/convolutions.py +510 -0
- multipers/ml/invariants_with_persistable.py +79 -0
- multipers/ml/kernels.py +128 -0
- multipers/ml/mma.py +657 -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 +1479 -0
- multipers/ml/sliced_wasserstein.py +313 -0
- multipers/ml/tools.py +116 -0
- multipers/mma_structures.cpython-310-darwin.so +0 -0
- multipers/mma_structures.pxd +155 -0
- multipers/mma_structures.pyx +651 -0
- multipers/multiparameter_edge_collapse.py +29 -0
- multipers/multiparameter_module_approximation.cpython-310-darwin.so +0 -0
- multipers/multiparameter_module_approximation.pyi +439 -0
- multipers/multiparameter_module_approximation.pyx +311 -0
- multipers/pickle.py +53 -0
- multipers/plots.py +292 -0
- multipers/point_measure_integration.cpython-310-darwin.so +0 -0
- multipers/point_measure_integration.pyx +59 -0
- multipers/rank_invariant.cpython-310-darwin.so +0 -0
- multipers/rank_invariant.pyx +154 -0
- multipers/simplex_tree_multi.cpython-310-darwin.so +0 -0
- multipers/simplex_tree_multi.pxd +121 -0
- multipers/simplex_tree_multi.pyi +715 -0
- multipers/simplex_tree_multi.pyx +1417 -0
- multipers/slicer.cpython-310-darwin.so +0 -0
- multipers/slicer.pxd +94 -0
- multipers/slicer.pyx +276 -0
- multipers/tensor.pxd +13 -0
- multipers/test.pyx +44 -0
- multipers-1.1.3.dist-info/LICENSE +21 -0
- multipers-1.1.3.dist-info/METADATA +22 -0
- multipers-1.1.3.dist-info/RECORD +63 -0
- multipers-1.1.3.dist-info/WHEEL +5 -0
- multipers-1.1.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
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:
|
|
3
|
+
"""Generates a noisy annulus dataset.
|
|
4
|
+
|
|
5
|
+
Parameters
|
|
6
|
+
----------
|
|
7
|
+
r1 : float.
|
|
8
|
+
Lower radius of the annulus.
|
|
9
|
+
r2 : float.
|
|
10
|
+
Upper radius of the annulus.
|
|
11
|
+
n1 : int
|
|
12
|
+
Number of points in the annulus.
|
|
13
|
+
n2 : int
|
|
14
|
+
Number of points in the square.
|
|
15
|
+
dim : int
|
|
16
|
+
Dimension of the annulus.
|
|
17
|
+
center: list or array
|
|
18
|
+
center of the annulus.
|
|
19
|
+
|
|
20
|
+
Returns
|
|
21
|
+
-------
|
|
22
|
+
numpy array
|
|
23
|
+
Dataset. size : (n1+n2) x dim
|
|
24
|
+
|
|
25
|
+
"""
|
|
26
|
+
from numpy.random import uniform
|
|
27
|
+
from numpy.linalg import norm
|
|
28
|
+
|
|
29
|
+
set =[]
|
|
30
|
+
while len(set)<n1:
|
|
31
|
+
draw=uniform(low=-r2, high=r2, size=dim)
|
|
32
|
+
if norm(draw) > r1 and norm(draw) < r2:
|
|
33
|
+
set.append(draw)
|
|
34
|
+
annulus = np.array(set) if center == None else np.array(set) + np.array(center)
|
|
35
|
+
diffuse_noise = uniform(size=(n2,dim), low=-1.1*r2,high=1.1*r2)
|
|
36
|
+
if center is not None: diffuse_noise += np.array(center)
|
|
37
|
+
return np.vstack([annulus, diffuse_noise])
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def three_annulus(num_pts:int=500,num_outliers:int=500):
|
|
41
|
+
X = np.block([
|
|
42
|
+
[np.random.uniform(low=-2,high=2,size=(num_outliers,2))],
|
|
43
|
+
[np.array(noisy_annulus(r1=0.6,r2=0.9,n1=(int)(num_pts*1/3), n2=0, center = [1,-0.2]))],
|
|
44
|
+
[np.array(noisy_annulus(r1=0.4,r2=0.55,n1=(int)(num_pts*1/3), n2=0, center = [-1.2,-1]))],
|
|
45
|
+
[np.array(noisy_annulus(r1=0.3,r2=0.4,n1=(int)(num_pts*1/3), n2=0, center = [-0.7,1.1]))],
|
|
46
|
+
])
|
|
47
|
+
return X
|
|
48
|
+
|
|
49
|
+
def orbit(n:int=1000, r:float=1., x0=[]):
|
|
50
|
+
point_list=[]
|
|
51
|
+
if len(x0) != 2:
|
|
52
|
+
x,y=np.random.uniform(size=2)
|
|
53
|
+
else:
|
|
54
|
+
x,y = x0
|
|
55
|
+
point_list.append([x,y])
|
|
56
|
+
for _ in range(n-1):
|
|
57
|
+
x = (x + r*y*(1-y)) %1
|
|
58
|
+
y = (y + r*x*(1-x)) %1
|
|
59
|
+
point_list.append([x,y])
|
|
60
|
+
return np.asarray(point_list, dtype=float)
|
|
61
|
+
|
|
62
|
+
def get_orbit5k(num_pts = 1000, num_data=5000):
|
|
63
|
+
from sklearn.preprocessing import LabelEncoder
|
|
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,172 @@
|
|
|
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 PyModule
|
|
6
|
+
from multipers.mma_structures import PyMultiDiagrams
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def sm2diff(sm1, sm2):
|
|
10
|
+
if isinstance(sm1[0], np.ndarray):
|
|
11
|
+
|
|
12
|
+
def backend_concatenate(a, b):
|
|
13
|
+
return np.concatenate([a, b], axis=0)
|
|
14
|
+
|
|
15
|
+
def backend_tensor(x):
|
|
16
|
+
return np.asarray(x, dtype=int)
|
|
17
|
+
elif isinstance(sm1[0], torch.Tensor):
|
|
18
|
+
|
|
19
|
+
def backend_concatenate(a, b):
|
|
20
|
+
return torch.concatenate([a, b], dim=0)
|
|
21
|
+
|
|
22
|
+
def backend_tensor(x):
|
|
23
|
+
return torch.tensor(x).type(torch.int)
|
|
24
|
+
else:
|
|
25
|
+
raise Exception("Invalid backend. Numpy or torch.")
|
|
26
|
+
pts1, w1 = sm1
|
|
27
|
+
pts2, w2 = sm2
|
|
28
|
+
pos_indices1 = backend_tensor(
|
|
29
|
+
[i for i, w in enumerate(w1) for _ in range(w) if w > 0]
|
|
30
|
+
)
|
|
31
|
+
pos_indices2 = backend_tensor(
|
|
32
|
+
[i for i, w in enumerate(w2) for _ in range(w) if w > 0]
|
|
33
|
+
)
|
|
34
|
+
neg_indices1 = backend_tensor(
|
|
35
|
+
[i for i, w in enumerate(w1) for _ in range(-w) if w < 0]
|
|
36
|
+
)
|
|
37
|
+
neg_indices2 = backend_tensor(
|
|
38
|
+
[i for i, w in enumerate(w2) for _ in range(-w) if w < 0]
|
|
39
|
+
)
|
|
40
|
+
x = backend_concatenate(pts1[pos_indices1], pts2[neg_indices2])
|
|
41
|
+
y = backend_concatenate(pts1[neg_indices1], pts2[pos_indices2])
|
|
42
|
+
return x, y
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def sm_distance(sm1, sm2, reg=0, reg_m=0, numItermax=10000, p=1):
|
|
46
|
+
x, y = sm2diff(sm1, sm2)
|
|
47
|
+
loss = ot.dist(
|
|
48
|
+
x, y, metric="sqeuclidean", p=2
|
|
49
|
+
) # only euc + sqeuclidian are implemented in pot for the moment with torch backend # TODO : check later
|
|
50
|
+
if isinstance(x, np.ndarray):
|
|
51
|
+
empty_tensor = np.array([]) # uniform weights
|
|
52
|
+
elif isinstance(x, torch.Tensor):
|
|
53
|
+
empty_tensor = torch.tensor([]) # uniform weights
|
|
54
|
+
|
|
55
|
+
if reg == 0:
|
|
56
|
+
return ot.lp.emd2(empty_tensor, empty_tensor, M=loss) * len(x)
|
|
57
|
+
if reg_m == 0:
|
|
58
|
+
return ot.sinkhorn2(
|
|
59
|
+
a=empty_tensor, b=empty_tensor, M=loss, reg=reg, numItermax=numItermax
|
|
60
|
+
)
|
|
61
|
+
return ot.sinkhorn_unbalanced2(
|
|
62
|
+
a=empty_tensor,
|
|
63
|
+
b=empty_tensor,
|
|
64
|
+
M=loss,
|
|
65
|
+
reg=reg,
|
|
66
|
+
reg_m=reg_m,
|
|
67
|
+
numItermax=numItermax,
|
|
68
|
+
)
|
|
69
|
+
# return ot.sinkhorn2(a=onesx,b=onesy,M=loss,reg=reg, numItermax=numItermax)
|
|
70
|
+
# return ot.bregman.empirical_sinkhorn2(x,y,reg=reg)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def estimate_matching(b1: PyMultiDiagrams, b2: PyMultiDiagrams):
|
|
74
|
+
assert len(b1) == len(b2)
|
|
75
|
+
from gudhi.bottleneck import bottleneck_distance
|
|
76
|
+
|
|
77
|
+
def get_bc(b: PyMultiDiagrams, i: int) -> np.ndarray:
|
|
78
|
+
temp = b[i].get_points()
|
|
79
|
+
out = (
|
|
80
|
+
np.array(temp)[:, :, 0] if len(temp) > 0 else np.empty((0, 2))
|
|
81
|
+
) # GUDHI FIX
|
|
82
|
+
return out
|
|
83
|
+
|
|
84
|
+
return max(
|
|
85
|
+
(bottleneck_distance(get_bc(b1, i), get_bc(b2, i))
|
|
86
|
+
for i in range(len(b1)))
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# Functions to estimate precision
|
|
91
|
+
def estimate_error(
|
|
92
|
+
st: SimplexTreeMulti,
|
|
93
|
+
module: PyModule,
|
|
94
|
+
degree: int,
|
|
95
|
+
nlines: int = 100,
|
|
96
|
+
verbose: bool = False,
|
|
97
|
+
):
|
|
98
|
+
"""
|
|
99
|
+
Given an MMA SimplexTree and PyModule, estimates the bottleneck distance using barcodes given by gudhi.
|
|
100
|
+
|
|
101
|
+
Parameters
|
|
102
|
+
----------
|
|
103
|
+
st:SimplexTree
|
|
104
|
+
The simplextree representing the n-filtered complex. Used to define the gudhi simplextrees on different lines.
|
|
105
|
+
module:PyModule
|
|
106
|
+
The module on which to estimate approximation error, w.r.t. the original simplextree st.
|
|
107
|
+
degree: The homology degree to consider
|
|
108
|
+
|
|
109
|
+
Returns
|
|
110
|
+
-------
|
|
111
|
+
The estimation of the matching distance, i.e., the maximum of the sampled bottleneck distances.
|
|
112
|
+
|
|
113
|
+
"""
|
|
114
|
+
from time import perf_counter
|
|
115
|
+
|
|
116
|
+
parameter = 0
|
|
117
|
+
|
|
118
|
+
def _get_bc_ST(st, basepoint, degree: int):
|
|
119
|
+
"""
|
|
120
|
+
Slices an mma simplextree to a gudhi simplextree, and compute its persistence on the diagonal line crossing the given basepoint.
|
|
121
|
+
"""
|
|
122
|
+
gst = st.project_on_line(
|
|
123
|
+
basepoint=basepoint, parameter=parameter
|
|
124
|
+
) # we consider only the 1rst coordinate (as )
|
|
125
|
+
gst.compute_persistence()
|
|
126
|
+
return gst.persistence_intervals_in_dimension(degree)
|
|
127
|
+
|
|
128
|
+
from gudhi.bottleneck import bottleneck_distance
|
|
129
|
+
|
|
130
|
+
low, high = module.get_box()
|
|
131
|
+
nfiltration = len(low)
|
|
132
|
+
basepoints = np.random.uniform(
|
|
133
|
+
low=low, high=high, size=(nlines, nfiltration))
|
|
134
|
+
# barcodes from module
|
|
135
|
+
print("Computing mma barcodes...", flush=1, end="") if verbose else None
|
|
136
|
+
time = perf_counter()
|
|
137
|
+
bcs_from_mod = module.barcodes(
|
|
138
|
+
degree=degree, basepoints=basepoints).get_points()
|
|
139
|
+
print(f"Done. {perf_counter() - time}s.") if verbose else None
|
|
140
|
+
|
|
141
|
+
def clean(dgm):
|
|
142
|
+
return np.array(
|
|
143
|
+
[
|
|
144
|
+
[birth[parameter], death[parameter]]
|
|
145
|
+
for birth, death in dgm
|
|
146
|
+
if len(birth) > 0 and birth[parameter] != np.inf
|
|
147
|
+
]
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
bcs_from_mod = [
|
|
151
|
+
clean(dgm) for dgm in bcs_from_mod
|
|
152
|
+
] # we only consider the 1st coordinate of the barcode
|
|
153
|
+
# Computes gudhi barcodes
|
|
154
|
+
from tqdm import tqdm
|
|
155
|
+
|
|
156
|
+
bcs_from_gudhi = [
|
|
157
|
+
_get_bc_ST(st, basepoint=basepoint, degree=degree)
|
|
158
|
+
for basepoint in tqdm(
|
|
159
|
+
basepoints, disable=not verbose, desc="Computing gudhi barcodes"
|
|
160
|
+
)
|
|
161
|
+
]
|
|
162
|
+
return max(
|
|
163
|
+
(
|
|
164
|
+
bottleneck_distance(a, b)
|
|
165
|
+
for a, b in tqdm(
|
|
166
|
+
zip(bcs_from_mod, bcs_from_gudhi),
|
|
167
|
+
disable=not verbose,
|
|
168
|
+
total=nlines,
|
|
169
|
+
desc="Computing bottleneck distances",
|
|
170
|
+
)
|
|
171
|
+
)
|
|
172
|
+
)
|
|
Binary file
|
|
@@ -0,0 +1,137 @@
|
|
|
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, grid_conversion=None):
|
|
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] if grid_conversion is None else grid_conversion
|
|
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
|
+
def empty_like(x):
|
|
84
|
+
if isinstance(grid_conversion[0], np.ndarray):
|
|
85
|
+
return np.empty_like(x, dtype=float)
|
|
86
|
+
import torch
|
|
87
|
+
assert isinstance(grid_conversion[0], torch.Tensor), f"Invalid grid type. Got {type(grid_conversion[0])}, expected numpy or torch array."
|
|
88
|
+
return torch.empty(x.shape,dtype=float)
|
|
89
|
+
coords = empty_like(pts)
|
|
90
|
+
for i in range(coords.shape[1]):
|
|
91
|
+
coords[:,i] = grid_conversion[i][pts[:,i]]
|
|
92
|
+
sm =(coords, weights)
|
|
93
|
+
if plot:
|
|
94
|
+
from multipers.plots import plot_signed_measures
|
|
95
|
+
plot_signed_measures([sm])
|
|
96
|
+
return sm
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def euler_surface(simplextree, bool mobius_inversion=False, bool zero_pad=False, plot=False, bool verbose=False):
|
|
100
|
+
"""
|
|
101
|
+
Computes the hilbert function.
|
|
102
|
+
|
|
103
|
+
Input
|
|
104
|
+
-----
|
|
105
|
+
- simplextree:SimplexTreeMulti, the multifiltered simplicial complex
|
|
106
|
+
- degrees:array-like of ints, the degrees to compute
|
|
107
|
+
- 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.
|
|
108
|
+
- plot:bool, plots the computed measures if true.
|
|
109
|
+
- n_jobs:int, number of jobs. Defaults to #cpu, but when doing parallel computations of signed measures, we recommend setting this to 1.
|
|
110
|
+
- verbose:bool, prints c++ logs.
|
|
111
|
+
|
|
112
|
+
Output
|
|
113
|
+
------
|
|
114
|
+
Integer array of the form `(num_degrees, num_filtration_values_of_parameter 1, ..., num_filtration_values_of_parameter n)`
|
|
115
|
+
"""
|
|
116
|
+
assert len(simplextree.filtration_grid[0]) > 0, "Squeeze grid first."
|
|
117
|
+
grid_conversion = [np.asarray(f) for f in simplextree.filtration_grid] if len(simplextree.filtration_grid[0]) > 0 else None
|
|
118
|
+
# assert simplextree.num_parameters == 2
|
|
119
|
+
grid_shape = [len(f) for f in grid_conversion]
|
|
120
|
+
assert len(grid_shape) == simplextree.num_parameters
|
|
121
|
+
container_array = np.ascontiguousarray(np.zeros(grid_shape, dtype=python_tensor_dtype).flatten())
|
|
122
|
+
cdef intptr_t simplextree_ptr = simplextree.thisptr
|
|
123
|
+
cdef vector[indices_type] c_grid_shape = grid_shape
|
|
124
|
+
cdef tensor_dtype[::1] container = container_array
|
|
125
|
+
cdef tensor_dtype* container_ptr = &container[0]
|
|
126
|
+
# cdef signed_measure_type out
|
|
127
|
+
# cdef indices_type i = 0
|
|
128
|
+
# cdef indices_type j = 1
|
|
129
|
+
# cdef vector[indices_type] fixed_values = np.asarray([0,0], dtype=int)
|
|
130
|
+
with nogil:
|
|
131
|
+
get_euler_surface_python(simplextree_ptr, container_ptr, c_grid_shape, mobius_inversion, zero_pad, verbose)
|
|
132
|
+
out = (grid_conversion, container_array.reshape(grid_shape))
|
|
133
|
+
if plot:
|
|
134
|
+
from multipers.plots import plot_surface
|
|
135
|
+
plot_surface(*out)
|
|
136
|
+
return out
|
|
137
|
+
|
|
Binary file
|
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
degree_rips_st._is_function_simplextree= True
|
|
54
|
+
return degree_rips_st,max_degree
|
|
55
|
+
|
|
56
|
+
def function_rips_surface(st_multi, vector[indices_type] homological_degrees, bool mobius_inversion=True, bool zero_pad=False, indices_type n_jobs=0):
|
|
57
|
+
assert st_multi._is_squeezed, "Squeeze first !"
|
|
58
|
+
cdef intptr_t st_multi_ptr = st_multi.thisptr
|
|
59
|
+
cdef indices_type I = len(st_multi.filtration_grid[0])
|
|
60
|
+
cdef indices_type J = st_multi.num_parameters
|
|
61
|
+
container_shape = (homological_degrees.size(),I,J)
|
|
62
|
+
container_array = np.ascontiguousarray(np.zeros(container_shape, dtype=python_tensor_dtype).flatten())
|
|
63
|
+
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[])"
|
|
64
|
+
cdef tensor_dtype[::1] container = container_array
|
|
65
|
+
cdef tensor_dtype* container_ptr = &container[0]
|
|
66
|
+
with nogil:
|
|
67
|
+
compute_function_rips_surface_python(st_multi_ptr,container_ptr, homological_degrees, I,J, mobius_inversion, zero_pad, n_jobs)
|
|
68
|
+
filtration_grid = st_multi.filtration_grid
|
|
69
|
+
if filtration_grid[0][-1] == np.inf:
|
|
70
|
+
filtration_grid[0][-1] = filtration_grid[0][-2]
|
|
71
|
+
return filtration_grid, container_array.reshape(container_shape)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
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):
|
|
76
|
+
assert st_multi._is_squeezed
|
|
77
|
+
cdef intptr_t st_multi_ptr = st_multi.thisptr
|
|
78
|
+
cdef indices_type I = len(st_multi.filtration_grid[0])
|
|
79
|
+
cdef indices_type J = st_multi.num_parameters
|
|
80
|
+
container_shape = (homological_degrees.size(),I,J)
|
|
81
|
+
container_array = np.ascontiguousarray(np.zeros(container_shape, dtype=python_tensor_dtype).flatten())
|
|
82
|
+
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[])"
|
|
83
|
+
cdef tensor_dtype[::1] container = container_array
|
|
84
|
+
cdef tensor_dtype* container_ptr = &container[0]
|
|
85
|
+
cdef signed_measure_type out
|
|
86
|
+
# TODO nogil
|
|
87
|
+
with nogil:
|
|
88
|
+
out = compute_function_rips_signed_measure_python(st_multi_ptr,container_ptr, homological_degrees, I,J, mobius_inversion, zero_pad, n_jobs)
|
|
89
|
+
pts, weights = np.asarray(out.first, dtype=int).reshape(-1, 3), np.asarray(out.second, dtype=int)
|
|
90
|
+
|
|
91
|
+
degree_indices = [np.argwhere(pts[:,0] == degree_index).flatten() for degree_index, degree in enumerate(homological_degrees)] ## TODO : maybe optimize
|
|
92
|
+
sms = [(pts[id,1:],weights[id]) for id in degree_indices]
|
|
93
|
+
if not reconvert: return sms
|
|
94
|
+
|
|
95
|
+
grid_conversion = st_multi.filtration_grid
|
|
96
|
+
for degree_index,(pts,weights) in enumerate(sms):
|
|
97
|
+
coords = np.empty(shape=pts.shape, dtype=float)
|
|
98
|
+
for i in range(coords.shape[1]):
|
|
99
|
+
coords[:,i] = np.asarray(grid_conversion[i])[pts[:,i]]
|
|
100
|
+
sms[degree_index]=(coords, weights)
|
|
101
|
+
|
|
102
|
+
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
|
|
@@ -0,0 +1,151 @@
|
|
|
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/hilbert_function.h" namespace "Gudhi::multiparameter::hilbert_function":
|
|
25
|
+
void get_hilbert_surface_python(const intptr_t, tensor_dtype* , const vector[indices_type], const vector[indices_type], bool, bool, indices_type, bool) except + nogil
|
|
26
|
+
signed_measure_type get_hilbert_signed_measure(const intptr_t, tensor_dtype* , const vector[indices_type], const vector[indices_type], bool, indices_type, bool, bool) except + nogil
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def hilbert_signed_measure(
|
|
30
|
+
simplextree,
|
|
31
|
+
vector[indices_type] degrees,
|
|
32
|
+
mass_default=None,
|
|
33
|
+
plot=False,
|
|
34
|
+
indices_type n_jobs=0,
|
|
35
|
+
bool verbose=False,
|
|
36
|
+
bool expand_collapse=False,
|
|
37
|
+
grid_conversion = None
|
|
38
|
+
):
|
|
39
|
+
"""
|
|
40
|
+
Computes the signed measures given by the decomposition of the hilbert function.
|
|
41
|
+
|
|
42
|
+
Input
|
|
43
|
+
-----
|
|
44
|
+
- simplextree:SimplexTreeMulti, the multifiltered simplicial complex
|
|
45
|
+
- degrees:array-like of ints, the degrees to compute
|
|
46
|
+
- 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.
|
|
47
|
+
- plot:bool, plots the computed measures if true.
|
|
48
|
+
- n_jobs:int, number of jobs. Defaults to #cpu, but when doing parallel computations of signed measures, we recommend setting this to 1.
|
|
49
|
+
- verbose:bool, prints c++ logs.
|
|
50
|
+
|
|
51
|
+
Output
|
|
52
|
+
------
|
|
53
|
+
`[signed_measure_of_degree for degree in degrees]`
|
|
54
|
+
with `signed_measure_of_degree` of the form `(dirac location, dirac weights)`.
|
|
55
|
+
"""
|
|
56
|
+
assert simplextree._is_squeezed > 0, "Squeeze grid first."
|
|
57
|
+
cdef bool zero_pad = mass_default is not None
|
|
58
|
+
grid_conversion = [np.asarray(f) for f in simplextree.filtration_grid] if grid_conversion is None else grid_conversion
|
|
59
|
+
# assert simplextree.num_parameters == 2
|
|
60
|
+
grid_shape = np.array([len(f) for f in grid_conversion])
|
|
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
|
+
grid_shape_with_degree = np.asarray(np.concatenate([[len(degrees)], grid_shape]), dtype=python_indices_type)
|
|
73
|
+
container_array = np.ascontiguousarray(np.zeros(grid_shape_with_degree, dtype=python_tensor_dtype).flatten())
|
|
74
|
+
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[])"
|
|
75
|
+
cdef intptr_t simplextree_ptr = simplextree.thisptr
|
|
76
|
+
cdef vector[indices_type] c_grid_shape = grid_shape_with_degree
|
|
77
|
+
cdef tensor_dtype[::1] container = container_array
|
|
78
|
+
cdef tensor_dtype* container_ptr = &container[0]
|
|
79
|
+
cdef signed_measure_type out
|
|
80
|
+
with nogil:
|
|
81
|
+
out = get_hilbert_signed_measure(simplextree_ptr, container_ptr, c_grid_shape, degrees, zero_pad, n_jobs, verbose, expand_collapse)
|
|
82
|
+
pts, weights = np.asarray(out.first, dtype=int).reshape(-1, simplextree.num_parameters+1), np.asarray(out.second, dtype=int)
|
|
83
|
+
# return pts, weights
|
|
84
|
+
degree_indices = [np.argwhere(pts[:,0] == degree_index).flatten() for degree_index, degree in enumerate(degrees)] ## TODO : maybe optimize
|
|
85
|
+
sms = [(pts[id,1:],weights[id]) for id in degree_indices]
|
|
86
|
+
|
|
87
|
+
def empty_like(x):
|
|
88
|
+
if isinstance(grid_conversion[0], np.ndarray):
|
|
89
|
+
return np.empty_like(x, dtype=float)
|
|
90
|
+
import torch
|
|
91
|
+
assert isinstance(grid_conversion[0], torch.Tensor), f"Invalid grid type. Got {type(grid_conversion[0])}, expected numpy or torch array."
|
|
92
|
+
return torch.empty(x.shape,dtype=float)
|
|
93
|
+
|
|
94
|
+
for degree_index,(pts,weights) in enumerate(sms):
|
|
95
|
+
coords = empty_like(pts)
|
|
96
|
+
for i in range(coords.shape[1]):
|
|
97
|
+
coords[:,i] = grid_conversion[i][pts[:,i]]
|
|
98
|
+
sms[degree_index]=(coords, weights)
|
|
99
|
+
if plot:
|
|
100
|
+
from multipers.plots import plot_signed_measures
|
|
101
|
+
plot_signed_measures(sms)
|
|
102
|
+
return sms
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def hilbert_surface(simplextree, vector[indices_type] degrees, mass_default=None, bool mobius_inversion=False, bool plot=False, indices_type n_jobs=0, bool expand_collapse=False):
|
|
106
|
+
"""
|
|
107
|
+
Computes the hilbert function.
|
|
108
|
+
|
|
109
|
+
Input
|
|
110
|
+
-----
|
|
111
|
+
- simplextree:SimplexTreeMulti, the multifiltered simplicial complex
|
|
112
|
+
- degrees:array-like of ints, the degrees to compute
|
|
113
|
+
- 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.
|
|
114
|
+
- plot:bool, plots the computed measures if true.
|
|
115
|
+
- n_jobs:int, number of jobs. Defaults to #cpu, but when doing parallel computations of signed measures, we recommend setting this to 1.
|
|
116
|
+
- verbose:bool, prints c++ logs.
|
|
117
|
+
|
|
118
|
+
Output
|
|
119
|
+
------
|
|
120
|
+
Integer array of the form `(num_degrees, num_filtration_values_of_parameter 1, ..., num_filtration_values_of_parameter n)`
|
|
121
|
+
"""
|
|
122
|
+
assert simplextree._is_squeezed > 0, "Squeeze grid first."
|
|
123
|
+
cdef bool zero_pad = mass_default is not None
|
|
124
|
+
grid_conversion = [np.asarray(f) for f in simplextree.filtration_grid]
|
|
125
|
+
grid_shape = np.array([len(f) for f in grid_conversion])
|
|
126
|
+
if mass_default is None:
|
|
127
|
+
mass_default = mass_default
|
|
128
|
+
else:
|
|
129
|
+
mass_default = np.asarray(mass_default)
|
|
130
|
+
assert mass_default.ndim == 1 and mass_default.shape[0] == simplextree.num_parameters
|
|
131
|
+
if zero_pad:
|
|
132
|
+
for i, _ in enumerate(grid_shape):
|
|
133
|
+
grid_shape[i] += 1 # adds a 0
|
|
134
|
+
for i,f in enumerate(grid_conversion):
|
|
135
|
+
grid_conversion[i] = np.concatenate([f, [mass_default[i]]])
|
|
136
|
+
assert len(grid_shape) == simplextree.num_parameters, "Grid shape size has to be the number of parameters."
|
|
137
|
+
grid_shape_with_degree = np.asarray(np.concatenate([[len(degrees)], grid_shape]), dtype=python_indices_type)
|
|
138
|
+
container_array = np.ascontiguousarray(np.zeros(grid_shape_with_degree, dtype=python_tensor_dtype).flatten())
|
|
139
|
+
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[])"
|
|
140
|
+
cdef intptr_t simplextree_ptr = simplextree.thisptr
|
|
141
|
+
cdef vector[indices_type] c_grid_shape = grid_shape_with_degree
|
|
142
|
+
cdef tensor_dtype[::1] container = container_array
|
|
143
|
+
cdef tensor_dtype* container_ptr = &container[0]
|
|
144
|
+
with nogil:
|
|
145
|
+
get_hilbert_surface_python(simplextree_ptr, container_ptr, c_grid_shape, degrees, mobius_inversion, zero_pad, n_jobs, expand_collapse)
|
|
146
|
+
out = (grid_conversion, container_array.reshape(grid_shape_with_degree))
|
|
147
|
+
if plot:
|
|
148
|
+
from multipers.plots import plot_surfaces
|
|
149
|
+
plot_surfaces(out)
|
|
150
|
+
return out
|
|
151
|
+
|
|
Binary file
|