summaryrefslogtreecommitdiff
path: root/src/Simplex_tree/test/UnitTestSimplexTree.cpp
blob: a3671f56c84ed21cee658f3b1e088e385738cce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#define BOOST_TEST_MODULE const_string test
#include <boost/test/included/unit_test.hpp>
#include <boost/system/error_code.hpp>
#include <boost/chrono/thread_clock.hpp>
#include <iostream>
#include <string>

#include <utility> // std::pair, std::make_pair

#include <cmath> // float comparison
#include <limits>

#include "gudhi/graph_simplicial_complex.h"
#include "gudhi/reader_utils.h"
#include "gudhi/Simplex_tree.h"


using namespace Gudhi;

typedef Simplex_tree<> typeST;
typedef std::pair< typeST::Simplex_handle, bool > typePairSimplexBool;
typedef std::vector< Vertex_handle > typeVectorVertex;
typedef std::pair<typeVectorVertex, Filtration_value> typeSimplex;

const Vertex_handle DEFAULT_VERTEX_HANDLE = (const Vertex_handle)-1;

void test_empty_simplex_tree(typeST& tst)
{
	BOOST_CHECK( tst.null_vertex() == DEFAULT_VERTEX_HANDLE );
	BOOST_CHECK( tst.filtration() == Filtration_value(0) );
	BOOST_CHECK( tst.num_vertices() == (size_t)0 );
	BOOST_CHECK( tst.num_simplices() == (size_t)0 );
	typeST::Siblings* STRoot = tst.root();
	BOOST_CHECK( STRoot != NULL );
	BOOST_CHECK( STRoot->oncles() == NULL );
	BOOST_CHECK( STRoot->parent() == DEFAULT_VERTEX_HANDLE );
	BOOST_CHECK( tst.dimension() == -1 );
}

void test_iterators_on_empty_simplex_tree(typeST& tst)
{
	std::cout << "Iterator on vertices: " << std::endl;
	for( auto vertex : tst.complex_vertex_range() )
	{
		std::cout << "vertice:" << vertex << std::endl;
		BOOST_CHECK( false); // shall be empty
	}
	std::cout << "Iterator on simplices: " << std::endl;
	for( auto simplex : tst.complex_simplex_range() )
	{
		BOOST_CHECK( false); // shall be empty
	}

	std::cout << "Iterator on Simplices in the filtration, with [filtration value]:" << std::endl;
	for( auto f_simplex : tst.filtration_simplex_range() )
	{
		BOOST_CHECK( false); // shall be empty
		std::cout << "test_simplex_tree_contains - filtration=" << tst.filtration(f_simplex) << std::endl;
	}
}

BOOST_AUTO_TEST_CASE( simplex_tree_when_empty )
{
	const Filtration_value DEFAULT_FILTRATION_VALUE = 0;

	// TEST OF DEFAULT CONSTRUCTOR
	std::cout << "********************************************************************" << std::endl;
	std::cout << "TEST OF DEFAULT CONSTRUCTOR" << std::endl;
	typeST st;

	test_empty_simplex_tree(st);

	test_iterators_on_empty_simplex_tree(st);
	// TEST OF EMPTY INSERTION
	std::cout << "TEST OF EMPTY INSERTION" << std::endl;
	typeVectorVertex simplexVectorEmpty;
	BOOST_CHECK(simplexVectorEmpty.empty() == true);
	typePairSimplexBool returnEmptyValue = st.insert ( simplexVectorEmpty, DEFAULT_FILTRATION_VALUE );
	BOOST_CHECK(returnEmptyValue.first == typeST::Simplex_handle(NULL));
	BOOST_CHECK(returnEmptyValue.second == true);

	test_empty_simplex_tree(st);

	test_iterators_on_empty_simplex_tree(st);
}

bool AreAlmostTheSame(float a, float b)
{
	return std::fabs(a - b) < std::numeric_limits<float>::epsilon();
}

