From a97349385407f453e2368af7f0d2dece0a161d5a Mon Sep 17 00:00:00 2001
From: "Olivier J.N. Bertrand" <olivier.bertrand@uni-bielefeld.de>
Date: Sat, 12 Oct 2019 22:06:55 +0200
Subject: [PATCH] Create tests for patterns

---
 navipy/arenatools/patterns.py     | 16 +++++------
 navipy/arenatools/test_pattern.py | 45 +++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 8 deletions(-)
 create mode 100644 navipy/arenatools/test_pattern.py

diff --git a/navipy/arenatools/patterns.py b/navipy/arenatools/patterns.py
index 10cc990..44f551d 100644
--- a/navipy/arenatools/patterns.py
+++ b/navipy/arenatools/patterns.py
@@ -32,7 +32,7 @@ the noise (default: random generated noise)
         noise = np.random.randn(s, s)
 
     # calculate filter
-    fx = np.arange(s/2)+1
+    fx = np.arange(s / 2) + 1
     # index from 0-s/2,s/2-0 -> middle highest index
     fx = np.hstack([fx, fx[-1::-1]])
     fx = fx[:, np.newaxis]
@@ -45,7 +45,7 @@ the noise (default: random generated noise)
     f = f**(-beta)
     # apply filter in frequency domain
     # calculate disrete furier transformations
-    fnoise = np.fft.ifft2(np.fft.fft2(noise)*f)
+    fnoise = np.fft.ifft2(np.fft.fft2(noise) * f)
 
     # trim to image size
     fnoise = fnoise[:img_size[0], :img_size[1]]
@@ -65,8 +65,8 @@ def gray2red(img):
     img = img[..., np.newaxis]
     img = img.repeat(3, axis=2)
     maxval = np.max(img)
-    img[:, :, 1] = maxval-img[:, :, 0]
-    img[:, :, 2] = maxval-img[:, :, 0]
+    img[:, :, 1] = maxval - img[:, :, 0]
+    img[:, :, 2] = maxval - img[:, :, 0]
     img[:, :, 0] = maxval
     return img
 
@@ -83,16 +83,16 @@ def norm_img(img):
     return img.astype(np.uint8)
 
 
-def rectangular_pattern(width, length, beta=1.4, pixel_per_mm=1):
+def rectangular_pattern(width, height, beta=1.4, pixel_per_mm=1):
     """generate a rectangular pattern
 
     :param width: width of the pattern in mm
-    :param length: length of the pattern in mm
+    :param height: height of the pattern in mm
     :param beta: beta coef for generating a 1/(f^beta) pattern
     :param pixel_per_mm: number of pixel per mm
     :returns: a rectangular random image
     :rtype: np.ndarray
     """
-    corridor = np.array([width, length])
-    corridor_px = corridor*pixel_per_mm  # in px
+    corridor = np.array([height, width])
+    corridor_px = corridor * pixel_per_mm  # in px
     return generate_1overf_noise(corridor_px, beta)
diff --git a/navipy/arenatools/test_pattern.py b/navipy/arenatools/test_pattern.py
new file mode 100644
index 0000000..ab6176e
--- /dev/null
+++ b/navipy/arenatools/test_pattern.py
@@ -0,0 +1,45 @@
+import unittest
+import numpy as np
+import navipy.arenatools.patterns as tobetested
+# import matplotlib.pyplot as plt
+
+
+class TestCase(unittest.TestCase):
+    def setUp(self):
+        self.width = 201
+        self.height = 100
+        self.beta = 2
+        self.pixel_per_mm = 2
+        self.image = tobetested.rectangular_pattern(self.width,
+                                                    self.height,
+                                                    self.beta,
+                                                    self.pixel_per_mm)
+
+    def test_rectangular_patter(self):
+        """Chech the image dimension"""
+        # Check image dimension
+        # * height
+        # * width
+        self.assertEqual(self.image.shape[0],
+                         self.height * self.pixel_per_mm)
+        self.assertEqual(self.image.shape[1],
+                         self.width * self.pixel_per_mm)
+
+    def test_norm_img(self):
+        """Check that normalised image span [0,255]"""
+        im = np.random.rand(100, 101)
+        im = tobetested.norm_img(im)
+        self.assertEqual(im.min(), 0)
+        self.assertEqual(im.max(), 255)
+
+    def test_gray2red(self):
+        """Check that green=blue, red == max"""
+        red = tobetested.gray2red(self.image.copy())
+        # The red channel should be equal to max
+        np.testing.assert_array_equal(red[..., 0], self.image.max())
+        # Green==Blue
+        np.testing.assert_array_equal(red[..., 1], red[..., 2])
+
+
+if __name__ == '__main__':
+    unittest.main()
-- 
GitLab