diff options
author | Marc Glisse <marc.glisse@inria.fr> | 2020-07-31 11:50:11 +0200 |
---|---|---|
committer | Marc Glisse <marc.glisse@inria.fr> | 2020-07-31 11:50:11 +0200 |
commit | 2830010c74cc74d29691faeeb7bb3a31cc53d87d (patch) | |
tree | a766681e82447a8258a439a22084bc71c77b2f69 | |
parent | 31acee0a124afc628b906349e468a9aa9fac4a2a (diff) |
static construction from array
-rw-r--r-- | src/python/gudhi/simplex_tree.pyx | 16 | ||||
-rwxr-xr-x | src/python/test/test_simplex_tree.py | 5 |
2 files changed, 12 insertions, 9 deletions
diff --git a/src/python/gudhi/simplex_tree.pyx b/src/python/gudhi/simplex_tree.pyx index 1df2420e..42f1e635 100644 --- a/src/python/gudhi/simplex_tree.pyx +++ b/src/python/gudhi/simplex_tree.pyx @@ -195,10 +195,11 @@ cdef class SimplexTree: """ return self.get_ptr().insert(simplex, <double>filtration) - def insert_edges_from_array(self, filtrations, double max_filtration=numpy.inf): - """Inserts edges in an empty complex. The vertices are numbered from 0 to n-1, and the filtration values are - encoded in the array, with the diagonal representing the vertices. It is the caller's responsibility to - ensure that this defines a filtration, which can be achieved with either:: + @staticmethod + def create_from_array(filtrations, double max_filtration=numpy.inf): + """Creates a new, empty complex and inserts vertices and edges. The vertices are numbered from 0 to n-1, and + the filtration values are encoded in the array, with the diagonal representing the vertices. It is the + caller's responsibility to ensure that this defines a filtration, which can be achieved with either:: filtrations[np.diag_indices_from(filtrations)] = filtrations.min(1) @@ -211,15 +212,18 @@ cdef class SimplexTree: :type filtrations: numpy.ndarray of shape (n,n) :param max_filtration: only insert vertices and edges with filtration values no larger than max_filtration :type max_filtration: float + :returns: the new complex + :rtype: SimplexTree """ # TODO: document which half of the matrix is actually read? filtrations = numpy.asanyarray(filtrations, dtype=float) cdef double[:,:] F = filtrations - assert self.num_vertices() == 0, "insert_edges_from_array requires an empty SimplexTree" + ret = SimplexTree() cdef int n = F.shape[0] assert n == F.shape[1] with nogil: - self.get_ptr().insert_matrix(&F[0,0], n, F.strides[0], F.strides[1], max_filtration) + ret.get_ptr().insert_matrix(&F[0,0], n, F.strides[0], F.strides[1], max_filtration) + return ret def insert_edges_from_coo_matrix(self, edges): """Inserts edges given by a sparse matrix in `COOrdinate format diff --git a/src/python/test/test_simplex_tree.py b/src/python/test/test_simplex_tree.py index c6c5dc0e..34173e78 100755 --- a/src/python/test/test_simplex_tree.py +++ b/src/python/test/test_simplex_tree.py @@ -345,10 +345,9 @@ def test_simplices_iterator(): assert st.filtration(simplex[0]) == simplex[1] -def test_insert_edges_from_array(): - st = SimplexTree() +def test_create_from_array(): a = np.array([[1, 4, 13, 6], [4, 3, 11, 5], [13, 11, 10, 12], [6, 5, 12, 2]]) - st.insert_edges_from_array(a, max_filtration=5) + st = SimplexTree.create_from_array(a, max_filtration=5) assert list(st.get_filtration()) == [([0], 1.0), ([3], 2.0), ([1], 3.0), ([0, 1], 4.0), ([1, 3], 5.0)] |