示例#1
0
def generate_spinning_brain_frames(trajectories, nframes_per_cycle=30, figsize=[800, 700]):
    '''
    Plots spike amps versus the time

    Parameters
    -------------
    trajectories: list of dicts
        list of dictionaries of trajectories fetched from ONE
    nframes_per_cycle: int, optional
        number of frames per cycle of spinning, default 30.
    figsize: list, [width, height], optional
        size of the images, default [800, 700]

    Return
    ------------
    frames: list of numpy.darray
        image in rgb for each frame
    '''

    fig = rendering.figure()
    for index, trj in enumerate(trajectories):
        if trj['coordinate_system'] == 'IBL-Allen':
            brain_atlas = ba_allen
        elif trj['coordinate_system'] == 'Needles-Allen':
            brain_atlas = ba_needles
        else:
            brain_atlas = ba_allen
        ins = atlas.Insertion.from_dict(trj, brain_atlas=brain_atlas)
        ins = atlas.Insertion.from_dict(trj, brain_atlas=ba_allen)
        mlapdv = brain_atlas.xyz2ccf(ins.xyz)
        if trj['provenance'] == 'Ephys aligned histology track':
            color = (0, 0, 1.)  # Blue
            mlab.plot3d(mlapdv[:, 1], mlapdv[:, 2], mlapdv[:, 0],
                        line_width=3, color=color, tube_radius=20)
    frames = []
    for i in range(nframes_per_cycle):
        mlab.view(azimuth=0, elevation=0 - i * (360 / nframes_per_cycle))
        mlab.roll(180)
        mlab.test_plot3d()
        f = mlab.gcf()
        f.scene._lift()
        frames.append(mlab.screenshot(mode='rgb', antialiased=True))

    return frames
#%gui qt
import numpy as np
from mayavi import mlab
from one.api import ONE
from iblutil.util import Bunch

import ibllib.plots
from atlaselectrophysiology import rendering
import ibllib.atlas as atlas

one = ONE(base_url='https://alyx.internationalbrainlab.org')
subjects = [
    'dop_24', 'dop_14', 'dop_13', 'dop_16', 'dop_21', 'dop_22', 'dop_36'
]

fig = rendering.figure()

for subject in subjects:
    ba = atlas.AllenAtlas(25)
    channels_rest = one.alyx.rest('channels', 'list', subject=subject)
    channels = Bunch({
        'atlas_id':
        np.array([ch['brain_region'] for ch in channels_rest]),
        'xyz':
        np.c_[np.array([ch['x'] for ch in channels_rest]),
              np.array([ch['y'] for ch in channels_rest]),
              np.array([ch['z'] for ch in channels_rest])] / 1e6,
        'axial_um':
        np.array([ch['axial'] for ch in channels_rest]),
        'lateral_um':
        np.array([ch['lateral'] for ch in channels_rest]),
                             'list',
                             session=eid,
                             provenance='Histology track',
                             probe=probe_label)[0]
        ses = one.alyx.rest('sessions', 'read', id=eid)
        channels = bbone.load_channel_locations(eid=ses,
                                                one=one,
                                                probe=probe_label)[probe_label]
        insertion = atlas.Insertion.from_dict(traj)
        ins.insertion.append(insertion)
        ins.channels.append(channels)
        ins.session.append(ses)
    pickle.dump(ins, open(file_pickle, 'wb'))

# Display part
fig = rendering.figure(grid=False)  # set grid=True for ugly axes
for m in np.arange(len(ins.eid)):
    print(ins.session[m]['subject'], ins.session[m]['start_time'][:10],
          ins.session[m]['number'], ins.probe_label[m])
    color = ibllib.plots.color_cycle(m)
    mlapdv = brain_atlas.xyz2ccf(ins.insertion[m].xyz)
    # display the trajectories
    mlab.plot3d(mlapdv[:, 1],
                mlapdv[:, 2],
                mlapdv[:, 0],
                line_width=1,
                tube_radius=10,
                color=color)
    xyz_channels = np.c_[ins.channels[m].x, ins.channels[m].y,
                         ins.channels[m].z]
    mlapdv_channels = brain_atlas.xyz2ccf(xyz_channels)
示例#4
0
traj_coords = np.empty((len(traj_ids), len(depths), 3))

# For each trajectory compute the xyz coords at positions depths along trajectory
for iT, traj in enumerate(trajectories):
    traj_coords[iT, :] = histology.interpolate_along_track(np.vstack([traj.tip, traj.entry]),
                                                           depths)

# Find the average distance between all positions compared to trjaectory of interest
avg_dist = np.mean(np.sqrt(np.sum((traj_coords - traj_coords[chosen_traj]) ** 2, axis=2)), axis=1)

# Sort according to those that are closest
closest_traj = np.argsort(avg_dist)

close_sessions = []
# Make a 3D plot showing trajectory of interest (in black) and the 10 nearest trajectories (blue)
fig = rendering.figure(grid=False)
for iSess, sess_idx in enumerate(closest_traj[0:10]):

    mlapdv = brain_atlas.xyz2ccf(traj_coords[sess_idx])
    if iSess == 0:
        mlab.plot3d(mlapdv[:, 1], mlapdv[:, 2], mlapdv[:, 0],
                    line_width=1, tube_radius=10, color=(0, 0, 0))
    else:
        mlab.plot3d(mlapdv[:, 1], mlapdv[:, 2], mlapdv[:, 0],
                    line_width=1, tube_radius=10, color=(0.0, 0.4, 0.5))

    mlab.text3d(mlapdv[0, 1], mlapdv[0, 2], mlapdv[0, 0], str(iSess),
                line_width=4, color=(0, 0, 0), figure=fig, scale=150)

    close_sessions.append((sess_with_hist[sess_idx]['session']['subject'] + ' ' +
                           sess_with_hist[sess_idx]['session']['start_time'][:10] +
                mlapdv[0, 2],
                mlapdv[0, 0] - 500,
                label,
                line_width=4,
                color=color,
                figure=fig_handle,
                scale=500)
    return mlapdv, ins


ba = atlas.AllenAtlas(25)
prob_des = one.load_dataset(eid, 'probes.description.json')
n_probe = len(prob_des[0])

# Plot empty atlas template
fig3D = rendering.figure()

# Loop over probes
for i_probe in range(0, n_probe):
    # Get single probe trajectory
    probe_label = prob_des[0][i_probe].get('label')

    # Histology (red)
    traj_h = one.alyx.rest('trajectories',
                           'list',
                           session=eid,
                           provenance='Histology track',
                           probe=probe_label)[0]
    color_h = (1., 0., 0.)

    # Planned (blue)