persim.PersLandscapeApprox¶
- class persim.PersLandscapeApprox(start: float = None, stop: float = None, num_steps: int = 500, dgms: list = [], hom_deg: int = 0, values=array([], dtype=float64), compute: bool = True)[source]¶
Persistence Landscape Approximate class.
This class implements an approximate version of Persistence Landscape, given by sampling the landscape functions on a grid. This version is only an approximation to the true landscape, but given a fine enough grid, this should suffice for most applications. If an exact calculation with no approximation is desired, consider PersLandscapeExact. Computations with this class are much faster compared to PersLandscapeExact in general.
The optional parameters start, stop, num_steps determine the approximating grid that the values are sampled on. If both dgms and values are passed but start and stop are not, start and stop will be determined by dgms.
- Parameters:
start (float, optional) – The start parameter of the approximating grid.
stop (float, optional) – The stop parameter of the approximating grid.
num_steps (int, optional) – The number of dimensions of the approximation, equivalently the number of steps in the grid.
dgms (list of (-,2) numpy.ndarrays, optional) – Nested list of numpy arrays, e.g., [array( array([:]), array([:]) ),…, array([:])]. Each entry in the list corresponds to a single homological degree. Each array represents the birth-death pairs in that homological degree. This is precisely the output format from ripser.py: ripser(data_user)[‘dgms’].
hom_deg (int) – Represents the homology degree of the persistence diagram.
values (numpy.ndarray, optional) – The approximated values of the landscapes, indexed by depth.
compute (bool, optional) – Flag determining whether landscape functions are computed upon instantiation.
Examples
Define a persistence diagram and instantiate the landscape:
>>> from persim import PersLandscapeApprox >>> import numpy as np >>> pd = [ np.array([[0,3],[1,4]]), np.array([[1,4]]) ] >>> pla = PersLandscapeApprox(dgms=pd, hom_deg=0); pla Approximate persistence landscape in homological degree 0 on grid from 0 to 4 with 500 steps
The start and stop parameters will be determined to be as tight as possible from dgms if they are not passed. They can be passed directly if desired:
>>> wide_pl = PersLandscapeApprox(dgms=pd, hom_deg=0, start=-1, stop=3.1415, num_steps=1000); wide_pl Approximate persistence landscape in homological degree 0 on grid from -1 to 3.1415 with 1000 steps
The approximating values are stored in the values attribute:
>>> wide_pl.values array([[0. , 0. , 0. , ..., 0.00829129, 0.00414565, 0. ], [0. , 0. , 0. , ..., 0. , 0. , 0. ]])
Arithmetic methods are implemented for approximate landscapes, so they can be summed, subtracted, and admit scalar multiplication.
Note
Landscapes must have the same grid parameters (start, stop, and num_steps) before any arithmetic is involved. See the snap_PL function for a method that will snap a list of landscapes onto a common grid.
>>> pla - wide_pl
ValueError: Start values of grids do not coincide
>>> from persim import snap_pl >>> [snapped_pla, snapped_wide_pl] = snap_pl([pla,wide_pl]) >>> print(snapped_pla, snapped_wide_pl)
Approximate persistence landscape in homological degree 0 on grid from -1 to 4 with 1000 steps Approximate persistence landscape in homological degree 0 on grid from -1 to 4 with 1000 steps
>>> sum_pl = snapped_pla + snapped_wide_pl; sum_pl.values array([[0. , 0. , 0. , ..., 0.01001001, 0.00500501, 0. ], [0. , 0. , 0. , ..., 0. , 0. , 0. ]])
Approximate landscapes are sliced by depth and slicing returns the approximated values in those depths:
>>> sum_pl[0] array([0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ..., 1.50150150e-02, 1.00100100e-02, 5.00500501e-03, 0.00000000e+00])
Norms are implemented for all p including the supremum norm:
>>> sum_pl.p_norm(p=5) 12.44665414332285 >>> sum_pl.sup_norm() 2.9958943943943943
- __init__(start: float = None, stop: float = None, num_steps: int = 500, dgms: list = [], hom_deg: int = 0, values=array([], dtype=float64), compute: bool = True) None [source]¶
Methods
__init__
([start, stop, num_steps, dgms, ...])compute_landscape
([verbose])Computes the approximate landscape values
p_norm
([p])Returns the L_{p} norm of an approximate persistence landscape
sup_norm
()Returns the supremum norm of an approximate persistence landscape
values_to_pairs
()Converts function values to ordered pairs and returns them