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

updates everything to the new convention, but one test in moving/test_math.py is not working

parent c4d90afa
No related branches found
No related tags found
No related merge requests found
...@@ -92,12 +92,17 @@ def diff_optic_flow(current, memory): ...@@ -92,12 +92,17 @@ def diff_optic_flow(current, memory):
to memory by using the optic flow under the to memory by using the optic flow under the
constrain that the brightness is constant, (small movement), constrain that the brightness is constant, (small movement),
using a taylor expansion and solving the equation: using a taylor expansion and solving the equation:
0=I_t+\delta I*<u,v> or I_x+I_y+I_t=0 .. math::
0=I_t+\delta I*<u,v> or I_x+I_y+I_t=0
afterwards the aperture problem is solved by a afterwards the aperture problem is solved by a
Matrix equation Ax=b, where x=(u,v) and Matrix equation Ax=b, where x=(u,v) and
A=(I_x,I_y) and b = I_t .. math::
A=(I_x,I_y) and b = I_t
The intput parameters are the following:
:param current: current place code :param current: current place code
:param memory: memorised place code :param memory: memorised place code
:returns: a directional vectors :returns: a directional vectors
......
...@@ -400,7 +400,6 @@ class DataBase(): ...@@ -400,7 +400,6 @@ class DataBase():
self.db.commit() self.db.commit()
return rowid return rowid
else: else:
print(posorient)
raise ValueError('posorient not found') raise ValueError('posorient not found')
@property @property
...@@ -459,7 +458,6 @@ class DataBaseLoad(DataBase): ...@@ -459,7 +458,6 @@ class DataBaseLoad(DataBase):
posorient = pd.read_sql_query( posorient = pd.read_sql_query(
"select * from position_orientation;", self.db) "select * from position_orientation;", self.db)
posorient.set_index('id', inplace=True) posorient.set_index('id', inplace=True)
if not isinstance(posorient.index, pd.core.index.MultiIndex): if not isinstance(posorient.index, pd.core.index.MultiIndex):
warnings.warn("you are loading a database with old conventions,\ warnings.warn("you are loading a database with old conventions,\
it will be transformed automatically into the new one") it will be transformed automatically into the new one")
...@@ -470,20 +468,11 @@ class DataBaseLoad(DataBase): ...@@ -470,20 +468,11 @@ class DataBaseLoad(DataBase):
tuples.append(('location', n)) tuples.append(('location', n))
else: else:
tuples.append((convention, n)) tuples.append((convention, n))
#tuples = [('location', 'x'), ('location', 'y'),
# ('location', 'z'), (convention, 'alpha_0'),
# (convention, 'alpha_1'), (convention, 'alpha_2')]
index = pd.MultiIndex.from_tuples(tuples, index = pd.MultiIndex.from_tuples(tuples,
names=['position', 'orientation']) names=['position', 'orientation'])
#posorient.rename(columns = {'x':('location', 'x')})
#posorient.rename(columns = {'y':('location', 'y')})
#posorient.rename(columns = {'z':('location', 'z')})
#posorient.rename(columns = {'alph_0':(convention, 'alpha_0')})
#posorient.rename(columns = {'alpha_1':(convention, 'alpha_1')})
#posorient.rename(columns = {'alpha_2':(convention, 'alpha_2')})
posorient.columns = index posorient.columns = index
return posorient return posorient
return posorient return posorient
@property @property
......
...@@ -68,12 +68,10 @@ class AbstractAgent(): ...@@ -68,12 +68,10 @@ class AbstractAgent():
('location', 'dz'), (convention, 'dalpha_0'), ('location', 'dz'), (convention, 'dalpha_0'),
(convention, 'dalpha_1'), (convention, 'dalpha_2')] (convention, 'dalpha_1'), (convention, 'dalpha_2')]
index_vel = pd.MultiIndex.from_tuples(tuples_vel, index_vel = pd.MultiIndex.from_tuples(tuples_vel,
names=['position', names=['position',
'orientation']) 'orientation'])
self._velocity_col = index_vel self._velocity_col = index_vel
self._posorient_vel_col = self._velocity_col.copy() self._posorient_vel_col = self._velocity_col.copy()
self._posorient_vel = pd.Series( self._posorient_vel = pd.Series(
index=self._posorient_vel_col, index=self._posorient_vel_col,
data=np.nan) data=np.nan)
...@@ -224,8 +222,9 @@ GridAgent is a close loop agent here its position is snap to a grid. ...@@ -224,8 +222,9 @@ GridAgent is a close loop agent here its position is snap to a grid.
posorient_vel, posorient_vel,
move_mode=self._mode_move['mode'], move_mode=self._mode_move['mode'],
move_param=self._mode_move['param']) move_param=self._mode_move['param'])
tmppos = self._brain.posorients
tmp = navimomath.closest_pos( tmp = navimomath.closest_pos(
posorient_vel, self._brain.posorients) posorient_vel, tmppos) # self._brain.posorients)
posorient_vel.loc[self._posorient_col] = \ posorient_vel.loc[self._posorient_col] = \
tmp.loc[self._posorient_col] tmp.loc[self._posorient_col]
posorient_vel.name = tmp.name posorient_vel.name = tmp.name
...@@ -264,7 +263,6 @@ GridAgent is a close loop agent here its position is snap to a grid. ...@@ -264,7 +263,6 @@ GridAgent is a close loop agent here its position is snap to a grid.
self._posorient_vel.loc[common_id] = start_posorient.loc[common_id] self._posorient_vel.loc[common_id] = start_posorient.loc[common_id]
self.move() self.move()
next_posorient = self._posorient_vel next_posorient = self._posorient_vel
self._posorients_queue.task_done() self._posorients_queue.task_done()
self._results_queue.put((start_posorient, next_posorient)) self._results_queue.put((start_posorient, next_posorient))
self._posorients_queue.task_done() self._posorients_queue.task_done()
......
...@@ -35,38 +35,51 @@ def next_pos(motion_vec, move_mode, move_param=None): ...@@ -35,38 +35,51 @@ def next_pos(motion_vec, move_mode, move_param=None):
if move_mode not in mode_moves_supported().keys(): if move_mode not in mode_moves_supported().keys():
raise KeyError( raise KeyError(
'move mode must is not supported {}'.format(move_mode)) 'move mode must is not supported {}'.format(move_mode))
tuples = [('location', 'dx'), ('location', 'dy'),
speed = pd.Series(index=['dx', 'dy', 'dz']) ('location', 'dz')]
speed.loc['dx'] = motion_vec['location']['dx'] index = pd.MultiIndex.from_tuples(tuples,
speed.loc['dy'] = motion_vec['location']['dy'] names=['position',
speed.loc['dz'] = motion_vec['location']['dz'] 'orientation'])
speed = pd.Series(index=index)
speed['location']['dx'] = motion_vec['location']['dx']
speed['location']['dy'] = motion_vec['location']['dy']
speed['location']['dz'] = motion_vec['location']['dz']
# speed = motion_vec.loc[['dx', 'dy', 'dz']] # speed = motion_vec.loc[['dx', 'dy', 'dz']]
if move_mode == 'on_cubic_grid': if move_mode == 'on_cubic_grid':
# speed in spherical coord # speed in spherical coord
epsilon = np.arctan2(speed.dz, np.sqrt(speed.dx**2 + speed.dy**2)) epsilon = np.arctan2(speed['location']['dz'],
phi = np.arctan2(speed.dy, speed.dx) np.sqrt(speed['location']['dx']**2 +
speed['location']['dy']**2))
phi = np.arctan2(speed['location']['dy'], speed['location']['dx'])
radius = np.sqrt(np.sum(speed**2)) radius = np.sqrt(np.sum(speed**2))
if np.isclose(radius, 0): if np.isclose(radius, 0):
scaling = 0 scaling = 0
speed = 0 * speed speed = 0 * speed
else: else:
deltas = pd.Series(index=['dx', 'dy', 'dz']) tuples = [('location', 'dx'), ('location', 'dy'),
deltas.dz = epsilon > (np.pi / 8) - epsilon < (np.pi / 8) ('location', 'dz')]
index = pd.MultiIndex.from_tuples(tuples,
names=['position',
'orientation'])
deltas = pd.Series(index=index)
deltas['location']['dz'] = float(epsilon > (np.pi / 8) -
epsilon < (np.pi / 8))
edgecases = np.linspace(-np.pi, np.pi, 9) edgecases = np.linspace(-np.pi, np.pi, 9)
case_i = np.argmin(np.abs(phi - edgecases)) case_i = np.argmin(np.abs(phi - edgecases))
if case_i == 8 or case_i == 0 or case_i == 1 or case_i == 7: if case_i == 8 or case_i == 0 or case_i == 1 or case_i == 7:
deltas.dx = -1 deltas['location']['dx'] = -1
elif case_i == 3 or case_i == 4 or case_i == 5: elif case_i == 3 or case_i == 4 or case_i == 5:
deltas.dx = 1 deltas['location']['dx'] = 1
else: else:
deltas.dx = 0 deltas['location']['dx'] = 0
if case_i == 1 or case_i == 2 or case_i == 3: if case_i == 1 or case_i == 2 or case_i == 3:
deltas.dy = -1 deltas['location']['dy'] = -1
elif case_i == 5 or case_i == 6 or case_i == 7: elif case_i == 5 or case_i == 6 or case_i == 7:
deltas.dy = 1 deltas['location']['dy'] = 1
else: else:
deltas.dy = 0 deltas['location']['dy'] = 0
scaling = 1 scaling = 1
speed = move_param['grid_spacing'] * deltas speed = move_param['grid_spacing'] * deltas
elif move_mode is 'free_run': elif move_mode is 'free_run':
...@@ -74,9 +87,12 @@ def next_pos(motion_vec, move_mode, move_param=None): ...@@ -74,9 +87,12 @@ def next_pos(motion_vec, move_mode, move_param=None):
else: else:
raise ValueError('grid_mode is not supported') raise ValueError('grid_mode is not supported')
toreturn = motion_vec toreturn = motion_vec
toreturn['location']['x'] = toreturn['location']['x'] + speed['dx'] toreturn['location']['x'] = (toreturn['location']['x'] +
toreturn['location']['y'] = toreturn['location']['y'] + speed['dy'] speed['location']['dx'])
toreturn['location']['z'] = toreturn['location']['z'] + speed['dz'] toreturn['location']['y'] = (toreturn['location']['y'] +
speed['location']['dy'])
toreturn['location']['z'] = (toreturn['location']['z'] +
speed['location']['dz'])
# toreturn.loc[['x', 'y', 'z']] += speed.rename({'dx': 'x', # toreturn.loc[['x', 'y', 'z']] += speed.rename({'dx': 'x',
# 'dy': 'y', # 'dy': 'y',
# 'dz': 'z'}) * scaling # 'dz': 'z'}) * scaling
...@@ -88,13 +104,15 @@ def closest_pos(pos, positions): ...@@ -88,13 +104,15 @@ def closest_pos(pos, positions):
:param pos: the position to find (a pandas Series with ['x','y','z'] :param pos: the position to find (a pandas Series with ['x','y','z']
:param positions: the possible closest positions :param positions: the possible closest positions
(a pandas dataframe with ['x','y','z']) (a pandas dataframe with
[['location','x'],['location','y'],['location','z']])
""" """
euclidian_dist = np.sqrt( euclidian_dist = np.sqrt(
(pos['location']['x'] - positions['location']['x'])**2 (pos['location']['x'] - positions['location']['x'])**2
+ (pos['location']['y'] - positions['location']['y'])**2 + (pos['location']['y'] - positions['location']['y'])**2
+ (pos['location']['z'] - positions['location']['z'])**2) + (pos['location']['z'] - positions['location']['z'])**2)
return positions.loc[euclidian_dist.idxmin(), :] return positions.loc[euclidian_dist.idxmin()]
def closest_pos_memory_friendly(pos, database): def closest_pos_memory_friendly(pos, database):
......
...@@ -33,8 +33,8 @@ class BrainTest(Brain): ...@@ -33,8 +33,8 @@ class BrainTest(Brain):
('location', 'dz'), (convention, 'dalpha_0'), ('location', 'dz'), (convention, 'dalpha_0'),
(convention, 'dalpha_1'), (convention, 'dalpha_2')] (convention, 'dalpha_1'), (convention, 'dalpha_2')]
index_vel = pd.MultiIndex.from_tuples(tuples_vel, index_vel = pd.MultiIndex.from_tuples(tuples_vel,
names=['position', names=['position',
'orientation']) 'orientation'])
self.__velocity_col = index_vel self.__velocity_col = index_vel
self.__posorient_vel_col = self.__velocity_col.copy() self.__posorient_vel_col = self.__velocity_col.copy()
# self.__posorient_vel_col.extend(self.__velocity_col) # self.__posorient_vel_col.extend(self.__velocity_col)
...@@ -64,8 +64,8 @@ class TestNavipyMovingAgent(unittest.TestCase): ...@@ -64,8 +64,8 @@ class TestNavipyMovingAgent(unittest.TestCase):
('location', 'dz'), (convention, 'dalpha_0'), ('location', 'dz'), (convention, 'dalpha_0'),
(convention, 'dalpha_1'), (convention, 'dalpha_2')] (convention, 'dalpha_1'), (convention, 'dalpha_2')]
index_vel = pd.MultiIndex.from_tuples(tuples_vel, index_vel = pd.MultiIndex.from_tuples(tuples_vel,
names=['position', names=['position',
'orientation']) 'orientation'])
self.__velocity_col = index_vel self.__velocity_col = index_vel
self.__posorient_vel_col = self.__posorient_col self.__posorient_vel_col = self.__posorient_col
# self.__posorient_vel_col.extend(self.__velocity_col) # self.__posorient_vel_col.extend(self.__velocity_col)
...@@ -89,7 +89,7 @@ class TestNavipyMovingAgent(unittest.TestCase): ...@@ -89,7 +89,7 @@ class TestNavipyMovingAgent(unittest.TestCase):
def test_move_gridagent(self): def test_move_gridagent(self):
agent = naviagent.GridAgent(self.brain) agent = naviagent.GridAgent(self.brain)
initposorient = None initposorient = None
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True):
initposorient = self.brain.posorients.loc[13, :] initposorient = self.brain.posorients.loc[13, :]
initposovel = pd.Series(data=0, initposovel = pd.Series(data=0,
index=self.__posorient_vel_col) index=self.__posorient_vel_col)
...@@ -97,10 +97,15 @@ class TestNavipyMovingAgent(unittest.TestCase): ...@@ -97,10 +97,15 @@ class TestNavipyMovingAgent(unittest.TestCase):
agent.posorient = initposovel agent.posorient = initposovel
with self.assertRaises(AttributeError): with self.assertRaises(AttributeError):
agent.move() agent.move()
tuples = [('location', 'dx'), ('location', 'dy'),
('location', 'dz')]
index = pd.MultiIndex.from_tuples(tuples,
names=['position',
'orientation'])
mode_move = {'mode': 'on_cubic_grid', mode_move = {'mode': 'on_cubic_grid',
'param': {'grid_spacing': 'param': {'grid_spacing':
pd.Series(data=1, pd.Series(data=1,
index=['dx', 'dy', 'dz'])}} index=index)}}
agent.mode_of_motion = mode_move agent.mode_of_motion = mode_move
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True) as w:
agent.move() agent.move()
...@@ -119,10 +124,15 @@ class TestNavipyMovingAgent(unittest.TestCase): ...@@ -119,10 +124,15 @@ class TestNavipyMovingAgent(unittest.TestCase):
agent.posorient = initposovel agent.posorient = initposovel
with self.assertRaises(AttributeError): with self.assertRaises(AttributeError):
agent.fly(max_nstep=10) agent.fly(max_nstep=10)
tuples = [('location', 'dx'), ('location', 'dy'),
('location', 'dz')]
index = pd.MultiIndex.from_tuples(tuples,
names=['position',
'orientation'])
mode_move = {'mode': 'on_cubic_grid', mode_move = {'mode': 'on_cubic_grid',
'param': {'grid_spacing': 'param': {'grid_spacing':
pd.Series(data=1, pd.Series(data=1,
index=['dx', 'dy', 'dz'])}} index=index)}}
agent.mode_of_motion = mode_move agent.mode_of_motion = mode_move
agent.fly(max_nstep=10) agent.fly(max_nstep=10)
obtained = agent.posorient obtained = agent.posorient
......
...@@ -26,8 +26,14 @@ class TestNavipyMovingMaths(unittest.TestCase): ...@@ -26,8 +26,14 @@ class TestNavipyMovingMaths(unittest.TestCase):
def test_notsupported_mofm(self): def test_notsupported_mofm(self):
""" Test that a TypeError is correctly raised """ Test that a TypeError is correctly raised
""" """
tuples = [('location', 'x'), ('location', 'y'),
('location', 'z'), ('location', 'dx'),
('location', 'dy'), ('location', 'dz')]
index = pd.MultiIndex.from_tuples(tuples,
names=['position',
'orientation'])
motion_vec = pd.Series(data=0, motion_vec = pd.Series(data=0,
index=['x', 'y', 'z', 'dx', 'dy', 'dz']) index=index)
move_mode = 'NotSupportedMode' move_mode = 'NotSupportedMode'
mode_param = dict() mode_param = dict()
mode_param['grid_spacing'] = 1 mode_param['grid_spacing'] = 1
...@@ -44,8 +50,14 @@ class TestNavipyMovingMaths(unittest.TestCase): ...@@ -44,8 +50,14 @@ class TestNavipyMovingMaths(unittest.TestCase):
test this by series equality. test this by series equality.
""" """
# Test if stay at same position. # Test if stay at same position.
tuples = [('location', 'x'), ('location', 'y'),
('location', 'z'), ('location', 'dx'),
('location', 'dy'), ('location', 'dz')]
index = pd.MultiIndex.from_tuples(tuples,
names=['position',
'orientation'])
motion_vec = pd.Series(data=0, motion_vec = pd.Series(data=0,
index=['x', 'y', 'z', 'dx', 'dy', 'dz']) index=index)
move_mode = 'on_cubic_grid' move_mode = 'on_cubic_grid'
mode_param = dict() mode_param = dict()
mode_param['grid_spacing'] = 1 mode_param['grid_spacing'] = 1
...@@ -61,14 +73,30 @@ class TestNavipyMovingMaths(unittest.TestCase): ...@@ -61,14 +73,30 @@ class TestNavipyMovingMaths(unittest.TestCase):
22.5 degrees modulo 45 degrees. We therefore test the functions 22.5 degrees modulo 45 degrees. We therefore test the functions
close to each instability. close to each instability.
""" """
positions = pd.DataFrame({'x': [0, 0, 0, 1, 1, 1, 2, 2, 2], tuples = [('location', 'x'), ('location', 'y'),
'y': [0, 1, 2, 0, 1, 2, 0, 1, 2], ('location', 'z')]
'z': [0, 0, 0, 0, 0, 0, 0, 0, 0]}, index = pd.MultiIndex.from_tuples(tuples,
names=['position',
'orientation'])
positions = pd.DataFrame(data=[[0, 0, 0],
[0, 1, 0],
[0, 2, 0],
[1, 0, 0],
[1, 1, 0],
[1, 2, 0],
[2, 0, 0],
[2, 1, 0],
[2, 2, 0]],
columns=index,
dtype=np.float) dtype=np.float)
move_mode = 'on_cubic_grid' move_mode = 'on_cubic_grid'
move_param = dict() move_param = dict()
tuples = [('location', 'dx'), ('location', 'dy'),
('location', 'dz')]
index = pd.MultiIndex.from_tuples(tuples,
names=['position', 'orientation'])
move_param['grid_spacing'] = pd.Series(data=1, move_param['grid_spacing'] = pd.Series(data=1,
index=['dx', 'dy', 'dz']) index=index)
expected_dict = dict() expected_dict = dict()
expected_dict[-22] = 7 # [2,1] expected_dict[-22] = 7 # [2,1]
expected_dict[22] = 7 expected_dict[22] = 7
...@@ -87,14 +115,18 @@ class TestNavipyMovingMaths(unittest.TestCase): ...@@ -87,14 +115,18 @@ class TestNavipyMovingMaths(unittest.TestCase):
expected_dict[293] = 6 # [2, 0] expected_dict[293] = 6 # [2, 0]
expected_dict[337] = 6 expected_dict[337] = 6
expected_dict[338] = 7 # equivalent to -22 expected_dict[338] = 7 # equivalent to -22
tuples = [('location', 'dx'), ('location', 'dy'),
('location', 'dz'), ('location', 'x'),
('location', 'y'), ('location', 'z')]
index = pd.MultiIndex.from_tuples(tuples,
names=['position', 'orientation'])
for angle, exp_i in expected_dict.items(): for angle, exp_i in expected_dict.items():
alpha = np.deg2rad(angle) alpha = np.deg2rad(angle)
motion_vec = pd.Series( motion_vec = pd.Series(
data=[1, 1, 0, data=[1, 1, 0,
np.cos(alpha), np.sin(alpha), 0], np.cos(alpha), np.sin(alpha), 0],
index=['x', 'y', 'z', index=index,
'dx', 'dy', 'dz'],
dtype=np.float) dtype=np.float)
newpos = navimaths.next_pos(motion_vec, newpos = navimaths.next_pos(motion_vec,
move_mode, move_mode,
......
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