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

added comparison init

parent 6a895ea7
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,87 @@ memorised places. ...@@ -6,6 +6,87 @@ 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):
"""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
def check_scene(scene):
if is_ibpc(scene):
# print("normal")
assert is_numeric_array(scene) # np.isscalar(scene)
assert ~np.any(np.isnan(scene))
assert len(scene.shape) == 4
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)))
elif is_obpc(scene):
assert is_numeric_array(scene) # np.isscalar(scene)
assert ~np.any(np.isnan(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)))
def simple_imagediff(current, memory):
"""Compute the root mean square difference between
the current and memorised place code
:param current: current place code
:param memory: memorised place code
:returns: the image difference
:rtype: float
..ref: Zeil, J., 2012. Visual homing: an insect perspective.
Current opinion in neurobiology
"""
assert isinstance(current, np.ndarray),\
'current place code should be a numpy array'
assert isinstance(memory, np.ndarray),\
'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)
diff = current - memory
if is_ibpc(current):
return diff
elif is_obpc(current):
return diff
else:
raise TypeError('place code is neither an ibpc nor obpc')
def imagediff(current, memory): def imagediff(current, memory):
"""Compute the root mean square difference between """Compute the root mean square difference between
...@@ -26,6 +107,8 @@ the current and memorised place code ...@@ -26,6 +107,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)
assert 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=1))
...@@ -51,8 +134,26 @@ the current and memorised place code. ...@@ -51,8 +134,26 @@ 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 check_scene(memory)
ridf = np.zeros(current.shape[1]) ridf = np.zeros(current.shape[1])
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) rot_im[azimuth_i] = imagediff(rot_im, memory)
return ridf return ridf
def diff_optic_flow(current,memory):
currrol = np.roll(current, 1, axis=1)
dx = current-currroll
memrrol = 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)
ATA = np.dot(np.transpose(A), A)
b = np.dot(np.transpose(A), di)
res = numpy.linalg.solve(ATA, b)
return res
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment