summaryrefslogtreecommitdiff
path: root/src/python/gudhi
diff options
context:
space:
mode:
Diffstat (limited to 'src/python/gudhi')
-rw-r--r--src/python/gudhi/__init__.py.in32
-rw-r--r--src/python/gudhi/alpha_complex.pyx27
-rw-r--r--src/python/gudhi/bottleneck.pyx19
-rw-r--r--src/python/gudhi/cubical_complex.pyx47
-rw-r--r--src/python/gudhi/euclidean_strong_witness_complex.pyx21
-rw-r--r--src/python/gudhi/euclidean_witness_complex.pyx21
-rw-r--r--src/python/gudhi/nerve_gic.pyx31
-rw-r--r--src/python/gudhi/off_reader.pyx25
-rw-r--r--src/python/gudhi/periodic_cubical_complex.pyx61
-rw-r--r--src/python/gudhi/persistence_graphical_tools.py163
-rw-r--r--src/python/gudhi/reader_utils.pyx33
-rw-r--r--src/python/gudhi/representations/__init__.py6
-rw-r--r--src/python/gudhi/representations/kernel_methods.py206
-rw-r--r--src/python/gudhi/representations/metrics.py244
-rw-r--r--src/python/gudhi/representations/preprocessing.py305
-rw-r--r--src/python/gudhi/representations/vector_methods.py492
-rw-r--r--src/python/gudhi/rips_complex.pyx19
-rw-r--r--src/python/gudhi/simplex_tree.pxd19
-rw-r--r--src/python/gudhi/simplex_tree.pyx36
-rw-r--r--src/python/gudhi/strong_witness_complex.pyx21
-rw-r--r--src/python/gudhi/subsampling.pyx41
-rw-r--r--src/python/gudhi/tangential_complex.pyx27
-rw-r--r--src/python/gudhi/wasserstein.py97
-rw-r--r--src/python/gudhi/witness_complex.pyx21
24 files changed, 1703 insertions, 311 deletions
diff --git a/src/python/gudhi/__init__.py.in b/src/python/gudhi/__init__.py.in
index 28bab0e1..79e12fbc 100644
--- a/src/python/gudhi/__init__.py.in
+++ b/src/python/gudhi/__init__.py.in
@@ -1,14 +1,14 @@
-from importlib import import_module
-
-"""This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
+from importlib import import_module
+from sys import exc_info
__author__ = "GUDHI Editorial Board"
__copyright__ = "Copyright (C) 2016 Inria"
@@ -18,16 +18,16 @@ __version__ = "@GUDHI_VERSION@"
__root_source_dir__ = "@CMAKE_SOURCE_DIR@"
__debug_info__ = @GUDHI_PYTHON_DEBUG_INFO@
-from sys import exc_info
-from importlib import import_module
-
-__all__ = [@GUDHI_PYTHON_MODULES@]
+__all__ = [@GUDHI_PYTHON_MODULES@ @GUDHI_PYTHON_MODULES_EXTRA@]
__available_modules = ''
__missing_modules = ''
-# try to import * from gudhi.__module_name
-for __module_name in __all__:
+# Try to import * from gudhi.__module_name for default modules.
+# Extra modules require an explicit import by the user (mostly because of
+# unusual dependencies, but also to avoid cluttering namespace gudhi and
+# speed up the basic import)
+for __module_name in [@GUDHI_PYTHON_MODULES@]:
try:
__module = import_module('gudhi.' + __module_name)
try:
diff --git a/src/python/gudhi/alpha_complex.pyx b/src/python/gudhi/alpha_complex.pyx
index 6d6309db..f3ca3dd5 100644
--- a/src/python/gudhi/alpha_complex.pyx
+++ b/src/python/gudhi/alpha_complex.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
@@ -9,16 +18,6 @@ import os
from gudhi.simplex_tree cimport *
from gudhi.simplex_tree import SimplexTree
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "GPL v3"
@@ -67,10 +66,10 @@ cdef class AlphaComplex:
"""
# The real cython constructor
- def __cinit__(self, points=None, off_file=''):
- if off_file is not '':
+ def __cinit__(self, points = None, off_file = ''):
+ if off_file:
if os.path.isfile(off_file):
- self.thisptr = new Alpha_complex_interface(str.encode(off_file), True)
+ self.thisptr = new Alpha_complex_interface(off_file.encode('utf-8'), True)
else:
print("file " + off_file + " not found.")
else:
@@ -100,7 +99,7 @@ cdef class AlphaComplex:
cdef vector[double] point = self.thisptr.get_point(vertex)
return point
- def create_simplex_tree(self, max_alpha_square=float('inf')):
+ def create_simplex_tree(self, max_alpha_square = float('inf')):
"""
:param max_alpha_square: The maximum alpha square threshold the
simplices shall not exceed. Default is set to infinity, and
diff --git a/src/python/gudhi/bottleneck.pyx b/src/python/gudhi/bottleneck.pyx
index 4b378cbc..af011e88 100644
--- a/src/python/gudhi/bottleneck.pyx
+++ b/src/python/gudhi/bottleneck.pyx
@@ -1,18 +1,17 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
import os
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "GPL v3"
diff --git a/src/python/gudhi/cubical_complex.pyx b/src/python/gudhi/cubical_complex.pyx
index 0dc133d1..cbeda014 100644
--- a/src/python/gudhi/cubical_complex.pyx
+++ b/src/python/gudhi/cubical_complex.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
@@ -5,17 +14,7 @@ from libcpp.string cimport string
from libcpp cimport bool
import os
-from numpy import array as np_array
-
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
+import numpy as np
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
@@ -48,7 +47,7 @@ cdef class CubicalComplex:
# Fake constructor that does nothing but documenting the constructor
def __init__(self, dimensions=None, top_dimensional_cells=None,
- perseus_file=''):
+ perseus_file=''):
"""CubicalComplex constructor from dimensions and
top_dimensional_cells or from a Perseus-style file name.
@@ -59,6 +58,12 @@ cdef class CubicalComplex:
Or
+ :param top_dimensional_cells: A multidimensional array of cells
+ filtration values.
+ :type top_dimensional_cells: anything convertible to a numpy ndarray
+
+ Or
+
:param perseus_file: A Perseus-style file name.
:type perseus_file: string
"""
@@ -66,11 +71,21 @@ cdef class CubicalComplex:
# The real cython constructor
def __cinit__(self, dimensions=None, top_dimensional_cells=None,
perseus_file=''):
- if (dimensions is not None) and (top_dimensional_cells is not None) and (perseus_file is ''):
+ if ((dimensions is not None) and (top_dimensional_cells is not None)
+ and (perseus_file == '')):
+ self.thisptr = new Bitmap_cubical_complex_base_interface(dimensions, top_dimensional_cells)
+ elif ((dimensions is None) and (top_dimensional_cells is not None)
+ and (perseus_file == '')):
+ top_dimensional_cells = np.array(top_dimensional_cells,
+ copy = False,
+ order = 'F')
+ dimensions = top_dimensional_cells.shape
+ top_dimensional_cells = top_dimensional_cells.ravel(order='F')
self.thisptr = new Bitmap_cubical_complex_base_interface(dimensions, top_dimensional_cells)
- elif (dimensions is None) and (top_dimensional_cells is None) and (perseus_file is not ''):
+ elif ((dimensions is None) and (top_dimensional_cells is None)
+ and (perseus_file != '')):
if os.path.isfile(perseus_file):
- self.thisptr = new Bitmap_cubical_complex_base_interface(str.encode(perseus_file))
+ self.thisptr = new Bitmap_cubical_complex_base_interface(perseus_file.encode('utf-8'))
else:
print("file " + perseus_file + " not found.")
else:
@@ -185,4 +200,4 @@ cdef class CubicalComplex:
else:
print("intervals_in_dim function requires persistence function"
" to be launched first.")
- return np_array(intervals_result)
+ return np.array(intervals_result)
diff --git a/src/python/gudhi/euclidean_strong_witness_complex.pyx b/src/python/gudhi/euclidean_strong_witness_complex.pyx
index 5d6e4fb9..9889f92c 100644
--- a/src/python/gudhi/euclidean_strong_witness_complex.pyx
+++ b/src/python/gudhi/euclidean_strong_witness_complex.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
@@ -6,16 +15,6 @@ from libc.stdint cimport intptr_t
from gudhi.simplex_tree cimport *
from gudhi.simplex_tree import SimplexTree
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "GPL v3"
@@ -71,7 +70,7 @@ cdef class EuclideanStrongWitnessComplex:
"""
stree = SimplexTree()
cdef intptr_t stree_int_ptr=stree.thisptr
- if limit_dimension is not -1:
+ if limit_dimension != -1:
self.thisptr.create_simplex_tree(<Simplex_tree_interface_full_featured*>stree_int_ptr,
max_alpha_square, limit_dimension)
else:
diff --git a/src/python/gudhi/euclidean_witness_complex.pyx b/src/python/gudhi/euclidean_witness_complex.pyx
index 2531919b..e3ce0e82 100644
--- a/src/python/gudhi/euclidean_witness_complex.pyx
+++ b/src/python/gudhi/euclidean_witness_complex.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
@@ -6,16 +15,6 @@ from libc.stdint cimport intptr_t
from gudhi.simplex_tree cimport *
from gudhi.simplex_tree import SimplexTree
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "GPL v3"
@@ -71,7 +70,7 @@ cdef class EuclideanWitnessComplex:
"""
stree = SimplexTree()
cdef intptr_t stree_int_ptr=stree.thisptr
- if limit_dimension is not -1:
+ if limit_dimension != -1:
self.thisptr.create_simplex_tree(<Simplex_tree_interface_full_featured*>stree_int_ptr,
max_alpha_square, limit_dimension)
else:
diff --git a/src/python/gudhi/nerve_gic.pyx b/src/python/gudhi/nerve_gic.pyx
index 2b230b8c..382e71c5 100644
--- a/src/python/gudhi/nerve_gic.pyx
+++ b/src/python/gudhi/nerve_gic.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2018 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
@@ -9,16 +18,6 @@ from libc.stdint cimport intptr_t
from gudhi.simplex_tree cimport *
from gudhi.simplex_tree import SimplexTree
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2018 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2018 Inria"
__license__ = "GPL v3"
@@ -181,7 +180,7 @@ cdef class CoverComplex:
:returns: Read file status.
"""
if os.path.isfile(off_file):
- return self.thisptr.read_point_cloud(str.encode(off_file))
+ return self.thisptr.read_point_cloud(off_file.encode('utf-8'))
else:
print("file " + off_file + " not found.")
return False
@@ -213,7 +212,7 @@ cdef class CoverComplex:
:type color_file_name: string
"""
if os.path.isfile(color_file_name):
- self.thisptr.set_color_from_file(str.encode(color_file_name))
+ self.thisptr.set_color_from_file(color_file_name.encode('utf-8'))
else:
print("file " + color_file_name + " not found.")
@@ -234,7 +233,7 @@ cdef class CoverComplex:
:type cover_file_name: string
"""
if os.path.isfile(cover_file_name):
- self.thisptr.set_cover_from_file(str.encode(cover_file_name))
+ self.thisptr.set_cover_from_file(cover_file_name.encode('utf-8'))
else:
print("file " + cover_file_name + " not found.")
@@ -267,7 +266,7 @@ cdef class CoverComplex:
:type func_file_name: string
"""
if os.path.isfile(func_file_name):
- self.thisptr.set_function_from_file(str.encode(func_file_name))
+ self.thisptr.set_function_from_file(func_file_name.encode('utf-8'))
else:
print("file " + func_file_name + " not found.")
@@ -308,7 +307,7 @@ cdef class CoverComplex:
:type graph_file_name: string
"""
if os.path.isfile(graph_file_name):
- self.thisptr.set_graph_from_file(str.encode(graph_file_name))
+ self.thisptr.set_graph_from_file(graph_file_name.encode('utf-8'))
else:
print("file " + graph_file_name + " not found.")
@@ -369,7 +368,7 @@ cdef class CoverComplex:
:param type: either "GIC" or "Nerve".
:type type: string
"""
- self.thisptr.set_type(str.encode(type))
+ self.thisptr.set_type(type.encode('utf-8'))
def set_verbose(self, verbose):
"""Specifies whether the program should display information or not.
diff --git a/src/python/gudhi/off_reader.pyx b/src/python/gudhi/off_reader.pyx
index 9efd97ff..a0d5bf25 100644
--- a/src/python/gudhi/off_reader.pyx
+++ b/src/python/gudhi/off_reader.pyx
@@ -1,18 +1,17 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.string cimport string
import os
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "MIT"
@@ -20,7 +19,7 @@ __license__ = "MIT"
cdef extern from "Off_reader_interface.h" namespace "Gudhi":
vector[vector[double]] read_points_from_OFF_file(string off_file)
-def read_off(off_file=''):
+def read_points_from_off_file(off_file=''):
"""Read points from OFF file.
:param off_file: An OFF file style name.
@@ -29,9 +28,9 @@ def read_off(off_file=''):
:returns: The point set.
:rtype: vector[vector[double]]
"""
- if off_file is not '':
+ if off_file:
if os.path.isfile(off_file):
- return read_points_from_OFF_file(str.encode(off_file))
+ return read_points_from_OFF_file(off_file.encode('utf-8'))
else:
print("file " + off_file + " not found.")
return []
diff --git a/src/python/gudhi/periodic_cubical_complex.pyx b/src/python/gudhi/periodic_cubical_complex.pyx
index 724fadd4..37f76201 100644
--- a/src/python/gudhi/periodic_cubical_complex.pyx
+++ b/src/python/gudhi/periodic_cubical_complex.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
@@ -5,17 +14,7 @@ from libcpp.string cimport string
from libcpp cimport bool
import os
-from numpy import array as np_array
-
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
+import numpy as np
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
@@ -48,7 +47,7 @@ cdef class PeriodicCubicalComplex:
# Fake constructor that does nothing but documenting the constructor
def __init__(self, dimensions=None, top_dimensional_cells=None,
- periodic_dimensions=None, perseus_file=''):
+ periodic_dimensions=None, perseus_file=''):
"""PeriodicCubicalComplex constructor from dimensions and
top_dimensional_cells or from a Perseus-style file name.
@@ -61,6 +60,14 @@ cdef class PeriodicCubicalComplex:
Or
+ :param top_dimensional_cells: A multidimensional array of cells
+ filtration values.
+ :type top_dimensional_cells: anything convertible to a numpy ndarray
+ :param periodic_dimensions: A list of top dimensional cells periodicity value.
+ :type periodic_dimensions: list of boolean
+
+ Or
+
:param perseus_file: A Perseus-style file name.
:type perseus_file: string
"""
@@ -68,16 +75,32 @@ cdef class PeriodicCubicalComplex:
# The real cython constructor
def __cinit__(self, dimensions=None, top_dimensional_cells=None,
periodic_dimensions=None, perseus_file=''):
- if (dimensions is not None) and (top_dimensional_cells is not None) and (periodic_dimensions is not None) and (perseus_file is ''):
- self.thisptr = new Periodic_cubical_complex_base_interface(dimensions, top_dimensional_cells, periodic_dimensions)
- elif (dimensions is None) and (top_dimensional_cells is None) and (periodic_dimensions is None) and (perseus_file is not ''):
+ if ((dimensions is not None) and (top_dimensional_cells is not None)
+ and (periodic_dimensions is not None) and (perseus_file == '')):
+ self.thisptr = new Periodic_cubical_complex_base_interface(dimensions,
+ top_dimensional_cells,
+ periodic_dimensions)
+ elif ((dimensions is None) and (top_dimensional_cells is not None)
+ and (periodic_dimensions is not None) and (perseus_file == '')):
+ top_dimensional_cells = np.array(top_dimensional_cells,
+ copy = False,
+ order = 'F')
+ dimensions = top_dimensional_cells.shape
+ top_dimensional_cells = top_dimensional_cells.ravel(order='F')
+ self.thisptr = new Periodic_cubical_complex_base_interface(dimensions,
+ top_dimensional_cells,
+ periodic_dimensions)
+ elif ((dimensions is None) and (top_dimensional_cells is None)
+ and (periodic_dimensions is None) and (perseus_file != '')):
if os.path.isfile(perseus_file):
- self.thisptr = new Periodic_cubical_complex_base_interface(str.encode(perseus_file))
+ self.thisptr = new Periodic_cubical_complex_base_interface(perseus_file.encode('utf-8'))
else:
print("file " + perseus_file + " not found.")
else:
- print("CubicalComplex can be constructed from dimensions and "
- "top_dimensional_cells or from a Perseus-style file name.")
+ print("CubicalComplex can be constructed from dimensions, "
+ "top_dimensional_cells and periodic_dimensions, or from "
+ "top_dimensional_cells and periodic_dimensions or from "
+ "a Perseus-style file name.")
def __dealloc__(self):
if self.thisptr != NULL:
@@ -187,4 +210,4 @@ cdef class PeriodicCubicalComplex:
else:
print("intervals_in_dim function requires persistence function"
" to be launched first.")
- return np_array(intervals_result)
+ return np.array(intervals_result)
diff --git a/src/python/gudhi/persistence_graphical_tools.py b/src/python/gudhi/persistence_graphical_tools.py
index 181bc8ea..246280de 100644
--- a/src/python/gudhi/persistence_graphical_tools.py
+++ b/src/python/gudhi/persistence_graphical_tools.py
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau, Bertrand Michel
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from os import path
from math import isfinite
import numpy as np
@@ -5,16 +14,6 @@ import numpy as np
from gudhi.reader_utils import read_persistence_intervals_in_dimension
from gudhi.reader_utils import read_persistence_intervals_grouped_by_dimension
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau, Bertrand Michel
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau, Bertrand Michel"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "MIT"
@@ -44,27 +43,6 @@ def __min_birth_max_death(persistence, band=0.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="",
@@ -73,6 +51,8 @@ def plot_persistence_barcode(
max_barcodes=1000,
inf_delta=0.1,
legend=False,
+ colormap=None,
+ axes=None
):
"""This function plots the persistence bar code from persistence values list
or from a :doc:`persistence file <fileformats>`.
@@ -95,14 +75,19 @@ def plot_persistence_barcode(
: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).
+ :param colormap: A matplotlib-like qualitative colormaps. Default is None
+ which means :code:`matplotlib.cm.Set1.colors`.
+ :type colormap: tuple of colors (3-tuple of float between 0. and 1.).
+ :param axes: A matplotlib-like subplot axes. If None, the plot is drawn on
+ a new set of axes.
+ :type axes: `matplotlib.axes.Axes`
+ :returns: (`matplotlib.axes.Axes`): The axes on which the plot was drawn.
"""
try:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
- if persistence_file is not "":
+ if persistence_file != "":
if path.isfile(persistence_file):
# Reset persistence
persistence = []
@@ -116,7 +101,7 @@ def plot_persistence_barcode(
print("file " + persistence_file + " not found.")
return None
- if max_barcodes is not 1000:
+ if max_barcodes != 1000:
print("Deprecated parameter. It has been replaced by max_intervals")
max_intervals = max_barcodes
@@ -127,6 +112,11 @@ def plot_persistence_barcode(
key=lambda life_time: life_time[1][1] - life_time[1][0],
reverse=True,
)[:max_intervals]
+
+ if colormap == None:
+ colormap = plt.cm.Set1.colors
+ if axes == None:
+ fig, axes = plt.subplots(1, 1)
persistence = sorted(persistence, key=lambda birth: birth[1][0])
@@ -141,41 +131,43 @@ def plot_persistence_barcode(
for interval in reversed(persistence):
if float(interval[1][1]) != float("inf"):
# Finite death case
- plt.barh(
+ axes.barh(
ind,
(interval[1][1] - interval[1][0]),
height=0.8,
left=interval[1][0],
alpha=alpha,
- color=palette[interval[0]],
+ color=colormap[interval[0]],
linewidth=0,
)
else:
# Infinite death case for diagram to be nicer
- plt.barh(
+ axes.barh(
ind,
(infinity - interval[1][0]),
height=0.8,
left=interval[1][0],
alpha=alpha,
- color=palette[interval[0]],
+ color=colormap[interval[0]],
linewidth=0,
)
ind = ind + 1
if legend:
dimensions = list(set(item[0] for item in persistence))
- plt.legend(
+ axes.legend(
handles=[
- mpatches.Patch(color=palette[dim], label=str(dim))
+ mpatches.Patch(color=colormap[dim], label=str(dim))
for dim in dimensions
],
loc="lower right",
)
- plt.title("Persistence barcode")
+
+ axes.set_title("Persistence barcode")
+
# Ends plot on infinity value and starts a little bit before min_birth
- plt.axis([axis_start, infinity, 0, ind])
- return plt
+ axes.axis([axis_start, infinity, 0, ind])
+ return axes
except ImportError:
print("This function is not available, you may be missing matplotlib.")
@@ -190,6 +182,8 @@ def plot_persistence_diagram(
max_plots=1000,
inf_delta=0.1,
legend=False,
+ colormap=None,
+ axes=None
):
"""This function plots the persistence diagram from persistence values
list or from a :doc:`persistence file <fileformats>`.
@@ -214,14 +208,19 @@ def plot_persistence_diagram(
: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).
+ :param colormap: A matplotlib-like qualitative colormaps. Default is None
+ which means :code:`matplotlib.cm.Set1.colors`.
+ :type colormap: tuple of colors (3-tuple of float between 0. and 1.).
+ :param axes: A matplotlib-like subplot axes. If None, the plot is drawn on
+ a new set of axes.
+ :type axes: `matplotlib.axes.Axes`
+ :returns: (`matplotlib.axes.Axes`): The axes on which the plot was drawn.
"""
try:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
- if persistence_file is not "":
+ if persistence_file != "":
if path.isfile(persistence_file):
# Reset persistence
persistence = []
@@ -235,7 +234,7 @@ def plot_persistence_diagram(
print("file " + persistence_file + " not found.")
return None
- if max_plots is not 1000:
+ if max_plots != 1000:
print("Deprecated parameter. It has been replaced by max_intervals")
max_intervals = max_plots
@@ -247,6 +246,11 @@ def plot_persistence_diagram(
reverse=True,
)[:max_intervals]
+ if colormap == None:
+ colormap = plt.cm.Set1.colors
+ if axes == None:
+ fig, axes = plt.subplots(1, 1)
+
(min_birth, max_death) = __min_birth_max_death(persistence, band)
delta = (max_death - min_birth) * inf_delta
# Replace infinity values with max_death + delta for diagram to be more
@@ -257,44 +261,44 @@ def plot_persistence_diagram(
# 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=alpha)
- plt.text(axis_start, infinity, r"$\infty$", color="k", alpha=alpha)
+ axes.plot(x, x, color="k", linewidth=1.0)
+ axes.plot(x, [infinity] * len(x), linewidth=1.0, color="k", alpha=alpha)
+ axes.text(axis_start, infinity, r"$\infty$", color="k", alpha=alpha)
# bootstrap band
if band > 0.0:
- plt.fill_between(x, x, x + band, alpha=alpha, facecolor="red")
+ axes.fill_between(x, x, x + band, alpha=alpha, facecolor="red")
# Draw points in loop
for interval in reversed(persistence):
if float(interval[1][1]) != float("inf"):
# Finite death case
- plt.scatter(
+ axes.scatter(
interval[1][0],
interval[1][1],
alpha=alpha,
- color=palette[interval[0]],
+ color=colormap[interval[0]],
)
else:
# Infinite death case for diagram to be nicer
- plt.scatter(
- interval[1][0], infinity, alpha=alpha, color=palette[interval[0]]
+ axes.scatter(
+ interval[1][0], infinity, alpha=alpha, color=colormap[interval[0]]
)
if legend:
dimensions = list(set(item[0] for item in persistence))
- plt.legend(
+ axes.legend(
handles=[
- mpatches.Patch(color=palette[dim], label=str(dim))
+ mpatches.Patch(color=colormap[dim], label=str(dim))
for dim in dimensions
]
)
- plt.title("Persistence diagram")
- plt.xlabel("Birth")
- plt.ylabel("Death")
+ axes.set_xlabel("Birth")
+ axes.set_ylabel("Death")
# Ends plot on infinity value and starts a little bit before min_birth
- plt.axis([axis_start, infinity, axis_start, infinity + delta])
- return plt
+ axes.axis([axis_start, infinity, axis_start, infinity + delta])
+ axes.set_title("Persistence diagram")
+ return axes
except ImportError:
print("This function is not available, you may be missing matplotlib.")
@@ -309,6 +313,7 @@ def plot_persistence_density(
dimension=None,
cmap=None,
legend=False,
+ axes=None
):
"""This function plots the persistence density from persistence
values list or from a :doc:`persistence file <fileformats>`. Be
@@ -347,14 +352,16 @@ def plot_persistence_density(
: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).
+ :param axes: A matplotlib-like subplot axes. If None, the plot is drawn on
+ a new set of axes.
+ :type axes: `matplotlib.axes.Axes`
+ :returns: (`matplotlib.axes.Axes`): The axes on which the plot was drawn.
"""
try:
import matplotlib.pyplot as plt
from scipy.stats import kde
- if persistence_file is not "":
+ if persistence_file != "":
if dimension is None:
# All dimension case
dimension = -1
@@ -362,7 +369,6 @@ def plot_persistence_density(
persistence_dim = read_persistence_intervals_in_dimension(
persistence_file=persistence_file, only_this_dim=dimension
)
- print(persistence_dim)
else:
print("file " + persistence_file + " not found.")
return None
@@ -391,9 +397,15 @@ def plot_persistence_density(
birth = persistence_dim[:, 0]
death = persistence_dim[:, 1]
+ # 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
+ if axes == None:
+ fig, axes = plt.subplots(1, 1)
+
# line display of equation : birth = death
x = np.linspace(death.min(), birth.max(), 1000)
- plt.plot(x, x, color="k", linewidth=1.0)
+ axes.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], bw_method=bw_method)
@@ -403,19 +415,16 @@ def plot_persistence_density(
]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
- # 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)
+ img = axes.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=cmap)
if legend:
- plt.colorbar()
+ plt.colorbar(img, ax=axes)
- plt.title("Persistence density")
- plt.xlabel("Birth")
- plt.ylabel("Death")
- return plt
+ axes.set_xlabel("Birth")
+ axes.set_ylabel("Death")
+ axes.set_title("Persistence density")
+ return axes
except ImportError:
print(
diff --git a/src/python/gudhi/reader_utils.pyx b/src/python/gudhi/reader_utils.pyx
index 147fae71..d6033b86 100644
--- a/src/python/gudhi/reader_utils.pyx
+++ b/src/python/gudhi/reader_utils.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2017 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.string cimport string
@@ -7,16 +16,6 @@ from libcpp.pair cimport pair
from os import path
from numpy import array as np_array
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2017 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2017 Inria"
__license__ = "MIT"
@@ -37,9 +36,9 @@ def read_lower_triangular_matrix_from_csv_file(csv_file='', separator=';'):
:returns: The lower triangular matrix.
:rtype: vector[vector[double]]
"""
- if csv_file is not '':
+ if csv_file:
if path.isfile(csv_file):
- return read_matrix_from_csv_file(str.encode(csv_file), ord(separator[0]))
+ return read_matrix_from_csv_file(csv_file.encode('utf-8'), ord(separator[0]))
print("file " + csv_file + " not set or not found.")
return []
@@ -56,9 +55,9 @@ def read_persistence_intervals_grouped_by_dimension(persistence_file=''):
:returns: The persistence pairs grouped by dimension.
:rtype: map[int, vector[pair[double, double]]]
"""
- if persistence_file is not '':
+ if persistence_file:
if path.isfile(persistence_file):
- return read_pers_intervals_grouped_by_dimension(str.encode(persistence_file))
+ return read_pers_intervals_grouped_by_dimension(persistence_file.encode('utf-8'))
print("file " + persistence_file + " not set or not found.")
return []
@@ -79,9 +78,9 @@ def read_persistence_intervals_in_dimension(persistence_file='', only_this_dim=-
:returns: The persistence intervals.
:rtype: numpy array of dimension 2
"""
- if persistence_file is not '':
+ if persistence_file:
if path.isfile(persistence_file):
- return np_array(read_pers_intervals_in_dimension(str.encode(
- persistence_file), only_this_dim))
+ return np_array(read_pers_intervals_in_dimension(persistence_file.encode(
+ 'utf-8'), only_this_dim))
print("file " + persistence_file + " not set or not found.")
return []
diff --git a/src/python/gudhi/representations/__init__.py b/src/python/gudhi/representations/__init__.py
new file mode 100644
index 00000000..f020248d
--- /dev/null
+++ b/src/python/gudhi/representations/__init__.py
@@ -0,0 +1,6 @@
+from .kernel_methods import *
+from .metrics import *
+from .preprocessing import *
+from .vector_methods import *
+
+__all__ = ["kernel_methods", "metrics", "preprocessing", "vector_methods"]
diff --git a/src/python/gudhi/representations/kernel_methods.py b/src/python/gudhi/representations/kernel_methods.py
new file mode 100644
index 00000000..bfc83aff
--- /dev/null
+++ b/src/python/gudhi/representations/kernel_methods.py
@@ -0,0 +1,206 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Mathieu Carrière
+#
+# Copyright (C) 2018-2019 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
+import numpy as np
+from sklearn.base import BaseEstimator, TransformerMixin
+from sklearn.metrics import pairwise_distances
+from .metrics import SlicedWassersteinDistance, PersistenceFisherDistance
+
+#############################################
+# Kernel methods ############################
+#############################################
+
+class SlicedWassersteinKernel(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing the sliced Wasserstein kernel matrix from a list of persistence diagrams. The sliced Wasserstein kernel is computed by exponentiating the corresponding sliced Wasserstein distance with a Gaussian kernel. See http://proceedings.mlr.press/v70/carriere17a.html for more details.
+ """
+ def __init__(self, num_directions=10, bandwidth=1.0):
+ """
+ Constructor for the SlicedWassersteinKernel class.
+
+ Parameters:
+ bandwidth (double): bandwidth of the Gaussian kernel applied to the sliced Wasserstein distance (default 1.).
+ num_directions (int): number of lines evenly sampled from [-pi/2,pi/2] in order to approximate and speed up the kernel computation (default 10).
+ """
+ self.bandwidth = bandwidth
+ self.sw_ = SlicedWassersteinDistance(num_directions=num_directions)
+
+ def fit(self, X, y=None):
+ """
+ Fit the SlicedWassersteinKernel class on a list of persistence diagrams: an instance of the SlicedWassersteinDistance class is fitted on the diagrams and then stored.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ self.sw_.fit(X, y)
+ return self
+
+ def transform(self, X):
+ """
+ Compute all sliced Wasserstein kernel values between the persistence diagrams that were stored after calling the fit() method, and a given list of (possibly different) persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array of shape (number of diagrams in **diagrams**) x (number of diagrams in X): matrix of pairwise sliced Wasserstein kernel values.
+ """
+ return np.exp(-self.sw_.transform(X)/self.bandwidth)
+
+class PersistenceWeightedGaussianKernel(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing the persistence weighted Gaussian kernel matrix from a list of persistence diagrams. The persistence weighted Gaussian kernel is computed by convolving the persistence diagram points with weighted Gaussian kernels. See http://proceedings.mlr.press/v48/kusano16.html for more details.
+ """
+ def __init__(self, bandwidth=1., weight=lambda x: 1, kernel_approx=None):
+ """
+ Constructor for the PersistenceWeightedGaussianKernel class.
+
+ Parameters:
+ bandwidth (double): bandwidth of the Gaussian kernel with which persistence diagrams will be convolved (default 1.)
+ weight (function): weight function for the persistence diagram points (default constant function, ie lambda x: 1). This function must be defined on 2D points, ie lists or numpy arrays of the form [p_x,p_y].
+ kernel_approx (class): kernel approximation class used to speed up computation (default None). Common kernel approximations classes can be found in the scikit-learn library (such as RBFSampler for instance).
+ """
+ self.bandwidth, self.weight = bandwidth, weight
+ self.kernel_approx = kernel_approx
+
+ def fit(self, X, y=None):
+ """
+ Fit the PersistenceWeightedGaussianKernel class on a list of persistence diagrams: persistence diagrams are stored in a numpy array called **diagrams** and the kernel approximation class (if not None) is applied on them.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ self.diagrams_ = list(X)
+ self.ws_ = [ np.array([self.weight(self.diagrams_[i][j,:]) for j in range(self.diagrams_[i].shape[0])]) for i in range(len(self.diagrams_)) ]
+ if self.kernel_approx is not None:
+ self.approx_ = np.concatenate([np.sum(np.multiply(self.ws_[i][:,np.newaxis], self.kernel_approx.transform(self.diagrams_[i])), axis=0)[np.newaxis,:] for i in range(len(self.diagrams_))])
+ return self
+
+ def transform(self, X):
+ """
+ Compute all persistence weighted Gaussian kernel values between the persistence diagrams that were stored after calling the fit() method, and a given list of (possibly different) persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array of shape (number of diagrams in **diagrams**) x (number of diagrams in X): matrix of pairwise persistence weighted Gaussian kernel values.
+ """
+ Xp = list(X)
+ Xfit = np.zeros((len(Xp), len(self.diagrams_)))
+ if len(self.diagrams_) == len(Xp) and np.all([np.array_equal(self.diagrams_[i], Xp[i]) for i in range(len(Xp))]):
+ if self.kernel_approx is not None:
+ Xfit = (1./(np.sqrt(2*np.pi)*self.bandwidth)) * np.matmul(self.approx_, self.approx_.T)
+ else:
+ for i in range(len(self.diagrams_)):
+ for j in range(i+1, len(self.diagrams_)):
+ W = np.matmul(self.ws_[i][:,np.newaxis], self.ws_[j][np.newaxis,:])
+ E = (1./(np.sqrt(2*np.pi)*self.bandwidth)) * np.exp(-np.square(pairwise_distances(self.diagrams_[i], self.diagrams_[j]))/(2*np.square(self.bandwidth)))
+ Xfit[i,j] = np.sum(np.multiply(W, E))
+ Xfit[j,i] = Xfit[i,j]
+ else:
+ ws = [ np.array([self.weight(Xp[i][j,:]) for j in range(Xp[i].shape[0])]) for i in range(len(Xp)) ]
+ if self.kernel_approx is not None:
+ approx = np.concatenate([np.sum(np.multiply(ws[i][:,np.newaxis], self.kernel_approx.transform(Xp[i])), axis=0)[np.newaxis,:] for i in range(len(Xp))])
+ Xfit = (1./(np.sqrt(2*np.pi)*self.bandwidth)) * np.matmul(approx, self.approx_.T)
+ else:
+ for i in range(len(Xp)):
+ for j in range(len(self.diagrams_)):
+ W = np.matmul(ws[i][:,np.newaxis], self.ws_[j][np.newaxis,:])
+ E = (1./(np.sqrt(2*np.pi)*self.bandwidth)) * np.exp(-np.square(pairwise_distances(Xp[i], self.diagrams_[j]))/(2*np.square(self.bandwidth)))
+ Xfit[i,j] = np.sum(np.multiply(W, E))
+
+ return Xfit
+
+class PersistenceScaleSpaceKernel(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing the persistence scale space kernel matrix from a list of persistence diagrams. The persistence scale space kernel is computed by adding the symmetric to the diagonal of each point in each persistence diagram, with negative weight, and then convolving the points with a Gaussian kernel. See https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Reininghaus_A_Stable_Multi-Scale_2015_CVPR_paper.pdf for more details.
+ """
+ def __init__(self, bandwidth=1., kernel_approx=None):
+ """
+ Constructor for the PersistenceScaleSpaceKernel class.
+
+ Parameters:
+ bandwidth (double): bandwidth of the Gaussian kernel with which persistence diagrams will be convolved (default 1.)
+ kernel_approx (class): kernel approximation class used to speed up computation (default None). Common kernel approximations classes can be found in the scikit-learn library (such as RBFSampler for instance).
+ """
+ self.pwg_ = PersistenceWeightedGaussianKernel(bandwidth=bandwidth, weight=lambda x: 1 if x[1] >= x[0] else -1, kernel_approx=kernel_approx)
+
+ def fit(self, X, y=None):
+ """
+ Fit the PersistenceScaleSpaceKernel class on a list of persistence diagrams: symmetric to the diagonal of all points are computed and an instance of the PersistenceWeightedGaussianKernel class is fitted on the diagrams and then stored.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ self.diagrams_ = list(X)
+ for i in range(len(self.diagrams_)):
+ op_D = self.diagrams_[i][:,[1,0]]
+ self.diagrams_[i] = np.concatenate([self.diagrams_[i], op_D], axis=0)
+ self.pwg_.fit(X)
+ return self
+
+ def transform(self, X):
+ """
+ Compute all persistence scale space kernel values between the persistence diagrams that were stored after calling the fit() method, and a given list of (possibly different) persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array of shape (number of diagrams in **diagrams**) x (number of diagrams in X): matrix of pairwise persistence scale space kernel values.
+ """
+ Xp = list(X)
+ for i in range(len(Xp)):
+ op_X = Xp[i][:,[1,0]]
+ Xp[i] = np.concatenate([Xp[i], op_X], axis=0)
+ return self.pwg_.transform(Xp)
+
+class PersistenceFisherKernel(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing the persistence Fisher kernel matrix from a list of persistence diagrams. The persistence Fisher kernel is computed by exponentiating the corresponding persistence Fisher distance with a Gaussian kernel. See papers.nips.cc/paper/8205-persistence-fisher-kernel-a-riemannian-manifold-kernel-for-persistence-diagrams for more details.
+ """
+ def __init__(self, bandwidth_fisher=1., bandwidth=1., kernel_approx=None):
+ """
+ Constructor for the PersistenceFisherKernel class.
+
+ Parameters:
+ bandwidth (double): bandwidth of the Gaussian kernel applied to the persistence Fisher distance (default 1.).
+ bandwidth_fisher (double): bandwidth of the Gaussian kernel used to turn persistence diagrams into probability distributions by PersistenceFisherDistance class (default 1.).
+ kernel_approx (class): kernel approximation class used to speed up computation (default None). Common kernel approximations classes can be found in the scikit-learn library (such as RBFSampler for instance).
+ """
+ self.bandwidth = bandwidth
+ self.pf_ = PersistenceFisherDistance(bandwidth=bandwidth_fisher, kernel_approx=kernel_approx)
+
+ def fit(self, X, y=None):
+ """
+ Fit the PersistenceFisherKernel class on a list of persistence diagrams: an instance of the PersistenceFisherDistance class is fitted on the diagrams and then stored.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ self.pf_.fit(X, y)
+ return self
+
+ def transform(self, X):
+ """
+ Compute all persistence Fisher kernel values between the persistence diagrams that were stored after calling the fit() method, and a given list of (possibly different) persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array of shape (number of diagrams in **diagrams**) x (number of diagrams in X): matrix of pairwise persistence Fisher kernel values.
+ """
+ return np.exp(-self.pf_.transform(X)/self.bandwidth)
+
diff --git a/src/python/gudhi/representations/metrics.py b/src/python/gudhi/representations/metrics.py
new file mode 100644
index 00000000..5f9ec6ab
--- /dev/null
+++ b/src/python/gudhi/representations/metrics.py
@@ -0,0 +1,244 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Mathieu Carrière
+#
+# Copyright (C) 2018-2019 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
+import numpy as np
+from sklearn.base import BaseEstimator, TransformerMixin
+from sklearn.metrics import pairwise_distances
+try:
+ from .. import bottleneck_distance
+ USE_GUDHI = True
+except ImportError:
+ USE_GUDHI = False
+ print("Gudhi built without CGAL: BottleneckDistance will return a null matrix")
+
+#############################################
+# Metrics ###################################
+#############################################
+
+class SlicedWassersteinDistance(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing the sliced Wasserstein distance matrix from a list of persistence diagrams. The Sliced Wasserstein distance is computed by projecting the persistence diagrams onto lines, comparing the projections with the 1-norm, and finally integrating over all possible lines. See http://proceedings.mlr.press/v70/carriere17a.html for more details.
+ """
+ def __init__(self, num_directions=10):
+ """
+ Constructor for the SlicedWassersteinDistance class.
+
+ Parameters:
+ num_directions (int): number of lines evenly sampled from [-pi/2,pi/2] in order to approximate and speed up the distance computation (default 10).
+ """
+ self.num_directions = num_directions
+ thetas = np.linspace(-np.pi/2, np.pi/2, num=self.num_directions+1)[np.newaxis,:-1]
+ self.lines_ = np.concatenate([np.cos(thetas), np.sin(thetas)], axis=0)
+
+ def fit(self, X, y=None):
+ """
+ Fit the SlicedWassersteinDistance class on a list of persistence diagrams: persistence diagrams are projected onto the different lines. The diagrams themselves and their projections are then stored in numpy arrays, called **diagrams_** and **approx_diag_**.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ self.diagrams_ = X
+ self.approx_ = [np.matmul(X[i], self.lines_) for i in range(len(X))]
+ diag_proj = (1./2) * np.ones((2,2))
+ self.approx_diag_ = [np.matmul(np.matmul(X[i], diag_proj), self.lines_) for i in range(len(X))]
+ return self
+
+ def transform(self, X):
+ """
+ Compute all sliced Wasserstein distances between the persistence diagrams that were stored after calling the fit() method, and a given list of (possibly different) persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array of shape (number of diagrams in **diagrams**) x (number of diagrams in X): matrix of pairwise sliced Wasserstein distances.
+ """
+ Xfit = np.zeros((len(X), len(self.approx_)))
+ if len(self.diagrams_) == len(X) and np.all([np.array_equal(self.diagrams_[i], X[i]) for i in range(len(X))]):
+ for i in range(len(self.approx_)):
+ for j in range(i+1, len(self.approx_)):
+ A = np.sort(np.concatenate([self.approx_[i], self.approx_diag_[j]], axis=0), axis=0)
+ B = np.sort(np.concatenate([self.approx_[j], self.approx_diag_[i]], axis=0), axis=0)
+ L1 = np.sum(np.abs(A-B), axis=0)
+ Xfit[i,j] = np.mean(L1)
+ Xfit[j,i] = Xfit[i,j]
+ else:
+ diag_proj = (1./2) * np.ones((2,2))
+ approx = [np.matmul(X[i], self.lines_) for i in range(len(X))]
+ approx_diag = [np.matmul(np.matmul(X[i], diag_proj), self.lines_) for i in range(len(X))]
+ for i in range(len(approx)):
+ for j in range(len(self.approx_)):
+ A = np.sort(np.concatenate([approx[i], self.approx_diag_[j]], axis=0), axis=0)
+ B = np.sort(np.concatenate([self.approx_[j], approx_diag[i]], axis=0), axis=0)
+ L1 = np.sum(np.abs(A-B), axis=0)
+ Xfit[i,j] = np.mean(L1)
+
+ return Xfit
+
+class BottleneckDistance(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing the bottleneck distance matrix from a list of persistence diagrams.
+ """
+ def __init__(self, epsilon=None):
+ """
+ Constructor for the BottleneckDistance class.
+
+ Parameters:
+ epsilon (double): absolute (additive) error tolerated on the distance (default is the smallest positive float), see :func:`gudhi.bottleneck_distance`.
+ """
+ self.epsilon = epsilon
+
+ def fit(self, X, y=None):
+ """
+ Fit the BottleneckDistance class on a list of persistence diagrams: persistence diagrams are stored in a numpy array called **diagrams**.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ self.diagrams_ = X
+ return self
+
+ def transform(self, X):
+ """
+ Compute all bottleneck distances between the persistence diagrams that were stored after calling the fit() method, and a given list of (possibly different) persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array of shape (number of diagrams in **diagrams**) x (number of diagrams in X): matrix of pairwise bottleneck distances.
+ """
+ num_diag1 = len(X)
+
+ #if len(self.diagrams_) == len(X) and np.all([np.array_equal(self.diagrams_[i], X[i]) for i in range(len(X))]):
+ if X is self.diagrams_:
+ matrix = np.zeros((num_diag1, num_diag1))
+
+ if USE_GUDHI:
+ for i in range(num_diag1):
+ for j in range(i+1, num_diag1):
+ matrix[i,j] = bottleneck_distance(X[i], X[j], self.epsilon)
+ matrix[j,i] = matrix[i,j]
+ else:
+ print("Gudhi built without CGAL: returning a null matrix")
+
+ else:
+ num_diag2 = len(self.diagrams_)
+ matrix = np.zeros((num_diag1, num_diag2))
+
+ if USE_GUDHI:
+ for i in range(num_diag1):
+ for j in range(num_diag2):
+ matrix[i,j] = bottleneck_distance(X[i], self.diagrams_[j], self.epsilon)
+ else:
+ print("Gudhi built without CGAL: returning a null matrix")
+
+ Xfit = matrix
+
+ return Xfit
+
+class PersistenceFisherDistance(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing the persistence Fisher distance matrix from a list of persistence diagrams. The persistence Fisher distance is obtained by computing the original Fisher distance between the probability distributions associated to the persistence diagrams given by convolving them with a Gaussian kernel. See http://papers.nips.cc/paper/8205-persistence-fisher-kernel-a-riemannian-manifold-kernel-for-persistence-diagrams for more details.
+ """
+ def __init__(self, bandwidth=1., kernel_approx=None):
+ """
+ Constructor for the PersistenceFisherDistance class.
+
+ Parameters:
+ bandwidth (double): bandwidth of the Gaussian kernel used to turn persistence diagrams into probability distributions (default 1.).
+ kernel_approx (class): kernel approximation class used to speed up computation (default None). Common kernel approximations classes can be found in the scikit-learn library (such as RBFSampler for instance).
+ """
+ self.bandwidth, self.kernel_approx = bandwidth, kernel_approx
+
+ def fit(self, X, y=None):
+ """
+ Fit the PersistenceFisherDistance class on a list of persistence diagrams: persistence diagrams are stored in a numpy array called **diagrams** and the kernel approximation class (if not None) is applied on them.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ self.diagrams_ = X
+ projection = (1./2) * np.ones((2,2))
+ self.diagonal_projections_ = [np.matmul(X[i], projection) for i in range(len(X))]
+ if self.kernel_approx is not None:
+ self.approx_ = [self.kernel_approx.transform(X[i]) for i in range(len(X))]
+ self.approx_diagonal_ = [self.kernel_approx.transform(self.diagonal_projections_[i]) for i in range(len(X))]
+ return self
+
+ def transform(self, X):
+ """
+ Compute all persistence Fisher distances between the persistence diagrams that were stored after calling the fit() method, and a given list of (possibly different) persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array of shape (number of diagrams in **diagrams**) x (number of diagrams in X): matrix of pairwise persistence Fisher distances.
+ """
+ Xfit = np.zeros((len(X), len(self.diagrams_)))
+ if len(self.diagrams_) == len(X) and np.all([np.array_equal(self.diagrams_[i], X[i]) for i in range(len(X))]):
+ for i in range(len(self.diagrams_)):
+ for j in range(i+1, len(self.diagrams_)):
+ if self.kernel_approx is not None:
+ Z = np.concatenate([self.approx_[i], self.approx_diagonal_[i], self.approx_[j], self.approx_diagonal_[j]], axis=0)
+ U, V = np.sum(np.concatenate([self.approx_[i], self.approx_diagonal_[j]], axis=0), axis=0), np.sum(np.concatenate([self.approx_[j], self.approx_diagonal_[i]], axis=0), axis=0)
+ vectori, vectorj = np.abs(np.matmul(Z, U.T)), np.abs(np.matmul(Z, V.T))
+ vectori_sum, vectorj_sum = np.sum(vectori), np.sum(vectorj)
+ if vectori_sum != 0:
+ vectori = vectori/vectori_sum
+ if vectorj_sum != 0:
+ vectorj = vectorj/vectorj_sum
+ Xfit[i,j] = np.arccos( min(np.dot(np.sqrt(vectori), np.sqrt(vectorj)), 1.) )
+ Xfit[j,i] = Xfit[i,j]
+ else:
+ Z = np.concatenate([self.diagrams_[i], self.diagonal_projections_[i], self.diagrams_[j], self.diagonal_projections_[j]], axis=0)
+ U, V = np.concatenate([self.diagrams_[i], self.diagonal_projections_[j]], axis=0), np.concatenate([self.diagrams_[j], self.diagonal_projections_[i]], axis=0)
+ vectori = np.sum(np.exp(-np.square(pairwise_distances(Z,U))/(2 * np.square(self.bandwidth)))/(self.bandwidth * np.sqrt(2*np.pi)), axis=1)
+ vectorj = np.sum(np.exp(-np.square(pairwise_distances(Z,V))/(2 * np.square(self.bandwidth)))/(self.bandwidth * np.sqrt(2*np.pi)), axis=1)
+ vectori_sum, vectorj_sum = np.sum(vectori), np.sum(vectorj)
+ if vectori_sum != 0:
+ vectori = vectori/vectori_sum
+ if vectorj_sum != 0:
+ vectorj = vectorj/vectorj_sum
+ Xfit[i,j] = np.arccos( min(np.dot(np.sqrt(vectori), np.sqrt(vectorj)), 1.) )
+ Xfit[j,i] = Xfit[i,j]
+ else:
+ projection = (1./2) * np.ones((2,2))
+ diagonal_projections = [np.matmul(X[i], projection) for i in range(len(X))]
+ if self.kernel_approx is not None:
+ approx = [self.kernel_approx.transform(X[i]) for i in range(len(X))]
+ approx_diagonal = [self.kernel_approx.transform(diagonal_projections[i]) for i in range(len(X))]
+ for i in range(len(X)):
+ for j in range(len(self.diagrams_)):
+ if self.kernel_approx is not None:
+ Z = np.concatenate([approx[i], approx_diagonal[i], self.approx_[j], self.approx_diagonal_[j]], axis=0)
+ U, V = np.sum(np.concatenate([approx[i], self.approx_diagonal_[j]], axis=0), axis=0), np.sum(np.concatenate([self.approx_[j], approx_diagonal[i]], axis=0), axis=0)
+ vectori, vectorj = np.abs(np.matmul(Z, U.T)), np.abs(np.matmul(Z, V.T))
+ vectori_sum, vectorj_sum = np.sum(vectori), np.sum(vectorj)
+ if vectori_sum != 0:
+ vectori = vectori/vectori_sum
+ if vectorj_sum != 0:
+ vectorj = vectorj/vectorj_sum
+ Xfit[i,j] = np.arccos( min(np.dot(np.sqrt(vectori), np.sqrt(vectorj)), 1.) )
+ else:
+ Z = np.concatenate([X[i], diagonal_projections[i], self.diagrams_[j], self.diagonal_projections_[j]], axis=0)
+ U, V = np.concatenate([X[i], self.diagonal_projections_[j]], axis=0), np.concatenate([self.diagrams_[j], diagonal_projections[i]], axis=0)
+ vectori = np.sum(np.exp(-np.square(pairwise_distances(Z,U))/(2 * np.square(self.bandwidth)))/(self.bandwidth * np.sqrt(2*np.pi)), axis=1)
+ vectorj = np.sum(np.exp(-np.square(pairwise_distances(Z,V))/(2 * np.square(self.bandwidth)))/(self.bandwidth * np.sqrt(2*np.pi)), axis=1)
+ vectori_sum, vectorj_sum = np.sum(vectori), np.sum(vectorj)
+ if vectori_sum != 0:
+ vectori = vectori/vectori_sum
+ if vectorj_sum != 0:
+ vectorj = vectorj/vectorj_sum
+ Xfit[i,j] = np.arccos( min(np.dot(np.sqrt(vectori), np.sqrt(vectorj)), 1.) )
+ return Xfit
diff --git a/src/python/gudhi/representations/preprocessing.py b/src/python/gudhi/representations/preprocessing.py
new file mode 100644
index 00000000..a39b00e4
--- /dev/null
+++ b/src/python/gudhi/representations/preprocessing.py
@@ -0,0 +1,305 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Mathieu Carrière
+#
+# Copyright (C) 2018-2019 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
+import numpy as np
+from sklearn.base import BaseEstimator, TransformerMixin
+from sklearn.preprocessing import StandardScaler
+
+#############################################
+# Preprocessing #############################
+#############################################
+
+class BirthPersistenceTransform(BaseEstimator, TransformerMixin):
+ """
+ This is a class for the affine transformation (x,y) -> (x,y-x) to be applied on persistence diagrams.
+ """
+ def __init__(self):
+ """
+ Constructor for BirthPersistenceTransform class.
+ """
+ return None
+
+ def fit(self, X, y=None):
+ """
+ Fit the BirthPersistenceTransform class on a list of persistence diagrams (this function actually does nothing but is useful when BirthPersistenceTransform is included in a scikit-learn Pipeline).
+
+ Parameters:
+ X (list of n x 2 numpy array): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ return self
+
+ def transform(self, X):
+ """
+ Apply the BirthPersistenceTransform function on the persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy array): input persistence diagrams.
+
+ Returns:
+ list of n x 2 numpy array: transformed persistence diagrams.
+ """
+ Xfit = []
+ for diag in X:
+ #new_diag = np.empty(diag.shape)
+ #np.copyto(new_diag, diag)
+ new_diag = np.copy(diag)
+ new_diag[:,1] = new_diag[:,1] - new_diag[:,0]
+ Xfit.append(new_diag)
+ return Xfit
+
+class Clamping(BaseEstimator, TransformerMixin):
+ """
+ This is a class for clamping values. It can be used as a parameter for the DiagramScaler class, for instance if you want to clamp abscissae or ordinates of persistence diagrams.
+ """
+ def __init__(self, minimum=-np.inf, maximum=np.inf):
+ """
+ Constructor for the Clamping class.
+
+ Parameters:
+ limit (double): clamping value (default np.inf).
+ """
+ self.minimum = minimum
+ self.maximum = maximum
+
+ def fit(self, X, y=None):
+ """
+ Fit the Clamping class on a list of values (this function actually does nothing but is useful when Clamping is included in a scikit-learn Pipeline).
+
+ Parameters:
+ X (numpy array of size n): input values.
+ y (n x 1 array): value labels (unused).
+ """
+ return self
+
+ def transform(self, X):
+ """
+ Clamp list of values.
+
+ Parameters:
+ X (numpy array of size n): input list of values.
+
+ Returns:
+ numpy array of size n: output list of values.
+ """
+ Xfit = np.clip(X, self.minimum, self.maximum)
+ return Xfit
+
+class DiagramScaler(BaseEstimator, TransformerMixin):
+ """
+ This is a class for preprocessing persistence diagrams with a given list of scalers, such as those included in scikit-learn.
+ """
+ def __init__(self, use=False, scalers=[]):
+ """
+ Constructor for the DiagramScaler class.
+
+ Parameters:
+ use (bool): whether to use the class or not (default False).
+ scalers (list of classes): list of scalers to be fit on the persistence diagrams (default []). Each element of the list is a tuple with two elements: the first one is a list of coordinates, and the second one is a scaler (i.e. a class with fit() and transform() methods) that is going to be applied to these coordinates. Common scalers can be found in the scikit-learn library (such as MinMaxScaler for instance).
+ """
+ self.scalers = scalers
+ self.use = use
+
+ def fit(self, X, y=None):
+ """
+ Fit the DiagramScaler class on a list of persistence diagrams: persistence diagrams are concatenated in a big numpy array, and scalers are fit (by calling their fit() method) on their corresponding coordinates in this big array.
+
+ Parameters:
+ X (list of n x 2 or n x 1 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ if self.use:
+ if len(X) == 1:
+ P = X[0]
+ else:
+ P = np.concatenate(X,0)
+ for (indices, scaler) in self.scalers:
+ scaler.fit(np.reshape(P[:,indices], [-1, 1]))
+ return self
+
+ def transform(self, X):
+ """
+ Apply the DiagramScaler function on the persistence diagrams. The fitted scalers are applied (by calling their transform() method) to their corresponding coordinates in each persistence diagram individually.
+
+ Parameters:
+ X (list of n x 2 or n x 1 numpy arrays): input persistence diagrams.
+
+ Returns:
+ list of n x 2 or n x 1 numpy arrays: transformed persistence diagrams.
+ """
+ Xfit = [np.copy(d) for d in X]
+ if self.use:
+ for i in range(len(Xfit)):
+ if Xfit[i].shape[0] > 0:
+ for (indices, scaler) in self.scalers:
+ for I in indices:
+ Xfit[i][:,I] = np.squeeze(scaler.transform(np.reshape(Xfit[i][:,I], [-1,1])))
+ return Xfit
+
+class Padding(BaseEstimator, TransformerMixin):
+ """
+ This is a class for padding a list of persistence diagrams with dummy points, so that all persistence diagrams end up with the same number of points.
+ """
+ def __init__(self, use=False):
+ """
+ Constructor for the Padding class.
+
+ Parameters:
+ use (bool): whether to use the class or not (default False).
+ """
+ self.use = use
+
+ def fit(self, X, y=None):
+ """
+ Fit the Padding class on a list of persistence diagrams (this function actually does nothing but is useful when Padding is included in a scikit-learn Pipeline).
+
+ Parameters:
+ X (list of n x 2 or n x 1 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ self.max_pts = max([len(diag) for diag in X])
+ return self
+
+ def transform(self, X):
+ """
+ Add dummy points to each persistence diagram so that they all have the same cardinality. All points are given an additional coordinate indicating if the point was added after padding (0) or already present before (1).
+
+ Parameters:
+ X (list of n x 2 or n x 1 numpy arrays): input persistence diagrams.
+
+ Returns:
+ list of n x 3 or n x 2 numpy arrays: padded persistence diagrams.
+ """
+ if self.use:
+ Xfit, num_diag = [], len(X)
+ for diag in X:
+ diag_pad = np.pad(diag, ((0,max(0, self.max_pts - diag.shape[0])), (0,1)), "constant", constant_values=((0,0),(0,0)))
+ diag_pad[:diag.shape[0],2] = np.ones(diag.shape[0])
+ Xfit.append(diag_pad)
+ else:
+ Xfit = X
+ return Xfit
+
+class ProminentPoints(BaseEstimator, TransformerMixin):
+ """
+ This is a class for removing points that are close or far from the diagonal in persistence diagrams. If persistence diagrams are n x 2 numpy arrays (i.e. persistence diagrams with ordinary features), points are ordered and thresholded by distance-to-diagonal. If persistence diagrams are n x 1 numpy arrays (i.e. persistence diagrams with essential features), points are not ordered and thresholded by first coordinate.
+ """
+ def __init__(self, use=False, num_pts=10, threshold=-1, location="upper"):
+ """
+ Constructor for the ProminentPoints class.
+
+ Parameters:
+ use (bool): whether to use the class or not (default False).
+ location (string): either "upper" or "lower" (default "upper"). Whether to keep the points that are far away ("upper") or close ("lower") to the diagonal.
+ num_pts (int): cardinality threshold (default 10). If location == "upper", keep the top **num_pts** points that are the farthest away from the diagonal. If location == "lower", keep the top **num_pts** points that are the closest to the diagonal.
+ threshold (double): distance-to-diagonal threshold (default -1). If location == "upper", keep the points that are at least at a distance **threshold** from the diagonal. If location == "lower", keep the points that are at most at a distance **threshold** from the diagonal.
+ """
+ self.num_pts = num_pts
+ self.threshold = threshold
+ self.use = use
+ self.location = location
+
+ def fit(self, X, y=None):
+ """
+ Fit the ProminentPoints class on a list of persistence diagrams (this function actually does nothing but is useful when ProminentPoints is included in a scikit-learn Pipeline).
+
+ Parameters:
+ X (list of n x 2 or n x 1 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ return self
+
+ def transform(self, X):
+ """
+ If location == "upper", first select the top **num_pts** points that are the farthest away from the diagonal, then select and return from these points the ones that are at least at distance **threshold** from the diagonal for each persistence diagram individually. If location == "lower", first select the top **num_pts** points that are the closest to the diagonal, then select and return from these points the ones that are at most at distance **threshold** from the diagonal for each persistence diagram individually.
+
+ Parameters:
+ X (list of n x 2 or n x 1 numpy arrays): input persistence diagrams.
+
+ Returns:
+ list of n x 2 or n x 1 numpy arrays: thresholded persistence diagrams.
+ """
+ if self.use:
+ Xfit, num_diag = [], len(X)
+ for i in range(num_diag):
+ diag = X[i]
+ if diag.shape[1] >= 2:
+ if diag.shape[0] > 0:
+ pers = np.abs(diag[:,1] - diag[:,0])
+ idx_thresh = pers >= self.threshold
+ thresh_diag, thresh_pers = diag[idx_thresh], pers[idx_thresh]
+ sort_index = np.flip(np.argsort(thresh_pers, axis=None), 0)
+ if self.location == "upper":
+ new_diag = thresh_diag[sort_index[:min(self.num_pts, thresh_diag.shape[0])],:]
+ if self.location == "lower":
+ new_diag = np.concatenate( [ thresh_diag[sort_index[min(self.num_pts, thresh_diag.shape[0]):],:], diag[~idx_thresh] ], axis=0)
+ else:
+ new_diag = diag
+
+ else:
+ if diag.shape[0] > 0:
+ birth = diag[:,:1]
+ idx_thresh = birth >= self.threshold
+ thresh_diag, thresh_birth = diag[idx_thresh], birth[idx_thresh]
+ if self.location == "upper":
+ new_diag = thresh_diag[:min(self.num_pts, thresh_diag.shape[0]),:]
+ if self.location == "lower":
+ new_diag = np.concatenate( [ thresh_diag[min(self.num_pts, thresh_diag.shape[0]):,:], diag[~idx_thresh] ], axis=0)
+ else:
+ new_diag = diag
+
+ Xfit.append(new_diag)
+ else:
+ Xfit = X
+ return Xfit
+
+class DiagramSelector(BaseEstimator, TransformerMixin):
+ """
+ This is a class for extracting finite or essential points in persistence diagrams.
+ """
+ def __init__(self, use=False, limit=np.inf, point_type="finite"):
+ """
+ Constructor for the DiagramSelector class.
+
+ Parameters:
+ use (bool): whether to use the class or not (default False).
+ limit (double): second coordinate value that is the criterion for being an essential point (default numpy.inf).
+ point_type (string): either "finite" or "essential". The type of the points that are going to be extracted.
+ """
+ self.use, self.limit, self.point_type = use, limit, point_type
+
+ def fit(self, X, y=None):
+ """
+ Fit the DiagramSelector class on a list of persistence diagrams (this function actually does nothing but is useful when DiagramSelector is included in a scikit-learn Pipeline).
+
+ Parameters:
+ X (list of n x 2 or n x 1 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ return self
+
+ def transform(self, X):
+ """
+ Extract and return the finite or essential points of each persistence diagram individually.
+
+ Parameters:
+ X (list of n x 2 or n x 1 numpy arrays): input persistence diagrams.
+
+ Returns:
+ list of n x 2 or n x 1 numpy arrays: extracted persistence diagrams.
+ """
+ if self.use:
+ Xfit, num_diag = [], len(X)
+ if self.point_type == "finite":
+ Xfit = [ diag[diag[:,1] < self.limit] if diag.shape[0] != 0 else diag for diag in X]
+ else:
+ Xfit = [ diag[diag[:,1] >= self.limit, 0:1] if diag.shape[0] != 0 else diag for diag in X]
+ else:
+ Xfit = X
+ return Xfit
diff --git a/src/python/gudhi/representations/vector_methods.py b/src/python/gudhi/representations/vector_methods.py
new file mode 100644
index 00000000..fe26dbe2
--- /dev/null
+++ b/src/python/gudhi/representations/vector_methods.py
@@ -0,0 +1,492 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Mathieu Carrière
+#
+# Copyright (C) 2018-2019 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
+import numpy as np
+from sklearn.base import BaseEstimator, TransformerMixin
+from sklearn.preprocessing import MinMaxScaler, MaxAbsScaler
+from sklearn.neighbors import DistanceMetric
+
+from .preprocessing import DiagramScaler, BirthPersistenceTransform
+
+#############################################
+# Finite Vectorization methods ##############
+#############################################
+
+class PersistenceImage(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing persistence images from a list of persistence diagrams. A persistence image is a 2D function computed from a persistence diagram by convolving the diagram points with a weighted Gaussian kernel. The plane is then discretized into an image with pixels, which is flattened and returned as a vector. See http://jmlr.org/papers/v18/16-337.html for more details.
+ """
+ def __init__(self, bandwidth=1., weight=lambda x: 1, resolution=[20,20], im_range=[np.nan, np.nan, np.nan, np.nan]):
+ """
+ Constructor for the PersistenceImage class.
+
+ Parameters:
+ bandwidth (double): bandwidth of the Gaussian kernel (default 1.).
+ weight (function): weight function for the persistence diagram points (default constant function, ie lambda x: 1). This function must be defined on 2D points, ie lists or numpy arrays of the form [p_x,p_y].
+ resolution ([int,int]): size (in pixels) of the persistence image (default [20,20]).
+ im_range ([double,double,double,double]): minimum and maximum of each axis of the persistence image, of the form [x_min, x_max, y_min, y_max] (default [numpy.nan, numpy.nan, numpy.nan, numpyp.nan]). If one of the values is numpy.nan, it can be computed from the persistence diagrams with the fit() method.
+ """
+ self.bandwidth, self.weight = bandwidth, weight
+ self.resolution, self.im_range = resolution, im_range
+
+ def fit(self, X, y=None):
+ """
+ Fit the PersistenceImage class on a list of persistence diagrams: if any of the values in **im_range** is numpy.nan, replace it with the corresponding value computed on the given list of persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ if np.isnan(np.array(self.im_range)).any():
+ new_X = BirthPersistenceTransform().fit_transform(X)
+ pre = DiagramScaler(use=True, scalers=[([0], MinMaxScaler()), ([1], MinMaxScaler())]).fit(new_X,y)
+ [mx,my],[Mx,My] = [pre.scalers[0][1].data_min_[0], pre.scalers[1][1].data_min_[0]], [pre.scalers[0][1].data_max_[0], pre.scalers[1][1].data_max_[0]]
+ self.im_range = np.where(np.isnan(np.array(self.im_range)), np.array([mx, Mx, my, My]), np.array(self.im_range))
+ return self
+
+ def transform(self, X):
+ """
+ Compute the persistence image for each persistence diagram individually and store the results in a single numpy array.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array with shape (number of diagrams) x (number of pixels = **resolution[0]** x **resolution[1]**): output persistence images.
+ """
+ num_diag, Xfit = len(X), []
+ new_X = BirthPersistenceTransform().fit_transform(X)
+
+ for i in range(num_diag):
+
+ diagram, num_pts_in_diag = new_X[i], X[i].shape[0]
+
+ w = np.empty(num_pts_in_diag)
+ for j in range(num_pts_in_diag):
+ w[j] = self.weight(diagram[j,:])
+
+ x_values, y_values = np.linspace(self.im_range[0], self.im_range[1], self.resolution[0]), np.linspace(self.im_range[2], self.im_range[3], self.resolution[1])
+ Xs, Ys = np.tile((diagram[:,0][:,np.newaxis,np.newaxis]-x_values[np.newaxis,np.newaxis,:]),[1,self.resolution[1],1]), np.tile(diagram[:,1][:,np.newaxis,np.newaxis]-y_values[np.newaxis,:,np.newaxis],[1,1,self.resolution[0]])
+ image = np.tensordot(w, np.exp((-np.square(Xs)-np.square(Ys))/(2*np.square(self.bandwidth)))/(np.square(self.bandwidth)*2*np.pi), 1)
+
+ Xfit.append(image.flatten()[np.newaxis,:])
+
+ Xfit = np.concatenate(Xfit,0)
+
+ return Xfit
+
+class Landscape(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing persistence landscapes from a list of persistence diagrams. A persistence landscape is a collection of 1D piecewise-linear functions computed from the rank function associated to the persistence diagram. These piecewise-linear functions are then sampled evenly on a given range and the corresponding vectors of samples are concatenated and returned. See http://jmlr.org/papers/v16/bubenik15a.html for more details.
+ """
+ def __init__(self, num_landscapes=5, resolution=100, sample_range=[np.nan, np.nan]):
+ """
+ Constructor for the Landscape class.
+
+ Parameters:
+ num_landscapes (int): number of piecewise-linear functions to output (default 5).
+ resolution (int): number of sample for all piecewise-linear functions (default 100).
+ sample_range ([double, double]): minimum and maximum of all piecewise-linear function domains, of the form [x_min, x_max] (default [numpy.nan, numpy.nan]). It is the interval on which samples will be drawn evenly. If one of the values is numpy.nan, it can be computed from the persistence diagrams with the fit() method.
+ """
+ self.num_landscapes, self.resolution, self.sample_range = num_landscapes, resolution, sample_range
+ self.nan_in_range = np.isnan(np.array(self.sample_range))
+ self.new_resolution = self.resolution + self.nan_in_range.sum()
+
+ def fit(self, X, y=None):
+ """
+ Fit the Landscape class on a list of persistence diagrams: if any of the values in **sample_range** is numpy.nan, replace it with the corresponding value computed on the given list of persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ if self.nan_in_range.any():
+ pre = DiagramScaler(use=True, scalers=[([0], MinMaxScaler()), ([1], MinMaxScaler())]).fit(X,y)
+ [mx,my],[Mx,My] = [pre.scalers[0][1].data_min_[0], pre.scalers[1][1].data_min_[0]], [pre.scalers[0][1].data_max_[0], pre.scalers[1][1].data_max_[0]]
+ self.sample_range = np.where(self.nan_in_range, np.array([mx, My]), np.array(self.sample_range))
+ return self
+
+ def transform(self, X):
+ """
+ Compute the persistence landscape for each persistence diagram individually and concatenate the results.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array with shape (number of diagrams) x (number of samples = **num_landscapes** x **resolution**): output persistence landscapes.
+ """
+ num_diag, Xfit = len(X), []
+ x_values = np.linspace(self.sample_range[0], self.sample_range[1], self.new_resolution)
+ step_x = x_values[1] - x_values[0]
+
+ for i in range(num_diag):
+
+ diagram, num_pts_in_diag = X[i], X[i].shape[0]
+
+ ls = np.zeros([self.num_landscapes, self.new_resolution])
+
+ events = []
+ for j in range(self.new_resolution):
+ events.append([])
+
+ for j in range(num_pts_in_diag):
+ [px,py] = diagram[j,:2]
+ min_idx = np.clip(np.ceil((px - self.sample_range[0]) / step_x).astype(int), 0, self.new_resolution)
+ mid_idx = np.clip(np.ceil((0.5*(py+px) - self.sample_range[0]) / step_x).astype(int), 0, self.new_resolution)
+ max_idx = np.clip(np.ceil((py - self.sample_range[0]) / step_x).astype(int), 0, self.new_resolution)
+
+ if min_idx < self.new_resolution and max_idx > 0:
+
+ landscape_value = self.sample_range[0] + min_idx * step_x - px
+ for k in range(min_idx, mid_idx):
+ events[k].append(landscape_value)
+ landscape_value += step_x
+
+ landscape_value = py - self.sample_range[0] - mid_idx * step_x
+ for k in range(mid_idx, max_idx):
+ events[k].append(landscape_value)
+ landscape_value -= step_x
+
+ for j in range(self.new_resolution):
+ events[j].sort(reverse=True)
+ for k in range( min(self.num_landscapes, len(events[j])) ):
+ ls[k,j] = events[j][k]
+
+ if self.nan_in_range[0]:
+ ls = ls[:,1:]
+ if self.nan_in_range[1]:
+ ls = ls[:,:-1]
+ ls = np.sqrt(2)*np.reshape(ls,[1,-1])
+ Xfit.append(ls)
+
+ Xfit = np.concatenate(Xfit,0)
+
+ return Xfit
+
+class Silhouette(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing persistence silhouettes from a list of persistence diagrams. A persistence silhouette is computed by taking a weighted average of the collection of 1D piecewise-linear functions given by the persistence landscapes, and then by evenly sampling this average on a given range. Finally, the corresponding vector of samples is returned. See https://arxiv.org/abs/1312.0308 for more details.
+ """
+ def __init__(self, weight=lambda x: 1, resolution=100, sample_range=[np.nan, np.nan]):
+ """
+ Constructor for the Silhouette class.
+
+ Parameters:
+ weight (function): weight function for the persistence diagram points (default constant function, ie lambda x: 1). This function must be defined on 2D points, ie on lists or numpy arrays of the form [p_x,p_y].
+ resolution (int): number of samples for the weighted average (default 100).
+ sample_range ([double, double]): minimum and maximum for the weighted average domain, of the form [x_min, x_max] (default [numpy.nan, numpy.nan]). It is the interval on which samples will be drawn evenly. If one of the values is numpy.nan, it can be computed from the persistence diagrams with the fit() method.
+ """
+ self.weight, self.resolution, self.sample_range = weight, resolution, sample_range
+
+ def fit(self, X, y=None):
+ """
+ Fit the Silhouette class on a list of persistence diagrams: if any of the values in **sample_range** is numpy.nan, replace it with the corresponding value computed on the given list of persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ if np.isnan(np.array(self.sample_range)).any():
+ pre = DiagramScaler(use=True, scalers=[([0], MinMaxScaler()), ([1], MinMaxScaler())]).fit(X,y)
+ [mx,my],[Mx,My] = [pre.scalers[0][1].data_min_[0], pre.scalers[1][1].data_min_[0]], [pre.scalers[0][1].data_max_[0], pre.scalers[1][1].data_max_[0]]
+ self.sample_range = np.where(np.isnan(np.array(self.sample_range)), np.array([mx, My]), np.array(self.sample_range))
+ return self
+
+ def transform(self, X):
+ """
+ Compute the persistence silhouette for each persistence diagram individually and concatenate the results.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array with shape (number of diagrams) x (**resolution**): output persistence silhouettes.
+ """
+ num_diag, Xfit = len(X), []
+ x_values = np.linspace(self.sample_range[0], self.sample_range[1], self.resolution)
+ step_x = x_values[1] - x_values[0]
+
+ for i in range(num_diag):
+
+ diagram, num_pts_in_diag = X[i], X[i].shape[0]
+
+ sh, weights = np.zeros(self.resolution), np.zeros(num_pts_in_diag)
+ for j in range(num_pts_in_diag):
+ weights[j] = self.weight(diagram[j,:])
+ total_weight = np.sum(weights)
+
+ for j in range(num_pts_in_diag):
+
+ [px,py] = diagram[j,:2]
+ weight = weights[j] / total_weight
+ min_idx = np.clip(np.ceil((px - self.sample_range[0]) / step_x).astype(int), 0, self.resolution)
+ mid_idx = np.clip(np.ceil((0.5*(py+px) - self.sample_range[0]) / step_x).astype(int), 0, self.resolution)
+ max_idx = np.clip(np.ceil((py - self.sample_range[0]) / step_x).astype(int), 0, self.resolution)
+
+ if min_idx < self.resolution and max_idx > 0:
+
+ silhouette_value = self.sample_range[0] + min_idx * step_x - px
+ for k in range(min_idx, mid_idx):
+ sh[k] += weight * silhouette_value
+ silhouette_value += step_x
+
+ silhouette_value = py - self.sample_range[0] - mid_idx * step_x
+ for k in range(mid_idx, max_idx):
+ sh[k] += weight * silhouette_value
+ silhouette_value -= step_x
+
+ Xfit.append(np.reshape(np.sqrt(2) * sh, [1,-1]))
+
+ Xfit = np.concatenate(Xfit, 0)
+
+ return Xfit
+
+class BettiCurve(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing Betti curves from a list of persistence diagrams. A Betti curve is a 1D piecewise-constant function obtained from the rank function. It is sampled evenly on a given range and the vector of samples is returned. See https://www.researchgate.net/publication/316604237_Time_Series_Classification_via_Topological_Data_Analysis for more details.
+ """
+ def __init__(self, resolution=100, sample_range=[np.nan, np.nan]):
+ """
+ Constructor for the BettiCurve class.
+
+ Parameters:
+ resolution (int): number of sample for the piecewise-constant function (default 100).
+ sample_range ([double, double]): minimum and maximum of the piecewise-constant function domain, of the form [x_min, x_max] (default [numpy.nan, numpy.nan]). It is the interval on which samples will be drawn evenly. If one of the values is numpy.nan, it can be computed from the persistence diagrams with the fit() method.
+ """
+ self.resolution, self.sample_range = resolution, sample_range
+
+ def fit(self, X, y=None):
+ """
+ Fit the BettiCurve class on a list of persistence diagrams: if any of the values in **sample_range** is numpy.nan, replace it with the corresponding value computed on the given list of persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ if np.isnan(np.array(self.sample_range)).any():
+ pre = DiagramScaler(use=True, scalers=[([0], MinMaxScaler()), ([1], MinMaxScaler())]).fit(X,y)
+ [mx,my],[Mx,My] = [pre.scalers[0][1].data_min_[0], pre.scalers[1][1].data_min_[0]], [pre.scalers[0][1].data_max_[0], pre.scalers[1][1].data_max_[0]]
+ self.sample_range = np.where(np.isnan(np.array(self.sample_range)), np.array([mx, My]), np.array(self.sample_range))
+ return self
+
+ def transform(self, X):
+ """
+ Compute the Betti curve for each persistence diagram individually and concatenate the results.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array with shape (number of diagrams) x (**resolution**): output Betti curves.
+ """
+ num_diag, Xfit = len(X), []
+ x_values = np.linspace(self.sample_range[0], self.sample_range[1], self.resolution)
+ step_x = x_values[1] - x_values[0]
+
+ for i in range(num_diag):
+
+ diagram, num_pts_in_diag = X[i], X[i].shape[0]
+
+ bc = np.zeros(self.resolution)
+ for j in range(num_pts_in_diag):
+ [px,py] = diagram[j,:2]
+ min_idx = np.clip(np.ceil((px - self.sample_range[0]) / step_x).astype(int), 0, self.resolution)
+ max_idx = np.clip(np.ceil((py - self.sample_range[0]) / step_x).astype(int), 0, self.resolution)
+ for k in range(min_idx, max_idx):
+ bc[k] += 1
+
+ Xfit.append(np.reshape(bc,[1,-1]))
+
+ Xfit = np.concatenate(Xfit, 0)
+
+ return Xfit
+
+class Entropy(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing persistence entropy. Persistence entropy is a statistic for persistence diagrams inspired from Shannon entropy. This statistic can also be used to compute a feature vector, called the entropy summary function. See https://arxiv.org/pdf/1803.08304.pdf for more details. Note that a previous implementation was contributed by Manuel Soriano-Trigueros.
+ """
+ def __init__(self, mode="scalar", normalized=True, resolution=100, sample_range=[np.nan, np.nan]):
+ """
+ Constructor for the Entropy class.
+
+ Parameters:
+ mode (string): what entropy to compute: either "scalar" for computing the entropy statistics, or "vector" for computing the entropy summary functions (default "scalar").
+ normalized (bool): whether to normalize the entropy summary function (default True). Used only if **mode** = "vector".
+ resolution (int): number of sample for the entropy summary function (default 100). Used only if **mode** = "vector".
+ sample_range ([double, double]): minimum and maximum of the entropy summary function domain, of the form [x_min, x_max] (default [numpy.nan, numpy.nan]). It is the interval on which samples will be drawn evenly. If one of the values is numpy.nan, it can be computed from the persistence diagrams with the fit() method. Used only if **mode** = "vector".
+ """
+ self.mode, self.normalized, self.resolution, self.sample_range = mode, normalized, resolution, sample_range
+
+ def fit(self, X, y=None):
+ """
+ Fit the Entropy class on a list of persistence diagrams.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ if np.isnan(np.array(self.sample_range)).any():
+ pre = DiagramScaler(use=True, scalers=[([0], MinMaxScaler()), ([1], MinMaxScaler())]).fit(X,y)
+ [mx,my],[Mx,My] = [pre.scalers[0][1].data_min_[0], pre.scalers[1][1].data_min_[0]], [pre.scalers[0][1].data_max_[0], pre.scalers[1][1].data_max_[0]]
+ self.sample_range = np.where(np.isnan(np.array(self.sample_range)), np.array([mx, My]), np.array(self.sample_range))
+ return self
+
+ def transform(self, X):
+ """
+ Compute the entropy for each persistence diagram individually and concatenate the results.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array with shape (number of diagrams) x (1 if **mode** = "scalar" else **resolution**): output entropy.
+ """
+ num_diag, Xfit = len(X), []
+ x_values = np.linspace(self.sample_range[0], self.sample_range[1], self.resolution)
+ step_x = x_values[1] - x_values[0]
+ new_X = BirthPersistenceTransform().fit_transform(X)
+
+ for i in range(num_diag):
+
+ orig_diagram, diagram, num_pts_in_diag = X[i], new_X[i], X[i].shape[0]
+ new_diagram = DiagramScaler(use=True, scalers=[([1], MaxAbsScaler())]).fit_transform([diagram])[0]
+
+ if self.mode == "scalar":
+ ent = - np.sum( np.multiply(new_diagram[:,1], np.log(new_diagram[:,1])) )
+ Xfit.append(np.array([[ent]]))
+
+ else:
+ ent = np.zeros(self.resolution)
+ for j in range(num_pts_in_diag):
+ [px,py] = orig_diagram[j,:2]
+ min_idx = np.clip(np.ceil((px - self.sample_range[0]) / step_x).astype(int), 0, self.resolution)
+ max_idx = np.clip(np.ceil((py - self.sample_range[0]) / step_x).astype(int), 0, self.resolution)
+ for k in range(min_idx, max_idx):
+ ent[k] += (-1) * new_diagram[j,1] * np.log(new_diagram[j,1])
+ if self.normalized:
+ ent = ent / np.linalg.norm(ent, ord=1)
+ Xfit.append(np.reshape(ent,[1,-1]))
+
+ Xfit = np.concatenate(Xfit, 0)
+
+ return Xfit
+
+class TopologicalVector(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing topological vectors from a list of persistence diagrams. The topological vector associated to a persistence diagram is the sorted vector of a slight modification of the pairwise distances between the persistence diagram points. See https://diglib.eg.org/handle/10.1111/cgf12692 for more details.
+ """
+ def __init__(self, threshold=10):
+ """
+ Constructor for the TopologicalVector class.
+
+ Parameters:
+ threshold (int): number of distances to keep (default 10). This is the dimension of the topological vector. If -1, this threshold is computed from the list of persistence diagrams by considering the one with the largest number of points and using the dimension of its corresponding topological vector as threshold.
+ """
+ self.threshold = threshold
+
+ def fit(self, X, y=None):
+ """
+ Fit the TopologicalVector class on a list of persistence diagrams (this function actually does nothing but is useful when TopologicalVector is included in a scikit-learn Pipeline).
+
+ Parameters:
+ X (list of n x 2 or n x 1 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ return self
+
+ def transform(self, X):
+ """
+ Compute the topological vector for each persistence diagram individually and concatenate the results.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array with shape (number of diagrams) x (**threshold**): output topological vectors.
+ """
+ if self.threshold == -1:
+ thresh = np.array([X[i].shape[0] for i in range(len(X))]).max()
+ else:
+ thresh = self.threshold
+
+ num_diag = len(X)
+ Xfit = np.zeros([num_diag, thresh])
+
+ for i in range(num_diag):
+
+ diagram, num_pts_in_diag = X[i], X[i].shape[0]
+ pers = 0.5 * (diagram[:,1]-diagram[:,0])
+ min_pers = np.minimum(pers,np.transpose(pers))
+ distances = DistanceMetric.get_metric("chebyshev").pairwise(diagram)
+ vect = np.flip(np.sort(np.triu(np.minimum(distances, min_pers)), axis=None), 0)
+ dim = min(len(vect), thresh)
+ Xfit[i, :dim] = vect[:dim]
+
+ return Xfit
+
+class ComplexPolynomial(BaseEstimator, TransformerMixin):
+ """
+ This is a class for computing complex polynomials from a list of persistence diagrams. The persistence diagram points are seen as the roots of some complex polynomial, whose coefficients are returned in a complex vector. See https://link.springer.com/chapter/10.1007%2F978-3-319-23231-7_27 for more details.
+ """
+ def __init__(self, polynomial_type="R", threshold=10):
+ """
+ Constructor for the ComplexPolynomial class.
+
+ Parameters:
+ polynomial_type (char): either "R", "S" or "T" (default "R"). Type of complex polynomial that is going to be computed (explained in https://link.springer.com/chapter/10.1007%2F978-3-319-23231-7_27).
+ threshold (int): number of coefficients (default 10). This is the dimension of the complex vector of coefficients, i.e. the number of coefficients corresponding to the largest degree terms of the polynomial. If -1, this threshold is computed from the list of persistence diagrams by considering the one with the largest number of points and using the dimension of its corresponding complex vector of coefficients as threshold.
+ """
+ self.threshold, self.polynomial_type = threshold, polynomial_type
+
+ def fit(self, X, y=None):
+ """
+ Fit the ComplexPolynomial class on a list of persistence diagrams (this function actually does nothing but is useful when ComplexPolynomial is included in a scikit-learn Pipeline).
+
+ Parameters:
+ X (list of n x 2 or n x 1 numpy arrays): input persistence diagrams.
+ y (n x 1 array): persistence diagram labels (unused).
+ """
+ return self
+
+ def transform(self, X):
+ """
+ Compute the complex vector of coefficients for each persistence diagram individually and concatenate the results.
+
+ Parameters:
+ X (list of n x 2 numpy arrays): input persistence diagrams.
+
+ Returns:
+ numpy array with shape (number of diagrams) x (**threshold**): output complex vectors of coefficients.
+ """
+ if self.threshold == -1:
+ thresh = np.array([X[i].shape[0] for i in range(len(X))]).max()
+ else:
+ thresh = self.threshold
+
+ Xfit = np.zeros([len(X), thresh]) + 1j * np.zeros([len(X), thresh])
+ for d in range(len(X)):
+ D, N = X[d], X[d].shape[0]
+ if self.polynomial_type == "R":
+ roots = D[:,0] + 1j * D[:,1]
+ elif self.polynomial_type == "S":
+ alpha = np.linalg.norm(D, axis=1)
+ alpha = np.where(alpha==0, np.ones(N), alpha)
+ roots = np.multiply( np.multiply( (D[:,0]+1j*D[:,1]), (D[:,1]-D[:,0]) ), 1./(np.sqrt(2)*alpha) )
+ elif self.polynomial_type == "T":
+ alpha = np.linalg.norm(D, axis=1)
+ roots = np.multiply( (D[:,1]-D[:,0])/2, np.cos(alpha) - np.sin(alpha) + 1j * (np.cos(alpha) + np.sin(alpha)) )
+ coeff = [0] * (N+1)
+ coeff[N] = 1
+ for i in range(1, N+1):
+ for j in range(N-i-1, N):
+ coeff[j] += ((-1) * roots[i-1] * coeff[j+1])
+ coeff = np.array(coeff[::-1])[1:]
+ Xfit[d, :min(thresh, coeff.shape[0])] = coeff[:min(thresh, coeff.shape[0])]
+ return Xfit
diff --git a/src/python/gudhi/rips_complex.pyx b/src/python/gudhi/rips_complex.pyx
index f2cd6a8d..722cdcdc 100644
--- a/src/python/gudhi/rips_complex.pyx
+++ b/src/python/gudhi/rips_complex.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
@@ -8,16 +17,6 @@ from libc.stdint cimport intptr_t
from gudhi.simplex_tree cimport *
from gudhi.simplex_tree import SimplexTree
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "MIT"
diff --git a/src/python/gudhi/simplex_tree.pxd b/src/python/gudhi/simplex_tree.pxd
index 5f86cfe2..1066d44b 100644
--- a/src/python/gudhi/simplex_tree.pxd
+++ b/src/python/gudhi/simplex_tree.pxd
@@ -1,19 +1,18 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
from libcpp cimport bool
from libcpp.string cimport string
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "MIT"
diff --git a/src/python/gudhi/simplex_tree.pyx b/src/python/gudhi/simplex_tree.pyx
index 9f490271..b18627c4 100644
--- a/src/python/gudhi/simplex_tree.pyx
+++ b/src/python/gudhi/simplex_tree.pyx
@@ -1,17 +1,16 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from libc.stdint cimport intptr_t
from numpy import array as np_array
cimport simplex_tree
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "MIT"
@@ -75,13 +74,22 @@ cdef class SimplexTree:
return self.get_ptr().simplex_filtration(simplex)
def assign_filtration(self, simplex, filtration):
- """This function assigns the simplicial complex filtration value for a
+ """This function assigns a new filtration value to a
given N-simplex.
:param simplex: The N-simplex, represented by a list of vertex.
:type simplex: list of int.
- :param filtration: The simplicial complex filtration value.
+ :param filtration: The new filtration value.
:type filtration: float
+
+ .. note::
+ Beware that after this operation, the structure may not be a valid
+ filtration anymore, a simplex could have a lower filtration value
+ than one of its faces. Callers are responsible for fixing this
+ (with more :meth:`assign_filtration` or
+ :meth:`make_filtration_non_decreasing` for instance) before calling
+ any function that relies on the filtration property, like
+ :meth:`initialize_filtration`.
"""
self.get_ptr().assign_simplex_filtration(simplex, filtration)
@@ -362,7 +370,7 @@ cdef class SimplexTree:
value than its faces by increasing the filtration values.
:returns: True if any filtration value was modified,
- False if the filtration was already non-decreasing.
+ False if the filtration was already non-decreasing.
:rtype: bool
@@ -500,7 +508,7 @@ cdef class SimplexTree:
"""
if self.pcohptr != NULL:
if persistence_file != '':
- self.pcohptr.write_output_diagram(str.encode(persistence_file))
+ self.pcohptr.write_output_diagram(persistence_file.encode('utf-8'))
else:
print("persistence_file must be specified")
else:
diff --git a/src/python/gudhi/strong_witness_complex.pyx b/src/python/gudhi/strong_witness_complex.pyx
index e757abea..2c33c3f2 100644
--- a/src/python/gudhi/strong_witness_complex.pyx
+++ b/src/python/gudhi/strong_witness_complex.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
@@ -6,16 +15,6 @@ from libc.stdint cimport intptr_t
from gudhi.simplex_tree cimport *
from gudhi.simplex_tree import SimplexTree
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "MIT"
@@ -69,7 +68,7 @@ cdef class StrongWitnessComplex:
"""
stree = SimplexTree()
cdef intptr_t stree_int_ptr=stree.thisptr
- if limit_dimension is not -1:
+ if limit_dimension != -1:
self.thisptr.create_simplex_tree(<Simplex_tree_interface_full_featured*>stree_int_ptr,
max_alpha_square, limit_dimension)
else:
diff --git a/src/python/gudhi/subsampling.pyx b/src/python/gudhi/subsampling.pyx
index 1135c1fb..c501d16b 100644
--- a/src/python/gudhi/subsampling.pyx
+++ b/src/python/gudhi/subsampling.pyx
@@ -1,19 +1,18 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.string cimport string
from libcpp cimport bool
import os
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "GPL v3"
@@ -44,19 +43,19 @@ def choose_n_farthest_points(points=None, off_file='', nb_points=0, starting_poi
:param nb_points: Number of points of the subsample.
:type nb_points: unsigned.
:param starting_point: The iteration starts with the landmark `starting \
- point`,which is the index of the poit to start with. If not set, this \
- index is choosen randomly.
+ point`,which is the index of the point to start with. If not set, this \
+ index is chosen randomly.
:type starting_point: unsigned.
:returns: The subsample point set.
:rtype: vector[vector[double]]
"""
- if off_file is not '':
+ if off_file:
if os.path.isfile(off_file):
- if starting_point is '':
- return subsampling_n_farthest_points_from_file(str.encode(off_file),
+ if starting_point == '':
+ return subsampling_n_farthest_points_from_file(off_file.encode('utf-8'),
nb_points)
else:
- return subsampling_n_farthest_points_from_file(str.encode(off_file),
+ return subsampling_n_farthest_points_from_file(off_file.encode('utf-8'),
nb_points,
starting_point)
else:
@@ -65,7 +64,7 @@ def choose_n_farthest_points(points=None, off_file='', nb_points=0, starting_poi
if points is None:
# Empty points
points=[]
- if starting_point is '':
+ if starting_point == '':
return subsampling_n_farthest_points(points, nb_points)
else:
return subsampling_n_farthest_points(points, nb_points,
@@ -87,9 +86,9 @@ def pick_n_random_points(points=None, off_file='', nb_points=0):
:returns: The subsample point set.
:rtype: vector[vector[double]]
"""
- if off_file is not '':
+ if off_file:
if os.path.isfile(off_file):
- return subsampling_n_random_points_from_file(str.encode(off_file),
+ return subsampling_n_random_points_from_file(off_file.encode('utf-8'),
nb_points)
else:
print("file " + off_file + " not found.")
@@ -117,9 +116,9 @@ def sparsify_point_set(points=None, off_file='', min_squared_dist=0.0):
:returns: The subsample point set.
:rtype: vector[vector[double]]
"""
- if off_file is not '':
+ if off_file:
if os.path.isfile(off_file):
- return subsampling_sparsify_points_from_file(str.encode(off_file),
+ return subsampling_sparsify_points_from_file(off_file.encode('utf-8'),
min_squared_dist)
else:
print("file " + off_file + " not found.")
diff --git a/src/python/gudhi/tangential_complex.pyx b/src/python/gudhi/tangential_complex.pyx
index 3a945fe2..6391488c 100644
--- a/src/python/gudhi/tangential_complex.pyx
+++ b/src/python/gudhi/tangential_complex.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
@@ -9,16 +18,6 @@ import os
from gudhi.simplex_tree cimport *
from gudhi.simplex_tree import SimplexTree
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "GPL v3"
@@ -65,9 +64,9 @@ cdef class TangentialComplex:
# The real cython constructor
def __cinit__(self, intrisic_dim, points=None, off_file=''):
- if off_file is not '':
+ if off_file:
if os.path.isfile(off_file):
- self.thisptr = new Tangential_complex_interface(intrisic_dim, str.encode(off_file), True)
+ self.thisptr = new Tangential_complex_interface(intrisic_dim, off_file.encode('utf-8'), True)
else:
print("file " + off_file + " not found.")
else:
@@ -92,7 +91,7 @@ cdef class TangentialComplex:
Raises:
ValueError: In debug mode, if the computed star dimension is too
low. Try to set a bigger maximal edge length value with
- :func:`~gudhi.Tangential_complex.set_max_squared_edge_length`
+ :meth:`set_max_squared_edge_length`
if this happens.
"""
self.thisptr.compute_tangential_complex()
@@ -167,7 +166,7 @@ cdef class TangentialComplex:
:type max_squared_edge_length: double
If the maximal edge length value is too low
- :func:`~gudhi.Tangential_complex.compute_tangential_complex`
+ :meth:`compute_tangential_complex`
will throw an exception in debug mode.
"""
self.thisptr.set_max_squared_edge_length(max_squared_edge_length)
diff --git a/src/python/gudhi/wasserstein.py b/src/python/gudhi/wasserstein.py
new file mode 100644
index 00000000..db5ddff2
--- /dev/null
+++ b/src/python/gudhi/wasserstein.py
@@ -0,0 +1,97 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Theo Lacombe
+#
+# Copyright (C) 2019 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
+import numpy as np
+import scipy.spatial.distance as sc
+try:
+ import ot
+except ImportError:
+ print("POT (Python Optimal Transport) package is not installed. Try to run $ conda install -c conda-forge pot ; or $ pip install POT")
+
+def _proj_on_diag(X):
+ '''
+ :param X: (n x 2) array encoding the points of a persistent diagram.
+ :returns: (n x 2) array encoding the (respective orthogonal) projections of the points onto the diagonal
+ '''
+ Z = (X[:,0] + X[:,1]) / 2.
+ return np.array([Z , Z]).T
+
+
+def _build_dist_matrix(X, Y, order=2., internal_p=2.):
+ '''
+ :param X: (n x 2) numpy.array encoding the (points of the) first diagram.
+ :param Y: (m x 2) numpy.array encoding the second diagram.
+ :param internal_p: Ground metric (i.e. norm l_p).
+ :param order: exponent for the Wasserstein metric.
+ :returns: (n+1) x (m+1) np.array encoding the cost matrix C.
+ For 1 <= i <= n, 1 <= j <= m, C[i,j] encodes the distance between X[i] and Y[j], while C[i, m+1] (resp. C[n+1, j]) encodes the distance (to the p) between X[i] (resp Y[j]) and its orthogonal proj onto the diagonal.
+ note also that C[n+1, m+1] = 0 (it costs nothing to move from the diagonal to the diagonal).
+ '''
+ Xdiag = _proj_on_diag(X)
+ Ydiag = _proj_on_diag(Y)
+ if np.isinf(internal_p):
+ C = sc.cdist(X,Y, metric='chebyshev')**order
+ Cxd = np.linalg.norm(X - Xdiag, ord=internal_p, axis=1)**order
+ Cdy = np.linalg.norm(Y - Ydiag, ord=internal_p, axis=1)**order
+ else:
+ C = sc.cdist(X,Y, metric='minkowski', p=internal_p)**order
+ Cxd = np.linalg.norm(X - Xdiag, ord=internal_p, axis=1)**order
+ Cdy = np.linalg.norm(Y - Ydiag, ord=internal_p, axis=1)**order
+ Cf = np.hstack((C, Cxd[:,None]))
+ Cdy = np.append(Cdy, 0)
+
+ Cf = np.vstack((Cf, Cdy[None,:]))
+
+ return Cf
+
+
+def _perstot(X, order, internal_p):
+ '''
+ :param X: (n x 2) numpy.array (points of a given diagram).
+ :param internal_p: Ground metric on the (upper-half) plane (i.e. norm l_p in R^2); Default value is 2 (Euclidean norm).
+ :param order: exponent for Wasserstein. Default value is 2.
+ :returns: float, the total persistence of the diagram (that is, its distance to the empty diagram).
+ '''
+ Xdiag = _proj_on_diag(X)
+ return (np.sum(np.linalg.norm(X - Xdiag, ord=internal_p, axis=1)**order))**(1./order)
+
+
+def wasserstein_distance(X, Y, order=2., internal_p=2.):
+ '''
+ :param X: (n x 2) numpy.array encoding the (finite points of the) first diagram. Must not contain essential points (i.e. with infinite coordinate).
+ :param Y: (m x 2) numpy.array encoding the second diagram.
+ :param internal_p: Ground metric on the (upper-half) plane (i.e. norm l_p in R^2); Default value is 2 (euclidean norm).
+ :param order: exponent for Wasserstein; Default value is 2.
+ :returns: the Wasserstein distance of order q (1 <= q < infinity) between persistence diagrams with respect to the internal_p-norm as ground metric.
+ :rtype: float
+ '''
+ n = len(X)
+ m = len(Y)
+
+ # handle empty diagrams
+ if X.size == 0:
+ if Y.size == 0:
+ return 0.
+ else:
+ return _perstot(Y, order, internal_p)
+ elif Y.size == 0:
+ return _perstot(X, order, internal_p)
+
+ M = _build_dist_matrix(X, Y, order=order, internal_p=internal_p)
+ a = np.full(n+1, 1. / (n + m) ) # weight vector of the input diagram. Uniform here.
+ a[-1] = a[-1] * m # normalized so that we have a probability measure, required by POT
+ b = np.full(m+1, 1. / (n + m) ) # weight vector of the input diagram. Uniform here.
+ b[-1] = b[-1] * n # so that we have a probability measure, required by POT
+
+ # Comptuation of the otcost using the ot.emd2 library.
+ # Note: it is the Wasserstein distance to the power q.
+ # The default numItermax=100000 is not sufficient for some examples with 5000 points, what is a good value?
+ ot_cost = (n+m) * ot.emd2(a, b, M, numItermax=2000000)
+
+ return ot_cost ** (1./order)
diff --git a/src/python/gudhi/witness_complex.pyx b/src/python/gudhi/witness_complex.pyx
index baa70b7a..b032a5a1 100644
--- a/src/python/gudhi/witness_complex.pyx
+++ b/src/python/gudhi/witness_complex.pyx
@@ -1,3 +1,12 @@
+# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+# Author(s): Vincent Rouvreau
+#
+# Copyright (C) 2016 Inria
+#
+# Modification(s):
+# - YYYY/MM Author: Description of the modification
+
from cython cimport numeric
from libcpp.vector cimport vector
from libcpp.utility cimport pair
@@ -6,16 +15,6 @@ from libc.stdint cimport intptr_t
from gudhi.simplex_tree cimport *
from gudhi.simplex_tree import SimplexTree
-""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
- See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
- Author(s): Vincent Rouvreau
-
- Copyright (C) 2016 Inria
-
- Modification(s):
- - YYYY/MM Author: Description of the modification
-"""
-
__author__ = "Vincent Rouvreau"
__copyright__ = "Copyright (C) 2016 Inria"
__license__ = "MIT"
@@ -69,7 +68,7 @@ cdef class WitnessComplex:
"""
stree = SimplexTree()
cdef intptr_t stree_int_ptr=stree.thisptr
- if limit_dimension is not -1:
+ if limit_dimension != -1:
self.thisptr.create_simplex_tree(<Simplex_tree_interface_full_featured*>stree_int_ptr,
max_alpha_square, limit_dimension)
else: