@@ -1723,3 +1723,81 @@ def _read_volume(file_volume):
17231723 raise ValueError (
17241724 f'"{ file_volume .suffix } " files not supported, must be either ".nrrd" or ".npz"' )
17251725 return volume
1726+
1727+
1728+ def get_bc (res_um = 10 ):
1729+ """
1730+ Get BrainCoordinates object for Allen Atlas with bregma origin without reading in volumes
1731+
1732+ Parameters
1733+ ----------
1734+ res_um : float or int
1735+ The resolution of the brain coordinates object to return in um. Options are 50, 25 or 10
1736+
1737+ Returns
1738+ -------
1739+ BrainCoordinates object with bregma origin
1740+ """
1741+
1742+ dims2xyz = np .array ([1 , 0 , 2 ])
1743+ scaling = np .array ([1 , 1 , 1 ])
1744+ sf = 50 / res_um
1745+ im_shape = np .array ([int (264 * sf ), int (228 * sf ), int (160 * sf )])
1746+ iorigin = (ALLEN_CCF_LANDMARKS_MLAPDV_UM ['bregma' ] / res_um )
1747+ dxyz = res_um * 1e-6 * np .array ([1 , - 1 , - 1 ]) * scaling
1748+ nxyz = np .array (im_shape )[dims2xyz ]
1749+ bc = BrainCoordinates (nxyz = nxyz , xyz0 = (0 , 0 , 0 ), dxyz = dxyz )
1750+ bc = BrainCoordinates (nxyz = nxyz , xyz0 = - bc .i2xyz (iorigin ), dxyz = dxyz )
1751+
1752+ return bc
1753+
1754+
1755+ def _download_depth_files (file_name ):
1756+ """
1757+ These files have been generated using this script
1758+ https://github.com/int-brain-lab/ibldevtools/blob/master/Mayo/flatmaps/2025-03-20_depths_from_streamlines.py
1759+ :param file_name:
1760+ :return:
1761+ """
1762+ file_path = BrainAtlas ._get_cache_dir ().joinpath ('depths' , file_name )
1763+ if not file_path .exists ():
1764+ file_path .parent .mkdir (exist_ok = True , parents = True )
1765+ aws .s3_download_file (f'atlas/depths/{ file_path .name } ' , file_path )
1766+
1767+ return np .load (file_path )
1768+
1769+
1770+ def xyz_to_depth (xyz , res_um = 25 ):
1771+ """
1772+ For a given xyz coordinates find the depth from the surface of the cortex. Note the lookup will only
1773+ work for xyz cooordinates that are in the Isocortex of the Allen volume. If coordinates outside of this
1774+ region are given then the depth is returned as nan.
1775+
1776+ Parameters
1777+ ----------
1778+ xyz : numpy.array
1779+ An (n, 3) array of Cartesian coordinates. The order is ML, AP, DV and coordinates should be given in meters
1780+ relative to bregma.
1781+
1782+ res_um : float or int
1783+ The resolution of the brain atlas to do the depth lookup
1784+
1785+ Returns
1786+ -------
1787+ numpy.array
1788+ The depths from the surface of the cortex for each cartesian coordinate. If the coordinate does not lie within
1789+ the Isocortex, depth value returned is nan
1790+ """
1791+
1792+ ind_flat = _download_depth_files (f'depths_ind_{ res_um } .npy' )
1793+ depths = _download_depth_files (f'depths_um_{ res_um } .npy' )
1794+ bc = get_bc (res_um = res_um )
1795+
1796+ ixyz = bc .xyz2i (xyz , mode = 'clip' )
1797+ iravel = np .ravel_multi_index ((ixyz [:, 1 ], ixyz [:, 0 ], ixyz [:, 2 ]), (bc .ny , bc .nx , bc .nz ))
1798+ a , b = ismember (ind_flat , iravel )
1799+
1800+ lookup_depths = np .full (iravel .shape , np .nan , dtype = np .float32 )
1801+ lookup_depths [b ] = depths [a ]
1802+
1803+ return lookup_depths
0 commit comments