def extract_mean(im, dim='c', keep_dtype=True, output='', report={}): """Calculate mean over channels.""" ods = 'mean' im.load(load_data=False) props = im.get_props() if len(im.dims) > 4: props = im.squeeze_props(props, dim=4) if len(im.dims) > 3: props = im.squeeze_props(props, dim=3) mo = Image(output.format(ods), **props) mo.create() zdim = im.axlab.index('z') if im.chunks is not None: nslcs = im.chunks[zdim] else: nslsc = 8 slc_thrs = [] for zstart in range(0, im.dims[zdim], nslcs): zstop = min(im.dims[zdim], zstart + nslcs) im.slices[zdim] = mo.slices[zdim] = slice(zstart, zstop, None) data_mean = np.mean(im.slice_dataset(), axis=im.axlab.index(dim)) if keep_dtype: data_mean = data_mean.astype(im.dtype) mo.write(data_mean) slc_thrs += list(np.median(np.reshape(data_mean, [data_mean.shape[0], -1]), axis=1)) mo.slices = None mo.set_slices() im.close() c_slcs = {dim: get_centreslice(mo, '', dim) for dim in 'zyx'} report['centreslices'][ods] = c_slcs return mo, report, slc_thrs
def stack_channels(images_in, outputpath=''): channels = [] for image_in in images_in: im = Image(image_in, permission='r') im.load(load_data=True) channels.append(im.ds[:]) data = np.stack(channels, axis=3) mo = Image(outputpath) mo.elsize = list(im.elsize) + [1] mo.axlab = im.axlab + 'c' mo.dims = data.shape mo.chunks = list(im.chunks) + [1] mo.dtype = im.dtype mo.set_slices() if outputpath: mo.create() mo.write(data) mo.close() return mo
def apply_bias_field_full(image_in, bias_in, dsfacs=[1, 64, 64, 1], in_place=False, write_to_single_file=False, blocksize_xy=1280, outputpath='', channel=None): """single-core in ~200 blocks""" perm = 'r+' if in_place else 'r' im = Image(image_in, permission=perm) im.load(load_data=False) bf = Image(bias_in, permission='r') bf.load(load_data=False) if channel is not None: im.slices[3] = slice(channel, channel + 1) if write_to_single_file: # assuming single-channel copied file here mo = Image(outputpath) mo.load() mo.slices[3] = slice(0, 1, 1) mpi = wmeMPI(usempi=False) mpi_nm = wmeMPI(usempi=False) if blocksize_xy: blocksize = [im.dims[0], blocksize_xy, blocksize_xy, 1, 1] blockmargin = [0, im.chunks[1], im.chunks[2], 0, 0] else: blocksize = im.dims[:3] + [1, 1] blockmargin = [0] * len(im.dims) mpi.set_blocks(im, blocksize, blockmargin) mpi_nm.set_blocks(im, blocksize) mpi.scatter_series() for i in mpi.series: print(i) block = mpi.blocks[i] data_shape = list(im.slices2shape(block['slices'])) block_nm = mpi_nm.blocks[i] it = zip(block['slices'], block_nm['slices'], blocksize, data_shape) data_shape = list(im.slices2shape(block_nm['slices'])) data_slices = [] for b_slc, n_slc, bs, ds in it: m_start = n_slc.start - b_slc.start m_stop = m_start + bs m_stop = min(m_stop, ds) data_slices.append(slice(m_start, m_stop, None)) data_slices[3] = block['slices'][3] data_shape = list(im.slices2shape(data_slices)) # get the fullres image block im.slices = block['slices'] data = im.slice_dataset().astype('float') # get the upsampled bias field bias = get_bias_field_block(bf, im.slices, data.shape) data /= bias data = np.nan_to_num(data, copy=False) if in_place: im.slices = block_nm['slices'] data = data[tuple(data_slices[:3])].astype(im.dtype) im.write(data) elif write_to_single_file: mo.slices = block_nm['slices'] mo.slices[3] = slice(0, 1, 1) data = data[tuple(data_slices[:3])].astype(mo.dtype) mo.write(data) else: props = im.get_props() if len(im.dims) > 4: props = im.squeeze_props(props, dim=4) if len(im.dims) > 3: props = im.squeeze_props(props, dim=3) props['axlab'] = 'zyx' # FIXME: axlab return as string-list props['shape'] = bias.shape props['slices'] = None props['dtype'] = bias.dtype mo = Image(block['path'], **props) # FIXME: needs channel mo.create(comm=mpi.comm) mo.slices = None mo.set_slices() mo.write(data=bias) mo.close() im.close() bf.close()