multipers 2.2.3__cp310-cp310-win_amd64.whl → 2.3.1__cp310-cp310-win_amd64.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.

Files changed (182) hide show
  1. multipers/__init__.py +33 -31
  2. multipers/_signed_measure_meta.py +430 -430
  3. multipers/_slicer_meta.py +211 -212
  4. multipers/data/MOL2.py +458 -458
  5. multipers/data/UCR.py +18 -18
  6. multipers/data/graphs.py +466 -466
  7. multipers/data/immuno_regions.py +27 -27
  8. multipers/data/pytorch2simplextree.py +90 -90
  9. multipers/data/shape3d.py +101 -101
  10. multipers/data/synthetic.py +113 -111
  11. multipers/distances.py +198 -198
  12. multipers/filtration_conversions.pxd.tp +84 -84
  13. multipers/filtrations/__init__.py +18 -0
  14. multipers/{ml/convolutions.py → filtrations/density.py} +563 -520
  15. multipers/filtrations/filtrations.py +289 -0
  16. multipers/filtrations.pxd +224 -224
  17. multipers/function_rips.cp310-win_amd64.pyd +0 -0
  18. multipers/function_rips.pyx +105 -105
  19. multipers/grids.cp310-win_amd64.pyd +0 -0
  20. multipers/grids.pyx +350 -350
  21. multipers/gudhi/Persistence_slices_interface.h +132 -132
  22. multipers/gudhi/Simplex_tree_interface.h +239 -245
  23. multipers/gudhi/Simplex_tree_multi_interface.h +516 -561
  24. multipers/gudhi/cubical_to_boundary.h +59 -59
  25. multipers/gudhi/gudhi/Bitmap_cubical_complex.h +450 -450
  26. multipers/gudhi/gudhi/Bitmap_cubical_complex_base.h +1070 -1070
  27. multipers/gudhi/gudhi/Bitmap_cubical_complex_periodic_boundary_conditions_base.h +579 -579
  28. multipers/gudhi/gudhi/Debug_utils.h +45 -45
  29. multipers/gudhi/gudhi/Fields/Multi_field.h +484 -484
  30. multipers/gudhi/gudhi/Fields/Multi_field_operators.h +455 -455
  31. multipers/gudhi/gudhi/Fields/Multi_field_shared.h +450 -450
  32. multipers/gudhi/gudhi/Fields/Multi_field_small.h +531 -531
  33. multipers/gudhi/gudhi/Fields/Multi_field_small_operators.h +507 -507
  34. multipers/gudhi/gudhi/Fields/Multi_field_small_shared.h +531 -531
  35. multipers/gudhi/gudhi/Fields/Z2_field.h +355 -355
  36. multipers/gudhi/gudhi/Fields/Z2_field_operators.h +376 -376
  37. multipers/gudhi/gudhi/Fields/Zp_field.h +420 -420
  38. multipers/gudhi/gudhi/Fields/Zp_field_operators.h +400 -400
  39. multipers/gudhi/gudhi/Fields/Zp_field_shared.h +418 -418
  40. multipers/gudhi/gudhi/Flag_complex_edge_collapser.h +337 -337
  41. multipers/gudhi/gudhi/Matrix.h +2107 -2107
  42. multipers/gudhi/gudhi/Multi_critical_filtration.h +1038 -1038
  43. multipers/gudhi/gudhi/Multi_persistence/Box.h +171 -171
  44. multipers/gudhi/gudhi/Multi_persistence/Line.h +282 -282
  45. multipers/gudhi/gudhi/Off_reader.h +173 -173
  46. multipers/gudhi/gudhi/One_critical_filtration.h +1433 -1431
  47. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix.h +769 -769
  48. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix_with_column_compression.h +686 -686
  49. multipers/gudhi/gudhi/Persistence_matrix/Boundary_matrix.h +842 -842
  50. multipers/gudhi/gudhi/Persistence_matrix/Chain_matrix.h +1350 -1350
  51. multipers/gudhi/gudhi/Persistence_matrix/Id_to_index_overlay.h +1105 -1105
  52. multipers/gudhi/gudhi/Persistence_matrix/Position_to_index_overlay.h +859 -859
  53. multipers/gudhi/gudhi/Persistence_matrix/RU_matrix.h +910 -910
  54. multipers/gudhi/gudhi/Persistence_matrix/allocators/entry_constructors.h +139 -139
  55. multipers/gudhi/gudhi/Persistence_matrix/base_pairing.h +230 -230
  56. multipers/gudhi/gudhi/Persistence_matrix/base_swap.h +211 -211
  57. multipers/gudhi/gudhi/Persistence_matrix/boundary_cell_position_to_id_mapper.h +60 -60
  58. multipers/gudhi/gudhi/Persistence_matrix/boundary_face_position_to_id_mapper.h +60 -60
  59. multipers/gudhi/gudhi/Persistence_matrix/chain_pairing.h +136 -136
  60. multipers/gudhi/gudhi/Persistence_matrix/chain_rep_cycles.h +190 -190
  61. multipers/gudhi/gudhi/Persistence_matrix/chain_vine_swap.h +616 -616
  62. multipers/gudhi/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h +150 -150
  63. multipers/gudhi/gudhi/Persistence_matrix/columns/column_dimension_holder.h +106 -106
  64. multipers/gudhi/gudhi/Persistence_matrix/columns/column_utilities.h +219 -219
  65. multipers/gudhi/gudhi/Persistence_matrix/columns/entry_types.h +327 -327
  66. multipers/gudhi/gudhi/Persistence_matrix/columns/heap_column.h +1140 -1140
  67. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_list_column.h +934 -934
  68. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_set_column.h +934 -934
  69. multipers/gudhi/gudhi/Persistence_matrix/columns/list_column.h +980 -980
  70. multipers/gudhi/gudhi/Persistence_matrix/columns/naive_vector_column.h +1092 -1092
  71. multipers/gudhi/gudhi/Persistence_matrix/columns/row_access.h +192 -192
  72. multipers/gudhi/gudhi/Persistence_matrix/columns/set_column.h +921 -921
  73. multipers/gudhi/gudhi/Persistence_matrix/columns/small_vector_column.h +1093 -1093
  74. multipers/gudhi/gudhi/Persistence_matrix/columns/unordered_set_column.h +1012 -1012
  75. multipers/gudhi/gudhi/Persistence_matrix/columns/vector_column.h +1244 -1244
  76. multipers/gudhi/gudhi/Persistence_matrix/matrix_dimension_holders.h +186 -186
  77. multipers/gudhi/gudhi/Persistence_matrix/matrix_row_access.h +164 -164
  78. multipers/gudhi/gudhi/Persistence_matrix/ru_pairing.h +156 -156
  79. multipers/gudhi/gudhi/Persistence_matrix/ru_rep_cycles.h +376 -376
  80. multipers/gudhi/gudhi/Persistence_matrix/ru_vine_swap.h +540 -540
  81. multipers/gudhi/gudhi/Persistent_cohomology/Field_Zp.h +118 -118
  82. multipers/gudhi/gudhi/Persistent_cohomology/Multi_field.h +173 -173
  83. multipers/gudhi/gudhi/Persistent_cohomology/Persistent_cohomology_column.h +128 -128
  84. multipers/gudhi/gudhi/Persistent_cohomology.h +745 -745
  85. multipers/gudhi/gudhi/Points_off_io.h +171 -171
  86. multipers/gudhi/gudhi/Simple_object_pool.h +69 -69
  87. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_iterators.h +463 -463
  88. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h +83 -83
  89. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_siblings.h +106 -106
  90. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_star_simplex_iterators.h +277 -277
  91. multipers/gudhi/gudhi/Simplex_tree/hooks_simplex_base.h +62 -62
  92. multipers/gudhi/gudhi/Simplex_tree/indexing_tag.h +27 -27
  93. multipers/gudhi/gudhi/Simplex_tree/serialization_utils.h +62 -62
  94. multipers/gudhi/gudhi/Simplex_tree/simplex_tree_options.h +157 -157
  95. multipers/gudhi/gudhi/Simplex_tree.h +2794 -2794
  96. multipers/gudhi/gudhi/Simplex_tree_multi.h +152 -163
  97. multipers/gudhi/gudhi/distance_functions.h +62 -62
  98. multipers/gudhi/gudhi/graph_simplicial_complex.h +104 -104
  99. multipers/gudhi/gudhi/persistence_interval.h +253 -253
  100. multipers/gudhi/gudhi/persistence_matrix_options.h +170 -170
  101. multipers/gudhi/gudhi/reader_utils.h +367 -367
  102. multipers/gudhi/mma_interface_coh.h +256 -255
  103. multipers/gudhi/mma_interface_h0.h +223 -231
  104. multipers/gudhi/mma_interface_matrix.h +291 -282
  105. multipers/gudhi/naive_merge_tree.h +536 -575
  106. multipers/gudhi/scc_io.h +310 -289
  107. multipers/gudhi/truc.h +957 -888
  108. multipers/io.cp310-win_amd64.pyd +0 -0
  109. multipers/io.pyx +714 -711
  110. multipers/ml/accuracies.py +90 -90
  111. multipers/ml/invariants_with_persistable.py +79 -79
  112. multipers/ml/kernels.py +176 -176
  113. multipers/ml/mma.py +713 -714
  114. multipers/ml/one.py +472 -472
  115. multipers/ml/point_clouds.py +352 -346
  116. multipers/ml/signed_measures.py +1589 -1589
  117. multipers/ml/sliced_wasserstein.py +461 -461
  118. multipers/ml/tools.py +113 -113
  119. multipers/mma_structures.cp310-win_amd64.pyd +0 -0
  120. multipers/mma_structures.pxd +127 -127
  121. multipers/mma_structures.pyx +4 -8
  122. multipers/mma_structures.pyx.tp +1083 -1085
  123. multipers/multi_parameter_rank_invariant/diff_helpers.h +84 -93
  124. multipers/multi_parameter_rank_invariant/euler_characteristic.h +97 -97
  125. multipers/multi_parameter_rank_invariant/function_rips.h +322 -322
  126. multipers/multi_parameter_rank_invariant/hilbert_function.h +769 -769
  127. multipers/multi_parameter_rank_invariant/persistence_slices.h +148 -148
  128. multipers/multi_parameter_rank_invariant/rank_invariant.h +369 -369
  129. multipers/multiparameter_edge_collapse.py +41 -41
  130. multipers/multiparameter_module_approximation/approximation.h +2298 -2295
  131. multipers/multiparameter_module_approximation/combinatory.h +129 -129
  132. multipers/multiparameter_module_approximation/debug.h +107 -107
  133. multipers/multiparameter_module_approximation/format_python-cpp.h +286 -286
  134. multipers/multiparameter_module_approximation/heap_column.h +238 -238
  135. multipers/multiparameter_module_approximation/images.h +79 -79
  136. multipers/multiparameter_module_approximation/list_column.h +174 -174
  137. multipers/multiparameter_module_approximation/list_column_2.h +232 -232
  138. multipers/multiparameter_module_approximation/ru_matrix.h +347 -347
  139. multipers/multiparameter_module_approximation/set_column.h +135 -135
  140. multipers/multiparameter_module_approximation/structure_higher_dim_barcode.h +36 -36
  141. multipers/multiparameter_module_approximation/unordered_set_column.h +166 -166
  142. multipers/multiparameter_module_approximation/utilities.h +403 -419
  143. multipers/multiparameter_module_approximation/vector_column.h +223 -223
  144. multipers/multiparameter_module_approximation/vector_matrix.h +331 -331
  145. multipers/multiparameter_module_approximation/vineyards.h +464 -464
  146. multipers/multiparameter_module_approximation/vineyards_trajectories.h +649 -649
  147. multipers/multiparameter_module_approximation.cp310-win_amd64.pyd +0 -0
  148. multipers/multiparameter_module_approximation.pyx +218 -217
  149. multipers/pickle.py +90 -53
  150. multipers/plots.py +342 -334
  151. multipers/point_measure.cp310-win_amd64.pyd +0 -0
  152. multipers/point_measure.pyx +322 -320
  153. multipers/simplex_tree_multi.cp310-win_amd64.pyd +0 -0
  154. multipers/simplex_tree_multi.pxd +133 -133
  155. multipers/simplex_tree_multi.pyx +115 -48
  156. multipers/simplex_tree_multi.pyx.tp +1947 -1935
  157. multipers/slicer.cp310-win_amd64.pyd +0 -0
  158. multipers/slicer.pxd +301 -120
  159. multipers/slicer.pxd.tp +218 -214
  160. multipers/slicer.pyx +1570 -507
  161. multipers/slicer.pyx.tp +931 -914
  162. multipers/tensor/tensor.h +672 -672
  163. multipers/tensor.pxd +13 -13
  164. multipers/test.pyx +44 -44
  165. multipers/tests/__init__.py +57 -57
  166. multipers/torch/diff_grids.py +217 -217
  167. multipers/torch/rips_density.py +310 -304
  168. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/LICENSE +21 -21
  169. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/METADATA +21 -11
  170. multipers-2.3.1.dist-info/RECORD +182 -0
  171. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/WHEEL +1 -1
  172. multipers/tests/test_diff_helper.py +0 -73
  173. multipers/tests/test_hilbert_function.py +0 -82
  174. multipers/tests/test_mma.py +0 -83
  175. multipers/tests/test_point_clouds.py +0 -49
  176. multipers/tests/test_python-cpp_conversion.py +0 -82
  177. multipers/tests/test_signed_betti.py +0 -181
  178. multipers/tests/test_signed_measure.py +0 -89
  179. multipers/tests/test_simplextreemulti.py +0 -221
  180. multipers/tests/test_slicer.py +0 -221
  181. multipers-2.2.3.dist-info/RECORD +0 -189
  182. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/top_level.txt +0 -0
