Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New format for persistence #925

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Persistent_cohomology/include/gudhi/Persistent_cohomology.h
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,20 @@ class Persistent_cohomology {
return result;
}

/** @brief Returns persistence intervals for each dimension.
* @return A vector of diagrams, one per dimension starting from 0, where each diagram is a vector of persistence intervals (birth and death).
*/
std::vector<std::vector<std::pair<Filtration_value, Filtration_value>>>
intervals_by_dimension() {
std::vector<std::vector<std::pair<Filtration_value, Filtration_value>>> result;
result.resize(dim_max_);
for (auto && pair : persistent_pairs_) {
auto b = get<0>(pair);
result[cpx_->dimension(b)].emplace_back(cpx_->filtration(b), cpx_->filtration(get<1>(pair)));
}
return result;
}

private:
/*
* Structure representing a cocycle.
Expand Down
1 change: 1 addition & 0 deletions src/python/gudhi/simplex_tree.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ cdef extern from "Persistent_cohomology_interface.h" namespace "Gudhi":
vector[int] betti_numbers() nogil
vector[int] persistent_betti_numbers(double from_value, double to_value) nogil
vector[pair[double,double]] intervals_in_dimension(int dimension) nogil
vector[vector[pair[double,double]]] intervals_by_dimension() nogil
void write_output_diagram(string diagram_file_name) nogil except +
vector[pair[vector[int], vector[int]]] persistence_pairs() nogil
pair[vector[vector[int]], vector[vector[int]]] lower_star_generators() nogil
Expand Down
11 changes: 9 additions & 2 deletions src/python/gudhi/simplex_tree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ cdef class SimplexTree:
"""
self.get_ptr().expansion_with_blockers_callback(max_dim, callback, <void*>blocker_func)

def persistence(self, homology_coeff_field=11, min_persistence=0, persistence_dim_max = False):
def persistence(self, homology_coeff_field=11, min_persistence=0, persistence_dim_max = False, output_type = 'old'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-- Not completely related to this PR, but I think we should do type annotations here, i.e.,

from typing import Optional,Literal, Union
def persistence(self, homology_coeff_field:int=11, min_persistence:float=0., persistence_dim_max:Optional[int] = None, output_type:Literal['old','array by dimension'] = 'old') -> list:
    pass

especially to have auto-completion for long arguments such as "array by dimension".
I also changed the False to a None.

"""This function computes and returns the persistence of the simplicial complex.

:param homology_coeff_field: The homology coefficient field. Must be a
Expand All @@ -627,11 +627,18 @@ cdef class SimplexTree:
maximal dimension in the complex is computed. If false, it is
ignored. Default is false.
:type persistence_dim_max: bool
:param output_type: Format of the output. 'old' for the legacy list of (dim,(birth,death)),
'array by dimension' for a list of nx2 numpy arrays (one per dimension).
:type output_type: str
:returns: The persistence of the simplicial complex.
:rtype: list of pairs(dimension, pair(birth, death))
"""
self.compute_persistence(homology_coeff_field, min_persistence, persistence_dim_max)
return self.pcohptr.get_persistence()
if output_type == 'array by dimension':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"array by dimension" seems a bit long to me, is "arrays" ok ? and 'old' isn't very telling : "tuples" ?
Also, should we raise a DeprecationWarning to change the default to the array one afterward ?

v = self.pcohptr.intervals_by_dimension()
return [ np.asarray(dgm) for dgm in v ] # std::move(dgm)?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably do the usual workaround for empty dgm to get a shape (0,2).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tuples are slightly faster than lists in python, but that's negligible here.

else:
return self.pcohptr.get_persistence()

def compute_persistence(self, homology_coeff_field=11, min_persistence=0, persistence_dim_max = False):
"""This function computes the persistence of the simplicial complex, so it can be accessed through
Expand Down