BOOST_AUTO_TEST_CASE( simplex_tree_from_file )
{
	// TEST OF INSERTION
	std::cout << "********************************************************************" << std::endl;
	std::cout << "TEST OF SIMPLEX TREE FROM A FILE" << std::endl;
	typeST st;

	std::string inputFile("simplex_tree_for_unit_test.txt");
	std::ifstream simplex_tree_stream(inputFile.c_str());
	simplex_tree_stream >> st;

	// Display the Simplex_tree
	std::cout << "The complex contains " << st.num_simplices() << " simplices" << std::endl;
	std::cout << "   - dimension " << st.dimension() << "   - filtration " << st.filtration() << std::endl;

	// Check
	BOOST_CHECK(st.num_simplices() == 143353);
	BOOST_CHECK(st.dimension() == 3);
	BOOST_CHECK(st.filtration() == 0.4);

	int previous_size=0;
	for( auto f_simplex : st.filtration_simplex_range() )
	{
		// Size of simplex
		int size=0;
		for( auto vertex : st.simplex_vertex_range(f_simplex) )
		{
			size++;
		}
		BOOST_CHECK(AreAlmostTheSame(st.filtration(f_simplex),(0.1* size))); // Specific test: filtration = 0.1 * simplex_size
		BOOST_CHECK(previous_size <= size); // Check list is sorted (because of sorted filtrations in simplex_tree.txt)
		previous_size = size;
	}
	simplex_tree_stream.close();
}

void test_simplex_tree_contains(typeST& simplexTree, typeSimplex simplex, int pos)
{
	auto f_simplex = simplexTree.filtration_simplex_range().begin();
	f_simplex += pos;
	std::cout << "test_simplex_tree_contains - filtration=" << simplexTree.filtration(*f_simplex) << "||" << simplex.second << std::endl;
	BOOST_CHECK( AreAlmostTheSame(simplexTree.filtration(*f_simplex),simplex.second) );

	typeVectorVertex::iterator simplexIter = simplex.first.end()-1;
	for( auto vertex : simplexTree.simplex_vertex_range(*f_simplex) )
	{
		std::cout << "test_simplex_tree_contains - vertex=" << vertex << "||" << *simplexIter << std::endl;
		BOOST_CHECK( vertex ==  *simplexIter);
		simplexIter--;
	}
}

void test_simplex_tree_insert_returns_true(const typePairSimplexBool& returnValue)
{
	BOOST_CHECK(returnValue.second == true);
	typeST::Simplex_handle shReturned = returnValue.first; // Simplex_handle = boost::container::flat_map< Vertex_handle, Node >::iterator
	BOOST_CHECK(shReturned != typeST::Simplex_handle(NULL));
}

// Global variables
Filtration_value max_fil = 0.0;
int dim_max = -1;

void set_and_test_simplex_tree_dim_fil(typeST& simplexTree, int vectorSize, const Filtration_value& fil)
{
	if (vectorSize > dim_max+1)
	{
		dim_max = vectorSize-1;
		simplexTree.set_dimension(dim_max);
		std::cout << "   set_and_test_simplex_tree_dim_fil - dim_max=" << dim_max << std::endl;
	}
	if (fil > max_fil)
	{
		max_fil = fil;
		simplexTree.set_filtration(max_fil);
		std::cout << "   set_and_test_simplex_tree_dim_fil - max_fil=" << max_fil << std::endl;
	}
	int nb_simplices = simplexTree.num_simplices() + 1;
	simplexTree.set_num_simplices(nb_simplices);

	BOOST_CHECK( simplexTree.dimension() == dim_max );
	BOOST_CHECK( AreAlmostTheSame(simplexTree.filtration(), max_fil) );
	BOOST_CHECK( simplexTree.num_simplices() == nb_simplices );
}

