summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/cython/cython/persistence_graphical_tools.py73
-rw-r--r--src/cython/doc/installation.rst9
-rw-r--r--src/cython/doc/persistence_graphical_tools_ref.rst1
-rw-r--r--src/cython/doc/persistence_graphical_tools_sum.inc4
-rw-r--r--src/cython/doc/persistence_graphical_tools_user.rst9
5 files changed, 89 insertions, 7 deletions
diff --git a/src/cython/cython/persistence_graphical_tools.py b/src/cython/cython/persistence_graphical_tools.py
index 314bd6db..e172c65b 100755
--- a/src/cython/cython/persistence_graphical_tools.py
+++ b/src/cython/cython/persistence_graphical_tools.py
@@ -28,7 +28,9 @@ 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.
@@ -80,6 +82,8 @@ try:
: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).
"""
@@ -155,6 +159,8 @@ try:
: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).
"""
@@ -175,7 +181,6 @@ try:
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
@@ -215,6 +220,72 @@ 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 <fileformats>`. 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 <fileformats>` 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
diff --git a/src/cython/doc/installation.rst b/src/cython/doc/installation.rst
index 43576ec9..ef2f7af2 100644
--- a/src/cython/doc/installation.rst
+++ b/src/cython/doc/installation.rst
@@ -195,7 +195,7 @@ The following examples require the `Matplotlib <http://matplotlib.org>`_:
* :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 </persistence_graphical_tools_user>`
@@ -216,6 +216,13 @@ The following examples require the `NumPy <http://numpy.org>`_:
* :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 </persistence_graphical_tools_user>`
+module requires `SciPy <http://scipy.org>`_, 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_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_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` |
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()