Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong coordinates for electrophysiology #83

Closed
AliceBS opened this issue Feb 13, 2023 · 9 comments
Closed

Wrong coordinates for electrophysiology #83

AliceBS opened this issue Feb 13, 2023 · 9 comments

Comments

@AliceBS
Copy link

AliceBS commented Feb 13, 2023

Hi,

Thanks for designing this tool. I have been trying to use it with probes aligned on allenCCF SHARP-Track (https://github.com/cortex-lab/allenCCF/wiki) and I cannot seem to be able to get the correct coordinates.

I have my data as AllenCCF coordinates, in ML/AP/DV order, with a resolution of 10um and I am using this code to transfer them to bregma coordinates

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

res = 10
brain_atlas = atlas.AllenAtlas(res)

with open(r'pathToPoints\probe_points_session.csv', newline='') as csvfile:
    probePoints = list(csv.reader(csvfile))
ccf_mlapdv = np.array(probePoints, dtype=np.float64)
bregma_mlapdv = brain_atlas.ccf2xyz(ccf_mlapdv, ccf_order='mlapdv')

xyz_picks = {'xyz_picks': bregma_mlapdv.tolist()}

output_path = Path(r'myPath\int-brain-lab\exp_data')
with open(Path(output_path, 'xyz_picks.json'), "w") as f:
    json.dump(xyz_picks, f, indent=2)

It works fine but when I get to the data, the electrode is outside of the brain:

EphysAtlasBug

This seems to be the right angle for our probe but not the right origin point, in all directions.
I thought that the problem might be the bregma coordinates, as in the allenCCF SHARP-Track tool the bregma coordinates are 570/540/0 in ML/AP/DV. I tried solving that by adding an offset in the DV dimension but it did not change much.
I also tried multiplicating the ccf_mlapdv matrix by 1e6 as I saw for the lasagna tool, it improves the location but it still does not give the right coordinate and I am not sure if it is correct for my data. Do you have an idea of what could be going wrong?

Thanks,
Alice

@mayofaulkner
Copy link
Contributor

Hello,

Would you mind attaching the output that you get from SHARP track so I can play around and see what conversion might me needed?

Many thanks,
Mayo

@AliceBS
Copy link
Author

AliceBS commented Feb 14, 2023

Of course. The original output is a matlab file containing multiple probes, I just wrote the matrix for this probe to CSV and changed the column order to put the coordinates in ML/AP/DV to get this file.
probe_points_session.csv

Thanks!

@mayofaulkner
Copy link
Contributor

Hello, so I have figured out the issue. The first is that the coordinates given out by SHARP track are in pixels so need to be multiplied by 10 to get them into um. The second is that the ccf2xyz gives out coordinates relative to bregma in metres but the GUI expects um. If you run the following code you should get the electrode track in the correct position

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

res = 10
brain_atlas = atlas.AllenAtlas(res)

with open(r'pathToPoints\probe_points_session.csv', newline='') as csvfile:
    probePoints = list(csv.reader(csvfile))
ccf_mlapdv = np.array(probePoints, dtype=np.float64) * res
bregma_mlapdv = brain_atlas.ccf2xyz(ccf_mlapdv, ccf_order='mlapdv') * 1e6

xyz_picks = {'xyz_picks': bregma_mlapdv.tolist()}

output_path = Path(r'myPath\int-brain-lab\exp_data')
with open(Path(output_path, 'xyz_picks.json'), "w") as f:
    json.dump(xyz_picks, f, indent=2)

It appears though that your tracked trace has many discontinuities, perhaps when scrolling through the slices. I would advise making sure these are smoothed out before aligning.
image

Let me know if that helps :)

Mayo

@AliceBS
Copy link
Author

AliceBS commented Feb 15, 2023

Hi,

Thank you very much, it works now and solves everything.
I just have to take the points from the aligned probe rather than the ones I have and I should be able to use it.

Thanks,
Alice

@mayofaulkner
Copy link
Contributor

Great! The aligned results of the GUI are interpolated along the original traced track that is input, so if this is kinked then the aligned version will also be.

@AliceBS
Copy link
Author

AliceBS commented Mar 14, 2023

Thanks, with the smoothing it works nicely.

@Virginia9733
Copy link

Hi there, I am trying to convert the probe tracking from AP_histology, and saved a NPY file for each probe.

%%%%%% Code from Andrew Peters %%%%%%%%%%%%%%
% Convert probe_ccf.mat into .npy for IBL ephys alignment
% Pick filename
probe_ccf = load('D:\repos\AP_histology\sample slides\20240326-NPX2.0-Chronic-CA1-DiI\Save Slices\probe_ccf.mat')
probe_ccf = probe_ccf.probe_ccf
coords = probe_ccf.trajectory_coords
%filename = uiputfile('probe_ccf.npy');
filename = 'D:\repos\AP_histology\sample slides\20240326-NPX2.0-Chronic-CA1-DiI\Save Slices\probe_ccf.npy';
% Write probe_ccf coordinates as NPY file
writeNPY(coords,filename)

% Convert probe_ccf.mat into .npy for IBL ephys alignment for each probe
% 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

After exporting the coordinates for each probe (each shank of NPX2.0), I tried to convert the coordinate in ML/AP/DV, but has some error messages:

(iblenv) d:\repos>python
Python 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:40:08) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import ibllib.atlas as atlas
C:\Users\muhang\miniconda3\envs\iblenv\lib\site-packages\ibllib\atlas_init_.py:205: DeprecationWarning: ibllib.atlas is deprecated. Please install iblatlas using "pip install iblatlas" and use this module instead
warnings.warn('ibllib.atlas is deprecated. Please install iblatlas using "pip install iblatlas" and use '
quit()
(iblenv) d:\repos>python
Python 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:40:08) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(iblenv) d:\repos>python
Python 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:40:08) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import iblatlas
import numpy as np
from pathlib import Path
import json
res = 10
brain_atlas = atlas.AllenAtlas(res)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'atlas' is not defined
brain_atlas = iblatlas.AllenAtlas(res)
Traceback (most recent call last):
File "", line 1, in
AttributeError: module 'iblatlas' has no attribute 'AllenAtlas'

I cannot upload npy file so I converted into .csv
probe_ccf.npy_probe1.csv

@Virginia9733
Copy link

Also, may I ask does iblapp supports slide scanning images, especially I only registered a few key slices which have the track of the probes using AP_histology.
8
9
10
11

@mayofaulkner
Copy link
Contributor

Hello,

Looking at the script you have written above it looks like the import that you have is incorrect. Can you change import iblatlas to from iblatlas.atlas import AllenAtlas. Hopefully that should work, but let me know if not.

For the second question, at the moment the app only supports a full histology volume, is the idea that you would like to be able to just show one specific slice instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants