multipers 2.2.3__cp310-cp310-win_amd64.whl → 2.3.0__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/filtrations/filtrations.py +289 -0
  15. multipers/filtrations.pxd +224 -224
  16. multipers/function_rips.cp310-win_amd64.pyd +0 -0
  17. multipers/function_rips.pyx +105 -105
  18. multipers/grids.cp310-win_amd64.pyd +0 -0
  19. multipers/grids.pyx +350 -350
  20. multipers/gudhi/Persistence_slices_interface.h +132 -132
  21. multipers/gudhi/Simplex_tree_interface.h +239 -245
  22. multipers/gudhi/Simplex_tree_multi_interface.h +516 -561
  23. multipers/gudhi/cubical_to_boundary.h +59 -59
  24. multipers/gudhi/gudhi/Bitmap_cubical_complex.h +450 -450
  25. multipers/gudhi/gudhi/Bitmap_cubical_complex_base.h +1070 -1070
  26. multipers/gudhi/gudhi/Bitmap_cubical_complex_periodic_boundary_conditions_base.h +579 -579
  27. multipers/gudhi/gudhi/Debug_utils.h +45 -45
  28. multipers/gudhi/gudhi/Fields/Multi_field.h +484 -484
  29. multipers/gudhi/gudhi/Fields/Multi_field_operators.h +455 -455
  30. multipers/gudhi/gudhi/Fields/Multi_field_shared.h +450 -450
  31. multipers/gudhi/gudhi/Fields/Multi_field_small.h +531 -531
  32. multipers/gudhi/gudhi/Fields/Multi_field_small_operators.h +507 -507
  33. multipers/gudhi/gudhi/Fields/Multi_field_small_shared.h +531 -531
  34. multipers/gudhi/gudhi/Fields/Z2_field.h +355 -355
  35. multipers/gudhi/gudhi/Fields/Z2_field_operators.h +376 -376
  36. multipers/gudhi/gudhi/Fields/Zp_field.h +420 -420
  37. multipers/gudhi/gudhi/Fields/Zp_field_operators.h +400 -400
  38. multipers/gudhi/gudhi/Fields/Zp_field_shared.h +418 -418
  39. multipers/gudhi/gudhi/Flag_complex_edge_collapser.h +337 -337
  40. multipers/gudhi/gudhi/Matrix.h +2107 -2107
  41. multipers/gudhi/gudhi/Multi_critical_filtration.h +1038 -1038
  42. multipers/gudhi/gudhi/Multi_persistence/Box.h +171 -171
  43. multipers/gudhi/gudhi/Multi_persistence/Line.h +282 -282
  44. multipers/gudhi/gudhi/Off_reader.h +173 -173
  45. multipers/gudhi/gudhi/One_critical_filtration.h +1432 -1431
  46. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix.h +769 -769
  47. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix_with_column_compression.h +686 -686
  48. multipers/gudhi/gudhi/Persistence_matrix/Boundary_matrix.h +842 -842
  49. multipers/gudhi/gudhi/Persistence_matrix/Chain_matrix.h +1350 -1350
  50. multipers/gudhi/gudhi/Persistence_matrix/Id_to_index_overlay.h +1105 -1105
  51. multipers/gudhi/gudhi/Persistence_matrix/Position_to_index_overlay.h +859 -859
  52. multipers/gudhi/gudhi/Persistence_matrix/RU_matrix.h +910 -910
  53. multipers/gudhi/gudhi/Persistence_matrix/allocators/entry_constructors.h +139 -139
  54. multipers/gudhi/gudhi/Persistence_matrix/base_pairing.h +230 -230
  55. multipers/gudhi/gudhi/Persistence_matrix/base_swap.h +211 -211
  56. multipers/gudhi/gudhi/Persistence_matrix/boundary_cell_position_to_id_mapper.h +60 -60
  57. multipers/gudhi/gudhi/Persistence_matrix/boundary_face_position_to_id_mapper.h +60 -60
  58. multipers/gudhi/gudhi/Persistence_matrix/chain_pairing.h +136 -136
  59. multipers/gudhi/gudhi/Persistence_matrix/chain_rep_cycles.h +190 -190
  60. multipers/gudhi/gudhi/Persistence_matrix/chain_vine_swap.h +616 -616
  61. multipers/gudhi/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h +150 -150
  62. multipers/gudhi/gudhi/Persistence_matrix/columns/column_dimension_holder.h +106 -106
  63. multipers/gudhi/gudhi/Persistence_matrix/columns/column_utilities.h +219 -219
  64. multipers/gudhi/gudhi/Persistence_matrix/columns/entry_types.h +327 -327
  65. multipers/gudhi/gudhi/Persistence_matrix/columns/heap_column.h +1140 -1140
  66. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_list_column.h +934 -934
  67. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_set_column.h +934 -934
  68. multipers/gudhi/gudhi/Persistence_matrix/columns/list_column.h +980 -980
  69. multipers/gudhi/gudhi/Persistence_matrix/columns/naive_vector_column.h +1092 -1092
  70. multipers/gudhi/gudhi/Persistence_matrix/columns/row_access.h +192 -192
  71. multipers/gudhi/gudhi/Persistence_matrix/columns/set_column.h +921 -921
  72. multipers/gudhi/gudhi/Persistence_matrix/columns/small_vector_column.h +1093 -1093
  73. multipers/gudhi/gudhi/Persistence_matrix/columns/unordered_set_column.h +1012 -1012
  74. multipers/gudhi/gudhi/Persistence_matrix/columns/vector_column.h +1244 -1244
  75. multipers/gudhi/gudhi/Persistence_matrix/matrix_dimension_holders.h +186 -186
  76. multipers/gudhi/gudhi/Persistence_matrix/matrix_row_access.h +164 -164
  77. multipers/gudhi/gudhi/Persistence_matrix/ru_pairing.h +156 -156
  78. multipers/gudhi/gudhi/Persistence_matrix/ru_rep_cycles.h +376 -376
  79. multipers/gudhi/gudhi/Persistence_matrix/ru_vine_swap.h +540 -540
  80. multipers/gudhi/gudhi/Persistent_cohomology/Field_Zp.h +118 -118
  81. multipers/gudhi/gudhi/Persistent_cohomology/Multi_field.h +173 -173
  82. multipers/gudhi/gudhi/Persistent_cohomology/Persistent_cohomology_column.h +128 -128
  83. multipers/gudhi/gudhi/Persistent_cohomology.h +745 -745
  84. multipers/gudhi/gudhi/Points_off_io.h +171 -171
  85. multipers/gudhi/gudhi/Simple_object_pool.h +69 -69
  86. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_iterators.h +463 -463
  87. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h +83 -83
  88. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_siblings.h +106 -106
  89. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_star_simplex_iterators.h +277 -277
  90. multipers/gudhi/gudhi/Simplex_tree/hooks_simplex_base.h +62 -62
  91. multipers/gudhi/gudhi/Simplex_tree/indexing_tag.h +27 -27
  92. multipers/gudhi/gudhi/Simplex_tree/serialization_utils.h +62 -62
  93. multipers/gudhi/gudhi/Simplex_tree/simplex_tree_options.h +157 -157
  94. multipers/gudhi/gudhi/Simplex_tree.h +2794 -2794
  95. multipers/gudhi/gudhi/Simplex_tree_multi.h +152 -163
  96. multipers/gudhi/gudhi/distance_functions.h +62 -62
  97. multipers/gudhi/gudhi/graph_simplicial_complex.h +104 -104
  98. multipers/gudhi/gudhi/persistence_interval.h +253 -253
  99. multipers/gudhi/gudhi/persistence_matrix_options.h +170 -170
  100. multipers/gudhi/gudhi/reader_utils.h +367 -367
  101. multipers/gudhi/mma_interface_coh.h +256 -255
  102. multipers/gudhi/mma_interface_h0.h +223 -231
  103. multipers/gudhi/mma_interface_matrix.h +284 -282
  104. multipers/gudhi/naive_merge_tree.h +536 -575
  105. multipers/gudhi/scc_io.h +310 -289
  106. multipers/gudhi/truc.h +890 -888
  107. multipers/io.cp310-win_amd64.pyd +0 -0
  108. multipers/io.pyx +711 -711
  109. multipers/ml/accuracies.py +90 -90
  110. multipers/ml/convolutions.py +520 -520
  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 -4
  122. multipers/mma_structures.pyx.tp +1085 -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 +2296 -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 +216 -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 +18 -15
  156. multipers/simplex_tree_multi.pyx.tp +1939 -1935
  157. multipers/slicer.cp310-win_amd64.pyd +0 -0
  158. multipers/slicer.pxd +81 -20
  159. multipers/slicer.pxd.tp +215 -214
  160. multipers/slicer.pyx +1091 -308
  161. multipers/slicer.pyx.tp +924 -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.0.dist-info}/LICENSE +21 -21
  169. {multipers-2.2.3.dist-info → multipers-2.3.0.dist-info}/METADATA +21 -11
  170. multipers-2.3.0.dist-info/RECORD +182 -0
  171. multipers/tests/test_diff_helper.py +0 -73
  172. multipers/tests/test_hilbert_function.py +0 -82
  173. multipers/tests/test_mma.py +0 -83
  174. multipers/tests/test_point_clouds.py +0 -49
  175. multipers/tests/test_python-cpp_conversion.py +0 -82
  176. multipers/tests/test_signed_betti.py +0 -181
  177. multipers/tests/test_signed_measure.py +0 -89
  178. multipers/tests/test_simplextreemulti.py +0 -221
  179. multipers/tests/test_slicer.py +0 -221
  180. multipers-2.2.3.dist-info/RECORD +0 -189
  181. {multipers-2.2.3.dist-info → multipers-2.3.0.dist-info}/WHEEL +0 -0
  182. {multipers-2.2.3.dist-info → multipers-2.3.0.dist-info}/top_level.txt +0 -0
multipers/gudhi/truc.h CHANGED
@@ -1,888 +1,890 @@
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 <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 truc_interface
889
+ } // namespace multiparameter
890
+ } // namespace Gudhi