WhiskiWrap provides tools for running whiski (http://whiskertracking.janelia.org) more easily and efficiently.
My goal is to improve whiski in the following ways:
- Make it more flexible about reading various input files. In my experience whiski has trouble reading certain input videos. Instead, WhiskiWrap uses your system's ffmpeg to read input files (because ffmpeg can typically read almost anything) and to generate simple tiff stacks which whiski can reliably read.
- Make it faster, by calling many instances of
trace
in parallel on non-overlapping chunks of the input video. - Make it more cross-platform and memory-efficient, by converting whiski's output files into HDF5 files which can be read by multiple programs (Python, Matlab) on any operating system. Importantly, HDF5 files can also be read partially to avoid overflowing your system's memory.
Note: The current best pipeline for running trace is interleaved_reading_and_trace
, but the command pipeline_trace
is the only one configured to run measure.
First start the interactive python environment by typing ipython at the terminal.
Import WhiskiWrap so it can be used:
import WhiskiWrap
Set the path to the input file. There are some test videos in this repository that you can use. Usually it's best to copy this file to a new directory, because a lot of temporary files will be created in the same directory.
mkdir ~/whiski_wrap_session
cp ~/dev/WhiskiWrap/test_video2.mp4 ~/whiski_wrap_session/test_video2.mp4
input_video = '~/whiski_wrap_session/test_video2.mp4'
Choose where you want the output HDF5 file to be.
output_file = 'output.hdf5'
Run the trace. Here we use 4 parallel processes.
WhiskiWrap.pipeline_trace(input_video, output_file, n_trace_processes=4)
If you go to the session directory, you'll see a bunch of tiff stacks and whiskers files that were generated by every instance of trace. There's also a combined HDF5 file with all the data combined. You can read it into Python like so:
import tables
import pandas
with tables.open_file(output_file) as fi:`
test_result = pandas.DataFrame.from_records(
fi.root.summary.read())
This just reads the "summary": the tip and follicle of every whisker in every frame. The HDF5 file also contains the x- and y-coordinates of every pixel in every whisker, but you probably don't want to read all of this in at once.
- Split the entire video into epochs of about 100K frames (~100MB of data). The entire epoch will be read into memory, so the epoch size cannot be too big.
- For each epoch:
- Split it into chunks of about 1000 frames, each of which will be traced separately. The frames can optionally be cropped at this point.
- Write each chunk to disk as a tiff stack (note: these files are quite large).
- Trace each chunk with parallel instances of
trace
. Awhiskers
file is generated for each chunk. - Parse in order each chunk's
whiskers
file and append the results to an output HDF5 file. - (Optional) delete the intermediate chunk files here.
The following parameters must be chosen:
n_trace_processes
- the number of parallel instances oftrace
to run at the same time. The most efficient choice is the number of CPUs on your system.epoch_sz_frames
- the number of frames per epoch. It is most efficient to make this value as large as possible. However, it should not be so large that you run out of memory when reading in the entire epoch of video. 100000 is a reasonable choice.chunk_sz_frames
- the size of each chunk. Ideally, this should beepoch_size
/n_trace_processes
, so that all the processes complete at about the same time. It could also beepoch_size
/ (N *n_trace_processes
) where N is an integer.
You may also add optional parameters to run the measure command
measure=True
- run measure command, default is Falseface='right'
- run measure with face on right side, can also specify to 'left' side
WhiskiWrap is written in Python and relies on ffmpeg
for reading input videos, tifffile
for writing tiff stacks, whiski
for tracing whiskers in the tiff stacks, and pytables
for creating HDF5 files with all of the results.
First install ffmpeg
and ensure it is available on your system path -- that is, you should be able to type ffmpeg
in the terminal and it should find it and run it.
Next install whiski
. There are several ways to do this:
- Download the pre-built binary. This is the easiest path because it doesn't require compiling anything. However, you still need to make a few changes to the Python code that is downloaded in order to make it work with
WhiskiWrap
. - Build
whiski
from source, using my lightly customized fork. This will probably require more trouble-shooting to make sure all of its parts are working.
To use the pre-built binary (preferred):
- Download the zipped binary and unpack it or get the file whisk-1.1.0d-64bit-Linux.tar.gz from someone. Unpack with
tar -xzf whisk-1.1.0d-64bit-Linux.tar.gz
. Rename the unpacked directory to~/dev/whisk
- Add the binaries to your system path so that you can run
trace
from the command line. - Add a few files to make
whiski
's Python code work more nicely with other packages. (Technically, we need to make it a module, and avoid name collisions with the unrelated built-in moduletrace
.) touch ~/dev/whisk/share/whisk/__init__.py
touch ~/dev/whisk/share/whisk/python/__init__.py
- Add these modules to your Python path.
ln -s ~/dev/whisk/share/whisk/python ~/dev/whisk/python
- or
echo "~/whisk/share" >> "~/.local/lib/python2.7/site-packages/whiski_wrap.pth
- Test that everything worked by opening python or ipython and running
from whisk.python import traj, trace
To build from source:
- Install required dependencies (gl.h, etc)
- Download the source from my lightly modified fork, which makes the
__init__
changes described above. cd ~/dev
git clone https://github.com/cxrodgers/whisk.git
cd whisk
mkdir build
cmake ..
make
- Copy a library into an expected location:
cp ~/dev/whisk/build/libwhisk.so ~/dev/whisk/python
- Test that everything worked by opening python or ipython and running
from whisk.python import traj, trace
Here I outline the use of conda
to manage and install Python modules. In the long run this is the easiest way. Unfortunately it doesn't work well with user-level pip
. Specifically, you should not have anything on your $PYTHONPATH
, and there shouldn't be any installed modules in your ~/.local
.
- Clone my into ~/dev for video processing functions.
cd ~/dev
git clone https://github.com/cxrodgers/my.git
0.5. Install scipy.
conda install scipy
- Create a new conda environment for WhiskiWrap.
conda create -n whiski_wrap python=2.7 pip numpy matplotlib pyqt pytables pandas ipython
2. Activate that environment and install tifffile
source activate whiski_wrap
pip install tifffile
If source activate whiski_wrap
doesn't work, try conda activate whiski_wrap
- Clone WhiskiWrap
cd ~/dev
git clone https://github.com/cxrodgers/WhiskiWrap.git
- Make sure the development directory is on your Python path.
echo "~/dev" >> ~/.local/lib/python2.7/site-packages/whiski_wrap.pth