summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Rouvreau <10407034+VincentRouvreau@users.noreply.github.com>2020-01-07 17:22:16 +0100
committerGitHub <noreply@github.com>2020-01-07 17:22:16 +0100
commitad4c4d8e3c40b278c05833672c78a5e1cf169284 (patch)
tree52e4f4cb94f386e03a847f2d6086efb5666c44f5
parent1a9c2e7e25751ec2399bf12d086d7e2113c02faf (diff)
parent6a7a7b6d6d81946dd903f68f525389ab2fd51f71 (diff)
Merge pull request #178 from VincentRouvreau/numpy_array_input_for_cubical
Fix #158 - Add numpy ndarray to init a cubical and a periodic one
-rw-r--r--src/python/doc/cubical_complex_user.rst3
-rw-r--r--src/python/gudhi/cubical_complex.pyx26
-rw-r--r--src/python/gudhi/periodic_cubical_complex.pyx40
-rwxr-xr-xsrc/python/test/test_cubical_complex.py61
4 files changed, 110 insertions, 20 deletions
diff --git a/src/python/doc/cubical_complex_user.rst b/src/python/doc/cubical_complex_user.rst
index b13b500e..56cf0170 100644
--- a/src/python/doc/cubical_complex_user.rst
+++ b/src/python/doc/cubical_complex_user.rst
@@ -142,8 +142,7 @@ Or it can be defined as follows:
.. testcode::
from gudhi import PeriodicCubicalComplex as pcc
- periodic_cc = pcc(dimensions=[3,3],
- top_dimensional_cells= [0, 0, 0, 0, 1, 0, 0, 0, 0],
+ periodic_cc = pcc(top_dimensional_cells = [[0, 0, 0], [0, 1, 0], [0, 0, 0]],
periodic_dimensions=[True, False])
result_str = 'Periodic cubical complex is of dimension ' + repr(periodic_cc.dimension()) + ' - ' + \
repr(periodic_cc.num_simplices()) + ' simplices.'
diff --git a/src/python/gudhi/cubical_complex.pyx b/src/python/gudhi/cubical_complex.pyx
index 011c407c..b7047d4f 100644
--- a/src/python/gudhi/cubical_complex.pyx
+++ b/src/python/gudhi/cubical_complex.pyx
@@ -5,7 +5,7 @@ from libcpp.string cimport string
from libcpp cimport bool
import os
-from numpy import array as np_array
+import numpy as np
# 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.
@@ -47,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.
@@ -58,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
"""
@@ -65,9 +71,19 @@ 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 == ''):
+ 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 != ''):
+ 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))
else:
@@ -184,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/periodic_cubical_complex.pyx b/src/python/gudhi/periodic_cubical_complex.pyx
index c89055db..eb96ab5f 100644
--- a/src/python/gudhi/periodic_cubical_complex.pyx
+++ b/src/python/gudhi/periodic_cubical_complex.pyx
@@ -5,7 +5,7 @@ from libcpp.string cimport string
from libcpp cimport bool
import os
-from numpy import array as np_array
+import numpy as np
# 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.
@@ -47,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.
@@ -60,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
"""
@@ -67,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 == ''):
- 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 ((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))
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:
@@ -186,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/test/test_cubical_complex.py b/src/python/test/test_cubical_complex.py
index 68f54fbe..121da12a 100755
--- a/src/python/test/test_cubical_complex.py
+++ b/src/python/test/test_cubical_complex.py
@@ -1,4 +1,5 @@
-from gudhi import CubicalComplex
+from gudhi import CubicalComplex, PeriodicCubicalComplex
+import numpy as np
""" 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.
@@ -56,7 +57,7 @@ def test_dimension_or_perseus_file_constructor():
assert cub.__is_persistence_defined() == False
-def test_dimension_simple_constructor():
+def simple_constructor(cub):
cub = CubicalComplex(
dimensions=[3, 3], top_dimensional_cells=[1, 2, 3, 4, 5, 6, 7, 8, 9]
)
@@ -67,12 +68,22 @@ def test_dimension_simple_constructor():
assert cub.betti_numbers() == [1, 0, 0]
assert cub.persistent_betti_numbers(0, 1000) == [0, 0, 0]
-
-def test_user_case_simple_constructor():
+def test_simple_constructor_from_top_cells():
cub = CubicalComplex(
dimensions=[3, 3],
- top_dimensional_cells=[float("inf"), 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
+ top_dimensional_cells=[1, 2, 3, 4, 5, 6, 7, 8, 9],
+ )
+ simple_constructor(cub)
+
+def test_simple_constructor_from_numpy_array():
+ cub = CubicalComplex(
+ top_dimensional_cells=np.array([[1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9]])
)
+ simple_constructor(cub)
+
+def user_case_simple_constructor(cub):
assert cub.__is_defined() == True
assert cub.__is_persistence_defined() == False
assert cub.persistence() == [(1, (0.0, 1.0)), (0, (0.0, float("inf")))]
@@ -83,6 +94,20 @@ def test_user_case_simple_constructor():
)
assert other_cub.persistence() == [(1, (0.0, 1.0)), (0, (0.0, float("inf")))]
+def test_user_case_simple_constructor_from_top_cells():
+ cub = CubicalComplex(
+ dimensions=[3, 3],
+ top_dimensional_cells=[float("inf"), 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
+ )
+ user_case_simple_constructor(cub)
+
+def test_user_case_simple_constructor_from_numpy_array():
+ cub = CubicalComplex(
+ top_dimensional_cells=np.array([[float("inf"), 0.0, 0.0],
+ [0.0, 1.0, 0.0],
+ [0.0, 0.0, 0.0]])
+ )
+ user_case_simple_constructor(cub)
def test_dimension_file_constructor():
# Create test file
@@ -96,3 +121,29 @@ def test_dimension_file_constructor():
assert cub.__is_persistence_defined() == True
assert cub.betti_numbers() == [1, 0, 0]
assert cub.persistent_betti_numbers(0, 1000) == [1, 0, 0]
+
+def test_connected_sublevel_sets():
+ array_cells = np.array([[3, 3], [2, 2], [4, 4]])
+ linear_cells = [3, 3, 2, 2, 4, 4]
+ dimensions = [2, 3]
+ periodic_dimensions = [False, False]
+ # with a numpy array version
+ cub = CubicalComplex(top_dimensional_cells = array_cells)
+ assert cub.persistence() == [(0, (2.0, float("inf")))]
+ assert cub.betti_numbers() == [1, 0, 0]
+ # with vector of dimensions
+ cub = CubicalComplex(dimensions = dimensions,
+ top_dimensional_cells = linear_cells)
+ assert cub.persistence() == [(0, (2.0, float("inf")))]
+ assert cub.betti_numbers() == [1, 0, 0]
+ # periodic with a numpy array version
+ cub = PeriodicCubicalComplex(top_dimensional_cells = array_cells,
+ periodic_dimensions = periodic_dimensions)
+ assert cub.persistence() == [(0, (2.0, float("inf")))]
+ assert cub.betti_numbers() == [1, 0, 0]
+ # periodic with vector of dimensions
+ cub = PeriodicCubicalComplex(dimensions = dimensions,
+ top_dimensional_cells = linear_cells,
+ periodic_dimensions = periodic_dimensions)
+ assert cub.persistence() == [(0, (2.0, float("inf")))]
+ assert cub.betti_numbers() == [1, 0, 0]