From 0e7f13ff6f87eb8c0e73a23eb0cfd1a6089fc60e Mon Sep 17 00:00:00 2001
From: "lodenthal@uni-bielefeld.de" <lodenthal@uni-bielefeld.de>
Date: Fri, 6 Apr 2018 10:34:05 +0300
Subject: [PATCH] test range for viewing direction in pcv

---
 navipy/processing/pcode.py |   6 ++
 navipy/processing/test.py  | 164 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 166 insertions(+), 4 deletions(-)

diff --git a/navipy/processing/pcode.py b/navipy/processing/pcode.py
index 8ed0ead..07c10a2 100644
--- a/navipy/processing/pcode.py
+++ b/navipy/processing/pcode.py
@@ -133,6 +133,12 @@ def pcv(place_code, viewing_directions):
                        should be 1'.format(place_code.shape[component_dim]))
     elevation = viewing_directions[..., __spherical_indeces__['elevation']]
     azimuth = viewing_directions[..., __spherical_indeces__['azimuth']]
+    # if (np.any(elevation < -np.pi/2) or np.any(elevation > np.pi/2)):
+    if (np.any(elevation < -2*np.pi) or np.any(elevation > 2*np.pi)):
+        raise ValueError(" Elevation must be radians in range [-2*pi;2*pi]")
+    # if (np.any(azimuth < 0) or np.any(azimuth > np.pi*2)):
+    if (np.any(azimuth < -2*np.pi) or np.any(azimuth > np.pi*2)):
+        raise ValueError(" Azimuth must be radians in range [-2*pi;2*pi]")
     x, y, z = spherical_to_cartesian(elevation, azimuth, radius=1)
     unscaled_lv = np.zeros((viewing_directions.shape[0],
                             viewing_directions.shape[1],
diff --git a/navipy/processing/test.py b/navipy/processing/test.py
index ff13c23..9286e9a 100644
--- a/navipy/processing/test.py
+++ b/navipy/processing/test.py
@@ -15,6 +15,19 @@ class TestCase(unittest.TestCase):
         self.mydb = database.DataBaseLoad(self.mydb_filename)
 
     def test_scene_posorient(self):
+        """
+        this test checks that the correct errors are raised if
+        wrong values for the input parameters are passed to the
+        function scene of the navipy.database module
+        it also contains some test where correct parameter values
+        were passed to the scene function and the output was
+        checked for correctness.
+        test cases:
+        missing entries in the posorient pd.series
+        None, NaN values in the posorient pd.series
+        posorient is of wrong type (dictionary instead of pd.series)
+        empty posorient pd.series
+        """
         conn = sqlite3.connect(self.mydb_filename)
         c = conn.cursor()
         c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """)
@@ -89,6 +102,18 @@ class TestCase(unittest.TestCase):
             image = self.mydb.scene(posorient=posorient2)
 
     def test_skyline_scene(self):
+        """
+        this test checks that the correct errors are raised if
+        wrong values for the input parameters are passed to the
+        function skyline_scene of the navipy.database module
+        it also contains some test where correct parameter values
+        were passed to the scene function and the output was
+        checked for correctness.
+        test cases:
+        None, NaN values in the the scene
+        scene is of wrong type (np.array)
+        scene is of wrong size
+        """
         scene = self.mydb.scene(rowid=1)
         scene2 = scene.copy()
         scene2[3, 5, 2, 0] = np.nan
@@ -97,11 +122,13 @@ class TestCase(unittest.TestCase):
         scene3 = np.array(scene3)
         scene4 = np.zeros((3, 4, 5, 0))
 
-        # put useless stuff here
+        # contains nan
         with self.assertRaises(ValueError):
             pcode.skyline(scene2)
+        # np.array instead of
         with self.assertRaises(TypeError):
             pcode.skyline(scene3)
+        # wrong size
         with self.assertRaises(Exception):
             pcode.skyline(scene4)
 
@@ -118,6 +145,20 @@ class TestCase(unittest.TestCase):
             self.assertTrue(skyline.shape[1] > 0)
 
     def test_id(self):
+        """
+        this test checks that the correct errors are raised if
+        wrong values for the input parameters id of the
+        function scene of the navipy.database module
+        it also contains some test where correct parameter values
+        were passed to the scene function and the output was
+        checked for correctness.
+        test cases:
+        zero and negative id
+        char for the id
+        None for the id
+        NaN for the id
+        float for the id
+        """
         for rowid in [0, -2]:
             with self.assertRaises(ValueError):
                 # print("rowid",rowid)
@@ -131,6 +172,7 @@ class TestCase(unittest.TestCase):
         with self.assertRaises(TypeError):
             self.mydb.scene(rowid=4.5)
 
+        # working cases
         for rowid in [1, 2, 3, 4, 5]:
             image = self.mydb.scene(rowid=rowid)
             # image=np.array(image)
@@ -144,6 +186,17 @@ class TestCase(unittest.TestCase):
             self.assertTrue(image.shape[1] > 0)
 
     def test_distance_channel(self):
+        """
+        this test checks that the correct errors are raised if
+        wrong values for the input parameters distance_channel is passed to the
+        function scene of the navipy.database module
+        it also contains some test where correct parameter values
+        were passed to the scene function and the output was
+        checked for correctness.
+        test cases:
+        None, NaN, float, char values in the for the distance channel
+        negative int for the distance channel
+        """
         scene = self.mydb.scene(rowid=1)
 
         # should not be working
@@ -166,6 +219,18 @@ class TestCase(unittest.TestCase):
         self.assertEqual(weighted_scene.shape, scene.shape)
 
     def test_contr_weight_scene(self):
+        """
+        this test checks that the correct errors are raised if
+        wrong values for the input parameter scene is passed to the
+        function contr_weight_scene of the navipy.database module
+        it also contains some test where correct parameter values
+        were passed to the scene function and the output was
+        checked for correctness.
+        test cases:
+        None, NaN values in the the scene
+        scene is of wrong type (np.array)
+        scene is of wrong size
+        """
         scene = self.mydb.scene(rowid=1)
 
         # working cases
@@ -194,6 +259,18 @@ class TestCase(unittest.TestCase):
             contrast = pcode.contrast_weighted_nearness(scene4)
 
     def test_contr_weight_contrast(self):
+        """
+        this test checks that the correct errors are raised if
+        wrong values for the input parameter contrast_size are passed to the
+        function skyline_scene of the navipy.database module.
+        correct values are in the range between 2 and 5.
+        it also contains some test where correct parameter values
+        were passed to the scene function and the output was
+        checked for correctness.
+        test cases:
+        None, NaN values, chars, floats for the contrast_size
+        int values that are out of range (<2;>5)
+        """
         scene = self.mydb.scene(rowid=1)
         for size in [9.4, 'g', None, np.nan]:
             with self.assertRaises(TypeError):
@@ -219,10 +296,26 @@ class TestCase(unittest.TestCase):
             self.assertEqual(contrast.shape[1], scene.shape[1])
 
     def test_pcv(self):
+        """
+        this test checks that the correct errors are raised if
+        wrong values for the input parameter direction is passed to the
+        function pcv of the navipy.database module.
+        correct values are in the range between 2 and 5.
+        it also contains some test where correct parameter values
+        were passed to the scene function and the output was
+        checked for correctness.
+        test cases:
+        wrong shape (must match the scenes shape)
+        last dimension shape does not match (must 2, azimuth, elevation)
+        direction has too many dimensions
+        is empty
+        contains wrong values (None, nan)
+        """
         # working case
         rowid = 1
         my_scene = self.mydb.scene(rowid=rowid)
-        directions = self.mydb.viewing_directions
+        directions = self.mydb.viewing_directions.copy()
+        directions = np.radians(directions)
         my_pcv = pcode.pcv(my_scene, directions)
         self.assertIsNotNone(my_pcv)
         self.assertFalse(sum(my_pcv.shape) == 0)
@@ -259,12 +352,51 @@ class TestCase(unittest.TestCase):
         with self.assertRaises(ValueError):
             my_pcv = pcode.pcv(my_scene, testdirection)
 
+        # test if error is throught for elevation or azimuth out of range
+        # check elevation, should be in [-pi*2;pi*2]
+        testdirections = np.zeros((180, 360, 2))
+        testdirections[10, 15, 0] = -np.pi*2 - 0.001
+        with self.assertRaises(ValueError):
+            my_pcv = pcode.pcv(my_scene, testdirections)
+
+        testdirections = np.zeros((180, 360, 2))
+        testdirections[10, 15, 0] = np.pi*2 + 0.001
+        with self.assertRaises(ValueError):
+            my_pcv = pcode.pcv(my_scene, testdirections)
+
+        # check azimuth, should be in [-2*pi;2*pi]
+        testdirections = np.zeros((180, 360, 2))
+        testdirections[10, 15, 1] = -2 * np.pi - 0.001
+        with self.assertRaises(ValueError):
+            my_pcv = pcode.pcv(my_scene, testdirections)
+
+        testdirections = np.zeros((180, 360, 2))
+        testdirections[10, 15, 1] = 2*np.pi + 0.001
+        with self.assertRaises(ValueError):
+            my_pcv = pcode.pcv(my_scene, testdirections)
+
     def test_apcv(self):
+        """
+        this test checks that the correct errors are raised if
+        wrong values for the input parameter direction is passed to the
+        function apcv of the navipy.database module.
+        correct values are in the range between 2 and 5.
+        it also contains some test where correct parameter values
+        were passed to the scene function and the output was
+        checked for correctness.
+        test cases:
+        wrong shape (must match the scenes shape)
+        last dimension shape does not match (must 2, azimuth, elevation)
+        direction has too many dimensions
+        is empty
+        contains wrong values (None, nan)
+        """
         # working case
         rowid = 1
         my_scene = self.mydb.scene(rowid=rowid)
-        # print("scene shape",my_scene.shape)
-        directions = self.mydb.viewing_directions
+        directions = self.mydb.viewing_directions.copy()
+        directions = np.radians(directions)
+
         my_pcv = pcode.apcv(my_scene, directions)
 
         self.assertIsNotNone(my_pcv)
@@ -303,6 +435,18 @@ class TestCase(unittest.TestCase):
             my_pcv = pcode.apcv(my_scene, testdirection)
 
     def test_size(self):
+        """
+        this test checks that the correct errors are raised if
+        wrong values for the input parameter size are passed to the
+        function michelson_contrast of the navipy.database module.
+        correct values are in the range between 2 and 5.
+        it also contains some test where correct parameter values
+        were passed to the scene function and the output was
+        checked for correctness.
+        test cases:
+        None, NaN values, chars, floats for the contrast_size
+        int values that are out of range (<2;>5)
+        """
         # not working cases:
         scene = self.mydb.scene(rowid=1)
 
@@ -328,6 +472,18 @@ class TestCase(unittest.TestCase):
             self.assertTrue(contrast.shape[1] > 0)
 
     def test_michelsoncontrast_scene(self):
+        """
+        this test checks that the correct errors are raised if
+        wrong values for the input parameter scene is passed to the
+        function michelson_contrast of the navipy.database module
+        it also contains some test where correct parameter values
+        were passed to the scene function and the output was
+        checked for correctness.
+        test cases:
+        None, NaN values in the the scene
+        scene is of wrong type (np.array)
+        scene is of wrong size
+        """
 
         scene = self.mydb.scene(rowid=1)
 
-- 
GitLab