From eaefe49f3d00ae7e085f9328c8e0037ead06d6a2 Mon Sep 17 00:00:00 2001 From: Christoph Boeddeker Date: Tue, 18 Jun 2024 12:56:54 +0200 Subject: [PATCH] fix doctests for numpy 2 --- padertorch/data/segment.py | 7 +++++-- padertorch/ops/losses/regression.py | 4 ++-- padertorch/utils.py | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/padertorch/data/segment.py b/padertorch/data/segment.py index 9b778b00..8fccb159 100644 --- a/padertorch/data/segment.py +++ b/padertorch/data/segment.py @@ -346,7 +346,7 @@ def _get_rand_int(rng, *args, **kwargs): def get_anchor( num_samples: int, length: int, shift: int = None, - mode: str = 'left', rng=np.random + mode: str = 'left', rng: 'np.random.RandomState' = np.random ) -> int: """ Calculates anchor for the boundaries for segmentation of a signal @@ -372,6 +372,9 @@ def get_anchor( random_max_segments: Randomly chooses the anchor such that the maximum number of segments are created rng: random number generator (`numpy.random`) + e.g. `np.random.RandomState(0)` or `np.random.default_rng(0)`. + Not, that `np.random.default_rng` differs from `np.random` and has + sometimes other return types (e.g. python `int` vs `np.int64`) Returns: integer value describing the anchor @@ -387,7 +390,7 @@ def get_anchor( 1 >>> get_anchor(24, 10, 3, mode='random') 10 - >>> get_anchor(24, 10, 3, mode='random', rng=np.random.default_rng(seed=4)) + >>> get_anchor(24, 10, 3, mode='random', rng=np.random.RandomState(seed=4)) 10 >>> get_anchor(24, 10, 3, mode='random_max_segments') 3 diff --git a/padertorch/ops/losses/regression.py b/padertorch/ops/losses/regression.py index 79eb75b7..83c0cb45 100644 --- a/padertorch/ops/losses/regression.py +++ b/padertorch/ops/losses/regression.py @@ -220,7 +220,7 @@ def si_sdr_loss(estimate, target, reduction='mean', offset_invariant=False, ... print('Numpy metric:', si_sdr(estimate.numpy(), target.numpy())) Perfect estimation - >>> si_sdr(reference.numpy(), reference.numpy()) + >>> print(si_sdr(reference.numpy(), reference.numpy())) inf >>> sdr_loss(reference, reference) tensor(-inf, dtype=torch.float64) @@ -228,7 +228,7 @@ def si_sdr_loss(estimate, target, reduction='mean', offset_invariant=False, tensor(True) >>> si_sdr_loss(reference.to(torch.float32), reference.to(torch.float32)) < -130 # Torch CPU is not hardware independent tensor(True) - >>> si_sdr(reference.numpy(), (reference * 2).numpy()) + >>> print(si_sdr(reference.numpy(), (reference * 2).numpy())) inf >>> si_sdr_loss(reference, reference * 2) < -300 # Torch CPU is not hardware independent tensor(True) diff --git a/padertorch/utils.py b/padertorch/utils.py index 40bb165c..ac594ff4 100644 --- a/padertorch/utils.py +++ b/padertorch/utils.py @@ -32,14 +32,16 @@ def to_list(x, length=None): Complicated corner cases are e.g. `range()` and `dict.values()`, which are handled here. + >>> from paderbox.utils.pretty import pprint + >>> to_list(1) [1] >>> to_list([1]) [1] >>> to_list((i for i in range(3))) [0, 1, 2] - >>> to_list(np.arange(3)) - [0, 1, 2] + >>> pprint(to_list(np.arange(3)), nep51=True) # use pprint to support numpy 1 and 2 + [np.int64(0), np.int64(1), np.int64(2)] >>> to_list({'a': 1}) [{'a': 1}] >>> to_list({'a': 1}.keys())