def create_luminance_levels_tasks(task_queue, layer_path, coverage_factor=0.01, shape=None, offset=(0, 0, 0), mip=0): vol = CloudVolume(layer_path) if shape == None: shape = vol.shape.clone() shape.z = 1 offset = Vec(*offset) for z in range(vol.bounds.minpt.z, vol.bounds.maxpt.z + 1): offset.z = z task = LuminanceLevelsTask( src_path=layer_path, shape=shape, offset=offset, coverage_factor=coverage_factor, mip=mip, ) task_queue.insert(task) task_queue.wait('Uploading Luminance Levels Tasks') vol.provenance.processing.append({ 'method': { 'task': 'LuminanceLevelsTask', 'src': layer_path, 'shape': Vec(*shape).tolist(), 'offset': Vec(*offset).tolist(), 'coverage_factor': coverage_factor, 'mip': mip, }, 'by': USER_EMAIL, 'date': strftime('%Y-%m-%d %H:%M %Z'), }) vol.commit_provenance()
def create_luminance_levels_tasks(layer_path, levels_path=None, coverage_factor=0.01, shape=None, offset=None, mip=0, bounds_mip=0, bounds=None): """ Compute per slice luminance level histogram and write them as $layer_path/levels/$z. Each z file looks like: { "levels": [ 0, 35122, 12, ... ], # 256 indices, index = luminance i.e. 0 is black, 255 is white "patch_size": [ sx, sy, sz ], # metadata on how large the patches were "num_patches": 20, # metadata on "coverage_ratio": 0.011, # actual sampled area on this slice normalized by ROI size. } Args: layer_path: source image to sample from levels_path: which path to write ./levels/ to (default: $layer_path) coverage_factor: what fraction of the image to sample offset & shape: Allows you to specify an ROI if much of the edges are black. Defaults to entire image. mip (int): which mip to work with, default maximum resolution bounds_mip (int): mip of the input bounds bounds (Bbox-like) """ if shape or offset: print( yellow( "Create Luminance Levels Tasks: Deprecation Notice: " "shape and offset parameters are deprecated in favor of the bounds argument." )) vol = CloudVolume(layer_path, mip=mip) if bounds is None: bounds = vol.bounds.clone() bounds = get_bounds(vol, bounds, mip, bounds_mip=bounds_mip) if shape is None and bounds is None: shape = Vec(*vol.shape) shape.z = 1 elif bounds is not None: shape = Vec(*bounds.size3()) shape.z = 1 if not offset: offset = bounds.minpt offset = Vec(*offset) zoffset = offset.clone() protocol = vol.meta.path.protocol class LuminanceLevelsTaskIterator(object): def __len__(self): return bounds.maxpt.z - bounds.minpt.z def __iter__(self): for z in range(bounds.minpt.z, bounds.maxpt.z + 1): zoffset.z = z yield LuminanceLevelsTask( src_path=layer_path, levels_path=levels_path, shape=shape, offset=zoffset, coverage_factor=coverage_factor, mip=mip, ) if protocol == 'boss': raise StopIteration() if levels_path: try: vol = CloudVolume(levels_path) except cloudvolume.exceptions.InfoUnavailableError: vol = CloudVolume(levels_path, info=vol.info) else: vol = CloudVolume(layer_path, mip=mip) vol.provenance.processing.append({ 'method': { 'task': 'LuminanceLevelsTask', 'src': layer_path, 'levels_path': levels_path, 'shape': Vec(*shape).tolist(), 'offset': Vec(*offset).tolist(), 'bounds': [bounds.minpt.tolist(), bounds.maxpt.tolist()], 'coverage_factor': coverage_factor, 'mip': mip, }, 'by': operator_contact(), 'date': strftime('%Y-%m-%d %H:%M %Z'), }) vol.commit_provenance() return LuminanceLevelsTaskIterator()
def create_transfer_tasks(src_layer_path, dest_layer_path, chunk_size=None, shape=None, fill_missing=False, translate=None, bounds=None, mip=0, preserve_chunk_size=True, encoding=None, skip_downsamples=False, delete_black_uploads=False, background_color=0, agglomerate=False, timestamp=None, compress='gzip', factor=None, sparse=False, dest_voxel_offset=None, memory_target=MEMORY_TARGET, max_mips=5, clean_info=False, no_src_update=False): """ Transfer data to a new data layer. You can use this operation to make changes to the dataset representation as well. For example, you can change the chunk size, compression, bounds, and offset. Downsamples will be automatically generated while transferring unless skip_downsamples is set. The number of downsamples will be determined by the chunk size and the task shape. bounds: Bbox specified in terms of the destination image and its highest resolution. translate: Vec3 pointing from source bounds to dest bounds and is in terms of the highest resolution of the source image. This allows you to compensate for differing voxel offsets or enables you to move part of the image to a new location. dest_voxel_offset: When creating a new image, move the global coordinate origin to this point. This is commonly used to "zero" a newly aligned image (e.g. (0,0,0)) background_color: Designates which color should be considered background. chunk_size: (overrides preserve_chunk_size) force chunk size for new layers to be this. clean_info: scrub additional fields from the info file that might interfere with later processing (e.g. mesh and skeleton related info). compress: None, 'gzip', or 'br' Determines which compression algorithm to use for new uploaded files. delete_black_uploads: issue delete commands instead of upload chunks that are all background. encoding: "raw", "jpeg", "compressed_segmentation", "compresso", "fpzip", or "kempressed" depending on which kind of data you're dealing with. raw works for everything (no compression) but you might get better compression with another encoding. You can think of encoding as the image type-specific first stage of compression and the "compress" flag as the data agnostic second stage compressor. For example, compressed_segmentation and gzip work well together, but not jpeg and gzip. factor: (overrides axis) can manually specify what each downsampling round is supposed to do: e.g. (2,2,1), (2,2,2), etc fill_missing: Treat missing image tiles as zeroed for both src and dest. max_mips: (pairs with memory_target) maximum number of downsamples to generate even if the memory budget is large enough for more. memory_target: given a task size in bytes, pick the task shape that will produce the maximum number of downsamples. Only works for (2,2,1) or (2,2,2). no_src_update: don't update the source's provenance file preserve_chunk_size: if true, maintain chunk size of starting mip, else, find the closest evenly divisible chunk size to 64,64,64 for this shape and use that. The latter can be useful when mip 0 uses huge chunks and you want to simply visualize the upper mips. shape: (overrides memory_target) The 3d size of each task. Choose a shape that meets the following criteria unless you're doing something out of the ordinary. (a) 2^n multiple of destination chunk size (b) doesn't consume too much memory (c) n is related to the downsample factor for each axis, so for a factor of (2,2,1) (default) z only needs to be a single chunk, but x and y should be 2, 4, 8,or 16 times the chunk size. Remember to multiply 4/3 * shape.x * shape.y * shape.z * data_type to estimate how much memory each task will require. If downsamples are off, you can skip the 4/3. In the future, if chunk sizes match we might be able to do a simple file transfer. The problem can be formulated as producing the largest number of downsamples within a given memory target. EXAMPLE: destination is uint64 with chunk size (128, 128, 64) with a memory target of at most 3GB per task and a downsample factor of (2,2,1). The largest number of downsamples is 4 using 2048 * 2048 * 64 sized tasks which will use 2.9 GB of memory. The next size up would use 11.5GB and is too big. sparse: When downsampling segmentation, if true, don't count black pixels when computing the mode. Useful for e.g. synapses and point labels. agglomerate: (graphene only) remap the watershed layer to a proofread segmentation. timestamp: (graphene only) integer UNIX timestamp indicating the proofreading state to represent. """ src_vol = CloudVolume(src_layer_path, mip=mip) if dest_voxel_offset: dest_voxel_offset = Vec(*dest_voxel_offset, dtype=int) else: dest_voxel_offset = src_vol.voxel_offset.clone() if factor is None: factor = (2, 2, 1) if skip_downsamples: factor = (1, 1, 1) if not chunk_size: chunk_size = src_vol.info['scales'][mip]['chunk_sizes'][0] chunk_size = Vec(*chunk_size) try: dest_vol = CloudVolume(dest_layer_path, mip=mip) except cloudvolume.exceptions.InfoUnavailableError: info = copy.deepcopy(src_vol.info) dest_vol = CloudVolume(dest_layer_path, info=info, mip=mip) dest_vol.commit_info() if dest_voxel_offset is not None: dest_vol.scale["voxel_offset"] = dest_voxel_offset # If translate is not set, but dest_voxel_offset is then it should naturally be # only be the difference between datasets. if translate is None: translate = dest_vol.voxel_offset - src_vol.voxel_offset # vector pointing from src to dest else: translate = Vec(*translate) // src_vol.downsample_ratio if encoding is not None: dest_vol.info['scales'][mip]['encoding'] = encoding if encoding == 'compressed_segmentation' and 'compressed_segmentation_block_size' not in dest_vol.info[ 'scales'][mip]: dest_vol.info['scales'][mip][ 'compressed_segmentation_block_size'] = (8, 8, 8) dest_vol.info['scales'] = dest_vol.info['scales'][:mip + 1] dest_vol.info['scales'][mip]['chunk_sizes'] = [chunk_size.tolist()] if clean_info: dest_vol.info = clean_xfer_info(dest_vol.info) dest_vol.commit_info() if shape is None: if memory_target is not None: shape = downsample_scales.downsample_shape_from_memory_target( np.dtype(src_vol.dtype).itemsize, dest_vol.chunk_size.x, dest_vol.chunk_size.y, dest_vol.chunk_size.z, factor, memory_target, max_mips) else: raise ValueError( "Either shape or memory_target must be specified.") shape = Vec(*shape) if factor[2] == 1: shape.z = int(dest_vol.chunk_size.z * round(shape.z / dest_vol.chunk_size.z)) if not skip_downsamples: downsample_scales.create_downsample_scales( dest_layer_path, mip=mip, ds_shape=shape, preserve_chunk_size=preserve_chunk_size, encoding=encoding) dest_bounds = get_bounds(dest_vol, bounds, mip, chunk_size) class TransferTaskIterator(FinelyDividedTaskIterator): def task(self, shape, offset): return partial( TransferTask, src_path=src_layer_path, dest_path=dest_layer_path, shape=shape.clone(), offset=offset.clone(), fill_missing=fill_missing, translate=translate, mip=mip, skip_downsamples=skip_downsamples, delete_black_uploads=bool(delete_black_uploads), background_color=background_color, agglomerate=agglomerate, timestamp=timestamp, compress=compress, factor=factor, sparse=sparse, ) def on_finish(self): job_details = { 'method': { 'task': 'TransferTask', 'src': src_layer_path, 'dest': dest_layer_path, 'shape': list(map(int, shape)), 'fill_missing': fill_missing, 'translate': list(map(int, translate)), 'skip_downsamples': skip_downsamples, 'delete_black_uploads': bool(delete_black_uploads), 'background_color': background_color, 'bounds': [dest_bounds.minpt.tolist(), dest_bounds.maxpt.tolist()], 'mip': mip, 'agglomerate': bool(agglomerate), 'timestamp': timestamp, 'compress': compress, 'encoding': encoding, 'memory_target': memory_target, 'factor': (tuple(factor) if factor else None), 'sparse': bool(sparse), }, 'by': operator_contact(), 'date': strftime('%Y-%m-%d %H:%M %Z'), } dest_vol = CloudVolume(dest_layer_path) dest_vol.provenance.sources = [src_layer_path] dest_vol.provenance.processing.append(job_details) dest_vol.commit_provenance() if not no_src_update and src_vol.meta.path.protocol in ('gs', 's3', 'file'): src_vol.provenance.processing.append(job_details) src_vol.commit_provenance() return TransferTaskIterator(dest_bounds, shape)