From 62038bb577496967d86dcd0b050eca6268dee926 Mon Sep 17 00:00:00 2001 From: lodenthal <lodenthal@uni-bielefeld.de> Date: Thu, 7 Dec 2017 11:23:55 +0300 Subject: [PATCH] debuged test and init for comparison --- navipy/comparing/__init__.py | 48 ++++++++++++++++----------- navipy/comparing/test.py | 63 +++++++++++++++++++++++------------- 2 files changed, 71 insertions(+), 40 deletions(-) diff --git a/navipy/comparing/__init__.py b/navipy/comparing/__init__.py index 019f216..b033fd1 100644 --- a/navipy/comparing/__init__.py +++ b/navipy/comparing/__init__.py @@ -6,6 +6,7 @@ memorised places. import numpy as np from navipy.processing.tools import is_ibpc, is_obpc + def is_numeric_array(array): """Checks if the dtype of the array is numeric. @@ -53,9 +54,7 @@ def check_scene(scene): assert len(scene.shape) == 3 assert ~(scene.shape[1] <= 0) assert ~(scene.shape[0] <= 0) - assert (scene.shape[2] == 4) - assert (scene.shape[3] == 1) - # assert ~(np.any(np.isNone(scene))) + assert (scene.shape[2] == 1) def simple_imagediff(current, memory): @@ -77,8 +76,8 @@ the current and memorised place code 'memory place code should be a numpy array' assert np.all(current.shape == memory.shape),\ 'memory and current place code should have the same shape' - assert check_scene(current) - assert check_scene(memory) + check_scene(current) + check_scene(memory) diff = current - memory if is_ibpc(current): return diff @@ -95,7 +94,7 @@ the current and memorised place code :param current: current place code :param memory: memorised place code :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. Current opinion in neurobiology @@ -107,11 +106,11 @@ the current and memorised place code 'memory place code should be a numpy array' assert np.all(current.shape == memory.shape),\ 'memory and current place code should have the same shape' - assert check_scene(current) - assert check_scene(memory) + check_scene(current) + check_scene(memory) diff = np.power(current - memory, 2) 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): return np.sqrt(diff.mean(axis=0).mean(axis=0)) else: @@ -134,26 +133,39 @@ the current and memorised place code. """ assert is_ibpc(current),\ 'The current and memory place code should be image based' - assert check_scene(current) - assert check_scene(memory) - ridf = np.zeros(current.shape[1]) + assert is_ibpc(memory),\ + 'The current and memory place code should be image based' + check_scene(current) + check_scene(memory) + ridf = np.zeros((current.shape[1], current.shape[2])) for azimuth_i in range(0, current.shape[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 -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 - memrrol = np.roll(memory, 1, axis=1) + memroll = np.roll(memory, 1, axis=1) 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 = 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) b = np.dot(np.transpose(A), di) - res = numpy.linalg.solve(ATA, b) + res = np.linalg.solve(ATA, b) return res + + +def gradient(current, memory): + return 0 diff --git a/navipy/comparing/test.py b/navipy/comparing/test.py index 435100b..0b4cf2f 100644 --- a/navipy/comparing/test.py +++ b/navipy/comparing/test.py @@ -1,6 +1,6 @@ import unittest import numpy as np -from navipy.database import database +import navipy.database as database import navipy.comparing as comparing import navipy.processing as processing import pkg_resources @@ -59,13 +59,10 @@ class TestCase(unittest.TestCase): # should be working -> check if result is correct for s in [curr]: diff = comparing.imagediff(s, mem) - self.assertFalse(diff.shape[1] <= 0) - self.assertTrue(diff.shape[2] == 4) + self.assertTrue(diff.shape[1] == 1) + self.assertTrue(diff.shape[0] > 0) self.assertFalse(np.any(np.isnan(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): curr = processing.scene(self.mydb, rowid=1) @@ -85,8 +82,8 @@ class TestCase(unittest.TestCase): # should be working -> check if result is correct for s in [mem]: diff = comparing.imagediff(curr, s) - self.assertFalse(diff.shape[1] <= 0) - self.assertTrue(diff.shape[2] == 4) + self.assertFalse(diff.shape[0] <= 0) + self.assertTrue(diff.shape[1] == 1) self.assertFalse(np.any(np.isnan(diff))) self.assertTrue(is_numeric_array(diff)) @@ -108,13 +105,10 @@ class TestCase(unittest.TestCase): # should be working -> check if result is correct for s in [curr]: diff = comparing.rot_imagediff(s, mem) - self.assertFalse(diff.shape[1] <= 0) - self.assertTrue(diff.shape[2] == 4) + self.assertFalse(diff.shape[0] <= 0) + self.assertTrue(diff.shape[1] == 4) self.assertFalse(np.any(np.isnan(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): curr = processing.scene(self.mydb, rowid=1) @@ -134,13 +128,10 @@ class TestCase(unittest.TestCase): # should be working -> check if result is correct for s in [mem]: diff = comparing.rot_imagediff(curr, s) - self.assertFalse(diff.shape[1] <= 0) - self.assertTrue(diff.shape[2] == 4) + self.assertFalse(diff.shape[0] <= 0) + self.assertTrue(diff.shape[1] == 4) self.assertFalse(np.any(np.isnan(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): curr = processing.scene(self.mydb, rowid=1) @@ -159,14 +150,38 @@ class TestCase(unittest.TestCase): # should be working -> check if result is correct for s in [curr]: - diff = comparing.rot_imagediff(s, mem) - self.assertFalse(diff.shape[1] <= 0) - self.assertTrue(diff.shape[2] == 4) + diff = comparing.simple_imagediff(s, mem) + self.assertFalse(diff.shape[0] <= 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) - 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.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): curr = processing.scene(self.mydb, rowid=1) @@ -189,3 +204,7 @@ class TestCase(unittest.TestCase): self.assertFalse(vec.shape[1] == (1, 2)) self.assertFalse(np.any(np.isnan(vec))) self.assertTrue(is_numeric_array(vec)) + + +if __name__ == '__main__': + unittest.main() -- GitLab