From 0d06b5d39febbf812b48d3fb7f800c98204718ea Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Wed, 8 Jun 2016 05:34:33 +0000 Subject: Persistence diagram first version working with alpha_complex_from_file_example.py Comment graphical tools in example Makefile section git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/ST_cythonize@1259 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: b315b7c414bd0e2b269dcc2fbcae645a025335f1 --- src/cython/Makefile | 3 +- .../example/alpha_complex_from_file_example.py | 9 +-- .../src/cython/persistence_graphical_tools.py | 79 ++++++++++++++++++---- 3 files changed, 72 insertions(+), 19 deletions(-) (limited to 'src/cython') diff --git a/src/cython/Makefile b/src/cython/Makefile index e6054122..e3952f12 100644 --- a/src/cython/Makefile +++ b/src/cython/Makefile @@ -13,7 +13,8 @@ example: python example/rips_complex_example.py python example/alpha_complex_example.py python example/random_cubical_complex_example.py 10 10 - python example/alpha_complex_from_file_example.py data/500_random_points_on_3D_Torus.csv + # python example/alpha_complex_from_file_example.py data/500_random_points_on_3D_Torus.csv + # python example/gudhi_graphical_tools_example.py clean: rm -rf build/ *.o *.so *.cpp diff --git a/src/cython/example/alpha_complex_from_file_example.py b/src/cython/example/alpha_complex_from_file_example.py index 04b4b279..da7ae857 100755 --- a/src/cython/example/alpha_complex_from_file_example.py +++ b/src/cython/example/alpha_complex_from_file_example.py @@ -49,15 +49,12 @@ points = pandas.read_csv(args.file, header=None) alpha_complex = gudhi.AlphaComplex(points=points.values, max_alpha_square=0.5) -print("dimension=", alpha_complex.dimension()) -print("point[0]=", alpha_complex.get_point(0)) -print("point[5]=", alpha_complex.get_point(5)) - alpha_complex.initialize_filtration() diag = alpha_complex.persistence(homology_coeff_field=2, min_persistence=0.1) print("betti_numbers()=") print(alpha_complex.betti_numbers()) -print("star([0])=", alpha_complex.get_star_tree([0])) -print("coface([0], 1)=", alpha_complex.get_coface_tree([0], 1)) +gudhi.diagram_persistence(diag) + +gudhi.bar_code_persistence(diag) diff --git a/src/cython/src/cython/persistence_graphical_tools.py b/src/cython/src/cython/persistence_graphical_tools.py index 6cacf4a5..f25b6b63 100755 --- a/src/cython/src/cython/persistence_graphical_tools.py +++ b/src/cython/src/cython/persistence_graphical_tools.py @@ -27,6 +27,26 @@ __author__ = "Vincent Rouvreau" __copyright__ = "Copyright (C) 2016 INRIA" __license__ = "GPL v3" +def __min_birth_max_death(persistence): + """This function returns (min_birth, max_death) from the persistence. + + :param persistence: The persistence to plot. + :type persistence: list of tuples(dimension, tuple(birth, death)). + :returns: (float, float) -- (min_birth, max_death). + """ + # Look for minimum birth date and maximum death date for plot optimisation + max_death = 0 + min_birth = persistence[0][1][0] + for interval in reversed(persistence): + if float(interval[1][1]) != float('inf'): + if float(interval[1][1]) > max_death: + max_death = float(interval[1][1]) + if float(interval[1][0]) > max_death: + max_death = float(interval[1][0]) + if float(interval[1][0]) < min_birth: + min_birth = float(interval[1][0]) + return (min_birth, max_death) + """ Only 13 colors for the palette """ @@ -59,20 +79,11 @@ def bar_code_persistence(persistence): :type persistence: list of tuples(dimension, tuple(birth, death)). :returns: plot -- An horizontal bar plot of persistence. """ - # Look for minimum birth date and maximum death date for plot optimisation - max_death = 0 - min_birth = persistence[0][1][0] - for interval in reversed(persistence): - if float(interval[1][1]) != float('inf'): - if float(interval[1][1]) > max_death: - max_death = float(interval[1][1]) - if float(interval[1][0]) > max_death: - max_death = float(interval[1][0]) - if float(interval[1][0]) < min_birth: - min_birth = float(interval[1][0]) - + (min_birth, max_death) = __min_birth_max_death(persistence) ind = 0 delta = ((max_death - min_birth) / 10.0) + # Replace infinity values with max_death + delta for bar code to be more + # readable infinity = max_death + delta axis_start = min_birth - delta # Draw horizontal bars in loop @@ -88,6 +99,50 @@ def bar_code_persistence(persistence): left = interval[1][0], alpha=0.4, color = palette[interval[0]]) ind = ind + 1 + + plt.title('Persistence bar code') + plt.xlabel('Birth - Death') # Ends plot on infinity value and starts a little bit before min_birth plt.axis([axis_start, infinity, 0, ind]) plt.show() + +def diagram_persistence(persistence): + """This function plots the persistence diagram. + + :param persistence: The persistence to plot. + :type persistence: list of tuples(dimension, tuple(birth, death)). + :returns: plot -- An diagram plot of persistence. + """ + (min_birth, max_death) = __min_birth_max_death(persistence) + ind = 0 + delta = ((max_death - min_birth) / 10.0) + # Replace infinity values with max_death + delta for diagram to be more + # readable + infinity = max_death + delta + axis_start = min_birth - delta + + # line display of equation : birth = death + x = np.linspace(axis_start, infinity, 1000) + # infinity line and text + plt.plot(x, x, color='k', linewidth=1.0) + plt.plot(x, [infinity] * len(x), linewidth=1.0, color='k', alpha=0.4) + plt.text(axis_start, infinity, r'$\infty$', color='k', alpha=0.4) + + # Draw points in loop + for interval in reversed(persistence): + if float(interval[1][1]) != float('inf'): + # Finite death case + plt.scatter(interval[1][0], interval[1][1], alpha=0.4, + color = palette[interval[0]]) + else: + # Infinite death case for diagram to be nicer + plt.scatter(interval[1][0], infinity, alpha=0.4, + color = palette[interval[0]]) + ind = ind + 1 + + plt.title('Persistence diagram') + plt.xlabel('Birth') + plt.ylabel('Death') + # Ends plot on infinity value and starts a little bit before min_birth + plt.axis([axis_start, infinity, axis_start, infinity + delta]) + plt.show() -- cgit v1.2.3