multipers 2.2.3__cp311-cp311-win_amd64.whl → 2.3.1__cp311-cp311-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.cp311-win_amd64.pyd +0 -0
  18. multipers/function_rips.pyx +105 -105
  19. multipers/grids.cp311-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.cp311-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.cp311-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.cp311-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.cp311-win_amd64.pyd +0 -0
  152. multipers/point_measure.pyx +322 -320
  153. multipers/simplex_tree_multi.cp311-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.cp311-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/gudhi/truc.h CHANGED
@@ -1,888 +1,957 @@
1
- #pragma once
2
- #include "gudhi/Multi_persistence/Line.h"
3
- #include "multiparameter_module_approximation/format_python-cpp.h"
4
- #include <gudhi/One_critical_filtration.h>
5
- #include <gudhi/Multi_critical_filtration.h>
6
- #include <algorithm>
7
- #include <boost/mpl/aux_/na_fwd.hpp>
8
- #include <cassert>
9
- #include <cstddef>
10
- #include <cstdint>
11
- // #include <gudhi/Simplex_tree/multi_filtrations/Finitely_critical_filtrations.h>
12
- #include <iostream>
13
- #include <limits>
14
- #include <numeric>
15
- #include <oneapi/tbb/enumerable_thread_specific.h>
16
- #include <oneapi/tbb/parallel_for.h>
17
- #include <oneapi/tbb/task_group.h>
18
- #include <sstream>
19
- #include <string>
20
- #include <utility>
21
- #include <vector>
22
- #include <type_traits> //std::invoke_result
23
- #include "scc_io.h"
24
-
25
- namespace Gudhi {
26
- namespace multiparameter {
27
- namespace truc_interface {
28
- using index_type = std::uint32_t;
29
-
30
- class PresentationStructure {
31
- public:
32
- PresentationStructure() {}
33
-
34
- /* SimplicialStructure &operator=(const SimplicialStructure &) = default; */
35
-
36
- PresentationStructure(const std::vector<std::vector<index_type>> &generators,
37
- const std::vector<int> &generator_dimensions)
38
- : generators(generators), generator_dimensions(generator_dimensions), num_vertices_(0) {
39
- for (const auto &stuff : generator_dimensions) {
40
- if (stuff == 0) num_vertices_++;
41
- }
42
- max_dimension_ = generator_dimensions.size() > 0
43
- ? *std::max_element(generator_dimensions.begin(), generator_dimensions.end())
44
- : 0;
45
- };
46
-
47
- PresentationStructure(const PresentationStructure &other)
48
- : generators(other.generators),
49
- generator_dimensions(other.generator_dimensions),
50
- num_vertices_(other.num_vertices_),
51
- max_dimension_(other.max_dimension_) {}
52
-
53
- /* PresentationStructure &operator=(const PresentationStructure &other) { */
54
- /* generators = other.generators; */
55
- /* generator_dimensions = other.generator_dimensions; */
56
- /* num_vertices_ = other.num_vertices_; */
57
- /* max_dimension_ = other.max_dimension_; */
58
- /**/
59
- /* return *this; */
60
- /* } */
61
-
62
- const std::vector<unsigned int> &operator[](std::size_t i) const {
63
- return generators[i];
64
- } // needs to be iterable (begin, end, size)
65
-
66
- std::vector<unsigned int> &operator[](std::size_t i) {
67
- return generators[i];
68
- } // needs to be iterable (begin, end, size)
69
-
70
- inline int dimension(std::size_t i) const { return generator_dimensions[i]; };
71
-
72
- inline friend std::ostream &operator<<(std::ostream &stream, const PresentationStructure &structure) {
73
- stream << "Boundary:\n";
74
- stream << "{";
75
- for (const auto &stuff : structure.generators) {
76
- stream << "{";
77
- for (auto truc : stuff) stream << truc << ", ";
78
-
79
- if (!stuff.empty()) stream << "\b" << "\b ";
80
-
81
- stream << "},\n";
82
- }
83
- stream << "}\n";
84
- stream << "Degrees: (max " << structure.max_dimension() << ")\n";
85
- stream << "{";
86
- for (const auto &stuff : structure.generator_dimensions) stream << stuff << ", ";
87
- if (structure.size() > 0) {
88
- stream << "\b" << "\b";
89
- }
90
- stream << "}\n";
91
- return stream;
92
- }
93
-
94
- inline void to_stream(std::ostream &stream, const std::vector<index_type> &order) {
95
- for (const auto &i : order) {
96
- const auto &stuff = this->operator[](i);
97
- stream << i << " : [";
98
- for (const auto &truc : stuff) stream << truc << ", ";
99
- stream << "]\n";
100
- }
101
- /* return stream; */
102
- }
103
-
104
- inline std::size_t size() const { return generators.size(); };
105
-
106
- unsigned int num_vertices() const { return num_vertices_; };
107
-
108
- unsigned int max_dimension() const { return max_dimension_; }
109
-
110
- int prune_above_dimension(int dim) {
111
- int idx = std::lower_bound(generator_dimensions.begin(), generator_dimensions.end(), dim + 1) -
112
- generator_dimensions.begin();
113
- generators.resize(idx);
114
- generator_dimensions.resize(idx);
115
- max_dimension_ = generator_dimensions.size() ? generator_dimensions.back() : -1;
116
- return idx;
117
- }
118
-
119
- private:
120
- std::vector<std::vector<index_type>> generators;
121
- std::vector<int> generator_dimensions;
122
- unsigned int num_vertices_;
123
- int max_dimension_ = -1;
124
- };
125
-
126
- class SimplicialStructure {
127
- public:
128
- template <typename SimplexTree>
129
- void from_simplextree(SimplexTree &st) {
130
- auto [filtration, boundary] = Gudhi::multiparameter::mma::simplextree_to_ordered_bf(st);
131
- this->boundaries = boundary;
132
- this->num_vertices_ = st.num_vertices();
133
- this->max_dimension_ = st.dimension();
134
- }
135
-
136
- SimplicialStructure() {}
137
-
138
- /* SimplicialStructure &operator=(const SimplicialStructure &) = default; */
139
-
140
- SimplicialStructure(const std::vector<std::vector<index_type>> &boundaries,
141
- unsigned int num_vertices,
142
- unsigned int max_dimension)
143
- : boundaries(boundaries), num_vertices_(num_vertices), max_dimension_(max_dimension) {
144
-
145
- };
146
-
147
- const std::vector<unsigned int> &operator[](std::size_t i) const {
148
- return boundaries[i];
149
- } // needs to be iterable (begin, end, size)
150
-
151
- std::vector<unsigned int> &operator[](std::size_t i) {
152
- return boundaries[i];
153
- } // needs to be iterable (begin, end, size)
154
-
155
- int dimension(std::size_t i) const { return boundaries[i].size() == 0 ? 0 : boundaries[i].size() - 1; };
156
-
157
- inline friend std::ostream &operator<<(std::ostream &stream, const SimplicialStructure &structure) {
158
- stream << "{";
159
- for (const auto &stuff : structure.boundaries) {
160
- stream << "{";
161
- for (auto truc : stuff) stream << truc << ", ";
162
-
163
- if (!stuff.empty()) stream << "\b" << "\b ";
164
-
165
- stream << "},\n";
166
- }
167
- stream << "}\n";
168
- return stream;
169
- }
170
-
171
- inline void to_stream(std::ostream &stream, const std::vector<index_type> &order) {
172
- for (const auto &i : order) {
173
- const auto &stuff = this->operator[](i);
174
- stream << i << " : [";
175
- for (const auto &truc : stuff) stream << truc << ", ";
176
- stream << "]\n";
177
- }
178
- /* return stream; */
179
- }
180
-
181
- inline std::size_t size() const { return boundaries.size(); };
182
-
183
- inline unsigned int num_vertices() const { return num_vertices_; }
184
-
185
- inline unsigned int max_dimension() const { return max_dimension_; }
186
-
187
- int prune_above_dimension(int dim) { throw; }
188
-
189
- private:
190
- std::vector<std::vector<index_type>> boundaries;
191
- unsigned int num_vertices_;
192
- unsigned int max_dimension_;
193
- };
194
-
195
- template <class PersBackend, class Structure, class MultiFiltration>
196
- class Truc {
197
- public:
198
- using Filtration_value = MultiFiltration;
199
- using value_type = typename MultiFiltration::value_type;
200
- using split_barcode =
201
- std::vector<std::vector<std::pair<typename MultiFiltration::value_type, typename MultiFiltration::value_type>>>;
202
- template <typename value_type = value_type>
203
- using flat_barcode = std::vector<std::pair<int, std::pair<value_type, value_type>>>;
204
-
205
- template <typename value_type = value_type>
206
- using flat_nodim_barcode = std::vector<std::pair<value_type, value_type>>;
207
-
208
- // CONSTRUCTORS.
209
- // - Need everything of the same size, generator order is a PERMUTATION
210
- //
211
- Truc(const Structure &structure, const std::vector<MultiFiltration> &generator_filtration_values)
212
- : generator_filtration_values(generator_filtration_values),
213
- generator_order(structure.size()),
214
- structure(structure),
215
- filtration_container(structure.size()) {
216
- std::iota(generator_order.begin(), generator_order.end(), 0); // range
217
- };
218
-
219
- template <class SimplexTree>
220
- Truc(SimplexTree *simplextree) {
221
- auto [filtration, boundary] = mma::simplextree_to_ordered_bf(*simplextree);
222
- structure = SimplicialStructure(boundary, (*simplextree).num_vertices(), (*simplextree).dimension());
223
- generator_filtration_values.resize(filtration.size());
224
- for (auto i = 0u; i < filtration.size(); i++)
225
- generator_filtration_values[i] = filtration[i]; // there is a copy here. TODO : deal with it.
226
- generator_order = std::vector<index_type>(structure.size());
227
- std::iota(generator_order.begin(), generator_order.end(), 0); // range
228
- filtration_container.resize(structure.size());
229
- }
230
-
231
- Truc(const std::vector<std::vector<index_type>> &generator_maps,
232
- const std::vector<int> &generator_dimensions,
233
- const std::vector<MultiFiltration> &generator_filtrations)
234
- : generator_filtration_values(generator_filtrations),
235
- generator_order(generator_filtrations.size(), 0),
236
- structure(PresentationStructure(generator_maps, generator_dimensions)),
237
- filtration_container(generator_filtrations.size()) {
238
- std::iota(generator_order.begin(), generator_order.end(), 0); // range
239
- }
240
-
241
- Truc() {};
242
-
243
- Truc &operator=(const Truc &other) {
244
- generator_filtration_values = other.generator_filtration_values;
245
- generator_order = other.generator_order;
246
- structure = other.structure;
247
- persistence = other.persistence;
248
- filtration_container = other.filtration_container;
249
- persistence._update_permutation_ptr(generator_order);
250
- return *this;
251
- };
252
-
253
- template <bool ignore_inf>
254
- std::vector<std::pair<int, std::vector<index_type>>> get_current_boundary_matrix() {
255
- std::vector<index_type> permutation(generator_order.size());
256
- std::iota(permutation.begin(), permutation.end(), 0);
257
- if constexpr (ignore_inf) {
258
- permutation.erase(std::remove_if(permutation.begin(),
259
- permutation.end(),
260
- [&](std::size_t val) {
261
- return filtration_container[val] == MultiFiltration::Generator::T_inf;
262
- }),
263
- permutation.end());
264
- std::sort(permutation.begin(), permutation.end());
265
- }
266
- std::sort(permutation.begin(), permutation.end(), [&](std::size_t i, std::size_t j) {
267
- if (structure.dimension(i) > structure.dimension(j)) return false;
268
- if (structure.dimension(i) < structure.dimension(j)) return true;
269
- return filtration_container[i] < filtration_container[j];
270
- });
271
-
272
- std::vector<std::pair<int, std::vector<index_type>>> matrix(permutation.size());
273
-
274
- std::vector<index_type> permutationInv(generator_order.size());
275
- std::size_t newPos = 0;
276
- for (std::size_t oldPos : permutation) {
277
- permutationInv[oldPos] = newPos;
278
- auto &boundary = matrix[newPos].second;
279
- boundary.resize(structure[oldPos].size());
280
- for (std::size_t j = 0; j < structure[oldPos].size(); ++j) {
281
- boundary[j] = permutationInv[structure[oldPos][j]];
282
- }
283
- std::sort(boundary.begin(), boundary.end());
284
- matrix[newPos].first = structure.dimension(oldPos);
285
- ++newPos;
286
- }
287
-
288
- return matrix;
289
- }
290
-
291
- inline std::size_t num_generators() const { return structure.size(); }
292
-
293
- inline std::size_t num_parameters() const {
294
- return num_generators() == 0 ? 0 : this->generator_filtration_values[0].num_parameters();
295
- }
296
-
297
- inline const Structure &get_structure() const { return structure; }
298
-
299
- template <class SubFiltration, bool original_order = true>
300
- inline void push_to_out(const SubFiltration &f,
301
- std::vector<typename MultiFiltration::value_type> &filtration_container,
302
- const std::vector<index_type> &generator_order) const {
303
- /* std::vector<NewFilrationType> out(this->num_generators()); */
304
-
305
- // filtration_container.resize(
306
- // this->num_generators()); // for some reasons it is necessary FIXME
307
- for (std::size_t i = 0u; i < this->num_generators(); i++) {
308
- if constexpr (original_order) {
309
- filtration_container[i] = f.compute_forward_intersection(generator_filtration_values[i]);
310
- } else {
311
- filtration_container[i] = f.compute_forward_intersection(generator_filtration_values[generator_order[i]]);
312
- }
313
- }
314
- }
315
-
316
- template <class SubFiltration, bool original_order = true>
317
- inline void push_to(const SubFiltration &f) {
318
- this->push_to_out<SubFiltration, original_order>(f, this->filtration_container, this->generator_order);
319
- }
320
-
321
- template <class array1d>
322
- inline void set_one_filtration(const array1d &truc) {
323
- assert(truc.size() == this->num_generators());
324
- this->filtration_container = truc;
325
- }
326
-
327
- inline const std::vector<typename MultiFiltration::value_type> &get_one_filtration() const {
328
- return this->filtration_container;
329
- }
330
-
331
- inline PersBackend compute_persistence_out(
332
- const std::vector<typename MultiFiltration::value_type> &one_filtration,
333
- std::vector<index_type> &out_gen_order,
334
- const bool ignore_inf) const { // needed ftm as PersBackend only points there
335
- constexpr const bool verbose = false;
336
- if (one_filtration.size() != this->num_generators()) {
337
- throw;
338
- }
339
- out_gen_order.resize(this->num_generators());
340
- std::iota(out_gen_order.begin(),
341
- out_gen_order.end(),
342
- 0); // we have to reset here, even though we're already doing this
343
- std::sort(out_gen_order.begin(), out_gen_order.end(), [&](index_type i, index_type j) {
344
- if (structure.dimension(i) > structure.dimension(j)) return false;
345
- if (structure.dimension(i) < structure.dimension(j)) return true;
346
- return one_filtration[i] < one_filtration[j];
347
- });
348
- if (!PersBackend::is_vine && ignore_inf) {
349
- if constexpr (verbose){
350
- std::cout << "Removing infinite simplices" << std::endl;
351
- }
352
- for (auto &i : out_gen_order)
353
- if (one_filtration[i] == MultiFiltration::Generator::T_inf) {
354
- // TODO : later
355
- // int d = structure.dimension(i);
356
- // d = d == 0 ? 1 : 0;
357
- // if (degrees.size()>d || degrees[d] || degrees[d-1])
358
- // continue;
359
- i = std::numeric_limits<typename std::remove_reference_t<decltype(out_gen_order)>::value_type>::max();
360
- }
361
- }
362
- if constexpr (false) {
363
- std::cout << structure << std::endl;
364
- std::cout << "[";
365
- for (auto i : out_gen_order) {
366
- std::cout << i << ", ";
367
- }
368
- std::cout << "]" << std::endl;
369
- std::cout << "[";
370
- for (auto i : one_filtration) {
371
- std::cout << i << ",";
372
- }
373
- std::cout << "]" << std::endl;
374
- }
375
- return PersBackend(structure, out_gen_order);
376
- }
377
-
378
- inline const bool has_persistence() { return this->persistence.size(); };
379
-
380
- inline void compute_persistence(const bool ignore_inf = true) {
381
- this->persistence = this->compute_persistence_out(
382
- // this->filtration_container, this->generator_order, degrees); // TODO
383
- // : later
384
- this->filtration_container,
385
- this->generator_order,
386
- ignore_inf);
387
- };
388
-
389
- // TODO : static ?
390
- inline void vineyard_update(PersBackend &persistence,
391
- const std::vector<typename MultiFiltration::value_type> &one_filtration,
392
- std::vector<index_type> &generator_order) const {
393
- constexpr const bool verbose = false;
394
- /* static_assert(PersBackend::has_vine_update); */
395
- // the first false is to get the generator order
396
- // insertion sort
397
- auto n = this->num_generators();
398
- if constexpr (verbose) {
399
- std::cout << "Vine updates : ";
400
- }
401
- for (std::size_t i = 0; i < n; i++) {
402
- auto j = i;
403
- while (j > 0 && persistence.get_dimension(j) == persistence.get_dimension(j - 1) &&
404
- one_filtration[generator_order[j]] < one_filtration[generator_order[j - 1]]) {
405
- if constexpr (verbose) {
406
- std::cout << j - 1 << ", ";
407
- }
408
- persistence.vine_swap(j - 1);
409
- std::swap(generator_order[j - 1], generator_order[j]);
410
- j--;
411
- }
412
- }
413
- if constexpr (verbose) {
414
- std::cout << std::endl;
415
- }
416
- }
417
-
418
- inline void vineyard_update() {
419
- vineyard_update(this->persistence, this->filtration_container, this->generator_order);
420
- }
421
-
422
- inline split_barcode get_barcode(
423
- PersBackend &persistence,
424
- const std::vector<typename MultiFiltration::value_type> &filtration_container) const {
425
- auto barcode_indices = persistence.get_barcode();
426
- split_barcode out(this->structure.max_dimension() + 1); // TODO : This doesn't allow for negative dimensions
427
- constexpr const bool verbose = false;
428
- constexpr const bool debug = false;
429
- const auto inf = MultiFiltration::Generator::T_inf;
430
- for (const auto &bar : barcode_indices) {
431
- if constexpr (verbose) std::cout << "BAR : " << bar.birth << " " << bar.death << "\n";
432
- if constexpr (debug) {
433
- if (bar.birth >= filtration_container.size() || bar.birth < 0) {
434
- std::cout << "Trying to add an incompatible birth... ";
435
- std::cout << bar.birth << std::endl;
436
- std::cout << "Death is " << bar.death << std::endl;
437
- std::cout << "Max size is " << filtration_container.size() << std::endl;
438
- continue;
439
- }
440
- if (bar.dim > static_cast<int>(this->structure.max_dimension())) {
441
- std::cout << "Incompatible dimension detected... " << bar.dim << std::endl;
442
- std::cout << "While max dim is " << this->structure.max_dimension() << std::endl;
443
- continue;
444
- }
445
- }
446
-
447
- auto birth_filtration = filtration_container[bar.birth];
448
- auto death_filtration = inf;
449
- if (bar.death != static_cast<typename PersBackend::pos_index>(-1))
450
- death_filtration = filtration_container[bar.death];
451
-
452
- if constexpr (verbose) {
453
- std::cout << "BAR: " << bar.birth << "(" << birth_filtration << ")"
454
- << " --" << bar.death << "(" << death_filtration << ")"
455
- << " dim " << bar.dim << std::endl;
456
- }
457
- if (birth_filtration < death_filtration)
458
- out[bar.dim].push_back({birth_filtration, death_filtration});
459
- else {
460
- out[bar.dim].push_back({inf, inf});
461
- }
462
- }
463
- return out;
464
- }
465
-
466
- inline split_barcode get_barcode() { return get_barcode(this->persistence, this->filtration_container); }
467
-
468
- template <typename value_type = value_type>
469
- static inline flat_nodim_barcode<value_type> get_flat_nodim_barcode(
470
- PersBackend &persistence,
471
- std::vector<typename MultiFiltration::value_type> &filtration_container) {
472
- constexpr const bool verbose = false;
473
- const auto &barcode_indices = persistence.get_barcode();
474
- auto num_bars = barcode_indices.size();
475
- flat_nodim_barcode<value_type> out(num_bars);
476
- if (num_bars <= 0) return out;
477
- auto idx = 0u;
478
- const value_type inf = MultiFiltration::Generator::T_inf;
479
- for (const auto &bar : barcode_indices) {
480
- value_type birth_filtration = inf;
481
- value_type death_filtration = -birth_filtration;
482
- if (bar.death == static_cast<typename PersBackend::pos_index>(-1))
483
- death_filtration = inf;
484
- else
485
- death_filtration = static_cast<value_type>(filtration_container[bar.death]);
486
- birth_filtration = static_cast<value_type>(filtration_container[bar.birth]);
487
- if constexpr (verbose) {
488
- std::cout << "PAIRING : " << bar.birth << " / " << bar.death << " dim " << bar.dim << std::endl;
489
- }
490
- if constexpr (verbose) {
491
- std::cout << "PAIRING filtration : " << birth_filtration << " " << death_filtration << " dim " << bar.dim
492
- << std::endl;
493
- }
494
-
495
- if (birth_filtration < death_filtration)
496
- out[idx] = {birth_filtration, death_filtration};
497
- else {
498
- out[idx] = {inf, inf};
499
- }
500
- idx++;
501
- }
502
- return out;
503
- }
504
-
505
- template <typename value_type = value_type>
506
- static inline flat_barcode<value_type> get_flat_barcode(
507
- PersBackend &persistence,
508
- std::vector<typename MultiFiltration::value_type> &filtration_container) {
509
- constexpr const bool verbose = false;
510
- const auto &barcode_indices = persistence.get_barcode();
511
- auto num_bars = barcode_indices.size();
512
- flat_barcode<value_type> out(num_bars);
513
- if (num_bars <= 0) return out;
514
- auto idx = 0u;
515
- const value_type inf = MultiFiltration::Generator::T_inf;
516
- for (const auto &bar : barcode_indices) {
517
- value_type birth_filtration = inf;
518
- value_type death_filtration = -birth_filtration;
519
- if (bar.death == static_cast<typename PersBackend::pos_index>(-1))
520
- death_filtration = inf;
521
- else
522
- death_filtration = static_cast<value_type>(filtration_container[bar.death]);
523
- birth_filtration = static_cast<value_type>(filtration_container[bar.birth]);
524
- if constexpr (verbose) {
525
- std::cout << "PAIRING : " << bar.birth << " / " << bar.death << " dim " << bar.dim << std::endl;
526
- }
527
- if constexpr (verbose) {
528
- std::cout << "PAIRING filtration : " << birth_filtration << " " << death_filtration << " dim " << bar.dim
529
- << std::endl;
530
- }
531
-
532
- if (birth_filtration < death_filtration)
533
- out[idx] = {bar.dim, {birth_filtration, death_filtration}};
534
- else {
535
- out[idx] = {bar.dim, {inf, inf}};
536
- }
537
- idx++;
538
- }
539
- return out;
540
- }
541
-
542
- template <typename value_type = value_type>
543
- inline flat_barcode<value_type> get_flat_barcode() {
544
- return get_flat_barcode(this->persistence, this->filtration_container);
545
- }
546
-
547
- template <typename value_type = value_type>
548
- inline flat_nodim_barcode<value_type> get_flat_nodim_barcode() {
549
- return get_flat_nodim_barcode(this->persistence, this->filtration_container);
550
- }
551
-
552
- inline friend std::ostream &operator<<(std::ostream &stream, Truc &truc) {
553
- stream << "-------------------- Truc \n";
554
- stream << "--- Structure \n";
555
- stream << truc.structure;
556
- /* stream << "-- Dimensions (max " << truc.structure.max_dimension() <<
557
- * ")\n"; */
558
- /* stream << "{"; */
559
- /* for (auto i = 0u; i < truc.num_generators(); i++) */
560
- /* stream << truc.structure.dimension(i) << ", "; */
561
- /* stream << "\b" */
562
- /* << "\b"; */
563
- /* stream << "}" << std::endl; */
564
- stream << "--- Order \n";
565
- stream << "{";
566
- for (const auto &idx : truc.generator_order) stream << idx << ", ";
567
- stream << "}" << std::endl;
568
-
569
- stream << "--- Current slice filtration\n";
570
- stream << "{";
571
- for (const auto &stuff : truc.filtration_container) stream << stuff << ", ";
572
- stream << "\b" << "\b";
573
- stream << "}" << std::endl;
574
-
575
- stream << "--- Filtrations \n";
576
- for (const auto &i : truc.generator_order) {
577
- stream << i << " : ";
578
- const auto &stuff = truc.generator_filtration_values[i];
579
- stream << stuff << "\n";
580
- }
581
- stream << "--- PersBackend \n";
582
- stream << truc.persistence;
583
-
584
- return stream;
585
- }
586
-
587
- inline std::string to_str() {
588
- std::stringstream stream;
589
- stream << *this;
590
- return stream.str();
591
- }
592
-
593
- inline std::pair<typename MultiFiltration::Generator, typename MultiFiltration::Generator> get_bounding_box() const {
594
- using OC = typename MultiFiltration::Generator;
595
- // assert(!generator_filtration_values.empty());
596
- OC a = OC::inf();
597
- OC b = -1 * a;
598
- for (const auto &filtration_value : generator_filtration_values) {
599
- if constexpr (MultiFiltration::is_multi_critical) {
600
- a.pull_to_greatest_common_lower_bound(factorize_below(filtration_value));
601
- b.push_to_least_common_upper_bound(factorize_above(filtration_value));
602
- } else {
603
- a.pull_to_greatest_common_lower_bound(filtration_value);
604
- b.push_to_least_common_upper_bound(filtration_value);
605
- }
606
- }
607
- return {a, b};
608
- }
609
-
610
- inline std::vector<typename MultiFiltration::Generator> get_filtration_values() const {
611
- if constexpr (MultiFiltration::is_multi_critical) {
612
- std::vector<typename MultiFiltration::Generator> out;
613
- out.reserve(generator_filtration_values.size()); // at least this, will dooble later
614
- for (std::size_t i = 0; i < generator_filtration_values.size(); ++i) {
615
- for (const auto &f : generator_filtration_values[i]) {
616
- out.push_back(f);
617
- }
618
- }
619
- return out;
620
- } else {
621
- return generator_filtration_values; // copy not necessary for Generator
622
- } // (could return const&)
623
- }
624
-
625
- inline std::vector<MultiFiltration> &get_filtrations() { return generator_filtration_values; }
626
-
627
- inline const std::vector<MultiFiltration> &get_filtrations() const { return generator_filtration_values; }
628
-
629
- inline const std::vector<int> get_dimensions() const {
630
- std::size_t n = this->num_generators();
631
- std::vector<int> out(n);
632
- for (std::size_t i = 0; i < n; ++i) {
633
- out[i] = structure.dimension(i);
634
- }
635
- return out;
636
- }
637
-
638
- inline void prune_above_dimension(int max_dim) {
639
- int idx = structure.prune_above_dimension(max_dim);
640
- generator_filtration_values.resize(idx);
641
- generator_order.resize(idx);
642
- filtration_container.resize(idx);
643
- }
644
-
645
- inline const std::vector<std::vector<index_type>> get_boundaries() {
646
- std::size_t n = this->num_generators();
647
- std::vector<std::vector<index_type>> out(n);
648
- for (auto i = 0u; i < n; ++i) {
649
- out[i] = this->structure[i];
650
- }
651
- return out;
652
- }
653
-
654
- auto coarsen_on_grid(const std::vector<std::vector<typename MultiFiltration::value_type>> grid) {
655
- using return_type = decltype(std::declval<MultiFiltration>().template as_type<std::int32_t>());
656
- std::vector<return_type> coords(this->num_generators());
657
- for (std::size_t gen = 0u; gen < coords.size(); ++gen) {
658
- coords[gen] = compute_coordinates_in_grid<int32_t>(generator_filtration_values[gen], grid);
659
- }
660
- return Truc<PersBackend, Structure, return_type>(structure, coords);
661
- }
662
-
663
- inline void coarsen_on_grid_inplace(const std::vector<std::vector<typename MultiFiltration::value_type>> &grid,
664
- bool coordinate = true) {
665
- for (auto gen = 0u; gen < this->num_generators(); ++gen) {
666
- generator_filtration_values[gen].project_onto_grid(grid, coordinate);
667
- }
668
- }
669
-
670
- // dim, num_cycle_of_dim, num_faces_in_cycle, vertices_in_face
671
- inline std::vector<std::vector<std::vector<std::vector<index_type>>>> get_representative_cycles(
672
- bool update = true,
673
- bool detailed = false) {
674
- // iterable iterable simplex key
675
- auto cycles_key = persistence.get_representative_cycles(update, detailed);
676
- auto num_cycles = cycles_key.size();
677
- std::vector<std::vector<std::vector<std::vector<index_type>>>> out(structure.max_dimension() + 1);
678
- for (auto &cycles_of_dim : out) cycles_of_dim.reserve(num_cycles);
679
- for (const auto &cycle : cycles_key) {
680
- int cycle_dim = 0; // for more generality, should be minimal dimension instead
681
- if (!cycle[0].empty()) { // if empty, cycle has no border -> assumes dimension 0 even if it could be min dim
682
- cycle_dim = structure.dimension(cycle[0][0]) + 1; // all faces have the same dim
683
- }
684
- out[cycle_dim].push_back(cycle);
685
- }
686
- return out;
687
- }
688
-
689
- const std::vector<index_type> &get_current_order() const { return generator_order; }
690
-
691
- const PersBackend &get_persistence() const { return persistence; }
692
-
693
- PersBackend &get_persistence() { return persistence; }
694
-
695
- // TrucThread get_safe_thread() { return TrucThread(*this); }
696
-
697
- class TrucThread {
698
- public:
699
- using Filtration_value = MultiFiltration;
700
- using value_type = typename MultiFiltration::value_type;
701
- using ThreadSafe = TrucThread;
702
-
703
- inline TrucThread(const Truc &truc)
704
- : truc_ptr(&truc),
705
- generator_order(truc.get_current_order()),
706
- filtration_container(truc.get_one_filtration()),
707
- persistence(truc.get_persistence()) {
708
- persistence._update_permutation_ptr(generator_order);
709
- };
710
-
711
- inline TrucThread(const TrucThread &truc)
712
- : truc_ptr(truc.truc_ptr),
713
- generator_order(truc.get_current_order()),
714
- filtration_container(truc.get_one_filtration()),
715
- persistence(truc.get_persistence()) {
716
- persistence._update_permutation_ptr(generator_order);
717
- };
718
-
719
- inline TrucThread weak_copy() const { return TrucThread(*truc_ptr); }
720
-
721
- inline const bool has_persistence() { return this->persistence.size(); };
722
-
723
- inline const PersBackend &get_persistence() const { return persistence; }
724
-
725
- inline PersBackend &get_persistence() { return persistence; }
726
-
727
- inline std::pair<MultiFiltration, MultiFiltration> get_bounding_box() const { return truc_ptr->get_bounding_box(); }
728
-
729
- inline const std::vector<index_type> &get_current_order() const { return generator_order; }
730
-
731
- inline const std::vector<MultiFiltration> &get_filtrations() const { return truc_ptr->get_filtrations(); }
732
-
733
- inline const std::vector<int> &get_dimensions() const { return truc_ptr->get_dimensions(); }
734
-
735
- inline const std::vector<std::vector<index_type>> &get_boundaries() const { return truc_ptr->get_boundaries(); }
736
-
737
- inline void coarsen_on_grid_inplace(const std::vector<std::vector<typename MultiFiltration::value_type>> &grid,
738
- bool coordinate = true) {
739
- truc_ptr->coarsen_on_grid_inplace(grid, coordinate);
740
- }
741
-
742
- template <typename Subfiltration>
743
- inline void push_to(const Subfiltration &f) {
744
- truc_ptr->push_to_out(f, this->filtration_container, this->generator_order);
745
- }
746
-
747
- inline std::vector<typename PersBackend::cycle_type> get_representative_cycles(bool update = true) {
748
- return truc_ptr->get_representative_cycles(update);
749
- }
750
-
751
- inline void compute_persistence(const bool ignore_inf=true) {
752
- this->persistence =
753
- this->truc_ptr->compute_persistence_out(this->filtration_container, this->generator_order, ignore_inf);
754
- };
755
-
756
- inline void vineyard_update() {
757
- truc_ptr->vineyard_update(this->persistence, this->filtration_container, this->generator_order);
758
- }
759
-
760
- template <typename value_type = value_type>
761
- inline flat_barcode<value_type> get_flat_barcode() {
762
- return truc_ptr->get_flat_barcode(this->persistence, this->filtration_container);
763
- }
764
-
765
- template <typename value_type = value_type>
766
- inline flat_nodim_barcode<value_type> get_flat_nodim_barcode() {
767
- return truc_ptr->get_flat_nodim_barcode(this->persistence, this->filtration_container);
768
- }
769
-
770
- inline split_barcode get_barcode() { return truc_ptr->get_barcode(this->persistence, this->filtration_container); }
771
-
772
- inline std::size_t num_generators() const { return this->truc_ptr->structure.size(); }
773
-
774
- inline std::size_t num_parameters() const {
775
- return num_generators() == 0 ? 0 : this->get_filtrations()[0].num_parameters();
776
- }
777
-
778
- inline const std::vector<typename MultiFiltration::value_type> &get_one_filtration() const {
779
- return this->filtration_container;
780
- }
781
-
782
- inline std::vector<typename MultiFiltration::value_type> &get_one_filtration() {
783
- return this->filtration_container;
784
- }
785
-
786
- private:
787
- const Truc *truc_ptr;
788
- std::vector<index_type> generator_order; // size fixed at construction time,
789
- std::vector<typename MultiFiltration::value_type> filtration_container; // filtration of the current slice
790
- PersBackend persistence; // generated by the structure, and generator_order.
791
-
792
- }; // class TrucThread
793
-
794
- /*
795
- * returns barcodes of the f(multipers)
796
- *
797
- */
798
- template <typename Fun, typename Fun_arg>
799
- inline std::vector<split_barcode> barcodes(Fun &&f, const std::vector<Fun_arg> &args, const bool ignore_inf = true) {
800
- if (args.size() == 0) {
801
- return {};
802
- }
803
- std::vector<split_barcode> out(args.size());
804
-
805
- if constexpr (PersBackend::is_vine) {
806
- this->push_to(f(args[0]));
807
- this->compute_persistence();
808
- out[0] = this->get_barcode();
809
- for (auto i = 1u; i < args.size(); ++i) {
810
- this->push_to(f(args[i]));
811
- this->vineyard_update();
812
- out[i] = this->get_barcode();
813
- }
814
-
815
- } else {
816
- ThreadSafe local_template = this->weak_copy();
817
- tbb::enumerable_thread_specific<ThreadSafe> thread_locals(local_template);
818
- tbb::parallel_for(static_cast<std::size_t>(0), args.size(), [&](const std::size_t &i) {
819
- ThreadSafe &s = thread_locals.local();
820
- s.push_to(f(args[i]));
821
- s.compute_persistence(ignore_inf);
822
- out[i] = s.get_barcode();
823
- });
824
- }
825
- return out;
826
- }
827
-
828
- // FOR Python interface, but I'm not fan. Todo: do the lambda function in
829
- // cython?
830
- inline std::vector<split_barcode> persistence_on_lines(const std::vector<std::vector<value_type>> &basepoints) {
831
- return barcodes(
832
- [](const std::vector<value_type> &basepoint) { return Gudhi::multi_persistence::Line<value_type>(basepoint); },
833
- basepoints);
834
- }
835
-
836
- inline std::vector<split_barcode> persistence_on_lines(
837
- const std::vector<std::pair<std::vector<value_type>, std::vector<value_type>>> &bp_dirs) {
838
- return barcodes(
839
- [](const std::pair<std::vector<value_type>, std::vector<value_type>> &bpdir) {
840
- return Gudhi::multi_persistence::Line<value_type>(bpdir.first, bpdir.second);
841
- },
842
- bp_dirs);
843
- }
844
-
845
- void build_from_scc_file(const std::string &inFilePath,
846
- bool isRivetCompatible = false,
847
- bool isReversed = false,
848
- int shiftDimensions = 0) {
849
- *this = read_scc_file<Truc>(inFilePath, isRivetCompatible, isReversed, shiftDimensions);
850
- }
851
-
852
- void write_to_scc_file(const std::string &outFilePath,
853
- int numberOfParameters = -1,
854
- int degree = -1,
855
- bool rivetCompatible = false,
856
- bool IgnoreLastGenerators = false,
857
- bool stripComments = false,
858
- bool reverse = false) {
859
- write_scc_file<Truc>(
860
- outFilePath, *this, numberOfParameters, degree, rivetCompatible, IgnoreLastGenerators, stripComments, reverse);
861
- }
862
-
863
- public:
864
- using ThreadSafe = TrucThread; // for outside
865
-
866
- TrucThread weak_copy() const { return TrucThread(*this); }
867
-
868
- // TODO: declare method here instead of scc_io.h
869
- // it is just temporary, until Truc is cleaned up
870
- // friend void write_scc_file<Truc>(const std::string &outFilePath,
871
- // const Truc &slicer,
872
- // int numberOfParameters,
873
- // int degree,
874
- // bool rivetCompatible,
875
- // bool IgnoreLastGenerators,
876
- // bool stripComments,
877
- // bool reverse);
878
-
879
- private:
880
- std::vector<MultiFiltration> generator_filtration_values; // defined at construction time. Const
881
- std::vector<index_type> generator_order; // size fixed at construction time, // TODO : CHANGE THAT TO UINT32
882
- Structure structure; // defined at construction time. Const
883
- std::vector<typename MultiFiltration::value_type> filtration_container; // filtration of the current slice
884
- PersBackend persistence; // generated by the structure, and generator_order.
885
-
886
- }; // class Truc
887
-
888
- }}} // namespace Gudhi::multiparameter::interface
1
+ #pragma once
2
+ #include "gudhi/Multi_persistence/Line.h"
3
+ #include "multiparameter_module_approximation/format_python-cpp.h"
4
+ #include <gudhi/One_critical_filtration.h>
5
+ #include <gudhi/Multi_critical_filtration.h>
6
+ #include <algorithm>
7
+ #include <boost/mpl/aux_/na_fwd.hpp>
8
+ #include <cassert>
9
+ #include <csignal>
10
+ #include <cstddef>
11
+ #include <cstdint>
12
+ // #include <gudhi/Simplex_tree/multi_filtrations/Finitely_critical_filtrations.h>
13
+ #include <iostream>
14
+ #include <limits>
15
+ #include <numeric>
16
+ #include <oneapi/tbb/enumerable_thread_specific.h>
17
+ #include <oneapi/tbb/parallel_for.h>
18
+ #include <oneapi/tbb/task_group.h>
19
+ #include <sstream>
20
+ #include <string>
21
+ #include <utility>
22
+ #include <vector>
23
+ #include <type_traits> //std::invoke_result
24
+ #include "scc_io.h"
25
+
26
+ namespace Gudhi {
27
+ namespace multiparameter {
28
+ namespace truc_interface {
29
+ using index_type = std::uint32_t;
30
+
31
+ class PresentationStructure {
32
+ public:
33
+ PresentationStructure() {}
34
+
35
+ /* SimplicialStructure &operator=(const SimplicialStructure &) = default; */
36
+
37
+ PresentationStructure(const std::vector<std::vector<index_type>> &generators,
38
+ const std::vector<int> &generator_dimensions)
39
+ : generators(generators), generator_dimensions(generator_dimensions), num_vertices_(0) {
40
+ for (const auto &stuff : generator_dimensions) {
41
+ if (stuff == 0) num_vertices_++;
42
+ }
43
+ max_dimension_ = generator_dimensions.size() > 0
44
+ ? *std::max_element(generator_dimensions.begin(), generator_dimensions.end())
45
+ : 0;
46
+ };
47
+
48
+ PresentationStructure(const PresentationStructure &other)
49
+ : generators(other.generators),
50
+ generator_dimensions(other.generator_dimensions),
51
+ num_vertices_(other.num_vertices_),
52
+ max_dimension_(other.max_dimension_) {}
53
+
54
+ /* PresentationStructure &operator=(const PresentationStructure &other) { */
55
+ /* generators = other.generators; */
56
+ /* generator_dimensions = other.generator_dimensions; */
57
+ /* num_vertices_ = other.num_vertices_; */
58
+ /* max_dimension_ = other.max_dimension_; */
59
+ /**/
60
+ /* return *this; */
61
+ /* } */
62
+
63
+ const std::vector<unsigned int> &operator[](std::size_t i) const {
64
+ return generators[i];
65
+ } // needs to be iterable (begin, end, size)
66
+
67
+ std::vector<unsigned int> &operator[](std::size_t i) {
68
+ return generators[i];
69
+ } // needs to be iterable (begin, end, size)
70
+
71
+ inline int dimension(std::size_t i) const { return generator_dimensions[i]; };
72
+
73
+ inline friend std::ostream &operator<<(std::ostream &stream, const PresentationStructure &structure) {
74
+ stream << "Boundary:\n";
75
+ stream << "{";
76
+ for (const auto &stuff : structure.generators) {
77
+ stream << "{";
78
+ for (auto truc : stuff) stream << truc << ", ";
79
+
80
+ if (!stuff.empty()) stream << "\b" << "\b ";
81
+
82
+ stream << "},\n";
83
+ }
84
+ stream << "}\n";
85
+ stream << "Degrees: (max " << structure.max_dimension() << ")\n";
86
+ stream << "{";
87
+ for (const auto &stuff : structure.generator_dimensions) stream << stuff << ", ";
88
+ if (structure.size() > 0) {
89
+ stream << "\b" << "\b";
90
+ }
91
+ stream << "}\n";
92
+ return stream;
93
+ }
94
+
95
+ inline void to_stream(std::ostream &stream, const std::vector<index_type> &order) {
96
+ for (const auto &i : order) {
97
+ const auto &stuff = this->operator[](i);
98
+ stream << i << " : [";
99
+ for (const auto &truc : stuff) stream << truc << ", ";
100
+ stream << "]\n";
101
+ }
102
+ /* return stream; */
103
+ }
104
+
105
+ inline std::size_t size() const { return generators.size(); };
106
+
107
+ unsigned int num_vertices() const { return num_vertices_; };
108
+
109
+ unsigned int max_dimension() const { return max_dimension_; }
110
+
111
+ int prune_above_dimension(int dim) {
112
+ int idx = std::lower_bound(generator_dimensions.begin(), generator_dimensions.end(), dim + 1) -
113
+ generator_dimensions.begin();
114
+ generators.resize(idx);
115
+ generator_dimensions.resize(idx);
116
+ max_dimension_ = generator_dimensions.size() ? generator_dimensions.back() : -1;
117
+ return idx;
118
+ }
119
+
120
+ PresentationStructure permute(const std::vector<index_type> &order) const {
121
+ std::vector<std::vector<index_type>> new_generators(generators.size());
122
+ std::vector<int> new_generator_dimensions(generator_dimensions.size());
123
+ for (std::size_t i = 0; i < order.size(); i++) {
124
+ new_generators[i] = std::vector<index_type>(generators[order[i]].size());
125
+ for (std::size_t j = 0; j < generators[order[i]].size(); j++) {
126
+ new_generators[i][j] = order[generators[order[i]][j]];
127
+ }
128
+ new_generator_dimensions[i] = generator_dimensions[order[i]];
129
+ }
130
+ return PresentationStructure(new_generators, new_generator_dimensions);
131
+ }
132
+
133
+ private:
134
+ std::vector<std::vector<index_type>> generators;
135
+ std::vector<int> generator_dimensions;
136
+ unsigned int num_vertices_;
137
+ int max_dimension_ = -1;
138
+ };
139
+
140
+ class SimplicialStructure {
141
+ public:
142
+ template <typename SimplexTree>
143
+ void from_simplextree(SimplexTree &st) {
144
+ auto [filtration, boundary] = Gudhi::multiparameter::mma::simplextree_to_ordered_bf(st);
145
+ this->boundaries = boundary;
146
+ this->num_vertices_ = st.num_vertices();
147
+ this->max_dimension_ = st.dimension();
148
+ }
149
+
150
+ SimplicialStructure() {}
151
+
152
+ /* SimplicialStructure &operator=(const SimplicialStructure &) = default; */
153
+
154
+ SimplicialStructure(const std::vector<std::vector<index_type>> &boundaries,
155
+ unsigned int num_vertices,
156
+ unsigned int max_dimension)
157
+ : boundaries(boundaries), num_vertices_(num_vertices), max_dimension_(max_dimension) {
158
+
159
+ };
160
+
161
+ const std::vector<unsigned int> &operator[](std::size_t i) const {
162
+ return boundaries[i];
163
+ } // needs to be iterable (begin, end, size)
164
+
165
+ std::vector<unsigned int> &operator[](std::size_t i) {
166
+ return boundaries[i];
167
+ } // needs to be iterable (begin, end, size)
168
+
169
+ int dimension(std::size_t i) const { return boundaries[i].size() == 0 ? 0 : boundaries[i].size() - 1; };
170
+
171
+ inline friend std::ostream &operator<<(std::ostream &stream, const SimplicialStructure &structure) {
172
+ stream << "{";
173
+ for (const auto &stuff : structure.boundaries) {
174
+ stream << "{";
175
+ for (auto truc : stuff) stream << truc << ", ";
176
+
177
+ if (!stuff.empty()) stream << "\b" << "\b ";
178
+
179
+ stream << "},\n";
180
+ }
181
+ stream << "}\n";
182
+ return stream;
183
+ }
184
+
185
+ inline void to_stream(std::ostream &stream, const std::vector<index_type> &order) {
186
+ for (const auto &i : order) {
187
+ const auto &stuff = this->operator[](i);
188
+ stream << i << " : [";
189
+ for (const auto &truc : stuff) stream << truc << ", ";
190
+ stream << "]\n";
191
+ }
192
+ /* return stream; */
193
+ }
194
+
195
+ inline std::size_t size() const { return boundaries.size(); };
196
+
197
+ inline unsigned int num_vertices() const { return num_vertices_; }
198
+
199
+ inline unsigned int max_dimension() const { return max_dimension_; }
200
+
201
+ int prune_above_dimension([[maybe_unused]] int dim) { throw "Not implemented"; }
202
+
203
+ private:
204
+ std::vector<std::vector<index_type>> boundaries;
205
+ unsigned int num_vertices_;
206
+ unsigned int max_dimension_;
207
+ };
208
+
209
+ template <class PersBackend, class Structure, class MultiFiltration>
210
+ class Truc {
211
+ public:
212
+ using Filtration_value = MultiFiltration;
213
+ using value_type = typename MultiFiltration::value_type;
214
+ using split_barcode =
215
+ std::vector<std::vector<std::pair<typename MultiFiltration::value_type, typename MultiFiltration::value_type>>>;
216
+ template <typename value_type = value_type>
217
+ using flat_barcode = std::vector<std::pair<int, std::pair<value_type, value_type>>>;
218
+
219
+ template <typename value_type = value_type>
220
+ using flat_nodim_barcode = std::vector<std::pair<value_type, value_type>>;
221
+
222
+ // CONSTRUCTORS.
223
+ // - Need everything of the same size, generator order is a PERMUTATION
224
+ //
225
+ Truc(const Structure &structure, const std::vector<MultiFiltration> &generator_filtration_values)
226
+ : generator_filtration_values(generator_filtration_values),
227
+ generator_order(structure.size()),
228
+ structure(structure),
229
+ filtration_container(structure.size()) {
230
+ std::iota(generator_order.begin(), generator_order.end(), 0); // range
231
+ };
232
+
233
+ template <class SimplexTree>
234
+ Truc(SimplexTree *simplextree) {
235
+ auto [filtration, boundary] = mma::simplextree_to_ordered_bf(*simplextree);
236
+ structure = SimplicialStructure(boundary, (*simplextree).num_vertices(), (*simplextree).dimension());
237
+ generator_filtration_values.resize(filtration.size());
238
+ for (auto i = 0u; i < filtration.size(); i++)
239
+ generator_filtration_values[i] = filtration[i]; // there is a copy here. TODO : deal with it.
240
+ generator_order = std::vector<index_type>(structure.size());
241
+ std::iota(generator_order.begin(), generator_order.end(), 0); // range
242
+ filtration_container.resize(structure.size());
243
+ }
244
+
245
+ Truc(const std::vector<std::vector<index_type>> &generator_maps,
246
+ const std::vector<int> &generator_dimensions,
247
+ const std::vector<MultiFiltration> &generator_filtrations)
248
+ : generator_filtration_values(generator_filtrations),
249
+ generator_order(generator_filtrations.size(), 0),
250
+ structure(PresentationStructure(generator_maps, generator_dimensions)),
251
+ filtration_container(generator_filtrations.size()) {
252
+ std::iota(generator_order.begin(), generator_order.end(), 0); // range
253
+ }
254
+
255
+ Truc(const Truc &other)
256
+ : generator_filtration_values(other.generator_filtration_values),
257
+ generator_order(other.generator_order),
258
+ structure(other.structure),
259
+ filtration_container(other.filtration_container),
260
+ persistence(other.persistence) {
261
+ persistence._update_permutation_ptr(generator_order);
262
+ }
263
+
264
+ Truc &operator=(Truc other) {
265
+ if (this != &other) {
266
+ generator_filtration_values = other.generator_filtration_values;
267
+ generator_order = other.generator_order;
268
+ structure = other.structure;
269
+ filtration_container = other.filtration_container;
270
+ persistence = other.persistence;
271
+ persistence._update_permutation_ptr(generator_order);
272
+ }
273
+ return *this;
274
+ }
275
+
276
+ Truc() {};
277
+
278
+ inline bool dimension_order(const index_type &i, const index_type &j) const {
279
+ return structure.dimension(i) < structure.dimension(j);
280
+ };
281
+
282
+ inline bool colexical_order(const index_type &i, const index_type &j) const {
283
+ if (structure.dimension(i) > structure.dimension(j)) return false;
284
+ if (structure.dimension(i) < structure.dimension(j)) return true;
285
+ if constexpr (MultiFiltration::is_multicritical()) // TODO : this may not be the best
286
+ throw "Not implemented in the multicritical case";
287
+
288
+ for (int idx = generator_filtration_values[i].num_parameters() - 1; idx >= 0; --idx) {
289
+ if (generator_filtration_values[i][idx] < generator_filtration_values[j][idx])
290
+ return true;
291
+ else if (generator_filtration_values[i][idx] > generator_filtration_values[j][idx])
292
+ return false;
293
+ }
294
+ return false;
295
+ };
296
+
297
+
298
+ template <class Fun>
299
+ inline Truc rearange_sort(const Fun&& fun) const {
300
+ std::vector<index_type> permutation(generator_order.size());
301
+ std::iota(permutation.begin(), permutation.end(), 0);
302
+ std::sort(permutation.begin(), permutation.end(), [&](std::size_t i, std::size_t j) {
303
+ return fun(i, j);
304
+ });
305
+ std::vector<MultiFiltration> new_filtration(generator_filtration_values.size());
306
+ for (std::size_t i = 0; i < generator_filtration_values.size(); i++) {
307
+ new_filtration[i] = generator_filtration_values[permutation[i]];
308
+ }
309
+ return Truc(structure.permute(permutation), new_filtration);
310
+ }
311
+
312
+ Truc colexical_rearange() const {
313
+ return rearange_sort([this](std::size_t i, std::size_t j) { return this->colexical_order(i, j); });
314
+ }
315
+
316
+ template <bool ignore_inf>
317
+ std::vector<std::pair<int, std::vector<index_type>>> get_current_boundary_matrix() {
318
+ std::vector<index_type> permutation(generator_order.size());
319
+ std::iota(permutation.begin(), permutation.end(), 0);
320
+ if constexpr (ignore_inf) {
321
+ permutation.erase(std::remove_if(permutation.begin(),
322
+ permutation.end(),
323
+ [&](std::size_t val) {
324
+ return filtration_container[val] == MultiFiltration::Generator::T_inf;
325
+ }),
326
+ permutation.end());
327
+ std::sort(permutation.begin(), permutation.end());
328
+ }
329
+ std::sort(permutation.begin(), permutation.end(), [&](std::size_t i, std::size_t j) {
330
+ if (structure.dimension(i) > structure.dimension(j)) return false;
331
+ if (structure.dimension(i) < structure.dimension(j)) return true;
332
+ return filtration_container[i] < filtration_container[j];
333
+ });
334
+
335
+ std::vector<std::pair<int, std::vector<index_type>>> matrix(permutation.size());
336
+
337
+ std::vector<index_type> permutationInv(generator_order.size());
338
+ std::size_t newPos = 0;
339
+ for (std::size_t oldPos : permutation) {
340
+ permutationInv[oldPos] = newPos;
341
+ auto &boundary = matrix[newPos].second;
342
+ boundary.resize(structure[oldPos].size());
343
+ for (std::size_t j = 0; j < structure[oldPos].size(); ++j) {
344
+ boundary[j] = permutationInv[structure[oldPos][j]];
345
+ }
346
+ std::sort(boundary.begin(), boundary.end());
347
+ matrix[newPos].first = structure.dimension(oldPos);
348
+ ++newPos;
349
+ }
350
+
351
+ return matrix;
352
+ }
353
+
354
+ inline std::size_t num_generators() const { return structure.size(); }
355
+
356
+ inline std::size_t num_parameters() const {
357
+ return num_generators() == 0 ? 0 : this->generator_filtration_values[0].num_parameters();
358
+ }
359
+
360
+ inline const Structure &get_structure() const { return structure; }
361
+
362
+ template <class SubFiltration, bool original_order = true>
363
+ inline void push_to_out(const SubFiltration &f,
364
+ std::vector<typename MultiFiltration::value_type> &filtration_container,
365
+ const std::vector<index_type> &generator_order) const {
366
+ /* std::vector<NewFilrationType> out(this->num_generators()); */
367
+
368
+ // filtration_container.resize(
369
+ // this->num_generators()); // for some reasons it is necessary FIXME
370
+ for (std::size_t i = 0u; i < this->num_generators(); i++) {
371
+ if constexpr (original_order) {
372
+ filtration_container[i] = f.compute_forward_intersection(generator_filtration_values[i]);
373
+ } else {
374
+ filtration_container[i] = f.compute_forward_intersection(generator_filtration_values[generator_order[i]]);
375
+ }
376
+ }
377
+ }
378
+
379
+ template <class SubFiltration, bool original_order = true>
380
+ inline void push_to(const SubFiltration &f) {
381
+ this->push_to_out<SubFiltration, original_order>(f, this->filtration_container, this->generator_order);
382
+ }
383
+
384
+ template <class array1d>
385
+ inline void set_one_filtration(const array1d &truc) {
386
+ assert(truc.size() == this->num_generators());
387
+ this->filtration_container = truc;
388
+ }
389
+
390
+ inline const std::vector<typename MultiFiltration::value_type> &get_one_filtration() const {
391
+ return this->filtration_container;
392
+ }
393
+
394
+ inline PersBackend compute_persistence_out(
395
+ const std::vector<typename MultiFiltration::value_type> &one_filtration,
396
+ std::vector<index_type> &out_gen_order,
397
+ const bool ignore_inf) const { // needed ftm as PersBackend only points there
398
+ constexpr const bool verbose = false;
399
+ if (one_filtration.size() != this->num_generators()) {
400
+ throw;
401
+ }
402
+ out_gen_order.resize(this->num_generators());
403
+ std::iota(out_gen_order.begin(),
404
+ out_gen_order.end(),
405
+ 0); // we have to reset here, even though we're already doing this
406
+ std::sort(out_gen_order.begin(), out_gen_order.end(), [&](index_type i, index_type j) {
407
+ if (structure.dimension(i) > structure.dimension(j)) return false;
408
+ if (structure.dimension(i) < structure.dimension(j)) return true;
409
+ return one_filtration[i] < one_filtration[j];
410
+ });
411
+ if (!PersBackend::is_vine && ignore_inf) {
412
+ if constexpr (verbose) {
413
+ std::cout << "Removing infinite simplices" << std::endl;
414
+ }
415
+ for (auto &i : out_gen_order)
416
+ if (one_filtration[i] == MultiFiltration::Generator::T_inf) {
417
+ // TODO : later
418
+ // int d = structure.dimension(i);
419
+ // d = d == 0 ? 1 : 0;
420
+ // if (degrees.size()>d || degrees[d] || degrees[d-1])
421
+ // continue;
422
+ i = std::numeric_limits<typename std::remove_reference_t<decltype(out_gen_order)>::value_type>::max();
423
+ }
424
+ }
425
+ if constexpr (false) {
426
+ std::cout << structure << std::endl;
427
+ std::cout << "[";
428
+ for (auto i : out_gen_order) {
429
+ std::cout << i << ", ";
430
+ }
431
+ std::cout << "]" << std::endl;
432
+ std::cout << "[";
433
+ for (auto i : one_filtration) {
434
+ std::cout << i << ",";
435
+ }
436
+ std::cout << "]" << std::endl;
437
+ }
438
+ return PersBackend(structure, out_gen_order);
439
+ }
440
+
441
+ inline bool has_persistence() const { return this->persistence.size(); };
442
+
443
+ inline void compute_persistence(const bool ignore_inf = true) {
444
+ this->persistence = this->compute_persistence_out(
445
+ // this->filtration_container, this->generator_order, degrees); // TODO
446
+ // : later
447
+ this->filtration_container,
448
+ this->generator_order,
449
+ ignore_inf);
450
+ };
451
+
452
+ // TODO : static ?
453
+ inline void vineyard_update(PersBackend &persistence,
454
+ const std::vector<typename MultiFiltration::value_type> &one_filtration,
455
+ std::vector<index_type> &generator_order) const {
456
+ constexpr const bool verbose = false;
457
+ /* static_assert(PersBackend::has_vine_update); */
458
+ // the first false is to get the generator order
459
+ // insertion sort
460
+ auto n = this->num_generators();
461
+ if constexpr (verbose) {
462
+ std::cout << "Vine updates : ";
463
+ }
464
+ for (std::size_t i = 0; i < n; i++) {
465
+ auto j = i;
466
+ while (j > 0 && persistence.get_dimension(j) == persistence.get_dimension(j - 1) &&
467
+ one_filtration[generator_order[j]] < one_filtration[generator_order[j - 1]]) {
468
+ if constexpr (verbose) {
469
+ std::cout << j - 1 << ", ";
470
+ }
471
+ persistence.vine_swap(j - 1);
472
+ std::swap(generator_order[j - 1], generator_order[j]);
473
+ j--;
474
+ }
475
+ }
476
+ if constexpr (verbose) {
477
+ std::cout << std::endl;
478
+ }
479
+ }
480
+
481
+ inline void vineyard_update() {
482
+ vineyard_update(this->persistence, this->filtration_container, this->generator_order);
483
+ }
484
+
485
+ inline split_barcode get_barcode(
486
+ PersBackend &persistence,
487
+ const std::vector<typename MultiFiltration::value_type> &filtration_container) const {
488
+ auto barcode_indices = persistence.get_barcode();
489
+ split_barcode out(this->structure.max_dimension() + 1); // TODO : This doesn't allow for negative dimensions
490
+ constexpr const bool verbose = false;
491
+ constexpr const bool debug = false;
492
+ const auto inf = MultiFiltration::Generator::T_inf;
493
+ for (const auto &bar : barcode_indices) {
494
+ if constexpr (verbose) std::cout << "BAR : " << bar.birth << " " << bar.death << "\n";
495
+ if constexpr (debug) {
496
+ if (bar.birth >= filtration_container.size() || bar.birth < 0) {
497
+ std::cout << "Trying to add an incompatible birth... ";
498
+ std::cout << bar.birth << std::endl;
499
+ std::cout << "Death is " << bar.death << std::endl;
500
+ std::cout << "Max size is " << filtration_container.size() << std::endl;
501
+ continue;
502
+ }
503
+ if (bar.dim > static_cast<int>(this->structure.max_dimension())) {
504
+ std::cout << "Incompatible dimension detected... " << bar.dim << std::endl;
505
+ std::cout << "While max dim is " << this->structure.max_dimension() << std::endl;
506
+ continue;
507
+ }
508
+ }
509
+
510
+ auto birth_filtration = filtration_container[bar.birth];
511
+ auto death_filtration = inf;
512
+ if (bar.death != static_cast<typename PersBackend::pos_index>(-1))
513
+ death_filtration = filtration_container[bar.death];
514
+
515
+ if constexpr (verbose) {
516
+ std::cout << "BAR: " << bar.birth << "(" << birth_filtration << ")"
517
+ << " --" << bar.death << "(" << death_filtration << ")"
518
+ << " dim " << bar.dim << std::endl;
519
+ }
520
+ if (birth_filtration < death_filtration)
521
+ out[bar.dim].push_back({birth_filtration, death_filtration});
522
+ else {
523
+ out[bar.dim].push_back({inf, inf});
524
+ }
525
+ }
526
+ return out;
527
+ }
528
+
529
+ inline split_barcode get_barcode() { return get_barcode(this->persistence, this->filtration_container); }
530
+
531
+ template <typename value_type = value_type>
532
+ static inline flat_nodim_barcode<value_type> get_flat_nodim_barcode(
533
+ PersBackend &persistence,
534
+ std::vector<typename MultiFiltration::value_type> &filtration_container) {
535
+ constexpr const bool verbose = false;
536
+ const auto &barcode_indices = persistence.get_barcode();
537
+ auto num_bars = barcode_indices.size();
538
+ flat_nodim_barcode<value_type> out(num_bars);
539
+ if (num_bars <= 0) return out;
540
+ auto idx = 0u;
541
+ const value_type inf = MultiFiltration::Generator::T_inf;
542
+ for (const auto &bar : barcode_indices) {
543
+ value_type birth_filtration = inf;
544
+ value_type death_filtration = -birth_filtration;
545
+ if (bar.death == static_cast<typename PersBackend::pos_index>(-1))
546
+ death_filtration = inf;
547
+ else
548
+ death_filtration = static_cast<value_type>(filtration_container[bar.death]);
549
+ birth_filtration = static_cast<value_type>(filtration_container[bar.birth]);
550
+ if constexpr (verbose) {
551
+ std::cout << "PAIRING : " << bar.birth << " / " << bar.death << " dim " << bar.dim << std::endl;
552
+ }
553
+ if constexpr (verbose) {
554
+ std::cout << "PAIRING filtration : " << birth_filtration << " " << death_filtration << " dim " << bar.dim
555
+ << std::endl;
556
+ }
557
+
558
+ if (birth_filtration < death_filtration)
559
+ out[idx] = {birth_filtration, death_filtration};
560
+ else {
561
+ out[idx] = {inf, inf};
562
+ }
563
+ idx++;
564
+ }
565
+ return out;
566
+ }
567
+
568
+ template <typename value_type = value_type>
569
+ static inline flat_barcode<value_type> get_flat_barcode(
570
+ PersBackend &persistence,
571
+ std::vector<typename MultiFiltration::value_type> &filtration_container) {
572
+ constexpr const bool verbose = false;
573
+ const auto &barcode_indices = persistence.get_barcode();
574
+ auto num_bars = barcode_indices.size();
575
+ flat_barcode<value_type> out(num_bars);
576
+ if (num_bars <= 0) return out;
577
+ auto idx = 0u;
578
+ const value_type inf = MultiFiltration::Generator::T_inf;
579
+ for (const auto &bar : barcode_indices) {
580
+ value_type birth_filtration = inf;
581
+ value_type death_filtration = -birth_filtration;
582
+ if (bar.death == static_cast<typename PersBackend::pos_index>(-1))
583
+ death_filtration = inf;
584
+ else
585
+ death_filtration = static_cast<value_type>(filtration_container[bar.death]);
586
+ birth_filtration = static_cast<value_type>(filtration_container[bar.birth]);
587
+ if constexpr (verbose) {
588
+ std::cout << "PAIRING : " << bar.birth << " / " << bar.death << " dim " << bar.dim << std::endl;
589
+ }
590
+ if constexpr (verbose) {
591
+ std::cout << "PAIRING filtration : " << birth_filtration << " " << death_filtration << " dim " << bar.dim
592
+ << std::endl;
593
+ }
594
+
595
+ if (birth_filtration < death_filtration)
596
+ out[idx] = {bar.dim, {birth_filtration, death_filtration}};
597
+ else {
598
+ out[idx] = {bar.dim, {inf, inf}};
599
+ }
600
+ idx++;
601
+ }
602
+ return out;
603
+ }
604
+
605
+ template <typename value_type = value_type>
606
+ inline flat_barcode<value_type> get_flat_barcode() {
607
+ return get_flat_barcode(this->persistence, this->filtration_container);
608
+ }
609
+
610
+ template <typename value_type = value_type>
611
+ inline flat_nodim_barcode<value_type> get_flat_nodim_barcode() {
612
+ return get_flat_nodim_barcode(this->persistence, this->filtration_container);
613
+ }
614
+
615
+ inline friend std::ostream &operator<<(std::ostream &stream, Truc &truc) {
616
+ stream << "-------------------- Truc \n";
617
+ stream << "--- Structure \n";
618
+ stream << truc.structure;
619
+ /* stream << "-- Dimensions (max " << truc.structure.max_dimension() <<
620
+ * ")\n"; */
621
+ /* stream << "{"; */
622
+ /* for (auto i = 0u; i < truc.num_generators(); i++) */
623
+ /* stream << truc.structure.dimension(i) << ", "; */
624
+ /* stream << "\b" */
625
+ /* << "\b"; */
626
+ /* stream << "}" << std::endl; */
627
+ stream << "--- Order \n";
628
+ stream << "{";
629
+ for (const auto &idx : truc.generator_order) stream << idx << ", ";
630
+ stream << "}" << std::endl;
631
+
632
+ stream << "--- Current slice filtration\n";
633
+ stream << "{";
634
+ for (const auto &stuff : truc.filtration_container) stream << stuff << ", ";
635
+ stream << "\b" << "\b";
636
+ stream << "}" << std::endl;
637
+
638
+ stream << "--- Filtrations \n";
639
+ for (const auto &i : truc.generator_order) {
640
+ stream << i << " : ";
641
+ const auto &stuff = truc.generator_filtration_values[i];
642
+ stream << stuff << "\n";
643
+ }
644
+ stream << "--- PersBackend \n";
645
+ stream << truc.persistence;
646
+
647
+ return stream;
648
+ }
649
+
650
+ inline std::string to_str() {
651
+ std::stringstream stream;
652
+ stream << *this;
653
+ return stream.str();
654
+ }
655
+
656
+ inline std::pair<typename MultiFiltration::Generator, typename MultiFiltration::Generator> get_bounding_box() const {
657
+ using OC = typename MultiFiltration::Generator;
658
+ // assert(!generator_filtration_values.empty());
659
+ OC a = OC::inf();
660
+ OC b = -1 * a;
661
+ for (const auto &filtration_value : generator_filtration_values) {
662
+ if constexpr (MultiFiltration::is_multi_critical) {
663
+ a.pull_to_greatest_common_lower_bound(factorize_below(filtration_value));
664
+ b.push_to_least_common_upper_bound(factorize_above(filtration_value));
665
+ } else {
666
+ a.pull_to_greatest_common_lower_bound(filtration_value);
667
+ b.push_to_least_common_upper_bound(filtration_value);
668
+ }
669
+ }
670
+ return {a, b};
671
+ }
672
+
673
+ inline std::vector<typename MultiFiltration::Generator> get_filtration_values() const {
674
+ if constexpr (MultiFiltration::is_multi_critical) {
675
+ std::vector<typename MultiFiltration::Generator> out;
676
+ out.reserve(generator_filtration_values.size()); // at least this, will dooble later
677
+ for (std::size_t i = 0; i < generator_filtration_values.size(); ++i) {
678
+ for (const auto &f : generator_filtration_values[i]) {
679
+ out.push_back(f);
680
+ }
681
+ }
682
+ return out;
683
+ } else {
684
+ return generator_filtration_values; // copy not necessary for Generator
685
+ } // (could return const&)
686
+ }
687
+
688
+ inline std::vector<MultiFiltration> &get_filtrations() { return generator_filtration_values; }
689
+
690
+ inline const std::vector<MultiFiltration> &get_filtrations() const { return generator_filtration_values; }
691
+
692
+ inline const std::vector<int> get_dimensions() const {
693
+ std::size_t n = this->num_generators();
694
+ std::vector<int> out(n);
695
+ for (std::size_t i = 0; i < n; ++i) {
696
+ out[i] = structure.dimension(i);
697
+ }
698
+ return out;
699
+ }
700
+
701
+ inline void prune_above_dimension(int max_dim) {
702
+ int idx = structure.prune_above_dimension(max_dim);
703
+ generator_filtration_values.resize(idx);
704
+ generator_order.resize(idx);
705
+ filtration_container.resize(idx);
706
+ }
707
+
708
+ inline const std::vector<std::vector<index_type>> get_boundaries() {
709
+ std::size_t n = this->num_generators();
710
+ std::vector<std::vector<index_type>> out(n);
711
+ for (auto i = 0u; i < n; ++i) {
712
+ out[i] = this->structure[i];
713
+ }
714
+ return out;
715
+ }
716
+
717
+ auto coarsen_on_grid(const std::vector<std::vector<typename MultiFiltration::value_type>> grid) {
718
+ using return_type = decltype(std::declval<MultiFiltration>().template as_type<std::int32_t>());
719
+ std::vector<return_type> coords(this->num_generators());
720
+ for (std::size_t gen = 0u; gen < coords.size(); ++gen) {
721
+ coords[gen] = compute_coordinates_in_grid<int32_t>(generator_filtration_values[gen], grid);
722
+ }
723
+ return Truc<PersBackend, Structure, return_type>(structure, coords);
724
+ }
725
+
726
+ inline void coarsen_on_grid_inplace(const std::vector<std::vector<typename MultiFiltration::value_type>> &grid,
727
+ bool coordinate = true) {
728
+ for (auto gen = 0u; gen < this->num_generators(); ++gen) {
729
+ generator_filtration_values[gen].project_onto_grid(grid, coordinate);
730
+ }
731
+ }
732
+
733
+ // dim, num_cycle_of_dim, num_faces_in_cycle, vertices_in_face
734
+ inline std::vector<std::vector<std::vector<std::vector<index_type>>>> get_representative_cycles(
735
+ bool update = true,
736
+ bool detailed = false) {
737
+ // iterable iterable simplex key
738
+ auto cycles_key = persistence.get_representative_cycles(update, detailed);
739
+ auto num_cycles = cycles_key.size();
740
+ std::vector<std::vector<std::vector<std::vector<index_type>>>> out(structure.max_dimension() + 1);
741
+ for (auto &cycles_of_dim : out) cycles_of_dim.reserve(num_cycles);
742
+ for (const auto &cycle : cycles_key) {
743
+ int cycle_dim = 0; // for more generality, should be minimal dimension instead
744
+ if (!cycle[0].empty()) { // if empty, cycle has no border -> assumes dimension 0 even if it could be min dim
745
+ cycle_dim = structure.dimension(cycle[0][0]) + 1; // all faces have the same dim
746
+ }
747
+ out[cycle_dim].push_back(cycle);
748
+ }
749
+ return out;
750
+ }
751
+
752
+ const std::vector<index_type> &get_current_order() const { return generator_order; }
753
+
754
+ const PersBackend &get_persistence() const { return persistence; }
755
+
756
+ PersBackend &get_persistence() { return persistence; }
757
+
758
+ // TrucThread get_safe_thread() { return TrucThread(*this); }
759
+
760
+ class TrucThread {
761
+ public:
762
+ using Filtration_value = MultiFiltration;
763
+ using value_type = typename MultiFiltration::value_type;
764
+ using ThreadSafe = TrucThread;
765
+
766
+ inline TrucThread(const Truc &truc)
767
+ : truc_ptr(&truc),
768
+ generator_order(truc.get_current_order()),
769
+ filtration_container(truc.get_one_filtration()),
770
+ persistence(truc.get_persistence()) {
771
+ persistence._update_permutation_ptr(generator_order);
772
+ };
773
+
774
+ inline TrucThread(const TrucThread &truc)
775
+ : truc_ptr(truc.truc_ptr),
776
+ generator_order(truc.get_current_order()),
777
+ filtration_container(truc.get_one_filtration()),
778
+ persistence(truc.get_persistence()) {
779
+ persistence._update_permutation_ptr(generator_order);
780
+ };
781
+
782
+ inline TrucThread weak_copy() const { return TrucThread(*truc_ptr); }
783
+
784
+ inline bool has_persistence() const { return this->persistence.size(); };
785
+
786
+ inline const PersBackend &get_persistence() const { return persistence; }
787
+
788
+ inline PersBackend &get_persistence() { return persistence; }
789
+
790
+ inline std::pair<MultiFiltration, MultiFiltration> get_bounding_box() const { return truc_ptr->get_bounding_box(); }
791
+
792
+ inline const std::vector<index_type> &get_current_order() const { return generator_order; }
793
+
794
+ inline const std::vector<MultiFiltration> &get_filtrations() const { return truc_ptr->get_filtrations(); }
795
+
796
+ inline const std::vector<int> &get_dimensions() const { return truc_ptr->get_dimensions(); }
797
+
798
+ inline const std::vector<std::vector<index_type>> &get_boundaries() const { return truc_ptr->get_boundaries(); }
799
+
800
+ inline void coarsen_on_grid_inplace(const std::vector<std::vector<typename MultiFiltration::value_type>> &grid,
801
+ bool coordinate = true) {
802
+ truc_ptr->coarsen_on_grid_inplace(grid, coordinate);
803
+ }
804
+
805
+ template <typename Subfiltration>
806
+ inline void push_to(const Subfiltration &f) {
807
+ truc_ptr->push_to_out(f, this->filtration_container, this->generator_order);
808
+ }
809
+
810
+ inline std::vector<typename PersBackend::cycle_type> get_representative_cycles(bool update = true) {
811
+ return truc_ptr->get_representative_cycles(update);
812
+ }
813
+
814
+ inline void compute_persistence(const bool ignore_inf = true) {
815
+ this->persistence =
816
+ this->truc_ptr->compute_persistence_out(this->filtration_container, this->generator_order, ignore_inf);
817
+ };
818
+
819
+ inline void vineyard_update() {
820
+ truc_ptr->vineyard_update(this->persistence, this->filtration_container, this->generator_order);
821
+ }
822
+
823
+ template <typename value_type = value_type>
824
+ inline flat_barcode<value_type> get_flat_barcode() {
825
+ return truc_ptr->get_flat_barcode(this->persistence, this->filtration_container);
826
+ }
827
+
828
+ template <typename value_type = value_type>
829
+ inline flat_nodim_barcode<value_type> get_flat_nodim_barcode() {
830
+ return truc_ptr->get_flat_nodim_barcode(this->persistence, this->filtration_container);
831
+ }
832
+
833
+ inline split_barcode get_barcode() { return truc_ptr->get_barcode(this->persistence, this->filtration_container); }
834
+
835
+ inline std::size_t num_generators() const { return this->truc_ptr->structure.size(); }
836
+
837
+ inline std::size_t num_parameters() const {
838
+ return num_generators() == 0 ? 0 : this->get_filtrations()[0].num_parameters();
839
+ }
840
+
841
+ inline const std::vector<typename MultiFiltration::value_type> &get_one_filtration() const {
842
+ return this->filtration_container;
843
+ }
844
+
845
+ inline std::vector<typename MultiFiltration::value_type> &get_one_filtration() {
846
+ return this->filtration_container;
847
+ }
848
+
849
+ private:
850
+ const Truc *truc_ptr;
851
+ std::vector<index_type> generator_order; // size fixed at construction time,
852
+ std::vector<typename MultiFiltration::value_type> filtration_container; // filtration of the current slice
853
+ PersBackend persistence; // generated by the structure, and generator_order.
854
+
855
+ }; // class TrucThread
856
+
857
+ /*
858
+ * returns barcodes of the f(multipers)
859
+ *
860
+ */
861
+ template <typename Fun, typename Fun_arg>
862
+ inline std::vector<split_barcode> barcodes(Fun &&f, const std::vector<Fun_arg> &args, const bool ignore_inf = true) {
863
+ if (args.size() == 0) {
864
+ return {};
865
+ }
866
+ std::vector<split_barcode> out(args.size());
867
+
868
+ if constexpr (PersBackend::is_vine) {
869
+ this->push_to(f(args[0]));
870
+ this->compute_persistence();
871
+ out[0] = this->get_barcode();
872
+ for (auto i = 1u; i < args.size(); ++i) {
873
+ this->push_to(f(args[i]));
874
+ this->vineyard_update();
875
+ out[i] = this->get_barcode();
876
+ }
877
+
878
+ } else {
879
+ ThreadSafe local_template = this->weak_copy();
880
+ tbb::enumerable_thread_specific<ThreadSafe> thread_locals(local_template);
881
+ tbb::parallel_for(static_cast<std::size_t>(0), args.size(), [&](const std::size_t &i) {
882
+ ThreadSafe &s = thread_locals.local();
883
+ s.push_to(f(args[i]));
884
+ s.compute_persistence(ignore_inf);
885
+ out[i] = s.get_barcode();
886
+ });
887
+ }
888
+ return out;
889
+ }
890
+
891
+ // FOR Python interface, but I'm not fan. Todo: do the lambda function in
892
+ // cython?
893
+ inline std::vector<split_barcode> persistence_on_lines(const std::vector<std::vector<value_type>> &basepoints,
894
+ bool ignore_inf) {
895
+ return barcodes(
896
+ [](const std::vector<value_type> &basepoint) { return Gudhi::multi_persistence::Line<value_type>(basepoint); },
897
+ basepoints,
898
+ ignore_inf);
899
+ }
900
+
901
+ inline std::vector<split_barcode> persistence_on_lines(
902
+ const std::vector<std::pair<std::vector<value_type>, std::vector<value_type>>> &bp_dirs,
903
+ bool ignore_inf) {
904
+ return barcodes(
905
+ [](const std::pair<std::vector<value_type>, std::vector<value_type>> &bpdir) {
906
+ return Gudhi::multi_persistence::Line<value_type>(bpdir.first, bpdir.second);
907
+ },
908
+ bp_dirs,
909
+ ignore_inf);
910
+ }
911
+
912
+ void build_from_scc_file(const std::string &inFilePath,
913
+ bool isRivetCompatible = false,
914
+ bool isReversed = false,
915
+ int shiftDimensions = 0) {
916
+ *this = read_scc_file<Truc>(inFilePath, isRivetCompatible, isReversed, shiftDimensions);
917
+ }
918
+
919
+ void write_to_scc_file(const std::string &outFilePath,
920
+ int numberOfParameters = -1,
921
+ int degree = -1,
922
+ bool rivetCompatible = false,
923
+ bool IgnoreLastGenerators = false,
924
+ bool stripComments = false,
925
+ bool reverse = false) {
926
+ write_scc_file<Truc>(
927
+ outFilePath, *this, numberOfParameters, degree, rivetCompatible, IgnoreLastGenerators, stripComments, reverse);
928
+ }
929
+
930
+ public:
931
+ using ThreadSafe = TrucThread; // for outside
932
+
933
+ TrucThread weak_copy() const { return TrucThread(*this); }
934
+
935
+ // TODO: declare method here instead of scc_io.h
936
+ // it is just temporary, until Truc is cleaned up
937
+ // friend void write_scc_file<Truc>(const std::string &outFilePath,
938
+ // const Truc &slicer,
939
+ // int numberOfParameters,
940
+ // int degree,
941
+ // bool rivetCompatible,
942
+ // bool IgnoreLastGenerators,
943
+ // bool stripComments,
944
+ // bool reverse);
945
+
946
+ private:
947
+ std::vector<MultiFiltration> generator_filtration_values; // defined at construction time. Const
948
+ std::vector<index_type> generator_order; // size fixed at construction time, // TODO : CHANGE THAT TO UINT32
949
+ Structure structure; // defined at construction time. Const
950
+ std::vector<typename MultiFiltration::value_type> filtration_container; // filtration of the current slice
951
+ PersBackend persistence; // generated by the structure, and generator_order.
952
+
953
+ }; // class Truc
954
+
955
+ } // namespace truc_interface
956
+ } // namespace multiparameter
957
+ } // namespace Gudhi