-- cgit v1.2.3 From d52036da23f887d56d13dd4f93c1049eb44301b1 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 3 Aug 2018 09:41:38 +0000 Subject: plot persistence density functionnality with its documentation Fix some documentation issue in other graphiacl tools Add scipy as a dependency git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3736 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 6481f1d6aa176bcf587f8875298a91cd22ac0319 --- src/cython/cython/persistence_graphical_tools.py | 74 +++++++++++++++++++++- src/cython/doc/persistence_graphical_tools_ref.rst | 1 + .../doc/persistence_graphical_tools_user.rst | 9 ++- src/cython/setup.py.in | 1 + 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index 216ab8d6..7f604368 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -1,7 +1,9 @@ import matplotlib.pyplot as plt import matplotlib.patches as mpatches import numpy as np +from scipy.stats import kde import os +import math """This file is part of the Gudhi Library. The Gudhi library (Geometric Understanding in Higher Dimensions) is a generic C++ @@ -79,6 +81,8 @@ def plot_persistence_barcode(persistence=[], persistence_file='', alpha=0.6, :param inf_delta: Infinity is placed at ((max_death - min_birth) x inf_delta). A reasonable value is between 0.05 and 0.5 - default is 0.1. :type inf_delta: float. + :param legend: Display the dimension color legend (default is False). + :type legend: boolean. :returns: A matplotlib object containing horizontal bar plot of persistence (launch `show()` method on it to display it). """ @@ -154,6 +158,8 @@ def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, :param inf_delta: Infinity is placed at ((max_death - min_birth) x inf_delta). A reasonable value is between 0.05 and 0.5 - default is 0.1. :type inf_delta: float. + :param legend: Display the dimension color legend (default is False). + :type legend: boolean. :returns: A matplotlib object containing diagram plot of persistence (launch `show()` method on it to display it). """ @@ -174,7 +180,6 @@ def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, persistence = sorted(persistence, key=lambda life_time: life_time[1][1]-life_time[1][0], reverse=True)[:max_plots] (min_birth, max_death) = __min_birth_max_death(persistence, band) - ind = 0 delta = ((max_death - min_birth) * inf_delta) # Replace infinity values with max_death + delta for diagram to be more # readable @@ -201,7 +206,6 @@ def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, # Infinite death case for diagram to be nicer plt.scatter(interval[1][0], infinity, alpha=alpha, color = palette[interval[0]]) - ind = ind + 1 if legend: dimensions = list(set(item[0] for item in persistence)) @@ -213,3 +217,69 @@ def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, # Ends plot on infinity value and starts a little bit before min_birth plt.axis([axis_start, infinity, axis_start, infinity + delta]) return plt + +def plot_persistence_density(persistence=[], persistence_file='', nbins=300, + max_plots=1000, cmap=plt.cm.hot_r, legend=False): + """This function plots the persistence density from persistence values list + or from a :doc:`persistence file `. Be aware that this + function does not distinguish the dimension, it is up to you to select the + required one. + + :param persistence: Persistence values list. + :type persistence: list of tuples(dimension, tuple(birth, death)). + :param persistence_file: A :doc:`persistence file ` style name + (reset persistence if both are set). + :type persistence_file: string + :param nbins: Evaluate a gaussian kde on a regular grid of nbins x nbins + over data extents (default is 300) + :type nbins: int. + :param max_plots: number of maximal plots to be displayed + Set it to 0 to see all, Default value is 1000. + (persistence will be sorted by life time if max_plots is set) + :type max_plots: int. + :param cmap: A matplotlib colormap (default is matplotlib.pyplot.cm.hot_r). + :type cmap: cf. matplotlib colormap. + :param legend: Display the color bar values (default is False). + :type legend: boolean. + :returns: A matplotlib object containing diagram plot of persistence + (launch `show()` method on it to display it). + """ + if persistence_file is not '': + if os.path.isfile(persistence_file): + # Reset persistence + persistence = [] + diag = read_persistence_intervals_grouped_by_dimension(persistence_file=persistence_file) + for key in diag.keys(): + for persistence_interval in diag[key]: + persistence.append((key, persistence_interval)) + else: + print("file " + persistence_file + " not found.") + return None + + if max_plots > 0 and max_plots < len(persistence): + # Sort by life time, then takes only the max_plots elements + persistence = sorted(persistence, key=lambda life_time: life_time[1][1]-life_time[1][0], reverse=True)[:max_plots] + + # Set as numpy array birth and death (remove undefined values - inf and NaN) + birth = np.asarray([(interval[1][0]) for interval in persistence if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) + death = np.asarray([(interval[1][1]) for interval in persistence if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) + + # line display of equation : birth = death + x = np.linspace(death.min(), birth.max(), 1000) + plt.plot(x, x, color='k', linewidth=1.0) + + # Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents + k = kde.gaussian_kde([birth,death]) + xi, yi = np.mgrid[birth.min():birth.max():nbins*1j, death.min():death.max():nbins*1j] + zi = k(np.vstack([xi.flatten(), yi.flatten()])) + + # Make the plot + plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=cmap) + + if legend: + plt.colorbar() + + plt.title('Persistence density') + plt.xlabel('Birth') + plt.ylabel('Death') + return plt diff --git a/src/cython/doc/persistence_graphical_tools_ref.rst b/src/cython/doc/persistence_graphical_tools_ref.rst index a2c6bcef..54aff4bc 100644 --- a/src/cython/doc/persistence_graphical_tools_ref.rst +++ b/src/cython/doc/persistence_graphical_tools_ref.rst @@ -9,3 +9,4 @@ Persistence graphical tools reference manual .. autofunction:: gudhi.__min_birth_max_death .. autofunction:: gudhi.plot_persistence_barcode .. autofunction:: gudhi.plot_persistence_diagram +.. autofunction:: gudhi.plot_persistence_density diff --git a/src/cython/doc/persistence_graphical_tools_user.rst b/src/cython/doc/persistence_graphical_tools_user.rst index 292915eb..b21de06a 100644 --- a/src/cython/doc/persistence_graphical_tools_user.rst +++ b/src/cython/doc/persistence_graphical_tools_user.rst @@ -43,6 +43,9 @@ This function can display the persistence result as a diagram: legend=True) plt.show() +Persistence density +------------------- + If you want more information on a specific dimension, for instance: .. plot:: @@ -56,7 +59,7 @@ If you want more information on a specific dimension, for instance: gudhi.read_persistence_intervals_grouped_by_dimension(persistence_file=\ persistence_file) dim = 1 - # Display all points with some transparency - plt = gudhi.plot_persistence_diagram([(dim,interval) for interval in diag[dim]], - max_plots=0, alpha=0.1) + # Display persistence density + plt = gudhi.plot_persistence_density([(dim,interval) for interval in diag[dim]], + max_plots=0, legend=True) plt.show() diff --git a/src/cython/setup.py.in b/src/cython/setup.py.in index ee381a1b..22b2954e 100644 --- a/src/cython/setup.py.in +++ b/src/cython/setup.py.in @@ -49,6 +49,7 @@ setup( install_requires = [ "matplotlib", "numpy", + "scipy", "cython", ], ) -- cgit v1.2.3 From feb6244de8f602e36fe101eb553ad5a6265665ee Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 3 Aug 2018 09:55:26 +0000 Subject: Add SciPy as a dependency in the installation process documentation git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3737 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 2d9d2c3f32ba3af5e9565f89e986ac437370b68e --- src/cython/doc/installation.rst | 9 ++++++++- src/cython/doc/persistence_graphical_tools_sum.inc | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/cython/doc/installation.rst b/src/cython/doc/installation.rst index 43ff85c5..ff6c7273 100644 --- a/src/cython/doc/installation.rst +++ b/src/cython/doc/installation.rst @@ -143,7 +143,7 @@ The following examples require the `Matplotlib `_: * :download:`euclidean_strong_witness_complex_diagram_persistence_from_off_file_example.py <../example/euclidean_strong_witness_complex_diagram_persistence_from_off_file_example.py>` * :download:`euclidean_witness_complex_diagram_persistence_from_off_file_example.py <../example/euclidean_witness_complex_diagram_persistence_from_off_file_example.py>` -Numpy +NumPy ===== The :doc:`persistence graphical tools ` @@ -164,6 +164,13 @@ The following examples require the `NumPy `_: * :download:`euclidean_strong_witness_complex_diagram_persistence_from_off_file_example.py <../example/euclidean_strong_witness_complex_diagram_persistence_from_off_file_example.py>` * :download:`euclidean_witness_complex_diagram_persistence_from_off_file_example.py <../example/euclidean_witness_complex_diagram_persistence_from_off_file_example.py>` +SciPy +===== + +The :doc:`persistence graphical tools ` +module requires `SciPy `_, a Python-based ecosystem of +open-source software for mathematics, science, and engineering. + Threading Building Blocks ========================= diff --git a/src/cython/doc/persistence_graphical_tools_sum.inc b/src/cython/doc/persistence_graphical_tools_sum.inc index d602daa7..c793a352 100644 --- a/src/cython/doc/persistence_graphical_tools_sum.inc +++ b/src/cython/doc/persistence_graphical_tools_sum.inc @@ -1,11 +1,11 @@ ================================================================= =================================== =================================== :Author: Vincent Rouvreau :Introduced in: GUDHI 2.0.0 :Copyright: GPL v3 -:Requires: Matplotlib Numpy +:Requires: Matplotlib Numpy Scipy ================================================================= =================================== =================================== +-----------------------------------------------------------------+-----------------------------------------------------------------------+ | .. figure:: | These graphical tools comes on top of persistence results and allows | -| img/graphical_tools_representation.png | the user to build easily barcode and persistence diagram. | +| img/graphical_tools_representation.png | the user to build easily persistence barcode, diagram or density. | | | | +-----------------------------------------------------------------+-----------------------------------------------------------------------+ | :doc:`persistence_graphical_tools_user` | :doc:`persistence_graphical_tools_ref` | -- cgit v1.2.3 From 2f10f837b791a1b002a18f1ba2b22de918eb54fe Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Thu, 16 Aug 2018 13:40:06 +0000 Subject: Code review : Try to import module. Functions are unavailable if not available. git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3785 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 53c89a30f7992aa8d20e8330628c03291cea6473 --- .../modules/GUDHI_third_party_libraries.cmake | 1 + src/cython/CMakeLists.txt | 8 +- src/cython/cython/persistence_graphical_tools.py | 202 +++++++++++---------- 3 files changed, 108 insertions(+), 103 deletions(-) diff --git a/src/cmake/modules/GUDHI_third_party_libraries.cmake b/src/cmake/modules/GUDHI_third_party_libraries.cmake index f03c2177..1ad0c1fd 100644 --- a/src/cmake/modules/GUDHI_third_party_libraries.cmake +++ b/src/cmake/modules/GUDHI_third_party_libraries.cmake @@ -143,6 +143,7 @@ if( PYTHONINTERP_FOUND ) find_python_module("pytest") find_python_module("matplotlib") find_python_module("numpy") + find_python_module("scipy") endif() if(NOT GUDHI_CYTHON_PATH) diff --git a/src/cython/CMakeLists.txt b/src/cython/CMakeLists.txt index 1849a6ec..542be9b0 100644 --- a/src/cython/CMakeLists.txt +++ b/src/cython/CMakeLists.txt @@ -54,10 +54,8 @@ if(CYTHON_FOUND) if(NUMPY_FOUND) add_gudhi_debug_info("Numpy version ${NUMPY_VERSION}") endif() - if(MATPLOTLIB_FOUND AND NUMPY_FOUND) - set(GUDHI_CYTHON_MODULES "${GUDHI_CYTHON_MODULES}persistence_graphical_tools;") - else() - set(GUDHI_CYTHON_MISSING_MODULES "${GUDHI_CYTHON_MODULES}persistence_graphical_tools;") + if(SCIPY_FOUND) + add_gudhi_debug_info("Scipy version ${SCIPY_VERSION}") endif() message("++ ${PYTHON_EXECUTABLE} v.${PYTHON_VERSION_STRING} - Cython is ${CYTHON_VERSION} - Sphinx is ${SPHINX_PATH}") @@ -368,7 +366,7 @@ if(CYTHON_FOUND) add_gudhi_py_test(test_reader_utils) # Documentation generation is available through sphinx - requires all modules - if(SPHINX_PATH AND MATPLOTLIB_FOUND AND NUMPY_FOUND AND NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.8.1) + if(SPHINX_PATH AND MATPLOTLIB_FOUND AND NUMPY_FOUND AND SCIPY_FOUND AND NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.8.1) set (GUDHI_SPHINX_MESSAGE "Generating API documentation with Sphinx in ${CMAKE_CURRENT_BINARY_DIR}/sphinx/") # User warning - Sphinx is a static pages generator, and configured to work fine with user_version # Images and biblio warnings because not found on developper version diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index e172c65b..e154c0b2 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -24,44 +24,42 @@ __author__ = "Vincent Rouvreau, Bertrand Michel" __copyright__ = "Copyright (C) 2016 Inria" __license__ = "GPL v3" +def __min_birth_max_death(persistence, band=0.): + """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)). + :param band: band + :type band: float. + :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]) + if band > 0.: + max_death += band + return (min_birth, max_death) + +""" +Only 13 colors for the palette +""" +palette = ['#ff0000', '#00ff00', '#0000ff', '#00ffff', '#ff00ff', '#ffff00', + '#000000', '#880000', '#008800', '#000088', '#888800', '#880088', + '#008888'] + try: import matplotlib.pyplot as plt import matplotlib.patches as mpatches import numpy as np - from scipy.stats import kde import os - import math - - def __min_birth_max_death(persistence, band=0.): - """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)). - :param band: band - :type band: float. - :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]) - if band > 0.: - max_death += band - return (min_birth, max_death) - - """ - Only 13 colors for the palette - """ - palette = ['#ff0000', '#00ff00', '#0000ff', '#00ffff', '#ff00ff', '#ffff00', - '#000000', '#880000', '#008800', '#000088', '#888800', '#880088', - '#008888'] def plot_persistence_barcode(persistence=[], persistence_file='', alpha=0.6, max_barcodes=1000, inf_delta=0.1, legend=False): @@ -220,71 +218,79 @@ try: plt.axis([axis_start, infinity, axis_start, infinity + delta]) return plt - def plot_persistence_density(persistence=[], persistence_file='', nbins=300, - max_plots=1000, cmap=plt.cm.hot_r, legend=False): - """This function plots the persistence density from persistence values list - or from a :doc:`persistence file `. Be aware that this - function does not distinguish the dimension, it is up to you to select the - required one. - - :param persistence: Persistence values list. - :type persistence: list of tuples(dimension, tuple(birth, death)). - :param persistence_file: A :doc:`persistence file ` style name - (reset persistence if both are set). - :type persistence_file: string - :param nbins: Evaluate a gaussian kde on a regular grid of nbins x nbins - over data extents (default is 300) - :type nbins: int. - :param max_plots: number of maximal plots to be displayed - Set it to 0 to see all, Default value is 1000. - (persistence will be sorted by life time if max_plots is set) - :type max_plots: int. - :param cmap: A matplotlib colormap (default is matplotlib.pyplot.cm.hot_r). - :type cmap: cf. matplotlib colormap. - :param legend: Display the color bar values (default is False). - :type legend: boolean. - :returns: A matplotlib object containing diagram plot of persistence - (launch `show()` method on it to display it). - """ - if persistence_file is not '': - if os.path.isfile(persistence_file): - # Reset persistence - persistence = [] - diag = read_persistence_intervals_grouped_by_dimension(persistence_file=persistence_file) - for key in diag.keys(): - for persistence_interval in diag[key]: - persistence.append((key, persistence_interval)) - else: - print("file " + persistence_file + " not found.") - return None - - if max_plots > 0 and max_plots < len(persistence): - # Sort by life time, then takes only the max_plots elements - persistence = sorted(persistence, key=lambda life_time: life_time[1][1]-life_time[1][0], reverse=True)[:max_plots] - - # Set as numpy array birth and death (remove undefined values - inf and NaN) - birth = np.asarray([(interval[1][0]) for interval in persistence if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) - death = np.asarray([(interval[1][1]) for interval in persistence if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) - - # line display of equation : birth = death - x = np.linspace(death.min(), birth.max(), 1000) - plt.plot(x, x, color='k', linewidth=1.0) - - # Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents - k = kde.gaussian_kde([birth,death]) - xi, yi = np.mgrid[birth.min():birth.max():nbins*1j, death.min():death.max():nbins*1j] - zi = k(np.vstack([xi.flatten(), yi.flatten()])) - - # Make the plot - plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=cmap) - - if legend: - plt.colorbar() - - plt.title('Persistence density') - plt.xlabel('Birth') - plt.ylabel('Death') - return plt + try: + from scipy.stats import kde + import math + + def plot_persistence_density(persistence=[], persistence_file='', nbins=300, + max_plots=1000, cmap=plt.cm.hot_r, legend=False): + """This function plots the persistence density from persistence values list + or from a :doc:`persistence file `. Be aware that this + function does not distinguish the dimension, it is up to you to select the + required one. + + :param persistence: Persistence values list. + :type persistence: list of tuples(dimension, tuple(birth, death)). + :param persistence_file: A :doc:`persistence file ` style name + (reset persistence if both are set). + :type persistence_file: string + :param nbins: Evaluate a gaussian kde on a regular grid of nbins x nbins + over data extents (default is 300) + :type nbins: int. + :param max_plots: number of maximal plots to be displayed + Set it to 0 to see all, Default value is 1000. + (persistence will be sorted by life time if max_plots is set) + :type max_plots: int. + :param cmap: A matplotlib colormap (default is matplotlib.pyplot.cm.hot_r). + :type cmap: cf. matplotlib colormap. + :param legend: Display the color bar values (default is False). + :type legend: boolean. + :returns: A matplotlib object containing diagram plot of persistence + (launch `show()` method on it to display it). + """ + if persistence_file is not '': + if os.path.isfile(persistence_file): + # Reset persistence + persistence = [] + diag = read_persistence_intervals_grouped_by_dimension(persistence_file=persistence_file) + for key in diag.keys(): + for persistence_interval in diag[key]: + persistence.append((key, persistence_interval)) + else: + print("file " + persistence_file + " not found.") + return None + + if max_plots > 0 and max_plots < len(persistence): + # Sort by life time, then takes only the max_plots elements + persistence = sorted(persistence, key=lambda life_time: life_time[1][1]-life_time[1][0], reverse=True)[:max_plots] + + # Set as numpy array birth and death (remove undefined values - inf and NaN) + birth = np.asarray([(interval[1][0]) for interval in persistence if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) + death = np.asarray([(interval[1][1]) for interval in persistence if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) + + # line display of equation : birth = death + x = np.linspace(death.min(), birth.max(), 1000) + plt.plot(x, x, color='k', linewidth=1.0) + + # Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents + k = kde.gaussian_kde([birth,death]) + xi, yi = np.mgrid[birth.min():birth.max():nbins*1j, death.min():death.max():nbins*1j] + zi = k(np.vstack([xi.flatten(), yi.flatten()])) + + # Make the plot + plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=cmap) + + if legend: + plt.colorbar() + + plt.title('Persistence density') + plt.xlabel('Birth') + plt.ylabel('Death') + return plt + + except ImportError: + # Continue in case of import error, functions won't be available + pass except ImportError: # Continue in case of import error, functions won't be available -- cgit v1.2.3 From efab7ccf5850ccc58018b7f856d209c4094e67c5 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Thu, 16 Aug 2018 14:35:08 +0000 Subject: Bug fix git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3788 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: d8b898ca963d8d956307bb1dfd8abea1acbe6c96 --- src/cython/cython/persistence_graphical_tools.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index e154c0b2..14c4cf3f 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -205,7 +205,6 @@ try: # Infinite death case for diagram to be nicer plt.scatter(interval[1][0], infinity, alpha=alpha, color = palette[interval[0]]) - ind = ind + 1 if legend: dimensions = list(set(item[0] for item in persistence)) -- cgit v1.2.3 From b7354cd2ab0b265787348866da5d08cce15aee66 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 17 Aug 2018 09:38:55 +0000 Subject: Code review : add a parameter to select dimension for plot_persistence_density. Default is None and mix all dimensions git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3794 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 493f5ecfad2bcf0ee4a40ec7b315863c471d088f --- src/cython/cython/persistence_graphical_tools.py | 43 +++++++++++++++--------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index 14c4cf3f..949f5c37 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -221,26 +221,31 @@ try: from scipy.stats import kde import math - def plot_persistence_density(persistence=[], persistence_file='', nbins=300, - max_plots=1000, cmap=plt.cm.hot_r, legend=False): - """This function plots the persistence density from persistence values list - or from a :doc:`persistence file `. Be aware that this - function does not distinguish the dimension, it is up to you to select the - required one. + def plot_persistence_density(persistence=[], persistence_file='', + nbins=300, max_plots=1000, dimension=None, + cmap=plt.cm.hot_r, legend=False): + """This function plots the persistence density from persistence + values list or from a :doc:`persistence file `. Be + aware that this function does not distinguish the dimension, it is + up to you to select the required one. :param persistence: Persistence values list. :type persistence: list of tuples(dimension, tuple(birth, death)). - :param persistence_file: A :doc:`persistence file ` style name - (reset persistence if both are set). + :param persistence_file: A :doc:`persistence file ` + style name (reset persistence if both are set). :type persistence_file: string - :param nbins: Evaluate a gaussian kde on a regular grid of nbins x nbins - over data extents (default is 300) + :param nbins: Evaluate a gaussian kde on a regular grid of nbins x + nbins over data extents (default is 300) :type nbins: int. :param max_plots: number of maximal plots to be displayed Set it to 0 to see all, Default value is 1000. (persistence will be sorted by life time if max_plots is set) :type max_plots: int. - :param cmap: A matplotlib colormap (default is matplotlib.pyplot.cm.hot_r). + :param dimension: the dimension to be selected in the intervals + (default is None to mix all dimensions). + :type dimension: int. + :param cmap: A matplotlib colormap (default is + matplotlib.pyplot.cm.hot_r). :type cmap: cf. matplotlib colormap. :param legend: Display the color bar values (default is False). :type legend: boolean. @@ -259,13 +264,21 @@ try: print("file " + persistence_file + " not found.") return None - if max_plots > 0 and max_plots < len(persistence): + persistence_dim = [] + if dimension is not None: + persistence_dim = [(dim_interval) for dim_interval in persistence if (dim_interval[0] == dimension)] + else: + persistence_dim = persistence + + if max_plots > 0 and max_plots < len(persistence_dim): # Sort by life time, then takes only the max_plots elements - persistence = sorted(persistence, key=lambda life_time: life_time[1][1]-life_time[1][0], reverse=True)[:max_plots] + persistence_dim = sorted(persistence_dim, + key=lambda life_time: life_time[1][1]-life_time[1][0], + reverse=True)[:max_plots] # Set as numpy array birth and death (remove undefined values - inf and NaN) - birth = np.asarray([(interval[1][0]) for interval in persistence if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) - death = np.asarray([(interval[1][1]) for interval in persistence if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) + birth = np.asarray([(interval[1][0]) for interval in persistence_dim if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) + death = np.asarray([(interval[1][1]) for interval in persistence_dim if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) # line display of equation : birth = death x = np.linspace(death.min(), birth.max(), 1000) -- cgit v1.2.3 From 1865751c055ad01ba159a13d22e42204195c2ccb Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 17 Aug 2018 09:50:15 +0000 Subject: Doc review : rephrase max_plots explanation git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3795 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: f6059d3a242eed53dfdddb76ffed33f99892f4bc --- src/cython/cython/persistence_graphical_tools.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index 949f5c37..0b3357f8 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -138,21 +138,22 @@ try: def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, band=0., max_plots=1000, inf_delta=0.1, legend=False): - """This function plots the persistence diagram from persistence values list - or from a :doc:`persistence file `. + """This function plots the persistence diagram from persistence values + list or from a :doc:`persistence file `. :param persistence: Persistence values list. :type persistence: list of tuples(dimension, tuple(birth, death)). :param persistence_file: A :doc:`persistence file ` style name (reset persistence if both are set). :type persistence_file: string - :param alpha: plot transparency value (0.0 transparent through 1.0 opaque - default is 0.6). + :param alpha: plot transparency value (0.0 transparent through 1.0 + opaque - default is 0.6). :type alpha: float. :param band: band (not displayed if :math:`\leq` 0. - default is 0.) :type band: float. - :param max_plots: number of maximal plots to be displayed - Set it to 0 to see all, Default value is 1000. - (persistence will be sorted by life time if max_plots is set) + :param max_plots: maximal number of points to display. Selected points + are those with the longest life time. Set it to 0 to see all, + default value is 1000. :type max_plots: int. :param inf_delta: Infinity is placed at ((max_death - min_birth) x inf_delta). A reasonable value is between 0.05 and 0.5 - default is 0.1. @@ -237,9 +238,9 @@ try: :param nbins: Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents (default is 300) :type nbins: int. - :param max_plots: number of maximal plots to be displayed - Set it to 0 to see all, Default value is 1000. - (persistence will be sorted by life time if max_plots is set) + :param max_plots: maximal number of points to display. Selected points + are those with the longest life time. Set it to 0 to see all, + default value is 1000. :type max_plots: int. :param dimension: the dimension to be selected in the intervals (default is None to mix all dimensions). -- cgit v1.2.3 From 67ae5aa70e70097f72f868ebba1fa8e7ff96297d Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 17 Aug 2018 12:41:10 +0000 Subject: doc review : modification of persistence barcode example to make it more relevant git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3796 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 17acc09cdf7d09b13c177334efb1e309c4fd8d9c --- .../doc/persistence_graphical_tools_user.rst | 25 +++++++++++----------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/cython/doc/persistence_graphical_tools_user.rst b/src/cython/doc/persistence_graphical_tools_user.rst index b21de06a..0047f9c8 100644 --- a/src/cython/doc/persistence_graphical_tools_user.rst +++ b/src/cython/doc/persistence_graphical_tools_user.rst @@ -19,12 +19,15 @@ This function can display the persistence result as a barcode: import gudhi - perseus_file = gudhi.__root_source_dir__ + '/data/bitmap/3d_torus.txt' - periodic_cc = gudhi.PeriodicCubicalComplex(perseus_file=perseus_file) - diag = periodic_cc.persistence() - print("diag = ", diag) - plt = gudhi.plot_persistence_barcode(diag) - plt.show() + off_file = gudhi.__root_source_dir__ + '/data/points/tore3D_300.off' + point_cloud = gudhi.read_off(off_file=off_file) + + rips_complex = gudhi.RipsComplex(points=point_cloud, max_edge_length=0.7) + simplex_tree = rips_complex.create_simplex_tree(max_dimension=3) + diag = simplex_tree.persistence(min_persistence=0.4) + + plot = gudhi.plot_persistence_barcode(diag) + plot.show() Show persistence as a diagram ----------------------------- @@ -53,13 +56,9 @@ If you want more information on a specific dimension, for instance: import gudhi + # rips_on_tore3D_1307.pers obtained from write_persistence_diagram method persistence_file=gudhi.__root_source_dir__ + \ '/data/persistence_diagram/rips_on_tore3D_1307.pers' - diag = \ - gudhi.read_persistence_intervals_grouped_by_dimension(persistence_file=\ - persistence_file) - dim = 1 - # Display persistence density - plt = gudhi.plot_persistence_density([(dim,interval) for interval in diag[dim]], - max_plots=0, legend=True) + plt = gudhi.plot_persistence_density(persistence_file=persistence_file, + max_plots=0, dimension=1, legend=True) plt.show() -- cgit v1.2.3 From a5464d68d2848d21fc777809ab686b1bbf4e0139 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 17 Aug 2018 13:32:41 +0000 Subject: Code review : Add a bw_method parameter to plot_persistence_density for the standard deviation of the Gaussian git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3799 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 3bbc988434951bb429acd44b1f31f8310bb9ba9c --- src/cython/cython/persistence_graphical_tools.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index 0b3357f8..026e365d 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -223,7 +223,8 @@ try: import math def plot_persistence_density(persistence=[], persistence_file='', - nbins=300, max_plots=1000, dimension=None, + nbins=300, bw_method=None, + max_plots=1000, dimension=None, cmap=plt.cm.hot_r, legend=False): """This function plots the persistence density from persistence values list or from a :doc:`persistence file `. Be @@ -238,6 +239,14 @@ try: :param nbins: Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents (default is 300) :type nbins: int. + :param bw_method: The method used to calculate the estimator + bandwidth. This can be 'scott', 'silverman', a scalar constant + or a callable. If a scalar, this will be used directly as + kde.factor. If a callable, it should take a gaussian_kde + instance as only parameter and return a scalar. If None + (default), 'scott' is used. See scipy.stats.gaussian_kde + documentation for more details. + :type bw_method: str, scalar or callable, optional. :param max_plots: maximal number of points to display. Selected points are those with the longest life time. Set it to 0 to see all, default value is 1000. @@ -286,7 +295,7 @@ try: plt.plot(x, x, color='k', linewidth=1.0) # Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents - k = kde.gaussian_kde([birth,death]) + k = kde.gaussian_kde([birth,death], bw_method=bw_method) xi, yi = np.mgrid[birth.min():birth.max():nbins*1j, death.min():death.max():nbins*1j] zi = k(np.vstack([xi.flatten(), yi.flatten()])) -- cgit v1.2.3 From 24c661e98b8f8e0f2c492fcf63e68615dbd9ae84 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 17 Aug 2018 13:45:08 +0000 Subject: Doc review : inf_delta explanation improvement git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3800 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 01fff74d79228431d77a46b713ac656ab7e84dc0 --- src/cython/cython/persistence_graphical_tools.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index 026e365d..4c8467bd 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -71,14 +71,16 @@ try: :param persistence_file: A :doc:`persistence file ` style name (reset persistence if both are set). :type persistence_file: string - :param alpha: barcode transparency value (0.0 transparent through 1.0 opaque - default is 0.6). + :param alpha: barcode transparency value (0.0 transparent through 1.0 + opaque - default is 0.6). :type alpha: float. :param max_barcodes: number of maximal barcodes to be displayed. Set it to 0 to see all, Default value is 1000. (persistence will be sorted by life time if max_barcodes is set) :type max_barcodes: int. - :param inf_delta: Infinity is placed at ((max_death - min_birth) x inf_delta). - A reasonable value is between 0.05 and 0.5 - default is 0.1. + :param inf_delta: Infinity is placed at ((max_death - min_birth) x + inf_delta) above the highest point. A reasonable value is between + 0.05 and 0.5 - default is 0.1. :type inf_delta: float. :param legend: Display the dimension color legend (default is False). :type legend: boolean. @@ -155,8 +157,9 @@ try: are those with the longest life time. Set it to 0 to see all, default value is 1000. :type max_plots: int. - :param inf_delta: Infinity is placed at ((max_death - min_birth) x inf_delta). - A reasonable value is between 0.05 and 0.5 - default is 0.1. + :param inf_delta: Infinity is placed at ((max_death - min_birth) x + inf_delta) above the highest point. A reasonable value is between + 0.05 and 0.5 - default is 0.1. :type inf_delta: float. :param legend: Display the dimension color legend (default is False). :type legend: boolean. -- cgit v1.2.3 From 7e1d78ec22e0e53bbf4a36aaea1b77fa48f5d40d Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 17 Aug 2018 13:58:46 +0000 Subject: Code review : Rename max_plots with max_intervals for persistence density git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3801 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 8d20ee2f721b856bba69bc5bc10db26032649129 --- src/cython/cython/persistence_graphical_tools.py | 14 +++++++------- src/cython/doc/persistence_graphical_tools_user.rst | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index 4c8467bd..96b579bd 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -250,10 +250,10 @@ try: (default), 'scott' is used. See scipy.stats.gaussian_kde documentation for more details. :type bw_method: str, scalar or callable, optional. - :param max_plots: maximal number of points to display. Selected points - are those with the longest life time. Set it to 0 to see all, - default value is 1000. - :type max_plots: int. + :param max_intervals: maximal number of intervals to display. + Selected points are those with the longest life time. Set it + to 0 to see all. Default value is 1000. + :type max_intervals: int. :param dimension: the dimension to be selected in the intervals (default is None to mix all dimensions). :type dimension: int. @@ -283,11 +283,11 @@ try: else: persistence_dim = persistence - if max_plots > 0 and max_plots < len(persistence_dim): - # Sort by life time, then takes only the max_plots elements + if max_intervals > 0 and max_intervals < len(persistence_dim): + # Sort by life time, then takes only the max_intervals elements persistence_dim = sorted(persistence_dim, key=lambda life_time: life_time[1][1]-life_time[1][0], - reverse=True)[:max_plots] + reverse=True)[:max_intervals] # Set as numpy array birth and death (remove undefined values - inf and NaN) birth = np.asarray([(interval[1][0]) for interval in persistence_dim if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) diff --git a/src/cython/doc/persistence_graphical_tools_user.rst b/src/cython/doc/persistence_graphical_tools_user.rst index 0047f9c8..fda086c3 100644 --- a/src/cython/doc/persistence_graphical_tools_user.rst +++ b/src/cython/doc/persistence_graphical_tools_user.rst @@ -60,5 +60,5 @@ If you want more information on a specific dimension, for instance: persistence_file=gudhi.__root_source_dir__ + \ '/data/persistence_diagram/rips_on_tore3D_1307.pers' plt = gudhi.plot_persistence_density(persistence_file=persistence_file, - max_plots=0, dimension=1, legend=True) + max_intervals=0, dimension=1, legend=True) plt.show() -- cgit v1.2.3 From 6ebc098c9bbf233b5a4325faec04236feedab191 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 17 Aug 2018 14:48:08 +0000 Subject: Code review : rename max_plots and max_barcodes as max_intervals for peristence_graphical_tools git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3803 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: ad73f7e6cc6d72c3ba24b5ac947265c78a29f387 --- src/cython/cython/persistence_graphical_tools.py | 43 ++++++++++++++---------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index 96b579bd..85d2a5ad 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -62,7 +62,8 @@ try: import os def plot_persistence_barcode(persistence=[], persistence_file='', alpha=0.6, - max_barcodes=1000, inf_delta=0.1, legend=False): + max_intervals=1000, max_barcodes=1000, + inf_delta=0.1, legend=False): """This function plots the persistence bar code from persistence values list or from a :doc:`persistence file `. @@ -74,10 +75,10 @@ try: :param alpha: barcode transparency value (0.0 transparent through 1.0 opaque - default is 0.6). :type alpha: float. - :param max_barcodes: number of maximal barcodes to be displayed. - Set it to 0 to see all, Default value is 1000. - (persistence will be sorted by life time if max_barcodes is set) - :type max_barcodes: int. + :param max_intervals: maximal number of intervals to display. + Selected points are those with the longest life time. Set it + to 0 to see all. Default value is 1000. + :type max_intervals: int. :param inf_delta: Infinity is placed at ((max_death - min_birth) x inf_delta) above the highest point. A reasonable value is between 0.05 and 0.5 - default is 0.1. @@ -99,9 +100,13 @@ try: print("file " + persistence_file + " not found.") return None - if max_barcodes > 0 and max_barcodes < len(persistence): - # Sort by life time, then takes only the max_plots elements - persistence = sorted(persistence, key=lambda life_time: life_time[1][1]-life_time[1][0], reverse=True)[:max_barcodes] + if max_barcodes is not 1000: + print('Deprecated parameter. It has been replaced by max_intervals') + max_intervals = max_barcodes + + if max_intervals > 0 and max_intervals < len(persistence): + # Sort by life time, then takes only the max_intervals elements + persistence = sorted(persistence, key=lambda life_time: life_time[1][1]-life_time[1][0], reverse=True)[:max_intervals] persistence = sorted(persistence, key=lambda birth: birth[1][0]) @@ -139,7 +144,7 @@ try: return plt def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, - band=0., max_plots=1000, inf_delta=0.1, legend=False): + band=0., max_intervals=1000, max_plots=1000, inf_delta=0.1, legend=False): """This function plots the persistence diagram from persistence values list or from a :doc:`persistence file `. @@ -153,10 +158,10 @@ try: :type alpha: float. :param band: band (not displayed if :math:`\leq` 0. - default is 0.) :type band: float. - :param max_plots: maximal number of points to display. Selected points - are those with the longest life time. Set it to 0 to see all, - default value is 1000. - :type max_plots: int. + :param max_intervals: maximal number of intervals to display. + Selected points are those with the longest life time. Set it + to 0 to see all. Default value is 1000. + :type max_intervals: int. :param inf_delta: Infinity is placed at ((max_death - min_birth) x inf_delta) above the highest point. A reasonable value is between 0.05 and 0.5 - default is 0.1. @@ -178,9 +183,13 @@ try: print("file " + persistence_file + " not found.") return None - if max_plots > 0 and max_plots < len(persistence): - # Sort by life time, then takes only the max_plots elements - persistence = sorted(persistence, key=lambda life_time: life_time[1][1]-life_time[1][0], reverse=True)[:max_plots] + if max_plots is not 1000: + print('Deprecated parameter. It has been replaced by max_intervals') + max_intervals = max_plots + + if max_intervals > 0 and max_intervals < len(persistence): + # Sort by life time, then takes only the max_intervals elements + persistence = sorted(persistence, key=lambda life_time: life_time[1][1]-life_time[1][0], reverse=True)[:max_intervals] (min_birth, max_death) = __min_birth_max_death(persistence, band) delta = ((max_death - min_birth) * inf_delta) @@ -227,7 +236,7 @@ try: def plot_persistence_density(persistence=[], persistence_file='', nbins=300, bw_method=None, - max_plots=1000, dimension=None, + max_intervals=1000, dimension=None, cmap=plt.cm.hot_r, legend=False): """This function plots the persistence density from persistence values list or from a :doc:`persistence file `. Be -- cgit v1.2.3 From b301bf80daaac521f16d43120a8a30a2af9eb515 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 17 Aug 2018 15:29:19 +0000 Subject: Makes clearer dependencies for each function of persistence graphical tools git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3804 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 7572f54368ff235f5e78df74121bf6b1ee107177 --- src/cython/doc/persistence_graphical_tools_sum.inc | 2 +- src/cython/doc/persistence_graphical_tools_user.rst | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cython/doc/persistence_graphical_tools_sum.inc b/src/cython/doc/persistence_graphical_tools_sum.inc index c793a352..5577cf99 100644 --- a/src/cython/doc/persistence_graphical_tools_sum.inc +++ b/src/cython/doc/persistence_graphical_tools_sum.inc @@ -1,6 +1,6 @@ ================================================================= =================================== =================================== :Author: Vincent Rouvreau :Introduced in: GUDHI 2.0.0 :Copyright: GPL v3 -:Requires: Matplotlib Numpy Scipy +:Requires: matplotlib numpy scipy ================================================================= =================================== =================================== +-----------------------------------------------------------------+-----------------------------------------------------------------------+ diff --git a/src/cython/doc/persistence_graphical_tools_user.rst b/src/cython/doc/persistence_graphical_tools_user.rst index fda086c3..b2124fdd 100644 --- a/src/cython/doc/persistence_graphical_tools_user.rst +++ b/src/cython/doc/persistence_graphical_tools_user.rst @@ -12,6 +12,9 @@ Definition Show persistence as a barcode ----------------------------- +.. note:: + this function requires matplotlib and numpy to be available + This function can display the persistence result as a barcode: .. plot:: @@ -32,6 +35,9 @@ This function can display the persistence result as a barcode: Show persistence as a diagram ----------------------------- +.. note:: + this function requires matplotlib and numpy to be available + This function can display the persistence result as a diagram: .. plot:: @@ -49,6 +55,9 @@ This function can display the persistence result as a diagram: Persistence density ------------------- +.. note:: + this function requires matplotlib, numpy and scipy to be available + If you want more information on a specific dimension, for instance: .. plot:: -- cgit v1.2.3 From 44aaa06e1da4a2e4557886c9c7096edbe9d00f53 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Mon, 10 Sep 2018 08:37:45 +0000 Subject: Doc review : Add a link to scipy.stats.gaussian_kde official documentation git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3878 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: dd6e1a4131be1ed0306a48f9b095e0d09ddee848 --- src/cython/cython/persistence_graphical_tools.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index 85d2a5ad..f5550e3a 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -256,8 +256,10 @@ try: or a callable. If a scalar, this will be used directly as kde.factor. If a callable, it should take a gaussian_kde instance as only parameter and return a scalar. If None - (default), 'scott' is used. See scipy.stats.gaussian_kde - documentation for more details. + (default), 'scott' is used. See + `scipy.stats.gaussian_kde documentation + `_ + for more details. :type bw_method: str, scalar or callable, optional. :param max_intervals: maximal number of intervals to display. Selected points are those with the longest life time. Set it -- cgit v1.2.3 From bdbbbc71e7d82526b24f76cc3b9916ad53919d41 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Mon, 10 Sep 2018 09:32:00 +0000 Subject: Code review : More explicit message when import fails in plot persistence graphical tools git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3881 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 96f9e3da3bd68d6cba65242b9890c4beb463c863 --- src/cython/cython/persistence_graphical_tools.py | 309 ++++++++++++----------- 1 file changed, 161 insertions(+), 148 deletions(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index f5550e3a..e66643d2 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -55,39 +55,39 @@ palette = ['#ff0000', '#00ff00', '#0000ff', '#00ffff', '#ff00ff', '#ffff00', '#000000', '#880000', '#008800', '#000088', '#888800', '#880088', '#008888'] -try: - import matplotlib.pyplot as plt - import matplotlib.patches as mpatches - import numpy as np - import os - - def plot_persistence_barcode(persistence=[], persistence_file='', alpha=0.6, - max_intervals=1000, max_barcodes=1000, - inf_delta=0.1, legend=False): - """This function plots the persistence bar code from persistence values list - or from a :doc:`persistence file `. - - :param persistence: Persistence values list. - :type persistence: list of tuples(dimension, tuple(birth, death)). - :param persistence_file: A :doc:`persistence file ` style name - (reset persistence if both are set). - :type persistence_file: string - :param alpha: barcode transparency value (0.0 transparent through 1.0 - opaque - default is 0.6). - :type alpha: float. - :param max_intervals: maximal number of intervals to display. - Selected points are those with the longest life time. Set it - to 0 to see all. Default value is 1000. - :type max_intervals: int. - :param inf_delta: Infinity is placed at ((max_death - min_birth) x - inf_delta) above the highest point. A reasonable value is between - 0.05 and 0.5 - default is 0.1. - :type inf_delta: float. - :param legend: Display the dimension color legend (default is False). - :type legend: boolean. - :returns: A matplotlib object containing horizontal bar plot of persistence - (launch `show()` method on it to display it). - """ +def plot_persistence_barcode(persistence=[], persistence_file='', alpha=0.6, + max_intervals=1000, max_barcodes=1000, + inf_delta=0.1, legend=False): + """This function plots the persistence bar code from persistence values list + or from a :doc:`persistence file `. + + :param persistence: Persistence values list. + :type persistence: list of tuples(dimension, tuple(birth, death)). + :param persistence_file: A :doc:`persistence file ` style name + (reset persistence if both are set). + :type persistence_file: string + :param alpha: barcode transparency value (0.0 transparent through 1.0 + opaque - default is 0.6). + :type alpha: float. + :param max_intervals: maximal number of intervals to display. + Selected points are those with the longest life time. Set it + to 0 to see all. Default value is 1000. + :type max_intervals: int. + :param inf_delta: Infinity is placed at ((max_death - min_birth) x + inf_delta) above the highest point. A reasonable value is between + 0.05 and 0.5 - default is 0.1. + :type inf_delta: float. + :param legend: Display the dimension color legend (default is False). + :type legend: boolean. + :returns: A matplotlib object containing horizontal bar plot of persistence + (launch `show()` method on it to display it). + """ + try: + import matplotlib.pyplot as plt + import matplotlib.patches as mpatches + import numpy as np + import os + if persistence_file is not '': if os.path.isfile(persistence_file): # Reset persistence @@ -143,34 +143,43 @@ try: plt.axis([axis_start, infinity, 0, ind]) return plt - def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, - band=0., max_intervals=1000, max_plots=1000, inf_delta=0.1, legend=False): - """This function plots the persistence diagram from persistence values - list or from a :doc:`persistence file `. - - :param persistence: Persistence values list. - :type persistence: list of tuples(dimension, tuple(birth, death)). - :param persistence_file: A :doc:`persistence file ` style name - (reset persistence if both are set). - :type persistence_file: string - :param alpha: plot transparency value (0.0 transparent through 1.0 - opaque - default is 0.6). - :type alpha: float. - :param band: band (not displayed if :math:`\leq` 0. - default is 0.) - :type band: float. - :param max_intervals: maximal number of intervals to display. - Selected points are those with the longest life time. Set it - to 0 to see all. Default value is 1000. - :type max_intervals: int. - :param inf_delta: Infinity is placed at ((max_death - min_birth) x - inf_delta) above the highest point. A reasonable value is between - 0.05 and 0.5 - default is 0.1. - :type inf_delta: float. - :param legend: Display the dimension color legend (default is False). - :type legend: boolean. - :returns: A matplotlib object containing diagram plot of persistence - (launch `show()` method on it to display it). - """ + except ImportError: + print("This function is not available, you may be missing numpy and/or matplotlib.") + +def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, + band=0., max_intervals=1000, max_plots=1000, inf_delta=0.1, legend=False): + """This function plots the persistence diagram from persistence values + list or from a :doc:`persistence file `. + + :param persistence: Persistence values list. + :type persistence: list of tuples(dimension, tuple(birth, death)). + :param persistence_file: A :doc:`persistence file ` style name + (reset persistence if both are set). + :type persistence_file: string + :param alpha: plot transparency value (0.0 transparent through 1.0 + opaque - default is 0.6). + :type alpha: float. + :param band: band (not displayed if :math:`\leq` 0. - default is 0.) + :type band: float. + :param max_intervals: maximal number of intervals to display. + Selected points are those with the longest life time. Set it + to 0 to see all. Default value is 1000. + :type max_intervals: int. + :param inf_delta: Infinity is placed at ((max_death - min_birth) x + inf_delta) above the highest point. A reasonable value is between + 0.05 and 0.5 - default is 0.1. + :type inf_delta: float. + :param legend: Display the dimension color legend (default is False). + :type legend: boolean. + :returns: A matplotlib object containing diagram plot of persistence + (launch `show()` method on it to display it). + """ + try: + import matplotlib.pyplot as plt + import matplotlib.patches as mpatches + import numpy as np + import os + if persistence_file is not '': if os.path.isfile(persistence_file): # Reset persistence @@ -230,104 +239,108 @@ try: plt.axis([axis_start, infinity, axis_start, infinity + delta]) return plt + except ImportError: + print("This function is not available, you may be missing numpy and/or matplotlib.") + +def plot_persistence_density(persistence=[], persistence_file='', + nbins=300, bw_method=None, + max_intervals=1000, dimension=None, + cmap=None, legend=False): + """This function plots the persistence density from persistence + values list or from a :doc:`persistence file `. Be + aware that this function does not distinguish the dimension, it is + up to you to select the required one. + + :param persistence: Persistence values list. + :type persistence: list of tuples(dimension, tuple(birth, death)). + :param persistence_file: A :doc:`persistence file ` + style name (reset persistence if both are set). + :type persistence_file: string + :param nbins: Evaluate a gaussian kde on a regular grid of nbins x + nbins over data extents (default is 300) + :type nbins: int. + :param bw_method: The method used to calculate the estimator + bandwidth. This can be 'scott', 'silverman', a scalar constant + or a callable. If a scalar, this will be used directly as + kde.factor. If a callable, it should take a gaussian_kde + instance as only parameter and return a scalar. If None + (default), 'scott' is used. See + `scipy.stats.gaussian_kde documentation + `_ + for more details. + :type bw_method: str, scalar or callable, optional. + :param max_intervals: maximal number of intervals to display. + Selected points are those with the longest life time. Set it + to 0 to see all. Default value is 1000. + :type max_intervals: int. + :param dimension: the dimension to be selected in the intervals + (default is None to mix all dimensions). + :type dimension: int. + :param cmap: A matplotlib colormap (default is + matplotlib.pyplot.cm.hot_r). + :type cmap: cf. matplotlib colormap. + :param legend: Display the color bar values (default is False). + :type legend: boolean. + :returns: A matplotlib object containing diagram plot of persistence + (launch `show()` method on it to display it). + """ try: + import matplotlib.pyplot as plt + import numpy as np from scipy.stats import kde + import os import math - def plot_persistence_density(persistence=[], persistence_file='', - nbins=300, bw_method=None, - max_intervals=1000, dimension=None, - cmap=plt.cm.hot_r, legend=False): - """This function plots the persistence density from persistence - values list or from a :doc:`persistence file `. Be - aware that this function does not distinguish the dimension, it is - up to you to select the required one. - - :param persistence: Persistence values list. - :type persistence: list of tuples(dimension, tuple(birth, death)). - :param persistence_file: A :doc:`persistence file ` - style name (reset persistence if both are set). - :type persistence_file: string - :param nbins: Evaluate a gaussian kde on a regular grid of nbins x - nbins over data extents (default is 300) - :type nbins: int. - :param bw_method: The method used to calculate the estimator - bandwidth. This can be 'scott', 'silverman', a scalar constant - or a callable. If a scalar, this will be used directly as - kde.factor. If a callable, it should take a gaussian_kde - instance as only parameter and return a scalar. If None - (default), 'scott' is used. See - `scipy.stats.gaussian_kde documentation - `_ - for more details. - :type bw_method: str, scalar or callable, optional. - :param max_intervals: maximal number of intervals to display. - Selected points are those with the longest life time. Set it - to 0 to see all. Default value is 1000. - :type max_intervals: int. - :param dimension: the dimension to be selected in the intervals - (default is None to mix all dimensions). - :type dimension: int. - :param cmap: A matplotlib colormap (default is - matplotlib.pyplot.cm.hot_r). - :type cmap: cf. matplotlib colormap. - :param legend: Display the color bar values (default is False). - :type legend: boolean. - :returns: A matplotlib object containing diagram plot of persistence - (launch `show()` method on it to display it). - """ - if persistence_file is not '': - if os.path.isfile(persistence_file): - # Reset persistence - persistence = [] - diag = read_persistence_intervals_grouped_by_dimension(persistence_file=persistence_file) - for key in diag.keys(): - for persistence_interval in diag[key]: - persistence.append((key, persistence_interval)) - else: - print("file " + persistence_file + " not found.") - return None - - persistence_dim = [] - if dimension is not None: - persistence_dim = [(dim_interval) for dim_interval in persistence if (dim_interval[0] == dimension)] + if persistence_file is not '': + if os.path.isfile(persistence_file): + # Reset persistence + persistence = [] + diag = read_persistence_intervals_grouped_by_dimension(persistence_file=persistence_file) + for key in diag.keys(): + for persistence_interval in diag[key]: + persistence.append((key, persistence_interval)) else: - persistence_dim = persistence + print("file " + persistence_file + " not found.") + return None - if max_intervals > 0 and max_intervals < len(persistence_dim): - # Sort by life time, then takes only the max_intervals elements - persistence_dim = sorted(persistence_dim, - key=lambda life_time: life_time[1][1]-life_time[1][0], - reverse=True)[:max_intervals] + persistence_dim = [] + if dimension is not None: + persistence_dim = [(dim_interval) for dim_interval in persistence if (dim_interval[0] == dimension)] + else: + persistence_dim = persistence - # Set as numpy array birth and death (remove undefined values - inf and NaN) - birth = np.asarray([(interval[1][0]) for interval in persistence_dim if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) - death = np.asarray([(interval[1][1]) for interval in persistence_dim if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) + if max_intervals > 0 and max_intervals < len(persistence_dim): + # Sort by life time, then takes only the max_intervals elements + persistence_dim = sorted(persistence_dim, + key=lambda life_time: life_time[1][1]-life_time[1][0], + reverse=True)[:max_intervals] - # line display of equation : birth = death - x = np.linspace(death.min(), birth.max(), 1000) - plt.plot(x, x, color='k', linewidth=1.0) + # Set as numpy array birth and death (remove undefined values - inf and NaN) + birth = np.asarray([(interval[1][0]) for interval in persistence_dim if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) + death = np.asarray([(interval[1][1]) for interval in persistence_dim if (math.isfinite(interval[1][1]) and math.isfinite(interval[1][0]))]) - # Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents - k = kde.gaussian_kde([birth,death], bw_method=bw_method) - xi, yi = np.mgrid[birth.min():birth.max():nbins*1j, death.min():death.max():nbins*1j] - zi = k(np.vstack([xi.flatten(), yi.flatten()])) + # line display of equation : birth = death + x = np.linspace(death.min(), birth.max(), 1000) + plt.plot(x, x, color='k', linewidth=1.0) - # Make the plot - plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=cmap) + # Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents + k = kde.gaussian_kde([birth,death], bw_method=bw_method) + xi, yi = np.mgrid[birth.min():birth.max():nbins*1j, death.min():death.max():nbins*1j] + zi = k(np.vstack([xi.flatten(), yi.flatten()])) - if legend: - plt.colorbar() + # default cmap value cannot be done at argument definition level as matplotlib is not yet defined. + if cmap is None: + cmap = plt.cm.hot_r + # Make the plot + plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=cmap) - plt.title('Persistence density') - plt.xlabel('Birth') - plt.ylabel('Death') - return plt + if legend: + plt.colorbar() - except ImportError: - # Continue in case of import error, functions won't be available - pass + plt.title('Persistence density') + plt.xlabel('Birth') + plt.ylabel('Death') + return plt -except ImportError: - # Continue in case of import error, functions won't be available - pass + except ImportError: + print("This function is not available, you may be missing numpy, matplotlib and/or scipy.") -- cgit v1.2.3 From 338b2f41bd14f04193a0b2f6fb0a2321443be45b Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Tue, 2 Oct 2018 07:45:51 +0000 Subject: Doc review: max_intervals documentation in plot_persistence_density git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3917 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 90cfd77ad4aad4cdb0937f43dd4f802688f98295 --- src/cython/cython/persistence_graphical_tools.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index e66643d2..139a2332 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -269,7 +269,8 @@ def plot_persistence_density(persistence=[], persistence_file='', `_ for more details. :type bw_method: str, scalar or callable, optional. - :param max_intervals: maximal number of intervals to display. + :param max_intervals: maximal number of points used in the density + estimation. Selected points are those with the longest life time. Set it to 0 to see all. Default value is 1000. :type max_intervals: int. -- cgit v1.2.3 From 5469c3637df2efd1a70a165484b992727782d15c Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Tue, 2 Oct 2018 07:51:22 +0000 Subject: Doc review : "Selected points are" -> "Selected intervals are" git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3918 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 79857a4609428fc623f8f4656762261ea664cf12 --- src/cython/cython/persistence_graphical_tools.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index 139a2332..abc205bb 100755 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -70,7 +70,7 @@ def plot_persistence_barcode(persistence=[], persistence_file='', alpha=0.6, opaque - default is 0.6). :type alpha: float. :param max_intervals: maximal number of intervals to display. - Selected points are those with the longest life time. Set it + Selected intervals are those with the longest life time. Set it to 0 to see all. Default value is 1000. :type max_intervals: int. :param inf_delta: Infinity is placed at ((max_death - min_birth) x @@ -162,7 +162,7 @@ def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, :param band: band (not displayed if :math:`\leq` 0. - default is 0.) :type band: float. :param max_intervals: maximal number of intervals to display. - Selected points are those with the longest life time. Set it + Selected intervals are those with the longest life time. Set it to 0 to see all. Default value is 1000. :type max_intervals: int. :param inf_delta: Infinity is placed at ((max_death - min_birth) x @@ -271,7 +271,7 @@ def plot_persistence_density(persistence=[], persistence_file='', :type bw_method: str, scalar or callable, optional. :param max_intervals: maximal number of points used in the density estimation. - Selected points are those with the longest life time. Set it + Selected intervals are those with the longest life time. Set it to 0 to see all. Default value is 1000. :type max_intervals: int. :param dimension: the dimension to be selected in the intervals -- cgit v1.2.3 From b4c51b3d92564e2e93e8fa4fe55f98cd06bba57a Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Tue, 2 Oct 2018 08:13:28 +0000 Subject: Code review : remove executable property Doc review : Rephrase inf_delta explanation git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3919 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: b45697b675a20cdca7bb42fa7a555df6c35fe0fd --- src/cython/cython/persistence_graphical_tools.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) mode change 100755 => 100644 src/cython/cython/persistence_graphical_tools.py diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py old mode 100755 new mode 100644 index abc205bb..e63b350b --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -61,7 +61,7 @@ def plot_persistence_barcode(persistence=[], persistence_file='', alpha=0.6, """This function plots the persistence bar code from persistence values list or from a :doc:`persistence file `. - :param persistence: Persistence values list. + :param persistence: Persistence intervals values list grouped by dimension. :type persistence: list of tuples(dimension, tuple(birth, death)). :param persistence_file: A :doc:`persistence file ` style name (reset persistence if both are set). @@ -73,9 +73,9 @@ def plot_persistence_barcode(persistence=[], persistence_file='', alpha=0.6, Selected intervals are those with the longest life time. Set it to 0 to see all. Default value is 1000. :type max_intervals: int. - :param inf_delta: Infinity is placed at ((max_death - min_birth) x - inf_delta) above the highest point. A reasonable value is between - 0.05 and 0.5 - default is 0.1. + :param inf_delta: Infinity is placed at :code:`((max_death - min_birth) x + inf_delta)` above :code:`max_death` value. A reasonable value is + between 0.05 and 0.5 - default is 0.1. :type inf_delta: float. :param legend: Display the dimension color legend (default is False). :type legend: boolean. @@ -151,7 +151,7 @@ def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, """This function plots the persistence diagram from persistence values list or from a :doc:`persistence file `. - :param persistence: Persistence values list. + :param persistence: Persistence intervals values list grouped by dimension. :type persistence: list of tuples(dimension, tuple(birth, death)). :param persistence_file: A :doc:`persistence file ` style name (reset persistence if both are set). @@ -165,9 +165,9 @@ def plot_persistence_diagram(persistence=[], persistence_file='', alpha=0.6, Selected intervals are those with the longest life time. Set it to 0 to see all. Default value is 1000. :type max_intervals: int. - :param inf_delta: Infinity is placed at ((max_death - min_birth) x - inf_delta) above the highest point. A reasonable value is between - 0.05 and 0.5 - default is 0.1. + :param inf_delta: Infinity is placed at :code:`((max_death - min_birth) x + inf_delta)` above :code:`max_death` value. A reasonable value is + between 0.05 and 0.5 - default is 0.1. :type inf_delta: float. :param legend: Display the dimension color legend (default is False). :type legend: boolean. @@ -251,7 +251,7 @@ def plot_persistence_density(persistence=[], persistence_file='', aware that this function does not distinguish the dimension, it is up to you to select the required one. - :param persistence: Persistence values list. + :param persistence: Persistence intervals values list grouped by dimension. :type persistence: list of tuples(dimension, tuple(birth, death)). :param persistence_file: A :doc:`persistence file ` style name (reset persistence if both are set). -- cgit v1.2.3 From 2dde0fc4ea8f24173789a1ce9a9442cb02b96e86 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Tue, 2 Oct 2018 08:23:51 +0000 Subject: Doc review : Explain that plot persistence density function doesn't handle degenerate data git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/plot_persistence_density_vincent@3920 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 7794f7959b98b0245e26077d2f2639f33655d5c9 --- src/cython/cython/persistence_graphical_tools.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py index e63b350b..d7be936f 100644 --- a/src/cython/cython/persistence_graphical_tools.py +++ b/src/cython/cython/persistence_graphical_tools.py @@ -249,7 +249,8 @@ def plot_persistence_density(persistence=[], persistence_file='', """This function plots the persistence density from persistence values list or from a :doc:`persistence file `. Be aware that this function does not distinguish the dimension, it is - up to you to select the required one. + up to you to select the required one. This function also does not handle + degenerate data set (scipy correlation matrix inversion can fail). :param persistence: Persistence intervals values list grouped by dimension. :type persistence: list of tuples(dimension, tuple(birth, death)). -- cgit v1.2.3