Skip to content

Commit 27d7f00

Browse files
committed
put back original make_unique function
1 parent 9043e8c commit 27d7f00

2 files changed

Lines changed: 19 additions & 48 deletions

File tree

findpeaks/stats.py

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -711,51 +711,21 @@ def disable_tqdm():
711711
"""Set the logger for verbosity messages."""
712712
return (True if (logger.getEffectiveLevel()>=30) else False)
713713

714-
# def _make_unique(arr: np.ndarray):
715-
# """Method iterates through elements of the input array to ensure all values are unique.
716-
# Duplicate values are reduced by the smallest possible increment for the given data type.
717-
# """
718-
# logger.debug('Making values unique')
719-
# res = np.empty_like(arr)
720-
# it = np.nditer([arr, res], [], [['readonly'], ['writeonly', 'allocate']])
721-
# seen = set()
722-
# with it:
723-
# while not it.finished:
724-
# a = it[0].item()
725-
# while a in seen and np.isfinite(a):
726-
# a = np.nextafter(a, -np.inf)
727-
# it[1] = a
728-
# if a not in seen:
729-
# seen.add(a)
730-
# it.iternext()
731-
# return res
732-
733-
def _make_unique(arr: np.ndarray) -> np.ndarray:
734-
"""Return array where duplicate finite values are slightly perturbed to make all values unique."""
735-
# Convert to float64 if needed
736-
if not np.issubdtype(arr.dtype, np.floating):
737-
arr = arr.astype(np.float64)
738-
dtype = arr.dtype
739-
740-
arr_flat = arr.ravel()
741-
res = arr_flat.copy()
742-
is_finite = np.isfinite(res)
743-
finite_vals = res[is_finite]
744-
unique, counts = np.unique(finite_vals, return_counts=True)
745-
dupes = unique[counts > 1]
746-
if dupes.size == 0:
747-
return arr.copy()
748-
749-
seen = {}
750-
mask = np.isin(finite_vals, dupes)
751-
indices = np.where(mask)[0]
752-
values = finite_vals[mask]
753-
754-
for i, val in tqdm(zip(indices, values), total=len(values), disable=disable_tqdm(), desc=logger.info("Making values unique")):
755-
count = seen.get(val, 0)
756-
perturbed = np.nextafter(val, -np.inf, dtype=dtype) - count * np.finfo(dtype).eps
757-
finite_vals[i] = perturbed
758-
seen[val] = count + 1
759-
760-
res[is_finite] = finite_vals
761-
return res.reshape(arr.shape)
714+
def _make_unique(arr: np.ndarray):
715+
"""Method iterates through elements of the input array to ensure all values are unique.
716+
Duplicate values are reduced by the smallest possible increment for the given data type.
717+
"""
718+
logger.debug('Making values unique')
719+
res = np.empty_like(arr)
720+
it = np.nditer([arr, res], [], [['readonly'], ['writeonly', 'allocate']])
721+
seen = set()
722+
with it:
723+
while not it.finished:
724+
a = it[0].item()
725+
while a in seen and np.isfinite(a):
726+
a = np.nextafter(a, -np.inf)
727+
it[1] = a
728+
if a not in seen:
729+
seen.add(a)
730+
it.iternext()
731+
return res

findpeaks/tests/test_findpeaks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_fit(self):
2626
assert len(results['Xranked'][results['Xranked'] != 0]) > 18
2727

2828
def test_topology_limit(self):
29+
from findpeaks import findpeaks
2930
# CHECK RESULTS METHOD with LIMIT functionality
3031
fp = findpeaks(method="topology", whitelist=['peak'], limit=0)
3132
X = fp.import_example('2dpeaks')

0 commit comments

Comments
 (0)