int-brain-lab / iblenv

Unified environment and Issue tracker for all IBL
MIT License
10 stars 10 forks source link

Issue with opening files on the IBL ENV GUI #309

Open antaramaj16 opened 2 years ago

antaramaj16 commented 2 years ago

I was able to prepare files for the IBLenv GUI but then I run into issues with loading it in and I get these error messages:

(iblenv) C:\Users\Antara\int-brain-lab\iblapps\atlaselectrophysiology>python ephys_atlas_gui.py -o True Failed to load the remote cache file Traceback (most recent call last): File "C:\Users\Antara\int-brain-lab\iblapps\atlaselectrophysiology\ephys_atlas_gui.py", line 1240, in data_button_pressed self.ephysalign = EphysAlignment(self.xyz_picks, self.chn_depths, File "C:\Users\Antara\int-brain-lab\ibllib-repo\ibllib\pipes\ephys_alignment.py", line 47, in init = self.get_histology_regions(self.xyz_samples, self.sampling_trk, self.brain_atlas) File "C:\Users\Antara\int-brain-lab\ibllib-repo\ibllib\pipes\ephys_alignment.py", line 229, in get_histology_regions region_ids = brain_atlas.get_labels(xyz_coords, mapping=mapping) File "C:\Users\Antara\int-brain-lab\ibllib-repo\ibllib\atlas\atlas.py", line 250, in get_labels regions_indices = self._get_mapping(mapping=mapping)[self.label.flat[self._lookup(xyz)]] File "C:\Users\Antara\int-brain-lab\ibllib-repo\ibllib\atlas\atlas.py", line 240, in _lookup return self._lookup_inds(self.bc.xyz2i(xyz)) File "C:\Users\Antara\int-brain-lab\ibllib-repo\ibllib\atlas\atlas.py", line 230, in _lookup_inds inds = np.ravel_multi_index(idims, self.bc.nxyz[self.xyz2dims]) File "<__array_function__ internals>", line 180, in ravel_multi_index ValueError: invalid entry in coordinates array

Could you let me know how to resolve this? Thank you!

mayofaulkner commented 2 years ago

Hi, could you briefly describe the method you used to get the xyz_picks.json file and also send me the file that you are getting this problem for.

Thanks

antaramaj16 commented 2 years ago

Here are the xyz picks: {"xyz_picks":[[-880,2550,-2770],[-1050,2430,-3270],[-850,2310,-3090],[-890,2310,-3560],[-930,2220,-3460],[-960,2220,-3860],[-870,2100,-3850],[-940,2100,-4410],[-930,1980,-4270],[-1000,1980,-4660],[-860,1860,-5060],[-940,1860,-5560],[-640,1740,-6520],[-850,1620,-6810],[-940,1620,-7300],[-810,1500,-6140],[-870,1500,-6620],[-800,1410,-7720],[-1000,1320,-7570]]}

antaramaj16 commented 2 years ago

I use the SHARP track script to get the probe points file and then I use this code to convert it into useable xyz picks files:

POINTLISTFILE = "\QNAP-AL001.dpag.ox.ac.uk\Data\AMR013\histology\probe_pointselectrodetrack.mat";

folder = dir(POINTLISTFILE); jsonOutputFolder = folder.folder;

probePoints = load(POINTLISTFILE);

numProbes = size(probePoints.pointList.pointList,1);

for p = 1:numProbes

thisProbe = probePoints.pointList.pointList{p,1}; %get first probe coords

% Convert probe coordinates (in pixels) to mm relative to bregma
ML = (thisProbe(:,1) - 570)*0.01;
DV = thisProbe(:,2)*0.01;
AP = (thisProbe(:,3) - 540)*0.01;

% Convert to um and the correct sign
ML = 1000*ML;
DV = -1000*DV;
AP = -1000*AP;

%Ouput should be ML AP DV
out = jsonencode( struct('xyz_picks', round([ML AP DV])) );
fid = fopen( fullfile(jsonOutputFolder, ['Probe' num2str(p) '_xyz_picks.json']) , 'w');
fprintf(fid, out);
fclose(fid);

end

mayofaulkner commented 2 years ago

Hello, sorry for the delay. Could you also give the original .mat file and let me know which columns relates to ML, AP, DV in this original mat file and in what coordinate system they are in (are they in Allen CCF)?

Thanks

antaramaj16 commented 2 years ago

Hi,

Here is the original .mat file and the first column has the six probe tracks listed:

[image: image.png]

and if you click on one of them it will open this up:

[image: image.png] which I believe is the ML AP DV coordinates in pixels (units) and we have a script to convert these into um units and they are in the Allen CCF coordinate system. I have attached the script which we use to do this. The output of this is the xyz_picks.json file which is generated for each probe.

Sincerely, Antara

On Thu, Jun 2, 2022 at 1:33 PM mayofaulkner @.***> wrote:

Hello, sorry for the delay. Could you also give the original .mat file and let me know which columns relates to ML, AP, DV in this original mat file and in what coordinate system they are in (are they in Allen CCF)?

Thanks

— Reply to this email directly, view it on GitHub https://github.com/int-brain-lab/iblenv/issues/309#issuecomment-1144810015, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARV7RISYMPUPNRQKYYUZCH3VNCSYPANCNFSM5WMWQTBQ . You are receiving this because you authored the thread.Message ID: @.***>

--

_____

Antara Majumdar

DPhil Physiology, Anatomy and Genetics | Kellogg College

University of Oxford | Lak Lab Department of Physiology, Anatomy and Genetics Sherrington Building, Sherrington Rd, Oxford OX1 3PT Tel: +44 07709252462

%Input: probePoints file generated after aligning histology slices to the atlas and selecting probe dye locations. % only one point should be selected per probe per slice. %Output: json file (one per probe) listing probe points [ML AP DV] in um from bregma POINTLISTFILE = "\QNAP-AL001.dpag.ox.ac.uk\Data\AMR001\Histology\RGB\probe_pointsAMR001_Probe_Points_single.mat";

folder = dir(POINTLISTFILE); jsonOutputFolder = folder.folder;

probePoints = load(POINTLISTFILE);

numProbes = size(probePoints.pointList.pointList,1);

for p = 1:numProbes

thisProbe = probePoints.pointList.pointList{p,1}; %get first probe coords

% Convert probe coordinates (in pixels) to mm relative to bregma
ML = (thisProbe(:,1) - 570)*0.01;
DV = thisProbe(:,2)*0.01;
AP = (thisProbe(:,3) - 540)*0.01;

% Convert to um and the correct sign
ML = 1000*ML;
DV = -1000*DV;
AP = -1000*AP;

%Ouput should be ML AP DV
out = jsonencode( struct('xyz_picks', round([ML AP DV])) );
fid = fopen( fullfile(jsonOutputFolder, ['Probe' num2str(p) '_xyz_picks.json']) , 'w');
fprintf(fid, out);
fclose(fid);

end

mayofaulkner commented 2 years ago

Hey, the files don't seem to be attached. Could you try sending again. Thanks

antaramaj16 commented 2 years ago

Hi,

I have attached them again.

Sincerely, Antara

On Fri, Jun 3, 2022 at 12:54 PM mayofaulkner @.***> wrote:

Hey, the files don't seem to be attached. Could you try sending again. Thanks

— Reply to this email directly, view it on GitHub https://github.com/int-brain-lab/iblenv/issues/309#issuecomment-1145886949, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARV7RIWB53G73TDFT2FPX33VNHW7JANCNFSM5WMWQTBQ . You are receiving this because you authored the thread.Message ID: @.***>

--

_____

Antara Majumdar

DPhil Physiology, Anatomy and Genetics | Kellogg College

University of Oxford | Lak Lab Department of Physiology, Anatomy and Genetics Sherrington Building, Sherrington Rd, Oxford OX1 3PT Tel: +44 07709252462

%Input: probePoints file generated after aligning histology slices to the atlas and selecting probe dye locations. % only one point should be selected per probe per slice. %Output: json file (one per probe) listing probe points [ML AP DV] in um from bregma POINTLISTFILE = "\QNAP-AL001.dpag.ox.ac.uk\Data\AMR001\Histology\RGB\probe_pointsAMR001_Probe_Points_single.mat";

folder = dir(POINTLISTFILE); jsonOutputFolder = folder.folder;

probePoints = load(POINTLISTFILE);

numProbes = size(probePoints.pointList.pointList,1);

for p = 1:numProbes

thisProbe = probePoints.pointList.pointList{p,1}; %get first probe coords

% Convert probe coordinates (in pixels) to mm relative to bregma
ML = (thisProbe(:,1) - 570)*0.01;
DV = thisProbe(:,2)*0.01;
AP = (thisProbe(:,3) - 540)*0.01;

% Convert to um and the correct sign
ML = 1000*ML;
DV = -1000*DV;
AP = -1000*AP;

%Ouput should be ML AP DV
out = jsonencode( struct('xyz_picks', round([ML AP DV])) );
fid = fopen( fullfile(jsonOutputFolder, ['Probe' num2str(p) '_xyz_picks.json']) , 'w');
fprintf(fid, out);
fclose(fid);

end

antaramaj16 commented 2 years ago

https://drive.google.com/drive/folders/1AoEm5YFrq-VHs1B2zfCi4AePpfGJzb4z?usp=sharing

mayofaulkner commented 2 years ago

Hi Antara,

I've had a look into this and the problem occurs because the dv position of the second from last point (-7720) falls outside of the ccf atlas. This is happening because in the script above when converting you are assuming that there is no offset from bregma in the dv axis when in reality there is ~ 330um difference. The quickest fix will be to add this in before saving to json

DV = DV + 330

There is, however, also a function that converts from ccf coordinates to the bregma ones used in the alignment gui that you can also use. Here I have written an example of how you would convert your points. It requires saving the points you have in matlab to an npy file and then reading them in in python to do the conversion

In matlab you can do the following (FYI here is the writeNPY function https://github.com/kwikteam/npy-matlab)

# In matlab save the picks as a .npy file
thisProbe = probePoints.pointList.pointList{6,1}
writeNPY(thisProbe, 'picks_ccf.npy')

and then in python

# In python load in the same file
# Load in the pixel for tracing for single probe
import numpy as np
picks_mldvap = np.load('picks_ccf.npy')

# Switch axis so that array is in order mlapdv
picks_mlapdv = np.copy(picks_mldvap)
picks_mlapdv[:, 1] = picks_mldvap[:, 2]
picks_mlapdv[:, 2] = picks_mldvap[:, 1]

# Convert from pixels to um (assumes pixels have been taken from 10um allen atlas)
picks_mlapdv = picks_mlapdv * 10

# Convert from ccf to IBL bregma coordinates
from ibllib.atlas import AllenAtlas
ba = AllenAtlas()
xyz_mlapdv = ba.ccf2xyz(picks_mlapdv, ccf_order='mlapdv') * 1e6
xyz_picks = {'xyz_picks': xyz_mlapdv.tolist()}

# Write to json
import json
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)

Two things that I noticed when looking at your tracing (see images for coronal and sagittal slice)

image image

  1. A lot of the traced points are outside the brain. I wonder if this is because the ml and ap coordinates above have been switched. From your experiment do you have an idea of where the ml and ap coordinates should be in the brain and if so does the location in the slices above seem correct?

  2. It may be worth seeing if you can get a smoother tracing as those sharp edges between points will lead to strange behaviour when doing alignments as points are interpolated along the line between the points. I would also definitely recommend sorting the values by dv value to make sure the points are decreasing.

I hope that all makes sense. Please let me know if you need any other help.