diff options
author | Hind-M <hind.montassif@gmail.com> | 2021-10-08 11:18:46 +0200 |
---|---|---|
committer | Hind-M <hind.montassif@gmail.com> | 2021-10-08 11:18:46 +0200 |
commit | f461f050ee8bad509814b4851ab7ae8f43502962 (patch) | |
tree | c806b23606d006e898b025cb8821efdc1f16d897 /src/python/gudhi | |
parent | dbdc62a494e54c3dd409a2e80fa169560355ce19 (diff) |
Add warnings in dtm.py for DistanceToMeasure and DTMDensity
Add DTMDensity warning test
Diffstat (limited to 'src/python/gudhi')
-rw-r--r-- | src/python/gudhi/point_cloud/dtm.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/python/gudhi/point_cloud/dtm.py b/src/python/gudhi/point_cloud/dtm.py index 55ac58e6..96a9e7bf 100644 --- a/src/python/gudhi/point_cloud/dtm.py +++ b/src/python/gudhi/point_cloud/dtm.py @@ -9,6 +9,7 @@ from .knn import KNearestNeighbors import numpy as np +import warnings __author__ = "Marc Glisse" __copyright__ = "Copyright (C) 2020 Inria" @@ -66,6 +67,11 @@ class DistanceToMeasure: distances = distances ** self.q dtm = distances.sum(-1) / self.k dtm = dtm ** (1.0 / self.q) + with warnings.catch_warnings(): + import torch + if isinstance(dtm, torch.Tensor): + if not(torch.isfinite(dtm).all()): + warnings.warn("Overflow/infinite value encountered while computing 'dtm'", RuntimeWarning) # We compute too many powers, 1/p in knn then q in dtm, 1/q in dtm then q or some log in the caller. # Add option to skip the final root? return dtm @@ -163,6 +169,11 @@ class DTMDensity: distances = self.knn.transform(X) distances = distances ** q dtm = (distances * weights).sum(-1) + with warnings.catch_warnings(): + import torch + if isinstance(dtm, torch.Tensor): + if not(torch.isfinite(dtm).all()): + warnings.warn("Overflow/infinite value encountered while computing 'dtm' for density", RuntimeWarning) if self.normalize: dtm /= (np.arange(1, k + 1) ** (q / dim) * weights).sum() density = dtm ** (-dim / q) |