def read_png_images(path_prefix: str, bbox: Bbox, volume_offset: tuple = (0, 0, 0), voxel_size: tuple = (1, 1, 1), dtype: np.dtype = np.uint8): chunk = Chunk.from_bbox(bbox, dtype=dtype, all_zero=True, voxel_size=voxel_size) assert len(bbox.minpt) == 3 assert len(volume_offset) == 3 for z in tqdm(range(bbox.minpt[0], bbox.maxpt[0])): file_name = '{}{:0>5d}.png'.format(path_prefix, z) file_name = path.expanduser(file_name) if path.exists(file_name): img = Image.open(file_name) img = np.asarray(img) img = np.expand_dims(img, axis=0) img_chunk = Chunk(img, voxel_offset=(z + volume_offset[0], volume_offset[1], volume_offset[2]), voxel_size=voxel_size) chunk.blend(img_chunk) else: logging.warning(f'image file do not exist: {file_name}') return chunk
def read_png_images(path_prefix: str, bbox: BoundingBox, volume_offset: tuple = (0, 0, 0), voxel_size: tuple = (1, 1, 1), digit_num: int = 5, dtype: np.dtype = np.uint8): chunk = Chunk.from_bbox( bbox, dtype=dtype, pattern='zero', voxel_size=voxel_size ) assert len(bbox.minpt) == 3 assert len(volume_offset) == 3 for z in tqdm( range(bbox.minpt[0], bbox.maxpt[0]) ): file_name = f'{path_prefix}{z:0>{digit_num}d}.png' file_name = path.expanduser(file_name) if path.exists(file_name): with open(file_name, "rb") as f: img = pyspng.load(f.read()) img = np.expand_dims(img, axis=0) img_chunk = Chunk( img, voxel_offset=( z+volume_offset[0], volume_offset[1], volume_offset[2]), voxel_size=voxel_size ) chunk.blend(img_chunk) else: logging.warning(f'image file do not exist: {file_name}') return chunk
def __call__(self, output_bbox: BoundingBox): # if we do not clone this bounding box, # the bounding box in task will be modified! assert isinstance(output_bbox, BoundingBox) output_bbox = output_bbox.clone() output_bbox.adjust(self.expand_margin_size) chunk_slices = output_bbox.to_slices() if self.dry_run: # input_bbox = BoundingBox.from_slices(chunk_slices) # we can not use pattern=zero since it might got skipped by # the operator of skip-all-zero return Chunk.from_bbox( output_bbox, pattern='random', dtype=self.vol.dtype, voxel_size=Cartesian.from_collection( self.vol.resolution[::-1]), ) logging.info('cutout {} from {}'.format(chunk_slices[::-1], self.volume_path)) # always reverse the indexes since cloudvolume use x,y,z indexing chunk = self.vol[chunk_slices[::-1]] chunk = np.asarray(chunk) # the cutout is fortran ordered, so need to transpose and make it C order chunk = chunk.transpose() # we can delay this transpose later # actually we do not need to make it contiguous # chunk = np.ascontiguousarray(chunk) # if the channel number is 1, squeeze it as 3d array # this should not be neccessary # TODO: remove this step and use 4D array all over this package. # always use 4D array will simplify some operations # voxel_offset = Cartesian(s.start for s in chunk_slices) if chunk.shape[0] == 1: chunk = np.squeeze(chunk, axis=0) chunk = Chunk(chunk, voxel_offset=output_bbox.start, voxel_size=Cartesian.from_collection( self.vol.resolution[::-1])) if self.blackout_sections: chunk = self._blackout_sections(chunk) if self.validate_mip: self._validate_chunk(chunk) return chunk
def __call__(self, output_bbox): #gevent.monkey.patch_all(thread=False) vol = CloudVolume(self.volume_path, bounded=False, fill_missing=self.fill_missing, progress=self.verbose, mip=self.mip, cache=False, use_https=self.use_https, green_threads=True) chunk_slices = tuple( slice(s.start - m, s.stop + m) for s, m in zip(output_bbox.to_slices(), self.expand_margin_size)) if self.dry_run: input_bbox = Bbox.from_slices(chunk_slices) return Chunk.from_bbox(input_bbox) if self.verbose: print('cutout {} from {}'.format(chunk_slices[::-1], self.volume_path)) # always reverse the indexes since cloudvolume use x,y,z indexing chunk = vol[chunk_slices[::-1]] # the cutout is fortran ordered, so need to transpose and make it C order chunk = chunk.transpose() # we can delay this transpose later # actually we do not need to make it contiguous # chunk = np.ascontiguousarray(chunk) # if the channel number is 1, squeeze it as 3d array # this should not be neccessary # TODO: remove this step and use 4D array all over this package. # always use 4D array will simplify some operations global_offset = tuple(s.start for s in chunk_slices) if chunk.shape[0] == 1: chunk = np.squeeze(chunk, axis=0) else: global_offset = (chunk.shape[0], ) + global_offset chunk = Chunk(chunk, global_offset=global_offset) if self.blackout_sections: chunk = self._blackout_sections(chunk) if self.validate_mip: self._validate_chunk(chunk, vol) return chunk
def __call__(self, output_bbox): chunk_slices = tuple( slice(s.start - m, s.stop + m) for s, m in zip(output_bbox.to_slices(), self.expand_margin_size)) if self.dry_run: input_bbox = Bbox.from_slices(chunk_slices) return Chunk.from_bbox(input_bbox) logging.info('cutout {} from {}'.format(chunk_slices[::-1], self.volume_path)) # always reverse the indexes since cloudvolume use x,y,z indexing chunk = self.vol[chunk_slices[::-1]] chunk = np.asarray(chunk) # the cutout is fortran ordered, so need to transpose and make it C order chunk = chunk.transpose() # we can delay this transpose later # actually we do not need to make it contiguous # chunk = np.ascontiguousarray(chunk) # if the channel number is 1, squeeze it as 3d array # this should not be neccessary # TODO: remove this step and use 4D array all over this package. # always use 4D array will simplify some operations voxel_offset = tuple(s.start for s in chunk_slices) if chunk.shape[0] == 1: chunk = np.squeeze(chunk, axis=0) else: voxel_offset = (0, ) + voxel_offset chunk = Chunk(chunk, voxel_offset=voxel_offset, voxel_size=tuple(self.vol.resolution[::-1])) if self.blackout_sections: chunk = self._blackout_sections(chunk) if self.validate_mip: self._validate_chunk(chunk) return chunk
def test_create_from_bounding_box(self): bbox = BoundingBox.from_delta(self.voxel_offset, self.size) bbox = BoundingBox.from_bbox(bbox) Chunk.from_bbox(bbox)
def test_create_from_bounding_box(self): bbox = Bbox.from_delta(self.global_offset, self.size) Chunk.from_bbox( bbox )