def vol_proc(vol_data, crop=None, resize_shape=None, # None (to not resize), or vector. If vector, third entry can be None interp_order=None, rescale=None, rescale_prctle=None, resize_slices=None, resize_slices_dim=None, offset=None, clip=None, extract_nd=None, # extracts a particular section force_binary=None, # forces anything > 0 to be 1 permute=None): ''' process a volume with a series of intensity rescale, resize and crop rescale''' if offset is not None: vol_data = vol_data + offset # intensity normalize data .* rescale if rescale is not None: vol_data = np.multiply(vol_data, rescale) if rescale_prctle is not None: # print("max:", np.max(vol_data.flat)) # print("test") rescale = np.percentile(vol_data.flat, rescale_prctle) # print("rescaling by 1/%f" % (rescale)) vol_data = np.multiply(vol_data.astype(float), 1/rescale) if resize_slices is not None: resize_slices = [*resize_slices] assert resize_shape is None, "if resize_slices is given, resize_shape has to be None" resize_shape = resize_slices if resize_slices_dim is None: resize_slices_dim = np.where([f is None for f in resize_slices])[0] assert len(resize_slices_dim) == 1, "Could not find dimension or slice resize" resize_slices_dim = resize_slices_dim[0] resize_shape[resize_slices_dim] = vol_data.shape[resize_slices_dim] # resize (downsample) matrices if resize_shape is not None and resize_shape != vol_data.shape: resize_shape = [*resize_shape] # allow for the last entry to be None if resize_shape[-1] is None: resize_ratio = np.divide(resize_shape[0], vol_data.shape[0]) resize_shape[-1] = np.round(resize_ratio * vol_data.shape[-1]).astype('int') resize_ratio = np.divide(resize_shape, vol_data.shape) vol_data = scipy.ndimage.interpolation.zoom(vol_data, resize_ratio, order=interp_order) # crop data if necessary if crop is not None: vol_data = nd.volcrop(vol_data, crop=crop) # needs to be last to guarantee clip limits. # For e.g., resize might screw this up due to bicubic interpolation if it was done after. if clip is not None: vol_data = np.clip(vol_data, clip[0], clip[1]) if extract_nd is not None: vol_data = vol_data[np.ix_(*extract_nd)] if force_binary: vol_data = (vol_data > 0).astype(float) # return with checks. this check should be right at the end before rturn if clip is not None: assert np.max(vol_data) <= clip[1], "clip failed" assert np.min(vol_data) >= clip[0], "clip failed" return vol_data
def vol_proc( vol_data, crop=None, resize_shape=None, # None (to not resize), or vector. If vector, third entry can be None interp_order=None, rescale=None, rescale_prctle=None, resize_slices=None, resize_slices_dim=None, offset=None, clip=None, extract_nd=None, # extracts a particular section force_binary=None, # forces anything > 0 to be 1 permute=None): ''' process a volume with a series of intensity rescale, resize and crop rescale''' if offset is not None: vol_data = vol_data + offset # intensity normalize data .* rescale if rescale is not None: vol_data = np.multiply(vol_data, rescale) if rescale_prctle is not None: # print("max:", np.max(vol_data.flat)) # print("test") rescale = np.percentile(vol_data.flat, rescale_prctle) # print("rescaling by 1/%f" % (rescale)) vol_data = np.multiply(vol_data.astype(float), 1 / rescale) if resize_slices is not None: resize_slices = [*resize_slices] assert resize_shape is None, "if resize_slices is given, resize_shape has to be None" resize_shape = resize_slices if resize_slices_dim is None: resize_slices_dim = np.where([f is None for f in resize_slices])[0] assert len(resize_slices_dim ) == 1, "Could not find dimension or slice resize" resize_slices_dim = resize_slices_dim[0] resize_shape[resize_slices_dim] = vol_data.shape[resize_slices_dim] # resize (downsample) matrices if resize_shape is not None and resize_shape != vol_data.shape: resize_shape = [*resize_shape] # allow for the last entry to be None if resize_shape[-1] is None: resize_ratio = np.divide(resize_shape[0], vol_data.shape[0]) resize_shape[-1] = np.round(resize_ratio * vol_data.shape[-1]).astype('int') resize_ratio = np.divide(resize_shape, vol_data.shape) vol_data = scipy.ndimage.interpolation.zoom(vol_data, resize_ratio, order=interp_order) # crop data if necessary if crop is not None: vol_data = nd.volcrop(vol_data, crop=crop) # needs to be last to guarantee clip limits. # For e.g., resize might screw this up due to bicubic interpolation if it was done after. if clip is not None: vol_data = np.clip(vol_data, clip[0], clip[1]) if extract_nd is not None: vol_data = vol_data[np.ix_(*extract_nd)] if force_binary: vol_data = (vol_data > 0).astype(float) # return with checks. this check should be right at the end before rturn if clip is not None: assert np.max(vol_data) <= clip[1], "clip failed" assert np.min(vol_data) >= clip[0], "clip failed" return vol_data