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

Add tutorials ipyynb

parent bbdb7396
No related branches found
No related tags found
No related merge requests found
......@@ -19,9 +19,7 @@
#
import os
import sys
sys.path.insert(0, os.path.abspath('../../src/'))
sys.path.insert(0, os.path.abspath('../../src/database/'))
sys.path.insert(0, os.path.abspath('../../tutorials'))
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
......
%% Cell type:markdown id: tags:
# Building an arena
## Check list.
1. Design of the arena (for example in blender)
2. Generating patterns
3. Setting up cameras
* Calibrating camera intrinsics
* Calibrating camera extrinsics
* Validating camera calibration
%% Cell type:markdown id: tags:
## Design the arena
%% Cell type:markdown id: tags:
## Generetting random patterns
### rectangular
%% Cell type:code id: tags:
``` python
from navipy.arenatools.patterns import rectangular_pattern, gray2red, norm_img
import matplotlib.pyplot as plt
from matplotlib.image import imsave
length = 1000 # mm
width = 250 # mm
beta = 1.4
pixel_per_mm = 1
pattern = rectangular_pattern(width, length, beta, pixel_per_mm)
pattern=norm_img(pattern)
pattern=gray2red(pattern)
plt.imshow(pattern)
imsave('rectangular_pattern.png', pattern)
```
%% Output
%% Cell type:markdown id: tags:
### pattern uses and convertions
The image saved as a png image can be used in blender for example to cover the objects with pattern.
A printer shop or facility would like to have a pdf of the image to print. We, thus, need to convert the png to a pdf. To obtain the correct dimension, we need to specify to the png to pdf converter the density, i.e. the number of pixel per mm (often pixel per inch). In linux, you can use the convert command:
```bash
convert pathtoimage.png -density mydensity pathtoimage.pdf
```
here, ```pathtoimage``` has to be replaced by the path to your image to be converted, and ```mydensity``` the number of pixel per inch. In our example we used 1 pixel per mm, and thus 25.4 pixel per inch.
%% Cell type:markdown id: tags:
# Recording animal trajectory
## Conversion to navipy trajectories
### From Matlab to navipy
%% Cell type:code id: tags:
``` python
from scipy.io import loadmat
import numpy as np
import os
from navipy.tools.trajectory import Trajectory
import pkg_resources
# Use the trafile from the resources
# You can adapt this code, by changing trajfile
# with your own trajectory file
trajfile = pkg_resources.resource_filename(
'navipy',
'resources/sample_experiment/Lobecke_JEB_2018/Y101_OBFlight_0001.mat')
csvtrajfile, _ = os.path.splitext(trajfile)
csvtrajfile = csvtrajfile+'.csv'
mymat = loadmat(trajfile)
# matlab files are loaded in a dictionary
# we need to identify, under which key the trajectory has been saved
print(mymat.keys())
key = 'trajectory'
# Arrays are placed in a tupple. We need to access the level
# of the array itself.
mymat = mymat[key][0][0][0]
# The array should be a numpy array, and therefore as the .shape function
# to display the array size:
print(mymat.shape)
# In this example the array has 7661 rows and 4 columns
# the columns are: x,y,z, yaw. Therefore pitch and roll were assumed to be constant (here null)
# We can therefore init a trajectory with the convention for rotation
# often yaw-pitch-roll (i.e. 'rzyx') and with indeces the number of sampling points we have
# in the trajectory
rotconvention = 'rzyx'
indeces = np.arange(0,mymat.shape[0])
mytraj = Trajectory(rotconv = rotconvention, indeces=indeces)
# We now can assign the values
mytraj.x = mymat[:,0]
mytraj.y = mymat[:,1]
mytraj.z = mymat[:,2]
mytraj.alpha_0 = mymat[:,3]
mytraj.alpha_1 = 0
mytraj.alpha_2 = 0
# We can then save mytraj as csv file for example
mytraj.to_csv(csvtrajfile)
mytraj.head()
```
%% Output
dict_keys(['__header__', '__version__', '__globals__', 'trajectory', 'nests', 'cylinders'])
(7661, 4)
location rzyx
x y z alpha_0 alpha_1 alpha_2
0 3.056519 -214.990482 9.330593 2.79751 0 0
1 4.611665 -215.020314 8.424138 2.80863 0 0
2 4.556650 -214.593236 9.185016 2.81407 0 0
3 4.643091 -213.829769 10.542035 2.82704 0 0
4 4.647302 -214.431592 7.461187 2.82896 0 0
%% Cell type:markdown id: tags:
### From csv to navipy
Navipy can read csv files with 7 columns and a two line headers
<table>
<tr>
<td>location</td>
<td>location</td>
<td>location</td>
<td>rzyx</td>
<td>rzyx</td>
<td>rzyx</td>
</tr>
<tr>
<td>x</td>
<td>y</td>
<td>z</td>
<td>alpha_0</td>
<td>alpha_1</td>
<td>alpha_2</td>
</tr>
</table>
But you may have csv file with a different format.
Here we are going to show, how to convert your csv file format to the one required by navipy
%% Cell type:markdown id: tags:
# Recording along a trajectory
In this tutorial, you will learn to take advantage of navipy and the rendering modules (blender) to render what has been seen by the animal along its trajectory.
To render along a trajectory, you will need to have:
* the environment (a blender file)
* the trajectory formatted for navipy (see 02-recording-animal-trajectory)
## Checking the position of the animal within the environment
Rendering a trajectory will take time, and thus we want to check - prior to rendering - the correctness of the animal within the environment. The best way to check, is to overlay the trajectory within the blender world, and by rotating the environment, one could look for: part of the trajectory crossing objects, a wrong height, etc...
To overlay the trajectory within your blender environment you can use the following command:
```bash
blendoverlaytraj --blenderworld='pathtomyworld.blend'
--trajectory='pathtomytrajectory.csv'
```
here ```pathtomyworld.blend``` and ```'pathtomytrajectory.csv'``` are the path to your blender environment and your trajectory respectively.
> The overlayed trajectory will not be rendered, because it is a simple line and its rendering is disable. If you want
> to render the trajectory, you can bevel the trajectory with a circle for example. It will create a extrude the circle
> along the trajectory and thus creating a 3D shaped. (Don't forget to enable rendering)
## Rendering the trajectory in a database
Once we know that the trajectory is correctly placed within the environment, it is time to render along the trajectory. We, however, recommand to render only the first view frame of your trajectory first in order to check for the orientation. Indeed we only checked the position of the animal, but not its orientation. Navipy supports all 24 Euler convention, and also quaternion. You need to figure out which one you used. Sadly we did not find until now an easy way to do it... Having said that, if you "only" tracked the yaw of the animal, you can safely use the 'rzyx' convention, here alpha_0 of your trajectory correspond to the yaw of the animal and all other angles are set to 0.
```bash
blendalongtraj --output-file='pathtodatabase.db'
--blenderworld='pathtomyworld.blend'
--trajectory='pathtomytrajectory.csv'
```
here ```pathtomyworld.blend```, ```'pathtomytrajectory.csv'```, ```pathtodatabase.db``` are the path to your blender environment, to your trajectory, and to the file to store the iamges respectively.
%% Cell type:markdown id: tags:
## Database to list of images
The database store all position-orientation and images along the trajectory in a single file. On the one hand, it is convenient, because you always know at which position and in which orientation an image has been rendered. On the other hand, we can not easily visualise the images.
To convert the database into an image sequence or list, you can run the following script in a ipython notebook (Don't forget to change the variable ```database``` to the path of your trajectory
%% Cell type:code id: tags:
``` python
# Load the necessary modules
from navipy.database import DataBaseLoad
from matplotlib.image import imsave
import numpy as np
import os
import matplotlib.pyplot as plt
# Load the database, and specify the
# the output directory to save the list
# of images
import pkg_resources
# Use the trafile from the resources
# You can adapt this code, by changing trajfile
# with your own trajectory file
database = pkg_resources.resource_filename(
'navipy',
'resources/database.db')
database_dir, _ = os.path.splitext(database)
if not os.path.exists(database_dir):
os.makedirs(database_dir)
database_template = os.path.join(database_dir, 'frame_{}.png')
mydb = DataBaseLoad(database)
for rowid in mydb.posorients.index:
my_scene = mydb.scene(rowid=rowid)
to_plot_im = my_scene[:, :, :3, 0]
to_plot_im -= to_plot_im.min()
to_plot_im /= to_plot_im.max()
to_plot_im = to_plot_im * 255
to_plot_im = to_plot_im.astype(np.uint8)
to_plot_dist = my_scene[:, :, 3, 0]
imsave(database_template.format(rowid), to_plot_im[::-1,...] )
```
%% Cell type:markdown id: tags:
## Plotting an image from the database
%% Cell type:code id: tags:
``` python
%matplotlib inline
my_scene = mydb.scene(rowid=2)
f, axarr = plt.subplots(1, 2, figsize=(15, 4))
to_plot_im = my_scene[:, :, :3, 0].astype(float)
to_plot_im -= to_plot_im.min()
to_plot_im /= to_plot_im.max()
to_plot_im = to_plot_im * 255
to_plot_im = to_plot_im.astype(np.uint8)
to_plot_dist = my_scene[:, :, 3, 0]
ax = axarr[0]
ax.imshow(to_plot_im)
ax.invert_yaxis()
ax = axarr[1]
ax.imshow(to_plot_dist)
ax.invert_yaxis()
f.show()
```
%% Output
/home/bolirev/.virtualenv/toolbox-navigation/lib/python3.6/site-packages/matplotlib-2.1.0-py3.6-linux-x86_64.egg/matplotlib/figure.py:418: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
"matplotlib is currently using a non-GUI backend, "
......@@ -3,6 +3,8 @@
(called navipy)
"""
from setuptools import setup, find_packages
import glob
import os
excluded = []
......@@ -22,6 +24,18 @@ def create_package_list(base_package):
if not exclude_package(pkg)])
def package_data_files(base_package):
os.chdir(base_package)
filelist = glob.glob(os.path.join('resources',
'*'))
filelist.extend(glob.glob(os.path.join('resources',
'**', '*'),
recursive=True))
os.chdir('../')
print(filelist)
return filelist
setup_dict = {'name': 'navipy',
'version': '0.1',
'author': "Olivier J.N. Bertrand",
......@@ -50,10 +64,7 @@ setup_dict = {'name': 'navipy',
'Pillow',
'tables'],
'package_data': {'navipy':
['resources/*.db',
'resources/*.blend',
'resources/*.csv',
'resources/configs/*.yaml']},
package_data_files("navipy")},
'include_package_data': True,
'entry_points': {
'console_scripts': [
......
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