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

Add files to allow conversion to IBL format #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions +ap_histology/export_probe_ccf_to_npy.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function export_probe_ccf_to_npy(~,~,histology_toolbar_gui)
% Part of AP_histology toolbox
%
% Export probe CCF to npy

% Get images (from path in GUI)
histology_toolbar_guidata = guidata(histology_toolbar_gui);

save_path = histology_toolbar_guidata.save_path;
probe_filename = fullfile(save_path,'probe_ccf.mat');
load(probe_filename, 'probe_ccf');

if length(probe_ccf) == 1

fname = fullfile(save_path, 'probe_ccf.traj_coords.npy');
writeNPY(probe_ccf.trajectory_coords, fname);

fname = fullfile(save_path, 'probe_ccf.points.npy');
writeNPY(probe_ccf.points, fname);

else

% Write probe_ccf coordinates as NPY file (separately for each shank)
for iShank = 1:length(probe_ccf)
fname = fullfile(save_path, sprintf('probe_ccf.traj_coords.shank%d.npy', iShank));
writeNPY(probe_ccf(iShank).trajectory_coords, fname);

fname = fullfile(save_path, sprintf('probe_ccf.points.shank%d.npy', iShank));
writeNPY(probe_ccf(iShank).points, fname);
end
end
fprintf("\nExported to npy files at %s \n", save_path)
end
1 change: 1 addition & 0 deletions +ap_histology/update_toolbar_gui.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function update_gui(histology_toolbar_gui)

gui_data.menu.annotation.Enable = atlas_alignment_present;
gui_data.menu.view.Enable = atlas_alignment_present;
gui_data.menu.export.Enable = neuropixels_annotations_present;


% Set text
Expand Down
13 changes: 12 additions & 1 deletion AP_histology.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
uimenu(gui_data.menu.view,'Text','View aligned histology','MenuSelectedFcn', ...
{@ap_histology.view_aligned_histology,histology_toolbar_gui});

% Export menu
gui_data.menu.export = uimenu(histology_toolbar_gui,'Text','Export');
uimenu(gui_data.menu.export,'Text','Export track pts to npy','MenuSelectedFcn', ...
{@ap_histology.export_probe_ccf_to_npy,histology_toolbar_gui});

% Create GUI variables
gui_data.image_path = char;
gui_data.save_path = char;
Expand All @@ -76,9 +81,12 @@ function set_image_path(h,eventdata,histology_toolbar_gui)

% Pick image path
gui_data.image_path = uigetdir([],'Select path with raw images');
if gui_data.image_path == 0
gui_data.image_path = '';
end

% Clear processed path (if there's one selected)
gui_data.save_path = [];
gui_data.save_path = '';

% Store guidata
guidata(histology_toolbar_gui,gui_data);
Expand All @@ -95,6 +103,9 @@ function set_save_path(h,eventdata,histology_toolbar_gui)

% Pick image path
gui_data.save_path = uigetdir([],'Select path to save processing');
if gui_data.save_path == 0
gui_data.save_path = '';
end

% Store guidata
guidata(histology_toolbar_gui,gui_data);
Expand Down
55 changes: 55 additions & 0 deletions python_scripts/probe_ccf_to_xyz_picks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import json
import sys
from pathlib import Path

import numpy as np
from iblatlas.atlas import AllenAtlas

# Requires iblenv conda environment or anything with the ibllib module:
# See: https://github.com/int-brain-lab/iblenv?tab=readme-ov-file#install-from-scratch

probe_scaling_factor_to_um = (
10 # AP histology uses 10um atlas, so we need to scale the probe coordinates by 10
)
allen_atlas_res_to_use = 25 # IBL atlas gui uses 25 um atlas, not sure it matters
atlas = AllenAtlas(allen_atlas_res_to_use)


def main(tracing_path):
tracing_path = Path(tracing_path)
one_shank_pts_files = sorted(list(tracing_path.glob("probe_ccf.points.npy")))
multi_shank_pts_files = sorted(
list(tracing_path.glob("probe_ccf.points.shank*.npy"))
)
if len(one_shank_pts_files) > 0 and len(multi_shank_pts_files) > 0:
raise ValueError(
"Found both single shank ('probe_ccf.points.npy') and\
multi shank ('probe_ccf.points.shank*.npy') files.\
Please only provide one type of file."
)

pts_files = (
one_shank_pts_files if len(one_shank_pts_files) > 0 else multi_shank_pts_files
)
for iShank, pt_file in enumerate(pts_files):
# Load in coordinates of track in CCF space (order - apdvml, origin - top, left, front voxel
xyz_apdvml = np.load(pt_file)
xyz_apdvml = (xyz_apdvml * probe_scaling_factor_to_um) # convert from CCF volume coords to microns

# Convert to IBL space (order - mlapdv, origin - bregma)
xyz_mlapdv = (atlas.ccf2xyz(xyz_apdvml, ccf_order="apdvml") * 1e6) # convert output in meters back to microns (lol)
xyz_picks = {"xyz_picks": xyz_mlapdv.tolist()}

if len(pts_files) == 1:
# Path to save the data (same folder as where you have all the data)
with open(Path(tracing_path.parent, "xyz_picks.json"), "w") as f:
json.dump(xyz_picks, f, indent=2)
else:
shank_idx = iShank + 1 # IBL shank files are 1-indexed
with open(Path(tracing_path.parent, f"xyz_picks_shank{shank_idx}.json"), "w") as f:
json.dump(xyz_picks, f, indent=2)


if __name__ == "__main__":
tracing_path = sys.argv[1]
main(tracing_path)