@@ -300,16 +300,34 @@ def _lookup(self, xyz):
300300 """
301301 return self ._lookup_inds (self .bc .xyz2i (xyz ))
302302
303- def get_labels (self , xyz , mapping = 'Allen' ):
303+ def get_labels (self , xyz , mapping = 'Allen' , radius_um = None ):
304304 """
305305 Performs a 3D lookup from real world coordinates to the volume labels
306306 and return the regions ids according to the mapping
307307 :param xyz: [n, 3] array of coordinates
308308 :param mapping: brain region mapping (defaults to original Allen mapping)
309+ :param radius_um: if not null, returns a regions ids array and an array of proportion
310+ of regions in a sphere of size radius around the coordinates.
309311 :return: n array of region ids
310312 """
311- regions_indices = self ._get_mapping (mapping = mapping )[self .label .flat [self ._lookup (xyz )]]
312- return self .regions .id [regions_indices ]
313+ if radius_um :
314+ nrx = int (np .ceil (radius_um / abs (self .bc .dx ) / 1e6 ))
315+ nry = int (np .ceil (radius_um / abs (self .bc .dy ) / 1e6 ))
316+ nrz = int (np .ceil (radius_um / abs (self .bc .dz ) / 1e6 ))
317+ nr = [nrx , nry , nrz ]
318+ iii = self .bc .xyz2i (xyz )
319+ # computing the cube radius and indices is more complicated as volume indices are not
320+ # necessariy in ml, ap, dv order so the indices order is dynamic
321+ rcube = np .meshgrid (* tuple ((np .arange (
322+ - nr [i ], nr [i ] + 1 ) * self .bc .dxyz [i ]) ** 2 for i in self .xyz2dims ))
323+ rcube = np .sqrt (rcube [0 ] + rcube [1 ], rcube [2 ]) * 1e6
324+ icube = tuple (slice (- nr [i ] + iii [i ], nr [i ] + iii [i ] + 1 ) for i in self .xyz2dims )
325+ cube = self .regions .mappings [mapping ][self .label [icube ]]
326+ ilabs , counts = np .unique (cube [rcube <= radius_um ], return_counts = True )
327+ return self .regions .id [ilabs ], counts / np .sum (counts )
328+ else :
329+ regions_indices = self ._get_mapping (mapping = mapping )[self .label .flat [self ._lookup (xyz )]]
330+ return self .regions .id [regions_indices ]
313331
314332 def _get_mapping (self , mapping = 'Allen' ):
315333 """
0 commit comments