Skip to content

Commit

Permalink
Raise a warning or error on invalid overlap_frac in Onedcover (#315)
Browse files Browse the repository at this point in the history
* Add warnings if the overlap fraction is close to 0. and raise  an error if it is 0.

* Correct the tests, so as only 'reasonable' values are used
  • Loading branch information
wreise committed Feb 20, 2020
1 parent b543bf8 commit 1152d55
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion gtda/mapper/cover.py
Expand Up @@ -3,6 +3,7 @@

from functools import partial
from itertools import product
import warnings

import numpy as np
from scipy.stats import rankdata
Expand Down Expand Up @@ -84,13 +85,22 @@ class OneDimensionalCover(BaseEstimator, TransformerMixin):

_hyperparameters = {'kind': [str, ['uniform', 'balanced']],
'n_intervals': [int, (1, np.inf)],
'overlap_frac': [float, (0, 1)]}
'overlap_frac': [float, (0., 1.)]}

def __init__(self, kind='uniform', n_intervals=10, overlap_frac=0.1):
self.kind = kind
self.n_intervals = n_intervals
self.overlap_frac = overlap_frac

if overlap_frac == 0.:
raise ValueError("`overlap_frac` must be positive,"
"as otherwise the intervals will not cover"
"the range")
if overlap_frac <= 1e-8:
warnings.warn("`overlap_frac` is close to zero,"
"which might cause numerical issues and errors",
RuntimeWarning)

def _fit_uniform(self, X):
self.left_limits_, self.right_limits_ = self._find_interval_limits(
X, self.n_intervals, self.overlap_frac, is_uniform=True)
Expand Down
2 changes: 1 addition & 1 deletion gtda/mapper/tests/test_cover.py
Expand Up @@ -36,7 +36,7 @@ def get_nb_intervals(draw):
def get_overlap_fraction(draw):
overlap = draw(floats(allow_nan=False,
allow_infinity=False,
min_value=0., exclude_min=True,
min_value=1e-8, exclude_min=True,
max_value=1., exclude_max=True),
)
return overlap
Expand Down

0 comments on commit 1152d55

Please sign in to comment.