petersaj / AP_histology

Histology processing
63 stars 20 forks source link

AP_align_probe_histology #17

Closed Virginia9733 closed 1 year ago

Virginia9733 commented 1 year ago

I have another question regarding mapping ephys with histology. template_depths

In the output folder of kilosort, I can't find the .npy file called template_depths. Then I searched for that it seems like needs some extra function to calculate the template_depths.

https://github.com/cortex-lab/spikes/blob/master/analysis/templatePositionsAmplitudes.m

Not sure if it is the correct way for doing so. I have lfp date from SpikeGLX, don't know how to load it as well..

Looking forward to hearing from you! Many thanks!

petersaj commented 1 year ago

Right that Kilosort doesn't output template depths, you can get them from that templatesPositionsAmplitudes code that you referenced, or here's how I do it using the Kilosort output templates:

% Get depth of each template
    % (get min-max range for each channel)
    template_chan_amp = squeeze(range(templates,2));
    % (zero-out low amplitude channels)
    template_chan_amp_thresh = max(template_chan_amp,[],2)*0.5;
    template_chan_amp_overthresh = template_chan_amp.*(template_chan_amp >= template_chan_amp_thresh);
    % (get center-of-mass on thresholded channel amplitudes)
    template_depths = sum(template_chan_amp_overthresh.*channel_positions(:,2)',2)./sum(template_chan_amp_overthresh,2);

This function isn't very clean at the moment (I align using landmarks, like lack of spikes for ventricles or correlation changes across regions), for this kind of alignment I might suggest these alternatives:

You can see that second alignment tool, and some other new alignment tools, demoed at our recent Neuropixels course (Lecture 9 here: https://www.ucl.ac.uk/neuropixels/training/2022-intro-neuropixels-course)

Virginia9733 commented 1 year ago

Hey Andy, thanks! May I ask a following up question, how do you load LFP from SpikeGLX? I saw someone raise this issue before and you said you are using OpenEphys, any suggestions for SpikeGLX?

Many thanks!

petersaj commented 1 year ago

Sorry, I've never used SpikeGLX so I don't know how to load the data! You could try the Neuropixels slack (https://app.slack.com/client/T93QUDDCM), or ping Enny van Beest at UCL for MATLAB code (https://github.com/EnnyvanBeest), or try NeuroPyxels for Python code (https://github.com/m-beau/NeuroPyxels)

Virginia9733 commented 1 year ago

Thanks a lot!!!!

Virginia9733 commented 1 year ago

Hey Andy, sorry, I have a following up question regarding how to use the output from AP_histology as the input of iblapps for mapping ephys with histology of the probe tracing.

The output of AP_histology is three .mat files: atlas2histology_tform.mat, histology_ccf.mat, probe_ccf.mat.

According to the wiki, https://github.com/int-brain-lab/iblapps/wiki/4.-Preparing-data-for-ephys-GUI, "If you have traced probe tracks that are in the Allen CCF coordinate framework, the following code snippets can be used to transform between Bregma coordinate space (origin bregma) and CCF coordinate space (origin front, top, left corner of Allen CCF)" Though they gave the codes to do so, but I am not sure what kind of file format I need to convert the .mat ?

They suggested to do Probe tracing using brainreg-segment and Lasagna, but I already have probe-tracing file from the output of AP_histology, how could I use them as the input. Sorry I am not sure if I understand their instruction clearly.

petersaj commented 1 year ago

You could convert the probe_ccf.mat to an npy file with something like this in matlab:

% Pick filename
filename = uiputfile('*.npy');
% Write probe_ccf coordinates as NPY file
writeNPY(probe_ccf.trajectory_coords,filename)

Note that I'm not totally sure what orientation they expect their array to be - probe_ccf stores it as n points x 3 coordinates, but the IBL might expect a different organization.

Then you could load that file in python and use their brain_atlas.ccf2xyz function referenced on that page (with the ap/dv/ml order - that's what probe_ccf stores since it's the native CCF order). So in their code (copied below), you would put the NPY file saved above as the brainreg path, set the output path for the json file, and then that ouput file is the xyz_picks file their GUI looks for:

import numpy as np
from pathlib import Path
import json
from ibllib.atlas import AllenAtlas

atlas = AllenAtlas(25)

brainreg_path = Path(r'C:\Users\Mayo\Downloads\brainreg\output\allen_mouse_25um\manual_segmentation\atlas_space\tracks\track_1.npy')
# Load in coordinates of track in CCF space (order - apdvml, origin - top, left, front voxel
xyz_apdvml = np.load(brainreg_path)
# Convert to IBL space (order - mlapdv, origin - bregma)
xyz_mlapdv = atlas.ccf2xyz(xyz_apdvml, ccf_order='apdvml') * 1e6
xyz_picks = {'xyz_picks': xyz_mlapdv.tolist()}

# Path to save the data (same folder as where you have all the data)
output_path = Path(r'C:/Users/Mayo/Downloads/FlatIron/SWC_023/alf')
with open(Path(output_path, 'xyz_picks.json'), "w") as f:
    json.dump(xyz_picks, f, indent=2)

Let me know if that works, would be useful if so.

Virginia9733 commented 1 year ago

Hey Andy, thanks so much! I am a bit confused then converting probe_ccf.mat to .npy I have 3 probes saved in probe_ccf.trajectory_coords And I have this error:

error-2 error-1

Error

petersaj commented 1 year ago

I see - I'm not clear on how IBL stores multiple probes, is it one xyz_picks per probe? If so, you could save the CCF points separately for each probe, e.g.:

% Pick filename
[filename, path] = uiputfile;
% Write probe_ccf coordinates as NPY file (separately for each probe)
for curr_probe = 1:length(probe_ccf)
curr_filename = sprintf('%s%s_probe%d.npy',path,filename,curr_probe)
writeNPY(probe_ccf(curr_probe).trajectory_coords,curr_filename)
end

Then you could create a xyz_picks for each of those probe files

Virginia9733 commented 1 year ago

THANK YOU SO MUCH!!! Also, you mentioned the GUI on another issue page, are you developing a GUI for the AP_histology? This is truly amazing and quite user-friendly! Also, the Neuropixels_Trajactory_Planner is fantastic! Thank you!!!

petersaj commented 1 year ago

No problem - were you successfully able to load it into the IBL application?

I haven't planned any other types of GUI - probably the GUI I was referring to was just the ones that are already there.

Virginia9733 commented 1 year ago

Not yet! Thanks for your code, now I have the .json file for probe tracing. But I have issues when converting the raw Ephys data from SpikeGLX using their codes, waiting for their responses. Will keep u posted if it works! Thanks a lot!