BOOST_AUTO_TEST_CASE( simplex_tree_insertion )
{
	const Filtration_value FIRST_FILTRATION_VALUE = 0.1;
	const Filtration_value SECOND_FILTRATION_VALUE = 0.2;
	const Filtration_value THIRD_FILTRATION_VALUE = 0.3;
	const Filtration_value FOURTH_FILTRATION_VALUE = 0.4;
	Vertex_handle FIRST_VERTEX_HANDLE =  (Vertex_handle)0;
	Vertex_handle SECOND_VERTEX_HANDLE = (Vertex_handle)1;
	Vertex_handle THIRD_VERTEX_HANDLE = (Vertex_handle)2;
	Vertex_handle FOURTH_VERTEX_HANDLE = (Vertex_handle)3;

	// TEST OF INSERTION
	std::cout << "********************************************************************" << std::endl;
	std::cout << "TEST OF INSERTION" << std::endl;
	typeST st;

	// ++ FIRST
	std::cout << "   - INSERT 0" << std::endl;
	typeVectorVertex firstSimplexVector;
	firstSimplexVector.push_back(FIRST_VERTEX_HANDLE);
	BOOST_CHECK( firstSimplexVector.size() == 1 );
	typeSimplex firstSimplex = std::make_pair(firstSimplexVector, Filtration_value(FIRST_FILTRATION_VALUE));
	typePairSimplexBool returnValue =
			st.insert ( firstSimplex.first, firstSimplex.second );

	test_simplex_tree_insert_returns_true(returnValue);
	set_and_test_simplex_tree_dim_fil(st, firstSimplexVector.size(), firstSimplex.second);
	BOOST_CHECK( st.num_vertices() == (size_t)1 );

	// ++ SECOND
	std::cout << "   - INSERT 1" << std::endl;
	typeVectorVertex secondSimplexVector;
	secondSimplexVector.push_back(SECOND_VERTEX_HANDLE);
	BOOST_CHECK( secondSimplexVector.size() == 1 );
	typeSimplex secondSimplex = std::make_pair(secondSimplexVector, Filtration_value(FIRST_FILTRATION_VALUE));
	returnValue =
			st.insert ( secondSimplex.first, secondSimplex.second );

	test_simplex_tree_insert_returns_true(returnValue);
	set_and_test_simplex_tree_dim_fil(st, secondSimplexVector.size(), secondSimplex.second);
	BOOST_CHECK( st.num_vertices() == (size_t)2 );

	// ++ THIRD
	std::cout << "   - INSERT (0,1)" << std::endl;
	typeVectorVertex thirdSimplexVector;
	thirdSimplexVector.push_back(FIRST_VERTEX_HANDLE);
	thirdSimplexVector.push_back(SECOND_VERTEX_HANDLE);
	BOOST_CHECK( thirdSimplexVector.size() == 2 );
	typeSimplex thirdSimplex = std::make_pair(thirdSimplexVector, Filtration_value(SECOND_FILTRATION_VALUE));
	returnValue =
			st.insert ( thirdSimplex.first, thirdSimplex.second );

	test_simplex_tree_insert_returns_true(returnValue);
	set_and_test_simplex_tree_dim_fil(st, thirdSimplexVector.size(), thirdSimplex.second);
	BOOST_CHECK( st.num_vertices() == (size_t)2 ); // Not incremented !!

	// ++ FOURTH
	std::cout << "   - INSERT 2" << std::endl;
	typeVectorVertex fourthSimplexVector;
	fourthSimplexVector.push_back(THIRD_VERTEX_HANDLE);
	BOOST_CHECK( fourthSimplexVector.size() == 1 );
	typeSimplex fourthSimplex = std::make_pair(fourthSimplexVector, Filtration_value(FIRST_FILTRATION_VALUE));
	returnValue =
			st.insert ( fourthSimplex.first, fourthSimplex.second );

	test_simplex_tree_insert_returns_true(returnValue);
	set_and_test_simplex_tree_dim_fil(st, fourthSimplexVector.size(), fourthSimplex.second);
	BOOST_CHECK( st.num_vertices() == (size_t)3 );

	// ++ FIFTH
	std::cout << "   - INSERT (2,0)" << std::endl;
	typeVectorVertex fifthSimplexVector;
	fifthSimplexVector.push_back(THIRD_VERTEX_HANDLE);
	fifthSimplexVector.push_back(FIRST_VERTEX_HANDLE);
	BOOST_CHECK( fifthSimplexVector.size() == 2 );
	typeSimplex fifthSimplex = std::make_pair(fifthSimplexVector, Filtration_value(SECOND_FILTRATION_VALUE));
	returnValue =
			st.insert ( fifthSimplex.first, fifthSimplex.second );

	test_simplex_tree_insert_returns_true(returnValue);
	set_and_test_simplex_tree_dim_fil(st, fifthSimplexVector.size(), fifthSimplex.second);
	BOOST_CHECK( st.num_vertices() == (size_t)3 ); // Not incremented !!

	// ++ SIXTH
	std::cout << "   - INSERT (2,1)" << std::endl;
	typeVectorVertex sixthSimplexVector;
	sixthSimplexVector.push_back(THIRD_VERTEX_HANDLE);
	sixthSimplexVector.push_back(SECOND_VERTEX_HANDLE);
	BOOST_CHECK( sixthSimplexVector.size() == 2 );
	typeSimplex sixthSimplex = std::make_pair(sixthSimplexVector, Filtration_value(SECOND_FILTRATION_VALUE));
	returnValue =
			st.insert ( sixthSimplex.first, sixthSimplex.second );

	test_simplex_tree_insert_returns_true(returnValue);
	set_and_test_simplex_tree_dim_fil(st, sixthSimplexVector.size(), sixthSimplex.second);
	BOOST_CHECK( st.num_vertices() == (size_t)3 ); // Not incremented !!

	// ++ SEVENTH
	std::cout << "   - INSERT (2,1,0)" << std::endl;
	typeVectorVertex seventhSimplexVector;
	seventhSimplexVector.push_back(THIRD_VERTEX_HANDLE);
	seventhSimplexVector.push_back(SECOND_VERTEX_HANDLE);
	seventhSimplexVector.push_back(FIRST_VERTEX_HANDLE);
	BOOST_CHECK( seventhSimplexVector.size() == 3 );
	typeSimplex seventhSimplex = std::make_pair(seventhSimplexVector, Filtration_value(THIRD_FILTRATION_VALUE));
	returnValue =
			st.insert ( seventhSimplex.first, seventhSimplex.second );

	test_simplex_tree_insert_returns_true(returnValue);
	set_and_test_simplex_tree_dim_fil(st, seventhSimplexVector.size(), seventhSimplex.second);
	BOOST_CHECK( st.num_vertices() == (size_t)3 ); // Not incremented !!

	// ++ EIGHTH
	std::cout << "   - INSERT 3" << std::endl;
	typeVectorVertex eighthSimplexVector;
	eighthSimplexVector.push_back(FOURTH_VERTEX_HANDLE);
	BOOST_CHECK( eighthSimplexVector.size() == 1 );
	typeSimplex eighthSimplex = std::make_pair(eighthSimplexVector, Filtration_value(FIRST_FILTRATION_VALUE));
	returnValue =
			st.insert ( eighthSimplex.first, eighthSimplex.second );

	test_simplex_tree_insert_returns_true(returnValue);
	set_and_test_simplex_tree_dim_fil(st, eighthSimplexVector.size(), eighthSimplex.second);
	BOOST_CHECK( st.num_vertices() == (size_t)4 );

	// ++ NINETH
	std::cout << "   - INSERT (3,0)" << std::endl;
	typeVectorVertex ninethSimplexVector;
	ninethSimplexVector.push_back(FOURTH_VERTEX_HANDLE);
	ninethSimplexVector.push_back(FIRST_VERTEX_HANDLE);
	BOOST_CHECK( ninethSimplexVector.size() == 2 );
	typeSimplex ninethSimplex = std::make_pair(ninethSimplexVector, Filtration_value(SECOND_FILTRATION_VALUE));
	returnValue =
			st.insert ( ninethSimplex.first, ninethSimplex.second );

	test_simplex_tree_insert_returns_true(returnValue);
	set_and_test_simplex_tree_dim_fil(st, ninethSimplexVector.size(), ninethSimplex.second);
	BOOST_CHECK( st.num_vertices() == (size_t)4 ); // Not incremented !!

	// ++ TENTH
	std::cout << "   - INSERT 0 (already inserted)" << std::endl;
	typeVectorVertex tenthSimplexVector;
	tenthSimplexVector.push_back(FIRST_VERTEX_HANDLE);
	BOOST_CHECK( tenthSimplexVector.size() == 1 );
	typeSimplex tenthSimplex = std::make_pair(tenthSimplexVector, Filtration_value(FOURTH_FILTRATION_VALUE)); // With a different filtration value
	returnValue =
			st.insert ( tenthSimplex.first, tenthSimplex.second );

	BOOST_CHECK(returnValue.second == false);
	typeST::Simplex_handle shReturned = returnValue.first; // Simplex_handle = boost::container::flat_map< Vertex_handle, Node >::iterator
	BOOST_CHECK(shReturned == typeST::Simplex_handle(NULL));
	BOOST_CHECK( st.num_vertices() == (size_t)4 ); // Not incremented !!
	BOOST_CHECK( st.dimension() == dim_max );
	BOOST_CHECK( AreAlmostTheSame(st.filtration(), max_fil) );

	// ++ ELEVENTH
	std::cout << "   - INSERT (2,1,0) (already inserted)" << std::endl;
	typeVectorVertex eleventhSimplexVector;
	eleventhSimplexVector.push_back(THIRD_VERTEX_HANDLE);
	eleventhSimplexVector.push_back(SECOND_VERTEX_HANDLE);
	eleventhSimplexVector.push_back(FIRST_VERTEX_HANDLE);
	BOOST_CHECK( eleventhSimplexVector.size() == 3 );
	typeSimplex eleventhSimplex = std::make_pair(eleventhSimplexVector, Filtration_value(FOURTH_FILTRATION_VALUE));
	returnValue =
			st.insert ( eleventhSimplex.first, eleventhSimplex.second );

	BOOST_CHECK(returnValue.second == false);
	shReturned = returnValue.first; // Simplex_handle = boost::container::flat_map< Vertex_handle, Node >::iterator
	BOOST_CHECK(shReturned == typeST::Simplex_handle(NULL));
	BOOST_CHECK( st.num_vertices() == (size_t)4 ); // Not incremented !!
	BOOST_CHECK( st.dimension() == dim_max );
	BOOST_CHECK( AreAlmostTheSame(st.filtration(), max_fil) );

	//    1
	//    o
	//   /X\
	//  o---o---o
	//  2   0   3

	//   [0.1] 0
	//   [0.1] 1
	//   [0.1] 2
	//   [0.1] 3
	//   [0.2] 1 0
	//   [0.2] 2 0
	//   [0.2] 2 1
	//   [0.2] 3 0
	//   [0.3] 2 1 0

	//  !! Be careful, simplex are sorted by filtration value on insertion !!
	std::cout << "simplex_tree_insertion - first - 0" << std::endl;
	test_simplex_tree_contains(st, firstSimplex, 0); // (0) -> 0
	std::cout << "simplex_tree_insertion - second - 1" << std::endl;
	test_simplex_tree_contains(st, secondSimplex, 1); // (1) -> 1
	std::cout << "simplex_tree_insertion - third - 4" << std::endl;
	test_simplex_tree_contains(st, thirdSimplex, 4); // (0,1) -> 4
	std::cout << "simplex_tree_insertion - fourth - 2" << std::endl;
	test_simplex_tree_contains(st, fourthSimplex, 2); // (2) -> 2
	std::cout << "simplex_tree_insertion - fifth - 5" << std::endl;
	test_simplex_tree_contains(st, fifthSimplex, 5); // (2,0) -> 5
	std::cout << "simplex_tree_insertion - sixth - 6" << std::endl;
	test_simplex_tree_contains(st, sixthSimplex, 6); //(2,1) -> 6
	std::cout << "simplex_tree_insertion - seventh - 8" << std::endl;
	test_simplex_tree_contains(st, seventhSimplex, 8); // (2,1,0) -> 8
	std::cout << "simplex_tree_insertion - eighth - 3" << std::endl;
	test_simplex_tree_contains(st, eighthSimplex, 3); // (3) -> 3
	std::cout << "simplex_tree_insertion - nineth - 7" << std::endl;
	test_simplex_tree_contains(st, ninethSimplex, 7); // (3,0) -> 7

	// Display the Simplex_tree - Can not be done in the middle of 2 inserts
	std::cout << "The complex contains " << st.num_simplices() << " simplices" << std::endl;
	std::cout << "   - dimension " << st.dimension() << "   - filtration " << st.filtration() << std::endl;
	std::cout << std::endl << std::endl << "Iterator on Simplices in the filtration, with [filtration value]:" << std::endl;
	for( auto f_simplex : st.filtration_simplex_range() )
	{
		std::cout << "   " << "[" << st.filtration(f_simplex) << "] ";
		for( auto vertex : st.simplex_vertex_range(f_simplex) )
		{
			std::cout << (int)vertex << " ";
		}
		std::cout << std::endl;
	}

}