multipers/io.pyx CHANGED
@@ -1,711 +1,714 @@
1
- import re
2
- from gudhi import SimplexTree
3
- import multipers.slicer as mps
4
- import gudhi as gd
5
- import numpy as np
6
- import os
7
- from shutil import which
8
- from libcpp cimport bool
9
- from typing import Optional, Literal
10
- from collections import defaultdict
11
- import itertools
12
- import threading
13
- import cython
14
- cimport cython
15
-
16
- # from multipers.filtration_conversions cimport *
17
- # from multipers.mma_structures cimport boundary_matrix,float,pair,vector,intptr_t
18
- # cimport numpy as cnp
19
-
20
- doc_soft_urls = {
21
- "mpfree":"https://bitbucket.org/mkerber/mpfree/",
22
- "multi_chunk":"",
23
- "function_delaunay":"https://bitbucket.org/mkerber/function_delaunay/",
24
- "2pac":"https://gitlab.com/flenzen/2pac",
25
- }
26
- doc_soft_easy_install = {
27
- "mpfree":f"""
28
- ```sh
29
- git clone {doc_soft_urls["mpfree"]}
30
- cd mpfree
31
- sudo cp mpfree /usr/bin/
32
- cd ..
33
- rm -rf mpfree
34
- ```
35
- """,
36
- "multi_chunk":f"""
37
- ```sh
38
- git clone {doc_soft_urls["multi_chunk"]}
39
- cd multi_chunk
40
- sudo cp multi_chunk /usr/bin/
41
- cd ..
42
- rm -rf multi_chunk
43
- ```
44
- """,
45
- "function_delaunay":f"""
46
- ```sh
47
- git clone {doc_soft_urls["function_delaunay"]}
48
- cd function_delaunay
49
- sudo cp main /usr/bin/function_delaunay
50
- cd ..
51
- rm -rf function_delaunay
52
- ```
53
- """,
54
- "2pac":f"""
55
- ```sh
56
- git clone {doc_soft_urls["2pac"]} 2pac
57
- cd 2pac && mkdir build && cd build
58
- cmake ..
59
- make
60
- sudo cp 2pac /usr/bin
61
- ```
62
- """,
63
- }
64
- doc_soft_urls = defaultdict(lambda:"<Unknown url>", doc_soft_urls)
65
- doc_soft_easy_install = defaultdict(lambda:"<Unknown>", doc_soft_easy_install)
66
-
67
- available_reduce_softs = Literal["mpfree","multi_chunk","2pac"]
68
-
69
-
70
- def _path_init(soft:str|os.PathLike):
71
- a = which(f"./{soft}")
72
- b = which(f"{soft}")
73
- if a:
74
- pathes[soft] = a
75
- elif b:
76
- pathes[soft] = b
77
-
78
- if pathes[soft] is not None:
79
- verbose_arg = "> /dev/null 2>&1"
80
- test = os.system(pathes[soft] + " --help " + verbose_arg)
81
- if test:
82
- from warnings import warn
83
- warn(f"""
84
- Found external software {soft} at {pathes[soft]}
85
- but may not behave well.
86
- """)
87
-
88
-
89
-
90
- cdef dict[str,str|None] pathes = {
91
- "mpfree":None,
92
- "2pac":None,
93
- "function_delaunay":None,
94
- "multi_chunk":None,
95
- }
96
-
97
- # mpfree_in_path:str|os.PathLike = "multipers_mpfree_input.scc"
98
- # mpfree_out_path:str|os.PathLike = "multipers_mpfree_output.scc"
99
- # twopac_in_path:str|os.PathLike = "multipers_twopac_input.scc"
100
- # twopac_out_path:str|os.PathLike = "multipers_twopac_output.scc"
101
- # multi_chunk_in_path:str|os.PathLike = "multipers_multi_chunk_input.scc"
102
- # multi_chunk_out_path:str|os.PathLike = "multipers_multi_chunk_output.scc"
103
- # function_delaunay_out_path:str|os.PathLike = "function_delaunay_output.scc"
104
- # function_delaunay_in_path:str|os.PathLike = "function_delaunay_input.txt" # point cloud
105
- input_path:str|os.PathLike = "multipers_input.scc"
106
- output_path:str|os.PathLike = "multipers_output.scc"
107
-
108
-
109
-
110
- ## TODO : optimize with Python.h ?
111
- def scc_parser(path: str| os.PathLike):
112
- """
113
- Parse an scc file into the scc python format, aka blocks.
114
- """
115
- pass_line_regex = re.compile(r"^\s*$|^#|^scc2020$")
116
- def valid_line(line):
117
- return pass_line_regex.match(line) is None
118
- parse_line_regex = re.compile(r"^(?P<filtration>[^;]+);(?P<boundary>[^;]*)$")
119
- cdef tuple[tuple[str,str]] clines
120
- with open(path, "r") as f:
121
- lines =(x.strip() for x in f if valid_line(x))
122
- num_parameters = int(next(lines))
123
- sizes = np.cumsum(np.asarray([0] + next(lines).split(), dtype=np.int32))
124
- lines = (parse_line_regex.match(a) for a in lines)
125
- clines = tuple((a.group("filtration"),a.group("boundary")) for a in lines)
126
- # F = np.fromiter((a[0].split() for a in clines), dtype=np.dtype((np.float64,2)), count = sizes[-1])
127
- F = np.fromiter((np.fromstring(a[0], sep=r' ', dtype=np.float64) for a in clines), dtype=np.dtype((np.float64,num_parameters)), count = sizes[-1])
128
-
129
- # B = tuple(np.asarray(a[1].split(), dtype=np.int32) if len(a[1])>0 else np.empty(0, dtype=np.int32) for a in clines) ## TODO : this is very slow : optimize
130
- B = tuple(np.fromstring(a[1], sep=' ', dtype=np.int32) for a in clines)
131
- # block_lines = (tuple(get_bf(x, num_parameters) for x in lines[sizes[i]:sizes[i+1]]) for i in range(len(sizes)-1))
132
-
133
- # blocks = [(np.asarray([x[0] for x in b if len(x)>0], dtype=float),tuple(x[1] for x in b)) for b in block_lines]
134
- blocks = [(F[sizes[i]:sizes[i+1]], B[sizes[i]:sizes[i+1]]) for i in range(len(sizes)-1)]
135
-
136
- return blocks
137
-
138
-
139
- def scc_parser__old(path: str):
140
- """
141
- Parse an scc file into the scc python format, aka blocks.
142
- """
143
- with open(path, "r") as f:
144
- lines = f.readlines()
145
- # Find scc2020
146
- while lines[0].strip() != "scc2020":
147
- lines = lines[1:]
148
- lines = lines[1:]
149
- # stripped scc2020 we can start
150
-
151
- def pass_line(line):
152
- return re.match(r"^\s*$|^#", line) is not None
153
-
154
- for i, line in enumerate(lines):
155
- line = line.strip()
156
- if pass_line(line):
157
- continue
158
- num_parameters = int(line)
159
- lines = lines[i + 1 :]
160
- break
161
-
162
- block_sizes = []
163
-
164
- for i, line in enumerate(lines):
165
- line = line.strip()
166
- if pass_line(line):
167
- continue
168
- block_sizes = tuple(int(i) for i in line.split(" "))
169
- lines = lines[i + 1 :]
170
- break
171
- blocks = []
172
- cdef int counter
173
- for block_size in block_sizes:
174
- counter = block_size
175
- block_filtrations = []
176
- block_boundaries = []
177
- for i, line in enumerate(lines):
178
- if counter == 0:
179
- lines = lines[i:]
180
- break
181
- line = line.strip()
182
- if pass_line(line):
183
- continue
184
- splitted_line = re.match(r"^(?P<floats>[^;]+);(?P<ints>[^;]*)$", line)
185
- filtrations = np.asarray(splitted_line.group("floats").split(), dtype=float)
186
- boundary = np.asarray(splitted_line.group("ints").split(), dtype=int)
187
- block_filtrations.append(filtrations)
188
- block_boundaries.append(boundary)
189
- # filtration_boundary = line.split(";")
190
- # if len(filtration_boundary) == 1:
191
- # # happens when last generators do not have a ";" in the end
192
- # filtration_boundary.append(" ")
193
- # filtration, boundary = filtration_boundary
194
- # block_filtrations.append(
195
- # tuple(float(x) for x in filtration.split(" ") if len(x) > 0)
196
- # )
197
- # block_boundaries.append(tuple(int(x) for x in boundary.split(" ") if len(x) > 0))
198
- counter -= 1
199
- blocks.append((np.asarray(block_filtrations, dtype=float), tuple(block_boundaries)))
200
-
201
- return blocks
202
-
203
-
204
-
205
- def _put_temp_files_to_ram():
206
- global input_path,output_path
207
- shm_memory = "/tmp/" # on unix, we can write in RAM instead of disk.
208
- if os.access(shm_memory, os.W_OK) and not input_path.startswith(shm_memory):
209
- input_path = shm_memory + input_path
210
- output_path = shm_memory + output_path
211
-
212
- def _init_external_softwares(requires=[]):
213
- global pathes
214
- cdef bool any = False
215
- for soft,soft_path in pathes.items():
216
- if soft_path is None:
217
- _path_init(soft)
218
- any = any or (soft in requires)
219
-
220
- if any:
221
- _put_temp_files_to_ram()
222
- for soft in requires:
223
- if pathes[soft] is None:
224
- global doc_soft_urls
225
- raise ValueError(f"""
226
- Did not found {soft}.
227
- Install it from {doc_soft_urls[soft]}, and put it in your current directory,
228
- or in you $PATH.
229
- For instance:
230
- {doc_soft_easy_install[soft]}
231
- """)
232
- def _check_available(soft:str):
233
- _init_external_softwares()
234
- return pathes.get(soft,None) is not None
235
-
236
-
237
- def scc_reduce_from_str(
238
- path:str|os.PathLike,
239
- bool full_resolution=True,
240
- int dimension: int | np.int64 = 1,
241
- bool clear: bool = True,
242
- id: Optional[str] = None, # For parallel stuff
243
- bool verbose:bool=False,
244
- backend:Literal["mpfree","multi_chunk","twopac"]="mpfree"
245
- ):
246
- """
247
- Computes a minimal presentation of the file in path,
248
- using mpfree.
249
-
250
- path:PathLike
251
- full_resolution: bool
252
- dimension: int, presentation dimension to consider
253
- clear: bool, removes temporary files if True
254
- id: str, temporary files are of this id, allowing for multiprocessing
255
- verbose: bool
256
- backend: "mpfree", "multi_chunk" or "2pac"
257
- """
258
- global pathes, input_path, output_path
259
- if pathes[backend] is None:
260
- _init_external_softwares(requires=[backend])
261
-
262
-
263
- resolution_str = "--resolution" if full_resolution else ""
264
- # print(mpfree_in_path + id, mpfree_out_path + id)
265
- if id is None:
266
- id = str(threading.get_native_id())
267
- if not os.path.exists(path):
268
- raise ValueError(f"No file found at {path}.")
269
- if os.path.exists(output_path + id):
270
- os.remove(output_path + id)
271
- verbose_arg = "> /dev/null 2>&1" if not verbose else ""
272
- if backend == "mpfree":
273
- more_verbose = "-v" if verbose else ""
274
- command = (
275
- f"{pathes[backend]} {more_verbose} {resolution_str} --dim={dimension} {path} {output_path+id} {verbose_arg}"
276
- )
277
- elif backend == "multi_chunk":
278
- command = (
279
- f"{pathes[backend]} {path} {output_path+id} {verbose_arg}"
280
- )
281
- elif backend in ["twopac", "2pac"]:
282
- command = (
283
- f"{pathes[backend]} -f {path} --scc-input -n{dimension} --save-resolution-scc {output_path+id} {verbose_arg}"
284
- )
285
- else:
286
- raise ValueError(f"Unsupported backend {backend}.")
287
- if verbose:
288
- print(f"Calling :\n\n {command}")
289
- os.system(command)
290
-
291
- blocks = scc_parser(output_path + id)
292
- if clear:
293
- clear_io(input_path+id, output_path + id)
294
-
295
-
296
- ## mpfree workaround: last size is 0 but shouldn't...
297
- if len(blocks) and not len(blocks[-1][1]):
298
- blocks=blocks[:-1]
299
-
300
- return blocks
301
-
302
- def scc_reduce_from_str_to_slicer(
303
- path:str|os.PathLike,
304
- slicer,
305
- bool full_resolution=True,
306
- int dimension: int | np.int64 = 1,
307
- bool clear: bool = True,
308
- id: Optional[str] = None, # For parallel stuff
309
- bool verbose:bool=False,
310
- backend:Literal["mpfree","multi_chunk","twopac"]="mpfree",
311
- shift_dimension=0
312
- ):
313
- """
314
- Computes a minimal presentation of the file in path,
315
- using mpfree.
316
-
317
- path:PathLike
318
- slicer: empty slicer to fill
319
- full_resolution: bool
320
- dimension: int, presentation dimension to consider
321
- clear: bool, removes temporary files if True
322
- id: str, temporary files are of this id, allowing for multiprocessing
323
- verbose: bool
324
- backend: "mpfree", "multi_chunk" or "2pac"
325
- """
326
- global pathes, input_path, output_path
327
- if pathes[backend] is None:
328
- _init_external_softwares(requires=[backend])
329
-
330
-
331
- resolution_str = "--resolution" if full_resolution else ""
332
- # print(mpfree_in_path + id, mpfree_out_path + id)
333
- if id is None:
334
- id = str(threading.get_native_id())
335
- if not os.path.exists(path):
336
- raise ValueError(f"No file found at {path}.")
337
- if os.path.exists(output_path + id):
338
- os.remove(output_path + id)
339
- verbose_arg = "> /dev/null 2>&1" if not verbose else ""
340
- if backend == "mpfree":
341
- more_verbose = "-v" if verbose else ""
342
- command = (
343
- f"{pathes[backend]} {more_verbose} {resolution_str} --dim={dimension} {path} {output_path+id} {verbose_arg}"
344
- )
345
- elif backend == "multi_chunk":
346
- command = (
347
- f"{pathes[backend]} {path} {output_path+id} {verbose_arg}"
348
- )
349
- elif backend in ["twopac", "2pac"]:
350
- command = (
351
- f"{pathes[backend]} -f {path} --scc-input -n{dimension} --save-resolution-scc {output_path+id} {verbose_arg}"
352
- )
353
- else:
354
- raise ValueError(f"Unsupported backend {backend}.")
355
- if verbose:
356
- print(f"Calling :\n\n {command}")
357
- os.system(command)
358
-
359
- slicer._build_from_scc_file(path=output_path+id, shift_dimension=shift_dimension)
360
-
361
- if clear:
362
- clear_io(input_path+id, output_path + id)
363
-
364
-
365
- def reduce_complex(
366
- complex, # Simplextree, Slicer, or str
367
- bool full_resolution: bool = True,
368
- int dimension: int | np.int64 = 1,
369
- bool clear: bool = True,
370
- id: Optional[str]=None, # For parallel stuff
371
- bool verbose:bool=False,
372
- backend:available_reduce_softs="mpfree"
373
- ):
374
- """
375
- Computes a minimal presentation of the file in path,
376
- using `backend`.
377
-
378
- simplextree
379
- full_resolution: bool
380
- dimension: int, presentation dimension to consider
381
- clear: bool, removes temporary files if True
382
- id: str, temporary files are of this id, allowing for multiprocessing
383
- verbose: bool
384
- """
385
-
386
- from multipers.simplex_tree_multi import is_simplextree_multi
387
- if id is None:
388
- id = str(threading.get_native_id())
389
- path = input_path+id
390
- if is_simplextree_multi(complex):
391
- complex.to_scc(
392
- path=path,
393
- rivet_compatible=False,
394
- strip_comments=False,
395
- ignore_last_generators=False,
396
- overwrite=True,
397
- reverse_block=False,
398
- )
399
- dimension = complex.dimension - dimension
400
- elif isinstance(complex,str):
401
- path = complex
402
- elif isinstance(complex, list) or isinstance(complex, tuple):
403
- scc2disk(complex,path=path)
404
- else:
405
- # Assumes its a slicer
406
- blocks = mps.slicer2blocks(complex)
407
- scc2disk(blocks,path=path)
408
- dimension = len(blocks) -2 -dimension
409
-
410
- return scc_reduce_from_str(
411
- path=path,
412
- full_resolution=full_resolution,
413
- dimension=dimension,
414
- clear=clear,
415
- id=id,
416
- verbose=verbose,
417
- backend=backend
418
- )
419
-
420
-
421
-
422
-
423
- def function_delaunay_presentation(
424
- point_cloud:np.ndarray,
425
- function_values:np.ndarray,
426
- id:Optional[str] = None,
427
- bool clear:bool = True,
428
- bool verbose:bool=False,
429
- int degree = -1,
430
- bool multi_chunk = False,
431
- ):
432
- """
433
- Computes a function delaunay presentation, and returns it as blocks.
434
-
435
- points : (num_pts, n) float array
436
- grades : (num_pts,) float array
437
- degree (opt) : if given, computes a minimal presentation of this homological degree first
438
- clear:bool, removes temporary files if true
439
- degree: computes minimal presentation of this degree if given
440
- verbose : bool
441
- """
442
- if id is None:
443
- id = str(threading.get_native_id())
444
- global input_path, output_path, pathes
445
- backend = "function_delaunay"
446
- if pathes[backend] is None :
447
- _init_external_softwares(requires=[backend])
448
-
449
- to_write = np.concatenate([point_cloud, function_values.reshape(-1,1)], axis=1)
450
- np.savetxt(input_path+id,to_write,delimiter=' ')
451
- verbose_arg = "> /dev/null 2>&1" if not verbose else ""
452
- degree_arg = f"--minpres {degree}" if degree >= 0 else ""
453
- multi_chunk_arg = "--multi-chunk" if multi_chunk else ""
454
- if os.path.exists(output_path + id):
455
- os.remove(output_path+ id)
456
- command = f"{pathes[backend]} {degree_arg} {multi_chunk_arg} {input_path+id} {output_path+id} {verbose_arg} --no-delaunay-compare"
457
- if verbose:
458
- print(command)
459
- os.system(command)
460
-
461
- blocks = scc_parser(output_path + id)
462
- if clear:
463
- clear_io(output_path + id, input_path + id)
464
- ## Function Delaunay workaround: last size is 0 but shouldn't...
465
- if degree<0 and len(blocks) and not len(blocks[-1][1]):
466
- blocks=blocks[:-1]
467
-
468
- return blocks
469
-
470
- def function_delaunay_presentation_to_slicer(
471
- slicer,
472
- point_cloud:np.ndarray,
473
- function_values:np.ndarray,
474
- id:Optional[str] = None,
475
- bool clear:bool = True,
476
- bool verbose:bool=False,
477
- int degree = -1,
478
- bool multi_chunk = False,
479
- ):
480
- """
481
- Computes a function delaunay presentation, and returns it as a slicer.
482
-
483
- slicer: empty slicer to fill
484
- points : (num_pts, n) float array
485
- grades : (num_pts,) float array
486
- degree (opt) : if given, computes a minimal presentation of this homological degree first
487
- clear:bool, removes temporary files if true
488
- degree: computes minimal presentation of this degree if given
489
- verbose : bool
490
- """
491
- if id is None:
492
- id = str(threading.get_native_id())
493
- global input_path, output_path, pathes
494
- backend = "function_delaunay"
495
- if pathes[backend] is None :
496
- _init_external_softwares(requires=[backend])
497
-
498
- to_write = np.concatenate([point_cloud, function_values.reshape(-1,1)], axis=1)
499
- np.savetxt(input_path+id,to_write,delimiter=' ')
500
- verbose_arg = "> /dev/null 2>&1" if not verbose else ""
501
- degree_arg = f"--minpres {degree}" if degree >= 0 else ""
502
- multi_chunk_arg = "--multi-chunk" if multi_chunk else ""
503
- if os.path.exists(output_path + id):
504
- os.remove(output_path+ id)
505
- command = f"{pathes[backend]} {degree_arg} {multi_chunk_arg} {input_path+id} {output_path+id} {verbose_arg} --no-delaunay-compare"
506
- if verbose:
507
- print(command)
508
- os.system(command)
509
-
510
- slicer._build_from_scc_file(path=output_path+id, shift_dimension=-1 if degree <= 0 else degree-1 )
511
-
512
- if clear:
513
- clear_io(output_path + id, input_path + id)
514
-
515
-
516
-
517
- def clear_io(*args):
518
- """Removes temporary files"""
519
- global input_path,output_path
520
- for x in [input_path,output_path] + list(args):
521
- if os.path.exists(x):
522
- os.remove(x)
523
-
524
-
525
-
526
-
527
-
528
-
529
- # cdef extern from "multiparameter_module_approximation/format_python-cpp.h" namespace "Gudhi::multiparameter::mma":
530
- # pair[boundary_matrix, vector[One_critical_filtration[double]]] simplextree_to_boundary_filtration(intptr_t)
531
- # vector[pair[ vector[vector[float]],boundary_matrix]] simplextree_to_scc(intptr_t)
532
- # vector[pair[ vector[vector[vector[float]]],boundary_matrix]] function_simplextree_to_scc(intptr_t)
533
- # pair[vector[vector[float]],boundary_matrix ] simplextree_to_ordered_bf(intptr_t)
534
-
535
- # def simplex_tree2boundary_filtrations(simplextree:SimplexTreeMulti | SimplexTree):
536
- # """Computes a (sparse) boundary matrix, with associated filtration. Can be used as an input of approx afterwards.
537
- #
538
- # Parameters
539
- # ----------
540
- # simplextree: Gudhi or mma simplextree
541
- # The simplextree defining the filtration to convert to boundary-filtration.
542
- #
543
- # Returns
544
- # -------
545
- # B:List of lists of ints
546
- # The boundary matrix.
547
- # F: List of 1D filtration
548
- # The filtrations aligned with B; the i-th simplex of this simplextree has boundary B[i] and filtration(s) F[i].
549
- #
550
- # """
551
- # cdef intptr_t cptr
552
- # if isinstance(simplextree, SimplexTreeMulti):
553
- # cptr = simplextree.thisptr
554
- # elif isinstance(simplextree, SimplexTree):
555
- # temp_st = gd.SimplexTreeMulti(simplextree, parameters=1)
556
- # cptr = temp_st.thisptr
557
- # else:
558
- # raise TypeError("Has to be a simplextree")
559
- # cdef pair[boundary_matrix, vector[One_critical_filtration[double]]] cboundary_filtration = simplextree_to_boundary_filtration(cptr)
560
- # boundary = cboundary_filtration.first
561
- # # multi_filtrations = np.array(<vector[vector[float]]>One_critical_filtration.to_python(cboundary_filtration.second))
562
- # cdef cnp.ndarray[double, ndim=2] multi_filtrations = _fmf2numpy_f64(cboundary_filtration.second)
563
- # return boundary, multi_filtrations
564
-
565
- # def simplextree2scc(simplextree:SimplexTreeMulti | SimplexTree, filtration_dtype=np.float32, bool flattened=False):
566
- # """
567
- # Turns a simplextree into a (simplicial) module presentation.
568
- # """
569
- # cdef intptr_t cptr
570
- # cdef bool is_function_st = False
571
- # if isinstance(simplextree, SimplexTreeMulti):
572
- # cptr = simplextree.thisptr
573
- # is_function_st = simplextree._is_function_simplextree
574
- # elif isinstance(simplextree, SimplexTree):
575
- # temp_st = gd.SimplexTreeMulti(simplextree, parameters=1)
576
- # cptr = temp_st.thisptr
577
- # else:
578
- # raise TypeError("Has to be a simplextree")
579
- #
580
- # cdef pair[vector[vector[float]], boundary_matrix] out
581
- # if flattened:
582
- # out = simplextree_to_ordered_bf(cptr)
583
- # return np.asarray(out.first,dtype=filtration_dtype), tuple(out.second)
584
- #
585
- # if is_function_st:
586
- # blocks = function_simplextree_to_scc(cptr)
587
- # else:
588
- # blocks = simplextree_to_scc(cptr)
589
- # # reduces the space in memory
590
- # if is_function_st:
591
- # blocks = [(tuple(f), tuple(b)) for f,b in blocks[::-1]]
592
- # else:
593
- # blocks = [(np.asarray(f,dtype=filtration_dtype), tuple(b)) for f,b in blocks[::-1]] ## presentation is on the other order
594
- # return blocks+[(np.empty(0,dtype=filtration_dtype),[])]
595
-
596
- @cython.boundscheck(False)
597
- @cython.wraparound(False)
598
- def scc2disk(
599
- stuff,
600
- path:str|os.PathLike,
601
- int num_parameters = -1,
602
- bool reverse_block = False,
603
- bool rivet_compatible = False,
604
- bool ignore_last_generators = False,
605
- bool strip_comments = False,
606
- ):
607
- """
608
- Writes a scc python format / blocks into a file.
609
- """
610
- if num_parameters == -1:
611
- for block in stuff:
612
- if len(block[0]) == 0:
613
- continue
614
- num_gens, num_parameters_= np.asarray(block[0]).shape
615
- num_parameters = num_parameters_
616
- break
617
- assert num_parameters > 0, f"Invalid number of parameters {num_parameters}"
618
-
619
- if reverse_block: stuff.reverse()
620
- with open(path, "w") as f:
621
- f.write("scc2020\n") if not rivet_compatible else f.write("firep\n")
622
- if not strip_comments and not rivet_compatible: f.write("# Number of parameters\n")
623
- if rivet_compatible:
624
- assert num_parameters == 2
625
- f.write("Filtration 1\n")
626
- f.write("Filtration 2\n")
627
- else:
628
- f.write(f"{num_parameters}\n")
629
-
630
- if not strip_comments: f.write("# Sizes of generating sets\n")
631
- for block in stuff: f.write(f"{len(block[0])} ")
632
- f.write("\n")
633
- for i,block in enumerate(stuff):
634
- if (rivet_compatible or ignore_last_generators) and i == len(stuff)-1: continue
635
- if not strip_comments: f.write(f"# Block of dimension {len(stuff)-1-i}\n")
636
- filtration, boundary = block
637
- filtration = np.asarray(filtration).astype(str)
638
- # boundary = tuple(x.astype(str) for x in boundary)
639
- f.write(" ".join(itertools.chain.from_iterable(
640
- ((*(f.tolist()), ";", *(np.asarray(b).astype(str).tolist()), "\n")
641
- for f,b in zip(filtration, boundary))
642
- )
643
- ))
644
- # for j in range(<int>len(filtration)):
645
- # line = " ".join((
646
- # *filtration[j],
647
- # ";",
648
- # *boundary[j],
649
- # "\n",
650
- # ))
651
- # f.write(line)
652
-
653
- def scc2disk_old(
654
- stuff,
655
- path:str|os.PathLike,
656
- num_parameters = -1,
657
- reverse_block = False,
658
- rivet_compatible = False,
659
- ignore_last_generators = False,
660
- strip_comments = False,
661
- ):
662
- """
663
- Writes a scc python format / blocks into a file.
664
- """
665
- if num_parameters == -1:
666
- for block in stuff:
667
- if len(block[0]) == 0:
668
- continue
669
- num_gens, num_parameters_= np.asarray(block[0]).shape
670
- num_parameters = num_parameters_
671
- break
672
- assert num_parameters > 0, f"Invalid number of parameters {num_parameters}"
673
-
674
- if reverse_block: stuff.reverse()
675
- out = []
676
- if rivet_compatible:
677
- out.append(r"firep")
678
- else:
679
- out.append(r"scc2020")
680
- if not strip_comments and not rivet_compatible:
681
- out.append(r"# Number of parameters")
682
- if rivet_compatible:
683
- out.append("Filtration 1")
684
- out.append("Filtration 2\n")
685
- else:
686
- out.append(f"{num_parameters}")
687
-
688
- if not strip_comments:
689
- out.append("# Sizes of generating sets")
690
-
691
- # for block in stuff:
692
- # f.write(f"{len(block[0])} ")
693
- out.append(" ".join(str(len(block[0])) for block in stuff))
694
- str_blocks = [out]
695
- for i,block in enumerate(stuff):
696
- if (rivet_compatible or ignore_last_generators) and i == len(stuff)-1: continue
697
- if not strip_comments:
698
- str_blocks.append([f"# Block of dimension {len(stuff)-1-i}"])
699
- filtration, boundary = block
700
- if len(filtration) == 0:
701
- continue
702
- filtration = filtration.astype(str)
703
- C = filtration[:,0]
704
- for i in range(1,filtration.shape[1]):
705
- C = np.char.add(C," ")
706
- C = np.char.add(C,filtration[:,i])
707
- C = np.char.add(C, ";")
708
- D = np.fromiter((" ".join(b.astype(str).tolist()) for b in boundary), dtype="<U11") #int32-> str is "<U11" #check np.array(1, dtype=np.int32).astype(str)
709
- str_blocks.append(np.char.add(C,D))
710
-
711
- np.savetxt("test.scc", np.concatenate(str_blocks), delimiter="", fmt="%s")
1
+ import re
2
+ from gudhi import SimplexTree
3
+ import gudhi as gd
4
+ import numpy as np
5
+ import os
6
+ from shutil import which
7
+ from libcpp cimport bool
8
+ from typing import Optional, Literal
9
+ from collections import defaultdict
10
+ import itertools
11
+ import threading
12
+ import cython
13
+ cimport cython
14
+
15
+ # from multipers.filtration_conversions cimport *
16
+ # from multipers.mma_structures cimport boundary_matrix,float,pair,vector,intptr_t
17
+ # cimport numpy as cnp
18
+ current_doc_url = "https://davidlapous.github.io/multipers/"
19
+ doc_soft_urls = {
20
+ "mpfree":"https://bitbucket.org/mkerber/mpfree/",
21
+ "multi_chunk":"",
22
+ "function_delaunay":"https://bitbucket.org/mkerber/function_delaunay/",
23
+ "2pac":"https://gitlab.com/flenzen/2pac",
24
+ }
25
+ doc_soft_easy_install = {
26
+ "mpfree":f"""
27
+ ```sh
28
+ git clone {doc_soft_urls["mpfree"]}
29
+ cd mpfree
30
+ cmake . --fresh
31
+ make
32
+ sudo cp mpfree /usr/bin/
33
+ cd ..
34
+ rm -rf mpfree
35
+ ```
36
+ """,
37
+ "multi_chunk":f"""
38
+ ```sh
39
+ git clone {doc_soft_urls["multi_chunk"]}
40
+ cd multi_chunk
41
+ cmake . --fresh
42
+ make
43
+ sudo cp multi_chunk /usr/bin/
44
+ cd ..
45
+ rm -rf multi_chunk
46
+ ```
47
+ """,
48
+ "function_delaunay":f"""
49
+ ```sh
50
+ git clone {doc_soft_urls["function_delaunay"]}
51
+ cd function_delaunay
52
+ cmake . --fresh
53
+ make
54
+ sudo cp main /usr/bin/function_delaunay
55
+ cd ..
56
+ rm -rf function_delaunay
57
+ ```
58
+ """,
59
+ "2pac":f"""
60
+ ```sh
61
+ git clone {doc_soft_urls["2pac"]} 2pac
62
+ cd 2pac && mkdir build && cd build
63
+ cmake ..
64
+ make
65
+ sudo cp 2pac /usr/bin
66
+ ```
67
+ """,
68
+ }
69
+ doc_soft_urls = defaultdict(lambda:"<Unknown url>", doc_soft_urls)
70
+ doc_soft_easy_install = defaultdict(lambda:"<Unknown>", doc_soft_easy_install)
71
+
72
+ available_reduce_softs = Literal["mpfree","multi_chunk","2pac"]
73
+
74
+ def _path_init(soft:str|os.PathLike):
75
+ a = which(f"./{soft}")
76
+ b = which(f"{soft}")
77
+ if a:
78
+ pathes[soft] = a
79
+ elif b:
80
+ pathes[soft] = b
81
+
82
+ if pathes[soft] is not None:
83
+ verbose_arg = "> /dev/null 2>&1"
84
+ test = os.system(pathes[soft] + " --help " + verbose_arg)
85
+ if test:
86
+ from warnings import warn
87
+ warn(f"""
88
+ Found external software {soft} at {pathes[soft]}
89
+ but may not behave well.
90
+ """)
91
+
92
+
93
+
94
+ cdef dict[str,str|None] pathes = {
95
+ "mpfree":None,
96
+ "2pac":None,
97
+ "function_delaunay":None,
98
+ "multi_chunk":None,
99
+ }
100
+
101
+ # mpfree_in_path:str|os.PathLike = "multipers_mpfree_input.scc"
102
+ # mpfree_out_path:str|os.PathLike = "multipers_mpfree_output.scc"
103
+ # twopac_in_path:str|os.PathLike = "multipers_twopac_input.scc"
104
+ # twopac_out_path:str|os.PathLike = "multipers_twopac_output.scc"
105
+ # multi_chunk_in_path:str|os.PathLike = "multipers_multi_chunk_input.scc"
106
+ # multi_chunk_out_path:str|os.PathLike = "multipers_multi_chunk_output.scc"
107
+ # function_delaunay_out_path:str|os.PathLike = "function_delaunay_output.scc"
108
+ # function_delaunay_in_path:str|os.PathLike = "function_delaunay_input.txt" # point cloud
109
+ input_path:str|os.PathLike = "multipers_input.scc"
110
+ output_path:str|os.PathLike = "multipers_output.scc"
111
+
112
+
113
+
114
+ ## TODO : optimize with Python.h ?
115
+ def scc_parser(path: str| os.PathLike):
116
+ """
117
+ Parse an scc file into the scc python format, aka blocks.
118
+ """
119
+ pass_line_regex = re.compile(r"^\s*$|^#|^scc2020$")
120
+ def valid_line(line):
121
+ return pass_line_regex.match(line) is None
122
+ parse_line_regex = re.compile(r"^(?P<filtration>[^;]+);(?P<boundary>[^;]*)$")
123
+ cdef tuple[tuple[str,str]] clines
124
+ with open(path, "r") as f:
125
+ lines =(x.strip() for x in f if valid_line(x))
126
+ num_parameters = int(next(lines))
127
+ sizes = np.cumsum(np.asarray([0] + next(lines).split(), dtype=np.int32))
128
+ lines = (parse_line_regex.match(a) for a in lines)
129
+ clines = tuple((a.group("filtration"),a.group("boundary")) for a in lines)
130
+ # F = np.fromiter((a[0].split() for a in clines), dtype=np.dtype((np.float64,2)), count = sizes[-1])
131
+ F = np.fromiter((np.fromstring(a[0], sep=r' ', dtype=np.float64) for a in clines), dtype=np.dtype((np.float64,num_parameters)), count = sizes[-1])
132
+
133
+ # B = tuple(np.asarray(a[1].split(), dtype=np.int32) if len(a[1])>0 else np.empty(0, dtype=np.int32) for a in clines) ## TODO : this is very slow : optimize
134
+ B = tuple(np.fromstring(a[1], sep=' ', dtype=np.int32) for a in clines)
135
+ # block_lines = (tuple(get_bf(x, num_parameters) for x in lines[sizes[i]:sizes[i+1]]) for i in range(len(sizes)-1))
136
+
137
+ # blocks = [(np.asarray([x[0] for x in b if len(x)>0], dtype=float),tuple(x[1] for x in b)) for b in block_lines]
138
+ blocks = [(F[sizes[i]:sizes[i+1]], B[sizes[i]:sizes[i+1]]) for i in range(len(sizes)-1)]
139
+
140
+ return blocks
141
+
142
+
143
+ def scc_parser__old(path: str):
144
+ """
145
+ Parse an scc file into the scc python format, aka blocks.
146
+ """
147
+ with open(path, "r") as f:
148
+ lines = f.readlines()
149
+ # Find scc2020
150
+ while lines[0].strip() != "scc2020":
151
+ lines = lines[1:]
152
+ lines = lines[1:]
153
+ # stripped scc2020 we can start
154
+
155
+ def pass_line(line):
156
+ return re.match(r"^\s*$|^#", line) is not None
157
+
158
+ for i, line in enumerate(lines):
159
+ line = line.strip()
160
+ if pass_line(line):
161
+ continue
162
+ num_parameters = int(line)
163
+ lines = lines[i + 1 :]
164
+ break
165
+
166
+ block_sizes = []
167
+
168
+ for i, line in enumerate(lines):
169
+ line = line.strip()
170
+ if pass_line(line):
171
+ continue
172
+ block_sizes = tuple(int(i) for i in line.split(" "))
173
+ lines = lines[i + 1 :]
174
+ break
175
+ blocks = []
176
+ cdef int counter
177
+ for block_size in block_sizes:
178
+ counter = block_size
179
+ block_filtrations = []
180
+ block_boundaries = []
181
+ for i, line in enumerate(lines):
182
+ if counter == 0:
183
+ lines = lines[i:]
184
+ break
185
+ line = line.strip()
186
+ if pass_line(line):
187
+ continue
188
+ splitted_line = re.match(r"^(?P<floats>[^;]+);(?P<ints>[^;]*)$", line)
189
+ filtrations = np.asarray(splitted_line.group("floats").split(), dtype=float)
190
+ boundary = np.asarray(splitted_line.group("ints").split(), dtype=int)
191
+ block_filtrations.append(filtrations)
192
+ block_boundaries.append(boundary)
193
+ # filtration_boundary = line.split(";")
194
+ # if len(filtration_boundary) == 1:
195
+ # # happens when last generators do not have a ";" in the end
196
+ # filtration_boundary.append(" ")
197
+ # filtration, boundary = filtration_boundary
198
+ # block_filtrations.append(
199
+ # tuple(float(x) for x in filtration.split(" ") if len(x) > 0)
200
+ # )
201
+ # block_boundaries.append(tuple(int(x) for x in boundary.split(" ") if len(x) > 0))
202
+ counter -= 1
203
+ blocks.append((np.asarray(block_filtrations, dtype=float), tuple(block_boundaries)))
204
+
205
+ return blocks
206
+
207
+
208
+
209
+ def _put_temp_files_to_ram():
210
+ global input_path,output_path
211
+ shm_memory = "/tmp/" # on unix, we can write in RAM instead of disk.
212
+ if os.access(shm_memory, os.W_OK) and not input_path.startswith(shm_memory):
213
+ input_path = shm_memory + input_path
214
+ output_path = shm_memory + output_path
215
+
216
+ def _init_external_softwares(requires=[]):
217
+ global pathes
218
+ cdef bool any = False
219
+ for soft,soft_path in pathes.items():
220
+ if soft_path is None:
221
+ _path_init(soft)
222
+ any = any or (soft in requires)
223
+
224
+ if any:
225
+ _put_temp_files_to_ram()
226
+ for soft in requires:
227
+ if pathes[soft] is None:
228
+ global doc_soft_urls
229
+ raise ValueError(f"""
230
+ Did not find {soft}.
231
+ Install it from {doc_soft_urls[soft]}, and put it in your current directory,
232
+ or in you $PATH.
233
+ Documentation is available here: {current_doc_url}compilation.html#external-libraries
234
+ For instance:
235
+ {doc_soft_easy_install[soft]}
236
+ """)
237
+ _init_external_softwares()
238
+ def _check_available(soft:str):
239
+ _init_external_softwares()
240
+ return pathes.get(soft,None) is not None
241
+
242
+
243
+ def scc_reduce_from_str(
244
+ path:str|os.PathLike,
245
+ bool full_resolution=True,
246
+ int dimension: int | np.int64 = 1,
247
+ bool clear: bool = True,
248
+ id: Optional[str] = None, # For parallel stuff
249
+ bool verbose:bool=False,
250
+ backend:Literal["mpfree","multi_chunk","twopac"]="mpfree"
251
+ ):
252
+ """
253
+ Computes a minimal presentation of the file in path,
254
+ using mpfree.
255
+
256
+ path:PathLike
257
+ full_resolution: bool
258
+ dimension: int, presentation dimension to consider
259
+ clear: bool, removes temporary files if True
260
+ id: str, temporary files are of this id, allowing for multiprocessing
261
+ verbose: bool
262
+ backend: "mpfree", "multi_chunk" or "2pac"
263
+ """
264
+ global pathes, input_path, output_path
265
+ assert _check_available(backend), f"Backend {backend} is not available."
266
+
267
+
268
+ resolution_str = "--resolution" if full_resolution else ""
269
+ # print(mpfree_in_path + id, mpfree_out_path + id)
270
+ if id is None:
271
+ id = str(threading.get_native_id())
272
+ if not os.path.exists(path):
273
+ raise ValueError(f"No file found at {path}.")
274
+ if os.path.exists(output_path + id):
275
+ os.remove(output_path + id)
276
+ verbose_arg = "> /dev/null 2>&1" if not verbose else ""
277
+ if backend == "mpfree":
278
+ more_verbose = "-v" if verbose else ""
279
+ command = (
280
+ f"{pathes[backend]} {more_verbose} {resolution_str} --dim={dimension} {path} {output_path+id} {verbose_arg}"
281
+ )
282
+ elif backend == "multi_chunk":
283
+ command = (
284
+ f"{pathes[backend]} {path} {output_path+id} {verbose_arg}"
285
+ )
286
+ elif backend in ["twopac", "2pac"]:
287
+ command = (
288
+ f"{pathes[backend]} -f {path} --scc-input -n{dimension} --save-resolution-scc {output_path+id} {verbose_arg}"
289
+ )
290
+ else:
291
+ raise ValueError(f"Unsupported backend {backend}.")
292
+ if verbose:
293
+ print(f"Calling :\n\n {command}")
294
+ os.system(command)
295
+
296
+ blocks = scc_parser(output_path + id)
297
+ if clear:
298
+ clear_io(input_path+id, output_path + id)
299
+
300
+
301
+ ## mpfree workaround: last size is 0 but shouldn't...
302
+ if len(blocks) and not len(blocks[-1][1]):
303
+ blocks=blocks[:-1]
304
+
305
+ return blocks
306
+
307
+ def scc_reduce_from_str_to_slicer(
308
+ path:str|os.PathLike,
309
+ slicer,
310
+ bool full_resolution=True,
311
+ int dimension: int | np.int64 = 1,
312
+ bool clear: bool = True,
313
+ id: Optional[str] = None, # For parallel stuff
314
+ bool verbose:bool=False,
315
+ backend:Literal["mpfree","multi_chunk","twopac"]="mpfree",
316
+ shift_dimension=0
317
+ ):
318
+ """
319
+ Computes a minimal presentation of the file in path,
320
+ using mpfree.
321
+
322
+ path:PathLike
323
+ slicer: empty slicer to fill
324
+ full_resolution: bool
325
+ dimension: int, presentation dimension to consider
326
+ clear: bool, removes temporary files if True
327
+ id: str, temporary files are of this id, allowing for multiprocessing
328
+ verbose: bool
329
+ backend: "mpfree", "multi_chunk" or "2pac"
330
+ """
331
+ global pathes, input_path, output_path
332
+ assert _check_available(backend), f"Backend {backend} is not available."
333
+
334
+
335
+ resolution_str = "--resolution" if full_resolution else ""
336
+ # print(mpfree_in_path + id, mpfree_out_path + id)
337
+ if id is None:
338
+ id = str(threading.get_native_id())
339
+ if not os.path.exists(path):
340
+ raise ValueError(f"No file found at {path}.")
341
+ if os.path.exists(output_path + id):
342
+ os.remove(output_path + id)
343
+ verbose_arg = "> /dev/null 2>&1" if not verbose else ""
344
+ if backend == "mpfree":
345
+ more_verbose = "-v" if verbose else ""
346
+ command = (
347
+ f"{pathes[backend]} {more_verbose} {resolution_str} --dim={dimension} {path} {output_path+id} {verbose_arg}"
348
+ )
349
+ elif backend == "multi_chunk":
350
+ command = (
351
+ f"{pathes[backend]} {path} {output_path+id} {verbose_arg}"
352
+ )
353
+ elif backend in ["twopac", "2pac"]:
354
+ command = (
355
+ f"{pathes[backend]} -f {path} --scc-input -n{dimension} --save-resolution-scc {output_path+id} {verbose_arg}"
356
+ )
357
+ else:
358
+ raise ValueError(f"Unsupported backend {backend}.")
359
+ if verbose:
360
+ print(f"Calling :\n\n {command}")
361
+ os.system(command)
362
+
363
+ slicer._build_from_scc_file(path=output_path+id, shift_dimension=shift_dimension)
364
+
365
+ if clear:
366
+ clear_io(input_path+id, output_path + id)
367
+
368
+
369
+ def reduce_complex(
370
+ complex, # Simplextree, Slicer, or str
371
+ bool full_resolution: bool = True,
372
+ int dimension: int | np.int64 = 1,
373
+ bool clear: bool = True,
374
+ id: Optional[str]=None, # For parallel stuff
375
+ bool verbose:bool=False,
376
+ backend:available_reduce_softs="mpfree"
377
+ ):
378
+ """
379
+ Computes a minimal presentation of the file in path,
380
+ using `backend`.
381
+
382
+ simplextree
383
+ full_resolution: bool
384
+ dimension: int, presentation dimension to consider
385
+ clear: bool, removes temporary files if True
386
+ id: str, temporary files are of this id, allowing for multiprocessing
387
+ verbose: bool
388
+ """
389
+
390
+ from multipers.simplex_tree_multi import is_simplextree_multi
391
+ from multipers.slicer import slicer2blocks
392
+ if id is None:
393
+ id = str(threading.get_native_id())
394
+ path = input_path+id
395
+ if is_simplextree_multi(complex):
396
+ complex.to_scc(
397
+ path=path,
398
+ rivet_compatible=False,
399
+ strip_comments=False,
400
+ ignore_last_generators=False,
401
+ overwrite=True,
402
+ reverse_block=False,
403
+ )
404
+ dimension = complex.dimension - dimension
405
+ elif isinstance(complex,str):
406
+ path = complex
407
+ elif isinstance(complex, list) or isinstance(complex, tuple):
408
+ scc2disk(complex,path=path)
409
+ else:
410
+ # Assumes its a slicer
411
+ blocks = slicer2blocks(complex)
412
+ scc2disk(blocks,path=path)
413
+ dimension = len(blocks) -2 -dimension
414
+
415
+ return scc_reduce_from_str(
416
+ path=path,
417
+ full_resolution=full_resolution,
418
+ dimension=dimension,
419
+ clear=clear,
420
+ id=id,
421
+ verbose=verbose,
422
+ backend=backend
423
+ )
424
+
425
+
426
+
427
+
428
+ def function_delaunay_presentation(
429
+ point_cloud:np.ndarray,
430
+ function_values:np.ndarray,
431
+ id:Optional[str] = None,
432
+ bool clear:bool = True,
433
+ bool verbose:bool=False,
434
+ int degree = -1,
435
+ bool multi_chunk = False,
436
+ ):
437
+ """
438
+ Computes a function delaunay presentation, and returns it as blocks.
439
+
440
+ points : (num_pts, n) float array
441
+ grades : (num_pts,) float array
442
+ degree (opt) : if given, computes a minimal presentation of this homological degree first
443
+ clear:bool, removes temporary files if true
444
+ degree: computes minimal presentation of this degree if given
445
+ verbose : bool
446
+ """
447
+ if id is None:
448
+ id = str(threading.get_native_id())
449
+ global input_path, output_path, pathes
450
+ backend = "function_delaunay"
451
+ assert _check_available(backend), f"Backend {backend} is not available."
452
+
453
+ to_write = np.concatenate([point_cloud, function_values.reshape(-1,1)], axis=1)
454
+ np.savetxt(input_path+id,to_write,delimiter=' ')
455
+ verbose_arg = "> /dev/null 2>&1" if not verbose else ""
456
+ degree_arg = f"--minpres {degree}" if degree >= 0 else ""
457
+ multi_chunk_arg = "--multi-chunk" if multi_chunk else ""
458
+ if os.path.exists(output_path + id):
459
+ os.remove(output_path+ id)
460
+ command = f"{pathes[backend]} {degree_arg} {multi_chunk_arg} {input_path+id} {output_path+id} {verbose_arg} --no-delaunay-compare"
461
+ if verbose:
462
+ print(command)
463
+ os.system(command)
464
+
465
+ blocks = scc_parser(output_path + id)
466
+ if clear:
467
+ clear_io(output_path + id, input_path + id)
468
+ ## Function Delaunay workaround: last size is 0 but shouldn't...
469
+ if degree<0 and len(blocks) and not len(blocks[-1][1]):
470
+ blocks=blocks[:-1]
471
+
472
+ return blocks
473
+
474
+ def function_delaunay_presentation_to_slicer(
475
+ slicer,
476
+ point_cloud:np.ndarray,
477
+ function_values:np.ndarray,
478
+ id:Optional[str] = None,
479
+ bool clear:bool = True,
480
+ bool verbose:bool=False,
481
+ int degree = -1,
482
+ bool multi_chunk = False,
483
+ ):
484
+ """
485
+ Computes a function delaunay presentation, and returns it as a slicer.
486
+
487
+ slicer: empty slicer to fill
488
+ points : (num_pts, n) float array
489
+ grades : (num_pts,) float array
490
+ degree (opt) : if given, computes a minimal presentation of this homological degree first
491
+ clear:bool, removes temporary files if true
492
+ degree: computes minimal presentation of this degree if given
493
+ verbose : bool
494
+ """
495
+ if id is None:
496
+ id = str(threading.get_native_id())
497
+ global input_path, output_path, pathes
498
+ backend = "function_delaunay"
499
+ assert _check_available(backend), f"Backend {backend} is not available."
500
+
501
+ to_write = np.concatenate([point_cloud, function_values.reshape(-1,1)], axis=1)
502
+ np.savetxt(input_path+id,to_write,delimiter=' ')
503
+ verbose_arg = "> /dev/null 2>&1" if not verbose else ""
504
+ degree_arg = f"--minpres {degree}" if degree >= 0 else ""
505
+ multi_chunk_arg = "--multi-chunk" if multi_chunk else ""
506
+ if os.path.exists(output_path + id):
507
+ os.remove(output_path+ id)
508
+ command = f"{pathes[backend]} {degree_arg} {multi_chunk_arg} {input_path+id} {output_path+id} {verbose_arg} --no-delaunay-compare"
509
+ if verbose:
510
+ print(command)
511
+ os.system(command)
512
+
513
+ slicer._build_from_scc_file(path=output_path+id, shift_dimension=-1 if degree <= 0 else degree-1 )
514
+
515
+ if clear:
516
+ clear_io(output_path + id, input_path + id)
517
+
518
+
519
+
520
+ def clear_io(*args):
521
+ """Removes temporary files"""
522
+ global input_path,output_path
523
+ for x in [input_path,output_path] + list(args):
524
+ if os.path.exists(x):
525
+ os.remove(x)
526
+
527
+
528
+
529
+
530
+
531
+
532
+ # cdef extern from "multiparameter_module_approximation/format_python-cpp.h" namespace "Gudhi::multiparameter::mma":
533
+ # pair[boundary_matrix, vector[One_critical_filtration[double]]] simplextree_to_boundary_filtration(intptr_t)
534
+ # vector[pair[ vector[vector[float]],boundary_matrix]] simplextree_to_scc(intptr_t)
535
+ # vector[pair[ vector[vector[vector[float]]],boundary_matrix]] function_simplextree_to_scc(intptr_t)
536
+ # pair[vector[vector[float]],boundary_matrix ] simplextree_to_ordered_bf(intptr_t)
537
+
538
+ # def simplex_tree2boundary_filtrations(simplextree:SimplexTreeMulti | SimplexTree):
539
+ # """Computes a (sparse) boundary matrix, with associated filtration. Can be used as an input of approx afterwards.
540
+ #
541
+ # Parameters
542
+ # ----------
543
+ # simplextree: Gudhi or mma simplextree
544
+ # The simplextree defining the filtration to convert to boundary-filtration.
545
+ #
546
+ # Returns
547
+ # -------
548
+ # B:List of lists of ints
549
+ # The boundary matrix.
550
+ # F: List of 1D filtration
551
+ # The filtrations aligned with B; the i-th simplex of this simplextree has boundary B[i] and filtration(s) F[i].
552
+ #
553
+ # """
554
+ # cdef intptr_t cptr
555
+ # if isinstance(simplextree, SimplexTreeMulti):
556
+ # cptr = simplextree.thisptr
557
+ # elif isinstance(simplextree, SimplexTree):
558
+ # temp_st = gd.SimplexTreeMulti(simplextree, parameters=1)
559
+ # cptr = temp_st.thisptr
560
+ # else:
561
+ # raise TypeError("Has to be a simplextree")
562
+ # cdef pair[boundary_matrix, vector[One_critical_filtration[double]]] cboundary_filtration = simplextree_to_boundary_filtration(cptr)
563
+ # boundary = cboundary_filtration.first
564
+ # # multi_filtrations = np.array(<vector[vector[float]]>One_critical_filtration.to_python(cboundary_filtration.second))
565
+ # cdef cnp.ndarray[double, ndim=2] multi_filtrations = _fmf2numpy_f64(cboundary_filtration.second)
566
+ # return boundary, multi_filtrations
567
+
568
+ # def simplextree2scc(simplextree:SimplexTreeMulti | SimplexTree, filtration_dtype=np.float32, bool flattened=False):
569
+ # """
570
+ # Turns a simplextree into a (simplicial) module presentation.
571
+ # """
572
+ # cdef intptr_t cptr
573
+ # cdef bool is_function_st = False
574
+ # if isinstance(simplextree, SimplexTreeMulti):
575
+ # cptr = simplextree.thisptr
576
+ # is_function_st = simplextree._is_function_simplextree
577
+ # elif isinstance(simplextree, SimplexTree):
578
+ # temp_st = gd.SimplexTreeMulti(simplextree, parameters=1)
579
+ # cptr = temp_st.thisptr
580
+ # else:
581
+ # raise TypeError("Has to be a simplextree")
582
+ #
583
+ # cdef pair[vector[vector[float]], boundary_matrix] out
584
+ # if flattened:
585
+ # out = simplextree_to_ordered_bf(cptr)
586
+ # return np.asarray(out.first,dtype=filtration_dtype), tuple(out.second)
587
+ #
588
+ # if is_function_st:
589
+ # blocks = function_simplextree_to_scc(cptr)
590
+ # else:
591
+ # blocks = simplextree_to_scc(cptr)
592
+ # # reduces the space in memory
593
+ # if is_function_st:
594
+ # blocks = [(tuple(f), tuple(b)) for f,b in blocks[::-1]]
595
+ # else:
596
+ # blocks = [(np.asarray(f,dtype=filtration_dtype), tuple(b)) for f,b in blocks[::-1]] ## presentation is on the other order
597
+ # return blocks+[(np.empty(0,dtype=filtration_dtype),[])]
598
+
599
+ @cython.boundscheck(False)
600
+ @cython.wraparound(False)
601
+ def scc2disk(
602
+ stuff,
603
+ path:str|os.PathLike,
604
+ int num_parameters = -1,
605
+ bool reverse_block = False,
606
+ bool rivet_compatible = False,
607
+ bool ignore_last_generators = False,
608
+ bool strip_comments = False,
609
+ ):
610
+ """
611
+ Writes a scc python format / blocks into a file.
612
+ """
613
+ if num_parameters == -1:
614
+ for block in stuff:
615
+ if len(block[0]) == 0:
616
+ continue
617
+ num_gens, num_parameters_= np.asarray(block[0]).shape
618
+ num_parameters = num_parameters_
619
+ break
620
+ assert num_parameters > 0, f"Invalid number of parameters {num_parameters}"
621
+
622
+ if reverse_block: stuff.reverse()
623
+ with open(path, "w") as f:
624
+ f.write("scc2020\n") if not rivet_compatible else f.write("firep\n")
625
+ if not strip_comments and not rivet_compatible: f.write("# Number of parameters\n")
626
+ if rivet_compatible:
627
+ assert num_parameters == 2
628
+ f.write("Filtration 1\n")
629
+ f.write("Filtration 2\n")
630
+ else:
631
+ f.write(f"{num_parameters}\n")
632
+
633
+ if not strip_comments: f.write("# Sizes of generating sets\n")
634
+ for block in stuff: f.write(f"{len(block[0])} ")
635
+ f.write("\n")
636
+ for i,block in enumerate(stuff):
637
+ if (rivet_compatible or ignore_last_generators) and i == len(stuff)-1: continue
638
+ if not strip_comments: f.write(f"# Block of dimension {len(stuff)-1-i}\n")
639
+ filtration, boundary = block
640
+ filtration = np.asarray(filtration).astype(str)
641
+ # boundary = tuple(x.astype(str) for x in boundary)
642
+ f.write(" ".join(itertools.chain.from_iterable(
643
+ ((*(f.tolist()), ";", *(np.asarray(b).astype(str).tolist()), "\n")
644
+ for f,b in zip(filtration, boundary))
645
+ )
646
+ ))
647
+ # for j in range(<int>len(filtration)):
648
+ # line = " ".join((
649
+ # *filtration[j],
650
+ # ";",
651
+ # *boundary[j],
652
+ # "\n",
653
+ # ))
654
+ # f.write(line)
655
+
656
+ def scc2disk_old(
657
+ stuff,
658
+ path:str|os.PathLike,
659
+ num_parameters = -1,
660
+ reverse_block = False,
661
+ rivet_compatible = False,
662
+ ignore_last_generators = False,
663
+ strip_comments = False,
664
+ ):
665
+ """
666
+ Writes a scc python format / blocks into a file.
667
+ """
668
+ if num_parameters == -1:
669
+ for block in stuff:
670
+ if len(block[0]) == 0:
671
+ continue
672
+ num_gens, num_parameters_= np.asarray(block[0]).shape
673
+ num_parameters = num_parameters_
674
+ break
675
+ assert num_parameters > 0, f"Invalid number of parameters {num_parameters}"
676
+
677
+ if reverse_block: stuff.reverse()
678
+ out = []
679
+ if rivet_compatible:
680
+ out.append(r"firep")
681
+ else:
682
+ out.append(r"scc2020")
683
+ if not strip_comments and not rivet_compatible:
684
+ out.append(r"# Number of parameters")
685
+ if rivet_compatible:
686
+ out.append("Filtration 1")
687
+ out.append("Filtration 2\n")
688
+ else:
689
+ out.append(f"{num_parameters}")
690
+
691
+ if not strip_comments:
692
+ out.append("# Sizes of generating sets")
693
+
694
+ # for block in stuff:
695
+ # f.write(f"{len(block[0])} ")
696
+ out.append(" ".join(str(len(block[0])) for block in stuff))
697
+ str_blocks = [out]
698
+ for i,block in enumerate(stuff):
699
+ if (rivet_compatible or ignore_last_generators) and i == len(stuff)-1: continue
700
+ if not strip_comments:
701
+ str_blocks.append([f"# Block of dimension {len(stuff)-1-i}"])
702
+ filtration, boundary = block
703
+ if len(filtration) == 0:
704
+ continue
705
+ filtration = filtration.astype(str)
706
+ C = filtration[:,0]
707
+ for i in range(1,filtration.shape[1]):
708
+ C = np.char.add(C," ")
709
+ C = np.char.add(C,filtration[:,i])
710
+ C = np.char.add(C, ";")
711
+ D = np.fromiter((" ".join(b.astype(str).tolist()) for b in boundary), dtype="<U11") #int32-> str is "<U11" #check np.array(1, dtype=np.int32).astype(str)
712
+ str_blocks.append(np.char.add(C,D))
713
+
714
+ np.savetxt("test.scc", np.concatenate(str_blocks), delimiter="", fmt="%s")