def _dl_point(args): p, cv = args bbox = cloudvolume.Bbox(p.astype(int), p.astype(int) + 1) return int( np.array( cv.download(bbox, agglomerate=root_ids, timestamp=timestamp)).squeeze())
def convert_to_mip(self, image_cv, bounds, out_mip, in_mip=0): corner = np.array([bounds[0], bounds[2], bounds[4]]) other_corner = np.array([bounds[1], bounds[3], bounds[5]]) bbox = cloudvolume.Bbox(corner, other_corner) new_bbox = image_cv.bbox_to_mip(bbox, in_mip, out_mip).to_list() new_bounds = [new_bbox[0], new_bbox[3], new_bbox[1], new_bbox[4], new_bbox[2], new_bbox[5]] return new_bounds
def get_closest_lvl2_chunk(point, root_id, client, cv=None, resolution=[4, 4, 40], mip_rescale=[2, 2, 1], radius=200, return_point=False): """Get the closest level 2 chunk on a root id Parameters ---------- point : array-like Point in space. root_id : int Root id of the object client : FrameworkClient Framework client to access data cv : cloudvolume.CloudVolume, optional Predefined cloudvolume, generated if None. By default None resolution : list, optional Point resolution to map between point resolution and mesh resolution, by default [4, 4, 40] mip_rescale : resolution difference between """ if cv is None: cv = cloudvolume.CloudVolume(client.info.segmentation_source(), use_https=True, bounded=False) # Get the closest adjacent point for the root id within the radius. pt = np.array(point) // mip_rescale offset = radius // (np.array(mip_rescale) * np.array(resolution)) lx = np.array(pt) - offset ux = np.array(pt) + offset bbox = cloudvolume.Bbox(lx, ux) vol = cv.download(bbox, segids=[root_id]) vol = np.squeeze(vol) if not bool(np.any(vol > 0)): raise ValueError('No point of the root id is near the specified point') ctr = offset * point * resolution xyz = np.vstack(np.where(vol > 0)).T xyz_nm = xyz * mip_rescale * resolution ind = np.argmin(np.linalg.norm(xyz_nm - ctr, axis=1)) closest_pt = vol.bounds.minpt + xyz[ind] # Look up the level 2 supervoxel for that id. closest_sv = int(cv.download_point(closest_pt, size=1)) lvl2_id = client.chunkedgraph.get_root_id(closest_sv, level2=True) if return_point: return lvl2_id, closest_pt * mip_rescale * resolution else: return lvl2_id
def get_cv_data(cv_path, offset_xyz, size_xyz): full_cv = cloudvolume.CloudVolume('file://%s' % cv_path, mip=0, parallel=True, progress=False) offset_xyz = np.array(offset_xyz) size_xyz = np.array(size_xyz) bbox = cloudvolume.Bbox(offset_xyz, offset_xyz + size_xyz) return full_cv[bbox][..., 0]
def chunk_location_from_segmentation(l2id, cv, mip=0): """Representative level 2 id location using the segmentation. This is typically slower than the mesh approach, but is more robust. Parameters ---------- l2id : int Level 2 id to look up cv : cloudvolume.CloudVolume CloudVolume associated with the mesh Returns ------- xyz_rep : np.array 3-element xyz array of the representative location in euclidean space. """ loc_ch = np.array(cv.mesh.meta.meta.decode_chunk_position(l2id)) loc_vox = np.atleast_2d(loc_ch) * cv.graph_chunk_size + np.array( cv.voxel_offset) bbox = cloudvolume.Bbox(loc_vox[0], loc_vox[0] + cv.graph_chunk_size) bbox_mip = cv.bbox_to_mip(bbox, 0, mip) try: sv_vol, _ = cv.download(bbox_mip, segids=(l2id, ), mip=mip, renumber=True) sv_vol = sv_vol > 0 x, y, z = np.where(sv_vol.squeeze()) if len(x) == 0 and mip > 0: # If too small for the requested mip level, jump to the highest mip level del sv_vol return chunk_location_from_segmentation(l2id, cv, mip=0) except: return np.array([np.nan, np.nan, np.nan]) xyz = np.vstack((x, y, z)).T * np.array(sv_vol.resolution) xyz_mean = np.mean(xyz, axis=0) xyz_box = xyz[np.argmin(np.linalg.norm(xyz - xyz_mean, axis=1))] xyz_rep = np.array(sv_vol.bounds.minpt) * np.array( sv_vol.resolution) + xyz_box del sv_vol return xyz_rep
def get_closest_lvl2_chunk( point, root_id, client, cv=None, voxel_resolution=None, radius=200, return_point=False, ): """Get the closest level 2 chunk on a root id Parameters ---------- point : array-like Point in voxel space. root_id : int Root id of the object client : FrameworkClient Framework client to access data cv : cloudvolume.CloudVolume or None, optional Cloudvolume associated with the dataset. One is created if None. voxel_resolution : list, optional Point resolution to map between point resolution and mesh resolution, by default [4, 4, 40] radius : int, optional Max distance to look for a nearby supervoxel. Optional, default is 200. return_point : bool, optional If True, returns the closest point in addition to the level 2 id. Optional, default is False. Returns ------- level2_id : int Level 2 id of the object nearest to the point specified. close_point : array, optional Closest point inside the object to the specified point. Only returned if return_point is True. """ if cv is None: cv = cloudvolume.CloudVolume( client.info.segmentation_source(), use_https=True, bounded=False, fill_missing=True, progress=False, secrets={"token": client.auth.token}, ) if voxel_resolution is None: voxel_resolution = np.array(cv.mip_resolution(0)) point = point * np.array(voxel_resolution) # Get the closest adjacent point for the root id within the radius. mip_scaling = np.array(cv.mip_resolution(0)) pt = np.array(np.array(point) // mip_scaling, np.int32) offset = np.array(radius // mip_scaling, np.int32) lx = np.array(pt) - offset ux = np.array(pt) + offset bbox = cloudvolume.Bbox(lx.astype(int), ux.astype(int)) vol = cv.download(bbox, segids=[root_id]) vol = np.squeeze(vol) if not bool(np.any(vol > 0)): raise ValueError("No point of the root id is near the specified point") ctr = offset * mip_scaling # Offset is at center of the volume. xyz = np.vstack(np.where(vol > 0)).T xyz_nm = xyz * mip_scaling ind = np.argmin(np.linalg.norm(xyz_nm - ctr, axis=1)) closest_pt = vol.bounds.minpt + xyz[ind] # Look up the level 2 supervoxel for that id. closest_sv = int(cv.download_point(closest_pt, size=1)) lvl2_id = client.chunkedgraph.get_root_id(closest_sv, level2=True) if return_point: return lvl2_id, closest_pt * mip_scaling else: return lvl2_id
def _bounds_to_slices(self, bounds): return cv.Bbox(bounds[0], bounds[1])