def make(self, key): x, y, z, axial = (ChannelBrainLocation & key).fetch( 'channel_x', 'channel_y', 'channel_z', 'channel_axial', order_by='channel_axial') xyz_channels = np.c_[x, y, z] key['region_boundaries'], key['region_label'], \ key['region_color'], key['region_id'] = \ EphysAlignment.get_histology_regions( xyz_channels.astype('float')/1e6, axial.astype('float')) self.insert1(key)
def _load_brain_regions(eid, probe_idx=0): one = ONE() ba = AllenAtlas() probe = 'probe0%d' % probe_idx ins = one.alyx.rest('insertions', 'list', session=eid, name=probe)[0] xyz_chans = load_channels_from_insertion(ins, depths=SITES_COORDINATES[:, 1], one=one, ba=ba) region, region_label, region_color, _ = EphysAlignment.get_histology_regions( xyz_chans, SITES_COORDINATES[:, 1], brain_atlas=ba) return region, region_label, region_color
def make(self, key): x, y, z = (ChannelBrainLocation & key).fetch('channel_ml', 'channel_ap', 'channel_dv') coords = (ephys.ChannelGroup & key).fetch1('channel_local_coordinates') xyz_channels = np.c_[x, y, z] key['region_boundaries'], key['region_label'], \ key['region_color'], key['region_id'] = \ EphysAlignment.get_histology_regions( xyz_channels.astype('float')/1e6, coords[:, 1]) self.insert1(key)
from ibllib.pipes.ephys_alignment import EphysAlignment from oneibl.one import ONE from brainbox.io.one import load_channel_locations import numpy as np import matplotlib.pyplot as plt one = ONE(base_url="https://alyx.internationalbrainlab.org") # Load data from 'ZM_2407' '2019-12-06' eid = '03d3cffc-755a-43db-a839-65d8359a7b93' probe = 'probe_00' channels = load_channel_locations(eid, one=one, probe=probe) # xyz coords of channels xyz_channels = np.c_[channels[probe].x, channels[probe].y, channels[probe].z] # depth along probe of channels depths = channels[probe].axial_um region, region_label, region_colour, region_id = EphysAlignment.get_histology_regions(xyz_channels, depths) fig, ax1 = plt.subplots(figsize=(4,10)) for reg, col in zip(region, region_colour): height = np.abs(reg[1]- reg[0]) bottom = reg[0] color = col/255 ax1.bar(x=0.5, height=height, width=1, color=color, bottom=reg[0], edgecolor='w') ax1.set_yticks(region_label[:, 0].astype(int)) ax1.set_yticklabels(region_label[:, 1]) ax1.hlines([0, 3840], *ax1.get_xlim(), linestyles='dashed', linewidth = 3, colors='k') plt.show()
def plot_alignment(insertion, traj, ind=None): depths = SITES_COORDINATES[:, 1] xyz_picks = np.array(insertion['json']['xyz_picks']) / 1e6 alignments = traj['json'].copy() k = list(alignments.keys())[-1] # if only I had a Walrus available ! alignments = {k: alignments[k]} # Create a figure and arrange using gridspec widths = [1, 2.5] heights = [1] * len(alignments) gs_kw = dict(width_ratios=widths, height_ratios=heights) fig, axis = plt.subplots(len(alignments), 2, constrained_layout=True, gridspec_kw=gs_kw, figsize=(8, 9)) # Iterate over all alignments for trajectory # 1. Plot brain regions that channel pass through # 2. Plot coronal slice along trajectory with location of channels shown as red points # 3. Save results for each alignment into a dict - channels channels = {} for iK, key in enumerate(alignments): # Location of reference lines used for alignmnet feature = np.array(alignments[key][0]) track = np.array(alignments[key][1]) chn_coords = SITES_COORDINATES # Instantiate EphysAlignment object ephysalign = EphysAlignment(xyz_picks, depths, track_prev=track, feature_prev=feature) # Find xyz location of all channels xyz_channels = ephysalign.get_channel_locations(feature, track) # Find brain region that each channel is located in brain_regions = ephysalign.get_brain_locations(xyz_channels) # Add extra keys to store all useful information as one bunch object brain_regions['xyz'] = xyz_channels brain_regions['lateral'] = chn_coords[:, 0] brain_regions['axial'] = chn_coords[:, 1] # Store brain regions result in channels dict with same key as in alignment channel_info = {key: brain_regions} channels.update(channel_info) # For plotting -> extract the boundaries of the brain regions, as well as CCF label and colour region, region_label, region_colour, _ = ephysalign.get_histology_regions( xyz_channels, depths) # Make plot that shows the brain regions that channels pass through ax_regions = fig.axes[iK * 2] for reg, col in zip(region, region_colour): height = np.abs(reg[1] - reg[0]) bottom = reg[0] color = col / 255 ax_regions.bar(x=0.5, height=height, width=1, color=color, bottom=reg[0], edgecolor='w') ax_regions.set_yticks(region_label[:, 0].astype(int)) ax_regions.yaxis.set_tick_params(labelsize=8) ax_regions.get_xaxis().set_visible(False) ax_regions.set_yticklabels(region_label[:, 1]) ax_regions.spines['right'].set_visible(False) ax_regions.spines['top'].set_visible(False) ax_regions.spines['bottom'].set_visible(False) ax_regions.hlines([0, 3840], *ax_regions.get_xlim(), linestyles='dashed', linewidth=3, colors='k') # ax_regions.plot(np.ones(channel_depths_track.shape), channel_depths_track, '*r') # Make plot that shows coronal slice that trajectory passes through with location of channels # shown in red ax_slice = fig.axes[iK * 2 + 1] brain_atlas.plot_tilted_slice(xyz_channels, axis=1, ax=ax_slice) ax_slice.plot(xyz_channels[:, 0] * 1e6, xyz_channels[:, 2] * 1e6, 'r*') ax_slice.title.set_text(insertion['id'] + '\n' + str(key)) # Make sure the plot displays plt.show()
def get_brain_regions(self, traj, ins=None, mapping='Allen'): depths = SITES_COORDINATES[:, 1] xyz_channels = self.get_channels(traj, ins=ins, depths=depths) (region, region_label, region_colour, _) = EphysAlignment.get_histology_regions(xyz_channels, depths, brain_atlas=self.ba, mapping=mapping) return region, region_label, region_colour # # cvol[np.unravel_index(ba._lookup(all_channels), cvol.shape)] = 1 # from ibllib.atlas import AllenAtlas # ba = AllenAtlas() # import vedo # import numpy as np # # actor = vedo.Volume(ba.image, c='bone', spacing = np.array([25]*3), mapper='smart', mode=0, alphaGradient=0.5) # plt = vedo.Plotter() # plt.add(actor) # plt.show() #from vedo import * #from ibllib.atlas import AllenAtlas #ba = AllenAtlas() #import numpy as np #import vtk #la = ba.label #la[la != 0] = 1 #vol2 = Volume(la).alpha([0, 0, 0.5]) #vol = Volume(ba.image).alpha([0, 0, 0.8]).c('bone').pickable(False) ##vol = Volume(ba.image).c('bone').pickable(False) # #plane = vtk.vtkPlane() #clipping_planes = vtk.vtkPlaneCollection() #clipping_planes.AddItem(plane) #vol2.mapper().SetClippingPlanes(clipping_planes) ## #plane.SetOrigin(vol.center() + np.array([0, -100, 0])) #plane.SetNormal(0.5, 0.866, 0) ## #sl = vol.slicePlane(origin=vol.center() + np.array([0, 100, 50]), normal=(0.5, 0.866, 0)) #s2 = vol.slicePlane(origin=vol.center(), normal=(0.5, 0, 0.866)) #s3 = vol.slicePlane(origin=vol.center() + np.array([0, -100, -50]), normal=(1, 0, 0)) # this is 30 degree in coronal ##s3 = vol.slicePlane(origin=vol.center(), normal=(0.5, 0, 0.866)) # this is 30 degree in coronal # #sl.cmap('Purples_r').lighting('off').addScalarBar(title='Slice', c='w') #s2.cmap('Blues_r').lighting('off') #s3.cmap('Greens_r').lighting('off') #def func(evt): # if not evt.actor: # return # pid = evt.actor.closestPoint(evt.picked3d, returnPointId=True) # txt = f"Probing:\n{precision(evt.actor.picked3d, 3)}\nvalue = {pid}" # sph = Sphere(evt.actor.points(pid), c='orange7').pickable(False) # vig = sph.vignette(txt, s=7, offset=(-150,15), font=2).followCamera() # plt.remove(plt.actors[-2:]).add([sph, vig]) #remove old 2 & add the new 2 # #plt = show(vol, sl, s2, s3, __doc__, axes=9, bg='k', bg2='bb', interactive=False) #plt.actors += [None, None] # 2 placeholders for [sphere, vignette] #plt.addCallback('as my mouse moves please call', func) #interactive() # #from vedo.utils import versor # # #from vedo import * # #vol = Volume(dataurl+'embryo.slc').alpha([0,0,0.5]).c('k') # #slices = [] #for i in range(4): # sl = vol.slicePlane(origin=[150,150,i*50+50], normal=(-1,0,1)) # slices.append(sl) # #amap = [0, 1, 1, 1, 1] # hide low value points giving them alpha 0 #mslices = merge(slices) # merge all slices into a single Mesh #mslices.cmap('hot_r', alpha=amap).lighting('off').addScalarBar3D() # #show(vol, mslices, __doc__, axes=1)
# Find xyz location of all channels xyz_channels = ephysalign.get_channel_locations(feature, track) # Find brain region that each channel is located in brain_regions = ephysalign.get_brain_locations(xyz_channels) # Add extra keys to store all useful information as one bunch object brain_regions['xyz'] = xyz_channels brain_regions['lateral'] = chn_coords[:, 0] brain_regions['axial'] = chn_coords[:, 1] # Store brain regions result in channels dict with same key as in alignment channel_info = {key: brain_regions} channels.update(channel_info) # For plotting -> extract the boundaries of the brain regions, as well as CCF label and colour region, region_label, region_colour, _ = ephysalign.get_histology_regions( xyz_channels, depths) channel_depths_track = (ephysalign.feature2track(depths, feature, track) - ephysalign.track_extent[0]) # Make plot that shows the brain regions that channels pass through ax_regions = fig.axes[iK * 2] for reg, col in zip(region, region_colour): height = np.abs(reg[1] - reg[0]) bottom = reg[0] color = col / 255 ax_regions.bar(x=0.5, height=height, width=1, color=color, bottom=reg[0],