From ecacfb8dd42e44c49d844de89b9be699249de5ed Mon Sep 17 00:00:00 2001 From: andreped Date: Mon, 8 Aug 2022 19:22:03 +0200 Subject: [PATCH 1/2] fixed tf percentile test - off by one --- tests/test_tf.py | 3 +-- tests/test_torch.py | 1 - torchstain/tf/utils/percentile.py | 4 ++-- torchstain/torch/utils/percentile.py | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/test_tf.py b/tests/test_tf.py index 075093d..b38ef74 100644 --- a/tests/test_tf.py +++ b/tests/test_tf.py @@ -18,11 +18,10 @@ def test_percentile(): x = np.random.randn(10, 10) p = 20 p_np = np.percentile(x, p, interpolation='nearest') - p_t = torchstain.tf.utils.percentile(x, p) + p_t = torchstain.tf.utils.percentile(tf.convert_to_tensor(x), p) np.testing.assert_almost_equal(p_np, p_t) - def test_normalize_tf(): size = 1024 curr_file_path = os.path.dirname(os.path.realpath(__file__)) diff --git a/tests/test_torch.py b/tests/test_torch.py index 0d79ed1..168d2f1 100644 --- a/tests/test_torch.py +++ b/tests/test_torch.py @@ -8,7 +8,6 @@ from torchvision import transforms from skimage.metrics import structural_similarity as ssim - def test_cov(): x = np.random.randn(10, 10) cov_np = np.cov(x) diff --git a/torchstain/tf/utils/percentile.py b/torchstain/tf/utils/percentile.py index 2db4aeb..de8c6ac 100644 --- a/torchstain/tf/utils/percentile.py +++ b/torchstain/tf/utils/percentile.py @@ -14,5 +14,5 @@ def percentile(t: tf.Tensor, q: float) -> Union[int, float]: :param q: Percentile to compute, which must be between 0 and 100 inclusive. :return: Resulting value (scalar). """ - k = 1 + tf.math.round(.01 * tf.cast(q, tf.float32) * (tf.cast(tf.size(t), tf.float32) - 1)) - return tf.sort(tf.reshape(t, [-1]))[tf.cast(k, tf.int32)] + k = 1 + tf.math.round(.01 * tf.cast(q, tf.float32) * (tf.cast(tf.math.reduce_prod(tf.size(t)), tf.float32) - 1)) + return tf.sort(tf.reshape(t, [-1]))[tf.cast(k - 1, tf.int32)] diff --git a/torchstain/torch/utils/percentile.py b/torchstain/torch/utils/percentile.py index 188f1bb..08273d4 100644 --- a/torchstain/torch/utils/percentile.py +++ b/torchstain/torch/utils/percentile.py @@ -6,7 +6,7 @@ """ def percentile(t: torch.tensor, q: float) -> Union[int, float]: """ - Return the ``q``-th percentile of the flattened input tensor's data. + Return the ``q``-th percentile of the flattenepip d input tensor's data. CAUTION: * Needs PyTorch >= 1.1.0, as ``torch.kthvalue()`` is used. From 89a84075f818a51570329403a54474ec5f47f724 Mon Sep 17 00:00:00 2001 From: andreped Date: Mon, 8 Aug 2022 19:24:31 +0200 Subject: [PATCH 2/2] removed redundant tensor conversion --- tests/test_tf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_tf.py b/tests/test_tf.py index b38ef74..b24757a 100644 --- a/tests/test_tf.py +++ b/tests/test_tf.py @@ -18,7 +18,7 @@ def test_percentile(): x = np.random.randn(10, 10) p = 20 p_np = np.percentile(x, p, interpolation='nearest') - p_t = torchstain.tf.utils.percentile(tf.convert_to_tensor(x), p) + p_t = torchstain.tf.utils.percentile(x, p) np.testing.assert_almost_equal(p_np, p_t)