def main(): parser = argparse.ArgumentParser() parser.add_argument('in_path',help='input tiff file or directory') parser.add_argument('min_size',help='minimum w*h*d',nargs='?',default=0,type=int) args = parser.parse_args() files = [] if os.path.isdir(args.in_path): for r,_,fnames in os.walk(args.in_path): for fname in fnames: files.append((r+'/'+fname)) elif os.path.isfile(args.in_path): files.append(args.in_path) resol,stepsize,C = 256,64,0 print('Found {} file{}'.format(len(files),'' if len(files)==1 else 's')) for f in files: img = tf.imread(f) d,h,w = img.shape print(f,w,h,d,args.min_size,img.nbytes//1e6) img = tf.transpose_axes(img, 'YXZ', 'XYZ') d,h,w = img.shape aux = 1 cmap = matplotlib.cm.get_cmap('inferno',stepsize) bounds = list(range(0,resol, resol//stepsize)) bounds.append(resol) norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N) if C: npz = np.arange(resol).astype('uint8') z11 = np.array([3.17108944e-04, 9.92360110e-01, 1.61116136e+00]) for i in range(len(npz)): aux = round(z11[0]*npz[i]*npz[i]+z11[1]*npz[i]+z11[2]) if aux < 256 and aux > 0: npz[i] = int(aux) elif aux > 255: npz[i] = 255 else: npz[i] = 0 with np.nditer(img, flags=['external_loop'], op_flags=['readwrite']) as it: for x in it: x[...] = npz[x] print(f,w,h,d,args.min_size) if d*h*w>=args.min_size: tf.imshow(img,cmap = cmap, norm = norm, title=f,origin='lower') # tf.imshow(img,title=f,cmap=plt.cm.magma) plt.show() C = 1
def __init__(self, filename): with TiffFile(filename) as tif: images = tif.asarray() images = transpose_axes(images, "XYZCT") images = images.squeeze(4) if tif.series[0].axes == "TZCYX": images = images.swapaxes(2, 3) self._image = images self._shape = self._image.shape
def array2cmap(data: numpy.ndarray, axes: str, cmapfile: PathLike, **kwargs) -> None: """Save numpy ndarray to Chimera MAP files, one per channel. Parameters ---------- data : ndarray Three to 5 dimensional array. axes : str Specifies type and order of axes in data array. May contain only 'CTZYX'. cmapfile : str Name of the output CMAP file. **kwargs Optional extra arguments passed to the CmapFile.addmap function, e.g. verbose, step, origin, cell_angles, rotation_axis, rotation_angle, subsample, chunks, and compression. """ if len(data.shape) != len(axes): raise ValueError('Number of axes do not match data shape') data = transpose_axes(data, axes, 'CTZYX') try: # create one CMAP file per channel cmaps = [] cmapfile = os.fspath(cmapfile) if cmapfile.lower().endswith('.cmap'): cmapfile = cmapfile[:-5] if data.shape[0] > 1: cmaps = [ CmapFile(f'{cmapfile}.ch{i:04d}.cmap') for i in range(data.shape[0]) ] else: cmaps = [CmapFile(f'{cmapfile}.cmap')] # iterate over data and write cmaps for c in range(data.shape[0]): # channels for t in range(data.shape[1]): # times cmaps[c].addmap(data=data[c, t], **kwargs) finally: for f in cmaps: f.close()
def transpose_axes(self, axes, dtype='float'): """Loads the stack in the order specified by axes""" stack = self.asarray().astype(dtype) actual_axes = self.get_axes() return tif.transpose_axes(stack, actual_axes, asaxes=axes)