Skip to content
Snippets Groups Projects
Commit 41a836f0 authored by Olivier Bertrand's avatar Olivier Bertrand
Browse files

All test pass now :)

parent 0b917de8
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@ memorised places.
"""
import numpy as np
from navipy.processing.tools import is_ibpc, is_obpc
from navipy.processing.__init__ import check_scene
from navipy.processing.tools import check_scene
def simple_imagediff(current, memory):
......@@ -125,12 +125,12 @@ A=(I_x,I_y) and b = I_t
check_scene(current)
check_scene(memory)
currroll = np.roll(current, 1, axis=1)
dx = current-currroll
dx = current - currroll
memroll = np.roll(memory, 1, axis=1)
dy = memory-memroll
dy = memory - memroll
dy = np.reshape(dy, (np.prod(dy.shape), 1))
dx = np.reshape(dx, (np.prod(dx.shape), 1))
di = current-memory
di = current - memory
di = np.reshape(di, (np.prod(di.shape), 1))
a_matrix = np.column_stack([dy, dx])
a_matrix_sqr = np.dot(np.transpose(a_matrix), a_matrix)
......
......@@ -2,39 +2,11 @@ import unittest
import numpy as np
import navipy.database as database
import navipy.comparing as comparing
import navipy.processing as processing
import navipy.processing.pcode as pcode
from navipy.processing.tools import is_numeric_array
import pkg_resources
def is_numeric_array(array):
"""Checks if the dtype of the array is numeric.
Booleans, unsigned integer, signed integer, floats and complex are
considered numeric.
Parameters
----------
array : `numpy.ndarray`-like
The array to check.
Returns
-------
is_numeric : `bool`
True if it is a recognized numerical and False if object or
string.
"""
numerical_dtype_kinds = {'b', # boolean
'u', # unsigned integer
'i', # signed integer
'f', # floats
'c'} # complex
try:
return array.dtype.kind in numerical_dtype_kinds
except AttributeError:
# in case it's not a numpy array it will probably have no dtype.
return np.asarray(array).dtype.kind in numerical_dtype_kinds
class TestCase(unittest.TestCase):
def setUp(self):
self.mydb_filename = pkg_resources.resource_filename(
......@@ -42,8 +14,8 @@ class TestCase(unittest.TestCase):
self.mydb = database.DataBaseLoad(self.mydb_filename)
def test_imagediff_curr(self):
curr = processing.scene(self.mydb, rowid=1)
mem = processing.scene(self.mydb, rowid=2)
curr = pcode.scene(self.mydb, rowid=1)
mem = pcode.scene(self.mydb, rowid=2)
curr2 = curr.copy()
curr2[3, 5, 2, 0] = np.nan
curr3 = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
......@@ -68,8 +40,8 @@ class TestCase(unittest.TestCase):
self.assertTrue(is_numeric_array(diff))
def test_imagediff_memory(self):
curr = processing.scene(self.mydb, rowid=1)
mem = processing.scene(self.mydb, rowid=2)
curr = pcode.scene(self.mydb, rowid=1)
mem = pcode.scene(self.mydb, rowid=2)
mem2 = curr.copy()
mem2[3, 5, 2, 0] = np.nan
mem3 = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
......@@ -93,8 +65,8 @@ class TestCase(unittest.TestCase):
self.assertTrue(is_numeric_array(diff))
def test_rot_imagediff_curr(self):
curr = processing.scene(self.mydb, rowid=1)
mem = processing.scene(self.mydb, rowid=2)
curr = pcode.scene(self.mydb, rowid=1)
mem = pcode.scene(self.mydb, rowid=2)
curr2 = curr.copy()
curr2[3, 5, 2, 0] = np.nan
curr3 = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
......@@ -118,8 +90,8 @@ class TestCase(unittest.TestCase):
self.assertTrue(is_numeric_array(diff))
def test_rotimagediff_memory(self):
curr = processing.scene(self.mydb, rowid=1)
mem = processing.scene(self.mydb, rowid=2)
curr = pcode.scene(self.mydb, rowid=1)
mem = pcode.scene(self.mydb, rowid=2)
mem2 = curr.copy()
mem2[3, 5, 2, 0] = np.nan
mem3 = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
......@@ -143,8 +115,8 @@ class TestCase(unittest.TestCase):
self.assertTrue(is_numeric_array(diff))
def test_simple_imagediff_curr(self):
curr = processing.scene(self.mydb, rowid=1)
mem = processing.scene(self.mydb, rowid=2)
curr = pcode.scene(self.mydb, rowid=1)
mem = pcode.scene(self.mydb, rowid=2)
curr2 = curr.copy()
curr2[3, 5, 2, 0] = np.nan
curr3 = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
......@@ -170,8 +142,8 @@ class TestCase(unittest.TestCase):
self.assertTrue(diff.shape[3] == 1)
def test_simple_imagediff_mem(self):
curr = processing.scene(self.mydb, rowid=1)
mem = processing.scene(self.mydb, rowid=2)
curr = pcode.scene(self.mydb, rowid=1)
mem = pcode.scene(self.mydb, rowid=2)
mem2 = curr.copy()
mem2[3, 5, 2, 0] = np.nan
mem3 = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
......@@ -197,8 +169,8 @@ class TestCase(unittest.TestCase):
self.assertTrue(diff.shape[3] == 1)
def test_diff_optic_flow_memory(self):
curr = processing.scene(self.mydb, rowid=1)
mem = processing.scene(self.mydb, rowid=2)
curr = pcode.scene(self.mydb, rowid=1)
mem = pcode.scene(self.mydb, rowid=2)
mem2 = curr.copy()
mem2[3, 5, 2, 0] = np.nan
mem3 = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
......@@ -221,8 +193,8 @@ class TestCase(unittest.TestCase):
self.assertTrue(is_numeric_array(vec))
def test_diff_optic_flow_curr(self):
curr = processing.scene(self.mydb, rowid=1)
mem = processing.scene(self.mydb, rowid=2)
curr = pcode.scene(self.mydb, rowid=1)
mem = pcode.scene(self.mydb, rowid=2)
curr2 = curr.copy()
curr2[3, 5, 2, 0] = np.nan
curr3 = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
......
......@@ -182,10 +182,10 @@ class TestNavipyMovingAgent(unittest.TestCase):
# Two loops attractors
graph_edges = list()
for snode, enode in zip(graph_nodes[:11],
np.alpha_2(graph_nodes[:11], 1)):
np.roll(graph_nodes[:11], 1)):
graph_edges.append((snode, enode))
for snode, enode in zip(graph_nodes[11:],
np.alpha_2(graph_nodes[11:], 1)):
np.roll(graph_nodes[11:], 1)):
graph_edges.append((snode, enode))
graph = nx.DiGraph()
......
......@@ -127,7 +127,14 @@ and minimum of the local image intensity
size=size, mode='wrap')
i_min = minimum_filter(scene[..., channel, 0],
size=size, mode='wrap')
contrast[..., channel, 0] = (i_max - i_min) / (i_max + i_min)
divider = i_max + i_min
nonzero = divider != 0
eqzero = divider == 0
i_min = i_min[nonzero]
i_max = i_max[nonzero]
divider = divider[nonzero]
contrast[nonzero, channel, 0] = (i_max - i_min) / divider
contrast[eqzero, channel, 0] = 0
return contrast
......
......@@ -3,7 +3,7 @@ import sqlite3
import numpy as np
import pandas as pd
import navipy.database as database
import navipy.processing as processing
import navipy.processing.pcode as pcode
from navipy.processing.tools import is_numeric_array
import pkg_resources
......@@ -19,9 +19,6 @@ class TestCase(unittest.TestCase):
c = conn.cursor()
c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """)
rows = c.fetchall()[0]
# print(rows)
# convert to pd.series
# working case
posorient = pd.Series(index=['x', 'y', 'z',
'alpha_0', 'alpha_1', 'alpha_2'])
......@@ -31,10 +28,7 @@ class TestCase(unittest.TestCase):
posorient.alpha_0 = rows[3]
posorient.alpha_1 = rows[2]
posorient.alpha_2 = rows[4]
# conn.close()
# print("posorient",posorient)
# print(posorientall.head())
image = processing.scene(self.mydb, posorient=posorient)
image = pcode.scene(self.mydb, posorient=posorient)
self.assertIsNotNone(image)
self.assertFalse(sum(image.shape) == 0)
# print("shape",image.shape)
......@@ -50,7 +44,7 @@ class TestCase(unittest.TestCase):
posorient2.alpha_1 = posorient.alpha_1
posorient2.alpha_2 = posorient.alpha_2
with self.assertRaises(Exception):
image = processing.scene(self.mydb, posorient=posorient2)
image = pcode.scene(self.mydb, posorient=posorient2)
# incorrect case None
posorient2 = pd.Series(index=['x', 'y', 'z',
......@@ -62,7 +56,7 @@ class TestCase(unittest.TestCase):
posorient2.alpha_1 = posorient.alpha_1
posorient2.alpha_2 = posorient.alpha_2
with self.assertRaises(ValueError):
image = processing.scene(self.mydb, posorient=posorient2)
image = pcode.scene(self.mydb, posorient=posorient2)
# incorrect case nan
posorient2 = pd.Series(index=['x', 'y', 'z',
......@@ -74,7 +68,7 @@ class TestCase(unittest.TestCase):
posorient2.alpha_1 = posorient.alpha_1
posorient2.alpha_2 = posorient.alpha_2
with self.assertRaises(ValueError):
image = processing.scene(self.mydb, posorient=posorient2)
image = pcode.scene(self.mydb, posorient=posorient2)
# incorrect case no pandas series but dict
posorient2 = {}
......@@ -85,44 +79,17 @@ class TestCase(unittest.TestCase):
posorient2['alpha_1'] = posorient.alpha_1
posorient2['alpha_2'] = posorient.alpha_2
with self.assertRaises(TypeError):
image = processing.scene(self.mydb, posorient=posorient2)
image = pcode.scene(self.mydb, posorient=posorient2)
# not working case empty
posorient2 = pd.Series(index=['x', 'y', 'z',
'alpha_0', 'alpha_1', 'alpha_2'])
with self.assertRaises(Exception):
image = processing.scene(self.mydb, posorient=posorient2)
"""
def test_db(self):
#build_test_db()
#empty database -> should not work
db_filename='test_db_1'
#loading should not work cuz not
#all required tables are in this database
with self.assertRaises(Exception) as cm:
db=database.DataBaseLoad(db_filename)
print("a wanted error occured", cm.exception)
#reading should not anyway
with self.assertRaises(Exception) as cm:
image = db.read_image(rowid=1)
#empty database -> should work??
db_filename='test_db_2'
#loading should work
db=database.DataBaseLoad(db_filename)
#reading should not
with self.assertRaises(Exception) as cm:
image = db.read_image(rowid=1)
"""
image = pcode.scene(self.mydb, posorient=posorient2)
def test_skyline_scene(self):
# image = processing.scene(self.mydb,posorient=posorient)
# print("image size skyline test",image.shape)
scene = processing.scene(self.mydb, rowid=1)
# print("scene size skypline test",scene.shape)
scene = pcode.scene(self.mydb, rowid=1)
scene2 = scene.copy()
scene2[3, 5, 2, 0] = np.nan
scene3 = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
......@@ -132,15 +99,15 @@ class TestCase(unittest.TestCase):
# put useless stuff here
with self.assertRaises(ValueError):
processing.skyline(scene2)
pcode.skyline(scene2)
with self.assertRaises(TypeError):
processing.skyline(scene3)
pcode.skyline(scene3)
with self.assertRaises(Exception):
processing.skyline(scene4)
pcode.skyline(scene4)
# should be working -> check if result(skyline) is correct
for s in [scene]:
skyline = processing.skyline(s)
skyline = pcode.skyline(s)
self.assertFalse(skyline.shape[1] <= 0)
self.assertTrue(skyline.shape[2] == 4)
self.assertFalse(np.any(np.isnan(skyline)))
......@@ -151,24 +118,21 @@ class TestCase(unittest.TestCase):
self.assertTrue(skyline.shape[1] > 0)
def test_id(self):
# print("test_id")
# check for wrong cases
# my_scene = processing.scene(self.mydb, rowid=-2)
for rowid in [0, -2]:
with self.assertRaises(ValueError):
# print("rowid",rowid)
processing.scene(self.mydb, rowid=rowid)
pcode.scene(self.mydb, rowid=rowid)
with self.assertRaises(TypeError):
processing.scene(self.mydb, rowid='T')
pcode.scene(self.mydb, rowid='T')
with self.assertRaises(ValueError):
processing.scene(self.mydb, rowid=None)
pcode.scene(self.mydb, rowid=None)
with self.assertRaises(TypeError):
processing.scene(self.mydb, rowid=np.nan)
pcode.scene(self.mydb, rowid=np.nan)
with self.assertRaises(TypeError):
processing.scene(self.mydb, rowid=4.5)
pcode.scene(self.mydb, rowid=4.5)
for rowid in [1, 2, 3, 4, 5]:
image = processing.scene(self.mydb, rowid=rowid)
image = pcode.scene(self.mydb, rowid=rowid)
# image=np.array(image)
self.assertIsNotNone(image)
self.assertFalse(sum(image.shape) == 0)
......@@ -180,36 +144,31 @@ class TestCase(unittest.TestCase):
self.assertTrue(image.shape[1] > 0)
def test_distance_channel(self):
scene = processing.scene(self.mydb, rowid=1)
# print("scene sizejhghghhg",scene.shape)
scene = pcode.scene(self.mydb, rowid=1)
# should not be working
for d in ['g', None, np.nan, 8.4]:
with self.assertRaises(TypeError):
processing.contrast_weighted_nearness(scene,
distance_channel=d)
pcode.contrast_weighted_nearness(scene,
distance_channel=d)
with self.assertRaises(ValueError):
processing.contrast_weighted_nearness(scene,
distance_channel=-1)
pcode.contrast_weighted_nearness(scene,
distance_channel=-1)
# should work
for d in [0, 1, 2, 3]:
weighted_scene = \
processing.contrast_weighted_nearness(scene,
distance_channel=d)
# print("last channel",d)
assert is_numeric_array(weighted_scene)
assert ~np.any(np.isnan(weighted_scene))
assert len(weighted_scene.shape) == 4
assert ~(weighted_scene.shape[1] <= 0)
assert ~(weighted_scene.shape[0] <= 0)
assert (weighted_scene.shape[2] == 4)
assert ~(np.any(np.isNone(weighted_scene)))
d = 3
weighted_scene = \
pcode.contrast_weighted_nearness(scene,
distance_channel=d)
# print("last channel",d)
self.assertTrue(is_numeric_array(weighted_scene))
self.assertTrue(~np.any(np.isnan(weighted_scene)))
self.assertEqual(weighted_scene.shape, scene.shape)
def test_contr_weight_scene(self):
scene = processing.scene(self.mydb, rowid=1)
scene = pcode.scene(self.mydb, rowid=1)
# working cases
contrast = processing.contrast_weighted_nearness(scene)
contrast = pcode.contrast_weighted_nearness(scene)
self.assertIsNotNone(contrast)
self.assertFalse(sum(contrast.shape) == 0)
self.assertTrue(len(contrast.shape) == 4)
......@@ -227,49 +186,43 @@ class TestCase(unittest.TestCase):
scene3 = np.array(scene3)
scene4 = np.zeros((3, 4, 5, 0))
with self.assertRaises(ValueError):
contrast = processing.contrast_weighted_nearness(scene2)
with self.assertRaises(ValueError):
contrast = processing.contrast_weighted_nearness(scene3)
contrast = pcode.contrast_weighted_nearness(scene2)
with self.assertRaises(Exception):
contrast = pcode.contrast_weighted_nearness(scene3)
with self.assertRaises(Exception):
contrast = processing.contrast_weighted_nearness(scene4)
contrast = pcode.contrast_weighted_nearness(scene4)
def test_contr_weight_contrast(self):
scene = processing.scene(self.mydb, rowid=1)
scene = pcode.scene(self.mydb, rowid=1)
for size in [9.4, 'g', None, np.nan]:
with self.assertRaises(TypeError):
contrast = \
processing.contrast_weighted_nearness(scene,
contrast_size=size)
contrast = pcode.contrast_weighted_nearness(
scene, contrast_size=size)
for size in [8, 1, 0, -4]:
with self.assertRaises(ValueError):
contrast = \
processing.contrast_weighted_nearness(scene,
contrast_size=size)
pcode.contrast_weighted_nearness(
scene, contrast_size=size)
# working cases
for size in [2, 3, 4, 5]:
contrast = \
processing.contrast_weighted_nearness(scene,
contrast_size=size)
contrast = pcode.contrast_weighted_nearness(scene,
contrast_size=size)
self.assertIsNotNone(contrast)
self.assertFalse(sum(contrast.shape) == 0)
self.assertTrue(len(contrast.shape) == 4)
self.assertFalse(np.any(np.isnan(contrast)))
self.assertTrue(contrast.shape[3] == 1)
self.assertTrue(contrast.shape[2] == 4)
self.assertTrue(contrast.shape[0] > 0)
self.assertTrue(contrast.shape[1] > 0)
self.assertEqual(contrast.shape[3], 1)
self.assertEqual(contrast.shape[2], scene.shape[2])
self.assertEqual(contrast.shape[0], scene.shape[0])
self.assertEqual(contrast.shape[1], scene.shape[1])
def test_pcv(self):
# working case
rowid = 1
my_scene = processing.scene(self.mydb, rowid=rowid)
# print("scene shape",my_scene.shape)
my_scene = pcode.scene(self.mydb, rowid=rowid)
directions = self.mydb.viewing_directions
# print("directions",directions.shape)
my_pcv = processing.pcv(my_scene, directions)
# print("place code shape",my_pcv.shape)
my_pcv = pcode.pcv(my_scene, directions)
self.assertIsNotNone(my_pcv)
self.assertFalse(sum(my_pcv.shape) == 0)
self.assertTrue(len(my_pcv.shape) == 4)
......@@ -282,37 +235,37 @@ class TestCase(unittest.TestCase):
# not working cases doesnt match with shape of place code
testdirection = np.zeros((2, 4, 2))
with self.assertRaises(Exception):
my_pcv = processing.pcv(my_scene, testdirection)
my_pcv = pcode.pcv(my_scene, testdirection)
# not working cases wrong last dimension
testdirection = np.zeros((180, 360, 1))
with self.assertRaises(Exception):
my_pcv = processing.pcv(my_scene, testdirection)
my_pcv = pcode.pcv(my_scene, testdirection)
# not working cases too many dimensions
testdirection = np.zeros((180, 360, 2, 4))
with self.assertRaises(Exception):
my_pcv = processing.pcv(my_scene, testdirection)
my_pcv = pcode.pcv(my_scene, testdirection)
# not working cases empty
testdirection = np.zeros(())
with self.assertRaises(Exception):
my_pcv = processing.pcv(my_scene, testdirection)
my_pcv = pcode.pcv(my_scene, testdirection)
# not working cases nans
testdirection = np.zeros((180, 360, 2, 4))
testdirection[2, 3, 0] = np.nan
with self.assertRaises(ValueError):
my_pcv = processing.pcv(my_scene, testdirection)
my_pcv = pcode.pcv(my_scene, testdirection)
def test_apcv(self):
# working case
rowid = 1
my_scene = processing.scene(self.mydb, rowid=rowid)
my_scene = pcode.scene(self.mydb, rowid=rowid)
# print("scene shape",my_scene.shape)
directions = self.mydb.viewing_directions
# print("directions",directions.shape)
my_pcv = processing.apcv(my_scene, directions)
my_pcv = pcode.apcv(my_scene, directions)
self.assertIsNotNone(my_pcv)
self.assertFalse(sum(my_pcv.shape) == 0)
......@@ -326,42 +279,44 @@ class TestCase(unittest.TestCase):
# not working cases doesnt match with shape of place code
testdirection = np.zeros((2, 4, 2))
with self.assertRaises(Exception):
my_pcv = processing.apcv(my_scene, testdirection)
my_pcv = pcode.apcv(my_scene, testdirection)
# not working cases wrong last dimension
testdirection = np.zeros((180, 360, 1))
with self.assertRaises(Exception):
my_pcv = processing.apcv(my_scene, testdirection)
my_pcv = pcode.apcv(my_scene, testdirection)
# not working cases too many dimensions
testdirection = np.zeros((180, 360, 2, 4))
with self.assertRaises(Exception):
my_pcv = processing.apcv(my_scene, testdirection)
my_pcv = pcode.apcv(my_scene, testdirection)
# not working cases empty
testdirection = np.zeros(())
with self.assertRaises(Exception):
my_pcv = processing.apcv(my_scene, testdirection)
my_pcv = pcode.apcv(my_scene, testdirection)
# not working cases nans
testdirection = np.zeros((180, 360, 2, 4))
testdirection[2, 3, 0] = np.nan
with self.assertRaises(ValueError):
my_pcv = processing.apcv(my_scene, testdirection)
my_pcv = pcode.apcv(my_scene, testdirection)
def test_size(self):
# not working cases:
scene = processing.scene(self.mydb, rowid=1)
scene = pcode.scene(self.mydb, rowid=1)
for size in [8, 1, 0, -4]:
with self.assertRaises(ValueError):
contrast = processing.michelson_contrast(scene, size=size)
contrast = pcode.michelson_contrast(
scene, size=size)
for size in [9.4, 'g', None, np.nan]:
with self.assertRaises(TypeError):
contrast = processing.michelson_contrast(scene, size=size)
contrast = pcode.michelson_contrast(
scene, size=size)
# working cases
for size in [2, 3, 4, 5]:
contrast = processing.michelson_contrast(scene, size=size)
contrast = pcode.michelson_contrast(scene, size=size)
self.assertIsNotNone(contrast)
self.assertFalse(sum(contrast.shape) == 0)
self.assertTrue(len(contrast.shape) == 4)
......@@ -373,10 +328,10 @@ class TestCase(unittest.TestCase):
def test_michelsoncontrast_scene(self):
scene = processing.scene(self.mydb, rowid=1)
scene = pcode.scene(self.mydb, rowid=1)
# working cases
contrast = processing.michelson_contrast(scene)
contrast = pcode.michelson_contrast(scene)
self.assertIsNotNone(contrast)
self.assertFalse(sum(contrast.shape) == 0)
self.assertTrue(len(contrast.shape) == 4)
......@@ -395,7 +350,7 @@ class TestCase(unittest.TestCase):
scene4 = np.zeros((3, 4, 5, 0))
for s in [scene2, scene3, scene4]:
with self.assertRaises(Exception) as cm:
contrast = processing.michelson_contrast(s,)
contrast = pcode.michelson_contrast(s,)
print("wanted exception occured", cm.exception)
......
<<<<<<< HEAD
=======
0001: Remove DUPLICATE:
Remove DUPLICATE: is_numeric_array is in:
processing.__init__
and comparing.__init__
use the one it processing.__init__
Remove DUPLICATE: check_scene is in:
processing.__init__
and comparing.__init__
Move check_scene, is_numeric_array to:
processing.__init__
------------------------------------------------------
0002: Restructure processing:
Move function in processing/__init__ to processing/place_code.py
Move function optic_flow in processing/place_code.py to processing/motion_code.py
------------------------------------------------------
0003: Improve database
In the init database I would like to use class properties instead of get/read
......@@ -54,6 +35,7 @@ Need to propagate the changes through all the code (see rendering / processing /
------------------------------------------------------
0007: Fix test processing
>>>>>>> a85c0cc1d498234a17941f8c483c69983b367529
- Fix db in test function such that it work from any location. (probably need to add a module resources and function to load them)
- Test are failing WHY???
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment