Skip to content
Snippets Groups Projects
Commit 62038bb5 authored by Luise Odenthal's avatar Luise Odenthal
Browse files

debuged test and init for comparison

parent 394c53bc
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ memorised places. ...@@ -6,6 +6,7 @@ memorised places.
import numpy as np import numpy as np
from navipy.processing.tools import is_ibpc, is_obpc from navipy.processing.tools import is_ibpc, is_obpc
def is_numeric_array(array): def is_numeric_array(array):
"""Checks if the dtype of the array is numeric. """Checks if the dtype of the array is numeric.
...@@ -53,9 +54,7 @@ def check_scene(scene): ...@@ -53,9 +54,7 @@ def check_scene(scene):
assert len(scene.shape) == 3 assert len(scene.shape) == 3
assert ~(scene.shape[1] <= 0) assert ~(scene.shape[1] <= 0)
assert ~(scene.shape[0] <= 0) assert ~(scene.shape[0] <= 0)
assert (scene.shape[2] == 4) assert (scene.shape[2] == 1)
assert (scene.shape[3] == 1)
# assert ~(np.any(np.isNone(scene)))
def simple_imagediff(current, memory): def simple_imagediff(current, memory):
...@@ -77,8 +76,8 @@ the current and memorised place code ...@@ -77,8 +76,8 @@ the current and memorised place code
'memory place code should be a numpy array' 'memory place code should be a numpy array'
assert np.all(current.shape == memory.shape),\ assert np.all(current.shape == memory.shape),\
'memory and current place code should have the same shape' 'memory and current place code should have the same shape'
assert check_scene(current) check_scene(current)
assert check_scene(memory) check_scene(memory)
diff = current - memory diff = current - memory
if is_ibpc(current): if is_ibpc(current):
return diff return diff
...@@ -95,7 +94,7 @@ the current and memorised place code ...@@ -95,7 +94,7 @@ the current and memorised place code
:param current: current place code :param current: current place code
:param memory: memorised place code :param memory: memorised place code
:returns: the image difference :returns: the image difference
:rtype: float :rtype: float #array(1,4) for ibpc and float for obpc
..ref: Zeil, J., 2012. Visual homing: an insect perspective. ..ref: Zeil, J., 2012. Visual homing: an insect perspective.
Current opinion in neurobiology Current opinion in neurobiology
...@@ -107,11 +106,11 @@ the current and memorised place code ...@@ -107,11 +106,11 @@ the current and memorised place code
'memory place code should be a numpy array' 'memory place code should be a numpy array'
assert np.all(current.shape == memory.shape),\ assert np.all(current.shape == memory.shape),\
'memory and current place code should have the same shape' 'memory and current place code should have the same shape'
assert check_scene(current) check_scene(current)
assert check_scene(memory) check_scene(memory)
diff = np.power(current - memory, 2) diff = np.power(current - memory, 2)
if is_ibpc(current): if is_ibpc(current):
return np.sqrt(diff.mean(axis=0).mean(axis=1)) return np.sqrt(diff.mean(axis=0).mean(axis=0)) # 1
elif is_obpc(current): elif is_obpc(current):
return np.sqrt(diff.mean(axis=0).mean(axis=0)) return np.sqrt(diff.mean(axis=0).mean(axis=0))
else: else:
...@@ -134,26 +133,39 @@ the current and memorised place code. ...@@ -134,26 +133,39 @@ the current and memorised place code.
""" """
assert is_ibpc(current),\ assert is_ibpc(current),\
'The current and memory place code should be image based' 'The current and memory place code should be image based'
assert check_scene(current) assert is_ibpc(memory),\
assert check_scene(memory) 'The current and memory place code should be image based'
ridf = np.zeros(current.shape[1]) check_scene(current)
check_scene(memory)
ridf = np.zeros((current.shape[1], current.shape[2]))
for azimuth_i in range(0, current.shape[1]): for azimuth_i in range(0, current.shape[1]):
rot_im = np.roll(current, azimuth_i, axis=1) rot_im = np.roll(current, azimuth_i, axis=1)
rot_im[azimuth_i] = imagediff(rot_im, memory) ridf[azimuth_i, :] = np.squeeze(imagediff(rot_im, memory)) # rot_im
return ridf return ridf
def diff_optic_flow(current,memory):
currrol = np.roll(current, 1, axis=1) def diff_optic_flow(current, memory):
print("shape of current", current.shape)
assert is_ibpc(current),\
'The current and memory place code should be image based'
assert is_ibpc(memory),\
'The current and memory place code should be image based'
check_scene(current)
check_scene(memory)
currroll = np.roll(current, 1, axis=1)
dx = current-currroll dx = current-currroll
memrrol = np.roll(memory, 1, axis=1) memroll = np.roll(memory, 1, axis=1)
dy = memory-memroll dy = memory-memroll
dy = np.reshape(dy, (np.prod(dy.shape), 1)) dy = np.reshape(dy, (np.prod(dy.shape), 1))
dx = np.reshape(dx, (np.prod(dx.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)) di = np.reshape(di, (np.prod(di.shape), 1))
A = np.column_stack(dy, dx) A = np.column_stack([dy, dx])
ATA = np.dot(np.transpose(A), A) ATA = np.dot(np.transpose(A), A)
b = np.dot(np.transpose(A), di) b = np.dot(np.transpose(A), di)
res = numpy.linalg.solve(ATA, b) res = np.linalg.solve(ATA, b)
return res return res
def gradient(current, memory):
return 0
import unittest import unittest
import numpy as np import numpy as np
from navipy.database import database import navipy.database as database
import navipy.comparing as comparing import navipy.comparing as comparing
import navipy.processing as processing import navipy.processing as processing
import pkg_resources import pkg_resources
...@@ -59,13 +59,10 @@ class TestCase(unittest.TestCase): ...@@ -59,13 +59,10 @@ class TestCase(unittest.TestCase):
# should be working -> check if result is correct # should be working -> check if result is correct
for s in [curr]: for s in [curr]:
diff = comparing.imagediff(s, mem) diff = comparing.imagediff(s, mem)
self.assertFalse(diff.shape[1] <= 0) self.assertTrue(diff.shape[1] == 1)
self.assertTrue(diff.shape[2] == 4) self.assertTrue(diff.shape[0] > 0)
self.assertFalse(np.any(np.isnan(diff))) self.assertFalse(np.any(np.isnan(diff)))
self.assertTrue(is_numeric_array(diff)) self.assertTrue(is_numeric_array(diff))
self.assertTrue(diff.shape[3] == 1)
self.assertTrue(diff.shape[0] > 0)
self.assertTrue(diff.shape[1] > 0)
def test_imagediff_memory(self): def test_imagediff_memory(self):
curr = processing.scene(self.mydb, rowid=1) curr = processing.scene(self.mydb, rowid=1)
...@@ -85,8 +82,8 @@ class TestCase(unittest.TestCase): ...@@ -85,8 +82,8 @@ class TestCase(unittest.TestCase):
# should be working -> check if result is correct # should be working -> check if result is correct
for s in [mem]: for s in [mem]:
diff = comparing.imagediff(curr, s) diff = comparing.imagediff(curr, s)
self.assertFalse(diff.shape[1] <= 0) self.assertFalse(diff.shape[0] <= 0)
self.assertTrue(diff.shape[2] == 4) self.assertTrue(diff.shape[1] == 1)
self.assertFalse(np.any(np.isnan(diff))) self.assertFalse(np.any(np.isnan(diff)))
self.assertTrue(is_numeric_array(diff)) self.assertTrue(is_numeric_array(diff))
...@@ -108,13 +105,10 @@ class TestCase(unittest.TestCase): ...@@ -108,13 +105,10 @@ class TestCase(unittest.TestCase):
# should be working -> check if result is correct # should be working -> check if result is correct
for s in [curr]: for s in [curr]:
diff = comparing.rot_imagediff(s, mem) diff = comparing.rot_imagediff(s, mem)
self.assertFalse(diff.shape[1] <= 0) self.assertFalse(diff.shape[0] <= 0)
self.assertTrue(diff.shape[2] == 4) self.assertTrue(diff.shape[1] == 4)
self.assertFalse(np.any(np.isnan(diff))) self.assertFalse(np.any(np.isnan(diff)))
self.assertTrue(is_numeric_array(diff)) self.assertTrue(is_numeric_array(diff))
self.assertTrue(diff.shape[3] == 1)
self.assertTrue(diff.shape[0] > 0)
self.assertTrue(diff.shape[1] > 0)
def test_rotimagediff_memory(self): def test_rotimagediff_memory(self):
curr = processing.scene(self.mydb, rowid=1) curr = processing.scene(self.mydb, rowid=1)
...@@ -134,13 +128,10 @@ class TestCase(unittest.TestCase): ...@@ -134,13 +128,10 @@ class TestCase(unittest.TestCase):
# should be working -> check if result is correct # should be working -> check if result is correct
for s in [mem]: for s in [mem]:
diff = comparing.rot_imagediff(curr, s) diff = comparing.rot_imagediff(curr, s)
self.assertFalse(diff.shape[1] <= 0) self.assertFalse(diff.shape[0] <= 0)
self.assertTrue(diff.shape[2] == 4) self.assertTrue(diff.shape[1] == 4)
self.assertFalse(np.any(np.isnan(diff))) self.assertFalse(np.any(np.isnan(diff)))
self.assertTrue(is_numeric_array(diff)) self.assertTrue(is_numeric_array(diff))
self.assertTrue(diff.shape[3] == 1)
self.assertTrue(diff.shape[0] > 0)
self.assertTrue(diff.shape[1] > 0)
def test_simple_imagediff_curr(self): def test_simple_imagediff_curr(self):
curr = processing.scene(self.mydb, rowid=1) curr = processing.scene(self.mydb, rowid=1)
...@@ -159,14 +150,38 @@ class TestCase(unittest.TestCase): ...@@ -159,14 +150,38 @@ class TestCase(unittest.TestCase):
# should be working -> check if result is correct # should be working -> check if result is correct
for s in [curr]: for s in [curr]:
diff = comparing.rot_imagediff(s, mem) diff = comparing.simple_imagediff(s, mem)
self.assertFalse(diff.shape[1] <= 0) self.assertFalse(diff.shape[0] <= 0)
self.assertTrue(diff.shape[2] == 4) self.assertTrue(diff.shape[1] > 0)
self.assertFalse(np.any(np.isnan(diff))) self.assertFalse(np.any(np.isnan(diff)))
self.assertTrue(is_numeric_array(diff)) self.assertTrue(is_numeric_array(diff))
self.assertTrue(diff.shape[2] == 4)
self.assertTrue(diff.shape[3] == 1) self.assertTrue(diff.shape[3] == 1)
self.assertTrue(diff.shape[0] > 0)
def test_simple_imagediff_mem(self):
curr = processing.scene(self.mydb, rowid=1)
mem = processing.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]]
curr3 = [curr3, curr3, curr3]
curr3 = np.array(curr3)
curr4 = np.zeros((3, 4, 5, 0))
for s in [curr2, curr3, curr4]: # put useless stuff here
with self.assertRaises(Exception) as cm:
comparing.simple_imagediff(s, mem)
print("wanted exception occured", cm.exception)
# should be working -> check if result is correct
for s in [mem]:
diff = comparing.simple_imagediff(curr, s)
self.assertFalse(diff.shape[0] <= 0)
self.assertTrue(diff.shape[1] > 0) self.assertTrue(diff.shape[1] > 0)
self.assertFalse(np.any(np.isnan(diff)))
self.assertTrue(is_numeric_array(diff))
self.assertTrue(diff.shape[2] == 4)
self.assertTrue(diff.shape[3] == 1)
def test_diff_optic_flow_memory(self): def test_diff_optic_flow_memory(self):
curr = processing.scene(self.mydb, rowid=1) curr = processing.scene(self.mydb, rowid=1)
...@@ -189,3 +204,7 @@ class TestCase(unittest.TestCase): ...@@ -189,3 +204,7 @@ class TestCase(unittest.TestCase):
self.assertFalse(vec.shape[1] == (1, 2)) self.assertFalse(vec.shape[1] == (1, 2))
self.assertFalse(np.any(np.isnan(vec))) self.assertFalse(np.any(np.isnan(vec)))
self.assertTrue(is_numeric_array(vec)) self.assertTrue(is_numeric_array(vec))
if __name__ == '__main__':
unittest.main()
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