Skip to content

Commit 521779a

Browse files
authored
Merge pull request #158 from lucasimi/feature/default-overlap-frac
Feature/default overlap frac
2 parents 35efd43 + 838a1d6 commit 521779a

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/tdamapper/_common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def warn_deprecated(deprecated, substitute):
1313
)
1414

1515

16+
def warn_user(msg):
17+
warnings.warn(msg, UserWarning, stacklevel=2)
18+
19+
1620
class ParamsMixin:
1721
"""
1822
Mixin to add setters and getters for public parameters, compatible with

src/tdamapper/cover.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
Indeed, the overlaps of the open subsets define the edges of the Mapper graph.
77
"""
88

9+
import math
910
import numpy as np
1011

1112
from tdamapper.core import Proximity
1213
from tdamapper.utils.metrics import get_metric, chebyshev
1314
from tdamapper.utils.vptree import VPTree
1415

16+
from tdamapper._common import warn_user
17+
1518

1619
class _Pullback:
1720

@@ -245,7 +248,9 @@ class CubicalCover(Proximity):
245248
Defaults to 1.
246249
:type n_intervals: int
247250
:param overlap_frac: The fraction of overlap between adjacent intervals on
248-
each dimension, must be in the range (0.0, 1.0). Defaults to 0.5.
251+
each dimension, must be in the range (0.0, 0.5]. If not specified, the
252+
overlap_frac is computed such that the volume of the overlap within
253+
each hypercube is half the total volume. Defaults to None.
249254
:type overlap_frac: float
250255
:param metric: The metric used to define the distance between points.
251256
Accepts any value compatible with `tdamapper.utils.metrics.get_metric`.
@@ -273,7 +278,7 @@ class CubicalCover(Proximity):
273278
def __init__(
274279
self,
275280
n_intervals=1,
276-
overlap_frac=0.5,
281+
overlap_frac=None,
277282
kind='flat',
278283
leaf_capacity=1,
279284
leaf_radius=None,
@@ -311,6 +316,10 @@ def _get_bounds(self, data):
311316
def _convert(self, X):
312317
return np.asarray(X).reshape(len(X), -1).astype(float)
313318

319+
def _get_overlap_frac(self, dim, overlap_vol_frac):
320+
beta = math.pow(1.0 - overlap_vol_frac, 1.0 / dim)
321+
return 1.0 - 1.0 / (2.0 - beta)
322+
314323
def fit(self, X):
315324
"""
316325
Train internal parameters.
@@ -323,7 +332,14 @@ def fit(self, X):
323332
:return: The object itself.
324333
:rtype: self
325334
"""
326-
self.__overlap_frac = self.overlap_frac
335+
X = np.asarray(X)
336+
if self.overlap_frac is None:
337+
dim = 1 if X.ndim == 1 else X.shape[1]
338+
self.__overlap_frac = self._get_overlap_frac(dim, 0.5)
339+
else:
340+
self.__overlap_frac = self.overlap_frac
341+
if (self.__overlap_frac <= 0.0) or (self.__overlap_frac > 0.5):
342+
warn_user('The parameter overlap_frac is expected within range (0.0, 0.5]')
327343
self.__n_intervals = self.n_intervals
328344
self.__radius = 1.0 / (2.0 - 2.0 * self.__overlap_frac)
329345
XX = self._convert(X)

0 commit comments

Comments
 (0)