# Author: Olivier
# environment installation guide https://github.com/int-brain-lab/iblenv
# run "%qui qt" magic command from Ipython prompt for interactive mode

import pandas as pd
from mayavi import mlab
from atlaselectrophysiology import rendering

import ibllib.atlas as atlas

# the csv file is available here:
# https://github.com/int-brain-lab/ibllib-matlab/blob/master/needles/maps/first_pass_map.csv
output_video = '/home/olivier/Videos/first_pass.webm'
csv_file = "/home/olivier/Documents/MATLAB/ibllib-matlab/needles/maps/first_pass_map.csv"

# start of the code
brain_atlas = atlas.AllenAtlas(25)
brain_atlas = atlas.NeedlesAtlas(25)

df_map = pd.read_csv(csv_file)

fig = rendering.figure()

plt_trj = []
for index, rec in df_map.iterrows():
    ins = atlas.Insertion.from_dict({'x': rec.ml_um, 'y': rec.ap_um, 'z': rec.dv_um,
                                     'phi': rec.phi, 'theta': rec.theta, 'depth': rec.depth_um})
    mlapdv = brain_atlas.xyz2ccf(ins.xyz)
    plt = mlab.plot3d(mlapdv[:, 1], mlapdv[:, 2], mlapdv[:, 0],
                      line_width=3, color=(1, .6, .6), tube_radius=15)
    plt_trj.append(plt)

##
rendering.rotating_video(output_video, fig, fps=24, secs=16)
