From 134aaa68ab6a5983a9569a123a18550535afa2ef Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Tue, 26 Apr 2016 15:18:57 +0000 Subject: Unitary test of Simplex tree Rename example Makefile test part modification git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/ST_cythonize@1143 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: d70e1f471dfc6f4543335862b4c1aa5250e73343 --- src/cython/Makefile | 3 +- src/cython/Simplex_tree_UT.py | 73 ++++++++++++++++++++++++++++++++++++++ src/cython/Simplex_tree_example.py | 65 +++++++++++++++++++++++++++++++++ src/cython/main.py | 65 --------------------------------- 4 files changed, 140 insertions(+), 66 deletions(-) create mode 100755 src/cython/Simplex_tree_UT.py create mode 100755 src/cython/Simplex_tree_example.py delete mode 100755 src/cython/main.py diff --git a/src/cython/Makefile b/src/cython/Makefile index b694ab47..c17e9e09 100644 --- a/src/cython/Makefile +++ b/src/cython/Makefile @@ -2,7 +2,8 @@ ext: python setup.py build_ext --inplace test: - python main.py + python Simplex_tree_UT.py + python Simplex_tree_example.py clean: rm -rf build/ *.o *.so *.cpp diff --git a/src/cython/Simplex_tree_UT.py b/src/cython/Simplex_tree_UT.py new file mode 100755 index 00000000..56113370 --- /dev/null +++ b/src/cython/Simplex_tree_UT.py @@ -0,0 +1,73 @@ +import unittest + +import gudhi + +class TestSimplexTree(unittest.TestCase): + + def test_insertion(self): + st = gudhi.SimplexTree() + + # insert test + self.assertTrue(st.insert([0,1])) + self.assertTrue(st.insert([0,1,2], filtration=4.0)) + self.assertEqual(st.num_simplices(), 7) + self.assertEqual(st.num_vertices(), 3) + + # find test + self.assertTrue(st.find([0,1,2])) + self.assertTrue(st.find([0,1])) + self.assertTrue(st.find([0,2])) + self.assertTrue(st.find([0])) + self.assertTrue(st.find([1])) + self.assertTrue(st.find([2])) + self.assertFalse(st.find([3])) + self.assertFalse(st.find([0,3])) + self.assertFalse(st.find([1,3])) + self.assertFalse(st.find([2,3])) + + # filtration test + st.set_filtration(5.0) + st.initialize_filtration() + self.assertEqual(st.get_filtration(), 5.0) + self.assertEqual(st.filtration([0,1,2]), 4.0) + self.assertEqual(st.filtration([0,2]), 4.0) + self.assertEqual(st.filtration([1,2]), 4.0) + self.assertEqual(st.filtration([2]), 4.0) + self.assertEqual(st.filtration([0,1]), 0.0) + self.assertEqual(st.filtration([0]), 0.0) + self.assertEqual(st.filtration([1]), 0.0) + + # skeleton_tree test + self.assertEqual(st.get_skeleton_tree(2), [([0, 1, 2], 4.0), ([0, 1], 0.0), ([0, 2], 4.0), ([0], 0.0), ([1, 2], 4.0), ([1], 0.0), ([2], 4.0)]) + self.assertEqual(st.get_skeleton_tree(1), [([0, 1], 0.0), ([0, 2], 4.0), ([0], 0.0), ([1, 2], 4.0), ([1], 0.0), ([2], 4.0)]) + self.assertEqual(st.get_skeleton_tree(0), [([0], 0.0), ([1], 0.0), ([2], 4.0)]) + + def test_rips(self): + rips_complex = gudhi.SimplexTree(points=[[0,0],[1,0],[0,1],[1,1]],max_dimension=1,max_edge_length=42) + + self.assertEqual(rips_complex.num_simplices(), 10) + self.assertEqual(rips_complex.num_vertices(), 4) + + self.assertEqual(rips_complex.get_filtered_tree(), [([0], 0.0), ([1], 0.0), ([2], 0.0), ([3], 0.0), ([0, 1], 1.0), ([0, 2], 1.0), ([1, 3], 1.0), ([2, 3], 1.0), ([1, 2], 1.4142135623730951), ([0, 3], 1.4142135623730951)]) + self.assertEqual(rips_complex.get_star_tree([0]), [([0], 0.0), ([0, 1], 1.0), ([0, 2], 1.0), ([0, 3], 1.4142135623730951)]) + self.assertEqual(rips_complex.get_coface_tree([0], 1), [([0, 1], 1.0), ([0, 2], 1.0), ([0, 3], 1.4142135623730951)]) + + filtered_rips = gudhi.SimplexTree(points=[[0,0],[1,0],[0,1],[1,1]],max_dimension=1,max_edge_length=1.0) + self.assertEqual(filtered_rips.num_simplices(), 8) + self.assertEqual(filtered_rips.num_vertices(), 4) + + def test_split(self): + triangle012 = [0,1,2] + edge03 = [0,3] + mini_st = gudhi.MiniSimplexTree() + self.assertTrue(mini_st.insert(triangle012)) + self.assertTrue(mini_st.insert(edge03)) + # FIXME: Remove this line + mini_st.set_dimension(2); + + edge02 = [0,2] + self.assertTrue(mini_st.find(edge02)) + self.assertEqual(mini_st.get_coface_tree(edge02, 1), [([0, 1, 2], 0.0)]) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/src/cython/Simplex_tree_example.py b/src/cython/Simplex_tree_example.py new file mode 100755 index 00000000..e9459588 --- /dev/null +++ b/src/cython/Simplex_tree_example.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +import gudhi + +st = gudhi.SimplexTree() + +print("#######################################################################") +print("SimplexTree creation from insertion") +if st.insert([0,1]): + print("Inserted !!") +else: + print("Not inserted...") + +if st.find([0,1]): + print("Found !!") +else: + print("Not found...") + +if st.insert([0,1,2], filtration=4.0): + print("Inserted !!") +else: + print("Not inserted...") + +# FIXME: Remove this line +st.set_dimension(3) +print("dimension=", st.dimension()) + +st.set_filtration(4.0) +st.initialize_filtration() +print("filtration=", st.get_filtration()) +print("filtration[1,2]=", st.filtration([1,2])) +print("filtration[4,2]=", st.filtration([4,2])) + +print("num_simplices=", st.num_simplices()) +print("num_vertices=", st.num_vertices()) + +print("skeleton_tree[2]=", st.get_skeleton_tree(2)) +print("skeleton_tree[1]=", st.get_skeleton_tree(1)) +print("skeleton_tree[0]=", st.get_skeleton_tree(0)) + +print("#######################################################################") +print("SimplexTree creation from Rips") +st_from_rips = gudhi.SimplexTree(points=[[0,0],[1,0],[0,1],[1,1]],max_dimension=1,max_edge_length=42) + +print("filtered_tree=", st_from_rips.get_filtered_tree()) +print("star([0])=", st_from_rips.get_star_tree([0])) +print("coface([0],1)=", st_from_rips.get_coface_tree([0], 1)) + + +print("#######################################################################") +print("MiniSimplexTree creation from insertion") +triangle012 = [0, 1, 2] +edge03 = [0, 3] +mini_st = gudhi.MiniSimplexTree() +mini_st.insert(triangle012) +mini_st.insert(edge03) +# FIXME: Remove this line +mini_st.set_dimension(2); + +edge02 = [0, 2] +if mini_st.find(edge02): + # Only coface is 012 + print("coface(edge02,1)=", mini_st.get_coface_tree(edge02, 1)) +print("filtered_tree=", mini_st.get_filtered_tree()) + diff --git a/src/cython/main.py b/src/cython/main.py deleted file mode 100755 index e9459588..00000000 --- a/src/cython/main.py +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env python - -import gudhi - -st = gudhi.SimplexTree() - -print("#######################################################################") -print("SimplexTree creation from insertion") -if st.insert([0,1]): - print("Inserted !!") -else: - print("Not inserted...") - -if st.find([0,1]): - print("Found !!") -else: - print("Not found...") - -if st.insert([0,1,2], filtration=4.0): - print("Inserted !!") -else: - print("Not inserted...") - -# FIXME: Remove this line -st.set_dimension(3) -print("dimension=", st.dimension()) - -st.set_filtration(4.0) -st.initialize_filtration() -print("filtration=", st.get_filtration()) -print("filtration[1,2]=", st.filtration([1,2])) -print("filtration[4,2]=", st.filtration([4,2])) - -print("num_simplices=", st.num_simplices()) -print("num_vertices=", st.num_vertices()) - -print("skeleton_tree[2]=", st.get_skeleton_tree(2)) -print("skeleton_tree[1]=", st.get_skeleton_tree(1)) -print("skeleton_tree[0]=", st.get_skeleton_tree(0)) - -print("#######################################################################") -print("SimplexTree creation from Rips") -st_from_rips = gudhi.SimplexTree(points=[[0,0],[1,0],[0,1],[1,1]],max_dimension=1,max_edge_length=42) - -print("filtered_tree=", st_from_rips.get_filtered_tree()) -print("star([0])=", st_from_rips.get_star_tree([0])) -print("coface([0],1)=", st_from_rips.get_coface_tree([0], 1)) - - -print("#######################################################################") -print("MiniSimplexTree creation from insertion") -triangle012 = [0, 1, 2] -edge03 = [0, 3] -mini_st = gudhi.MiniSimplexTree() -mini_st.insert(triangle012) -mini_st.insert(edge03) -# FIXME: Remove this line -mini_st.set_dimension(2); - -edge02 = [0, 2] -if mini_st.find(edge02): - # Only coface is 012 - print("coface(edge02,1)=", mini_st.get_coface_tree(edge02, 1)) -print("filtered_tree=", mini_st.get_filtered_tree()) - -- cgit v1.2.3