Skip to content

Commit

Permalink
add a verbose flag and prints out own constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Saul committed Apr 21, 2018
1 parent 14f21c5 commit 69f4bac
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions ripser/ripser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,19 @@ class Rips(BaseEstimator):
"""



def __init__(self, maxdim=1, thresh=-1, coeff=2):
def __init__(self, maxdim=1, thresh=-1, coeff=2, verbose=True):
self.maxdim = maxdim
self.thresh = thresh
self.coeff = coeff
self.verbose = verbose

self.dgm_ = None
self.distance_matrix_ = None # indicator
self.metric_ = None

if self.verbose:
print("Rips(maxdim={}, thres={}, coef={}, verbose={})".format(
maxdim, thresh, coeff, verbose))

def transform(self, X, distance_matrix=False, metric='euclidean'):
"""Compute persistence diagrams for X data array.
Expand All @@ -64,31 +70,47 @@ def transform(self, X, distance_matrix=False, metric='euclidean'):
----------
X: ndarray (n_samples, n_features)
A numpy array of either data or distance matrix.
distance_matrix: bool
Indicator that X is a distance matrix, if not we compute a
distance matrix from X using the chosen metric.
metric: string or callable
The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options specified in PAIRED_DISTANCES, including “euclidean”, “manhattan”, or “cosine”. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them.
"""


# Default is to input point cloud data
if not distance_matrix:
X = pairwise_distances(X, metric=metric)

dgm = self._compute_rips(X)
self.dgm_ = dgm

return dgm

def fit_transform(self, X, distance_matrix=False, metric='euclidean'):
""" Run transform and return results
"""Compute persistence diagrams for X data array and return the diagrams.
Parameters
----------
X: ndarray (n_samples, n_features)
A numpy array of either data or distance matrix.
distance_matrix: bool
Indicator that X is a distance matrix, if not we compute a
distance matrix from X using the chosen metric.
metric: string or callable
The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options specified in PAIRED_DISTANCES, including “euclidean”, “manhattan”, or “cosine”. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them.
Return
------
dgms: list (size maxdim) of ndarray (n_pairs, 2)
A list of persistence diagrams, one for each dimension less than maxdim. Each diagram is an ndarray of size (n_pairs, 2) with the first column representing the birth time and the second column representing the death time of each pair.
"""
self.transform(X, distance_matrix, metric)
return self.dgm_


def _compute_rips(self, dm):
""" Compute the persistence diagram
Expand Down

0 comments on commit 69f4bac

Please sign in to comment.