def main(args): log = logging.getLogger(__name__) hdlr = logging.StreamHandler(sys.stdout) log.addHandler(hdlr) log.setLevel(logging.getLevelName(args.loglevel.upper())) data = {} hdr = {} for i, inp in enumerate(args.input[1:]): d, h = read(inp, inc_header=True) if args.normalize: d = vop.normalize(d) data[ascii_lowercase[i]] = d hdr[ascii_lowercase[i]] = h if args.eval: final = eval(args.input[0], globals(), data) else: final = ne.evaluate(args.input[0], local_dict=data) if args.apix is None: args.apix = hdr[ascii_lowercase[0]]['xlen'] / hdr[ ascii_lowercase[0]]['nx'] write(args.output, final.astype(np.single), psz=args.apix) return 0
def main(args): if args.threshold is None: print("Please provide a binarization threshold") return 1 data, hdr = read(args.input, inc_header=True) mask = binarize_volume(data, args.threshold, minvol=args.minvol, fill=args.fill) if args.base_map is not None: base_map = read(args.base_map, inc_header=False) base_mask = binarize_volume(base_map, args.threshold, minvol=args.minvol, fill=args.fill) total_width = args.extend + args.edge_width excl_mask = binary_dilate(mask, total_width, strel=args.relion) base_mask = binary_dilate(base_mask, args.extend, strel=args.relion) base_mask = base_mask &~ excl_mask if args.overlap > 0: incl_mask = binary_dilate(base_mask, args.overlap, strel=args.relion) & excl_mask base_mask = base_mask | incl_mask mask = base_mask elif args.extend > 0: mask = binary_dilate(mask, args.extend, strel=args.relion) if args.close: se = binary_sphere(args.extend, False) mask = binary_closing(mask, structure=se, iterations=1) final = mask.astype(np.single) if args.edge_width != 0: dt = distance_transform_edt(~mask) # Compute *outward* distance transform of mask. idx = (dt <= args.edge_width) & (dt > 0) # Identify edge points by distance from mask. x = np.arange(1, args.edge_width + 1) # Domain of the edge profile. if "sin" in args.edge_profile: y = np.sin(np.linspace(np.pi/2, 0, args.edge_width + 1)) # Range of the edge profile. f = interp1d(x, y[1:]) final[idx] = f(dt[idx]) # Insert edge heights interpolated at distance transform values. write(args.output, final, psz=hdr["xlen"] / hdr["nx"]) return 0
def main(args): if args.threshold is None: print("Please provide a binarization threshold") return 1 data, hdr = read(args.input, inc_header=True) mask = data >= args.threshold if args.minvol is not None: mask = binary_volume_opening(mask, args.minvol) if args.fill: mask = binary_fill_holes(mask) if args.extend is not None: se = binary_sphere(args.extend, False) mask = binary_dilation(mask, structure=se, iterations=1) if args.close: se = binary_sphere(args.extend, False) mask = binary_closing(mask, structure=se, iterations=1) final = mask.astype(np.single) if args.edge_width is not None: dt = distance_transform_edt( ~mask) # Compute *outward* distance transform of mask. idx = (dt <= args.edge_width) & ( dt > 0) # Identify edge points by distance from mask. x = np.arange(1, args.edge_width + 1) # Domain of the edge profile. if "sin" in args.edge_profile: y = np.sin(np.linspace(np.pi / 2, 0, args.edge_width + 1)) # Range of the edge profile. f = interp1d(x, y[1:]) final[idx] = f( dt[idx] ) # Insert edge heights interpolated at distance transform values. write(args.output, final, psz=hdr["xlen"] / hdr["nx"]) return 0
def main(args): x = mrc.read(args.input[0]) sigma = np.zeros(x.shape) mu = x.copy() for i, f in enumerate(args.input[1:]): x = mrc.read(f) olddif = x - mu mu += (x - mu) / (i + 1) sigma += olddif * (x - mu) sigma_sq = np.power(sigma, 2) mrc.write(args.output, sigma_sq) if args.mean is not None: mrc.write(args.mean, mu) return 0
def main(args): x = mrc.read(args.input[0]) m2 = np.zeros(x.shape) mu = x.copy() for i, f in enumerate(args.input[1:]): x = mrc.read(f) olddif = x - mu mu += (x - mu) / (i + 1) m2 += olddif * (x - mu) var = m2 / len(args.input) mrc.write(args.output, var) if args.mean is not None: mrc.write(args.mean, mu) return 0
def main(args): log = logging.getLogger(__name__) hdlr = logging.StreamHandler(sys.stdout) log.addHandler(hdlr) log.setLevel(logging.getLevelName(args.loglevel.upper())) data, hdr = read(args.input, inc_header=True) if args.half2 is not None: half2, hdr_half2 = read(args.input, inc_header=True) if data.shape == half2.shape: data += half2 else: log.error("--half2 map is not the same shape as input map!") return 1 final = None box = np.array([hdr[a] for a in ["nx", "ny", "nz"]]) center = box // 2 if args.fft: if args.final_mask is not None: final_mask = read(args.final_mask) data *= final_mask data_ft = vop.vol_ft(data.T, pfac=args.pfac, threads=args.threads) np.save(args.output, data_ft) return 0 if args.transpose is not None: try: tax = [np.int64(a) for a in args.transpose.split(",")] data = np.transpose(data, axes=tax) except: log.error( "Transpose axes must be comma-separated list of three integers" ) return 1 if args.flip is not None: if args.flip.isnumeric(): args.flip = int(args.flip) else: args.flip = vop.label_to_axis(args.flip) data = np.flip(data, axis=args.flip) if args.apix is None: args.apix = hdr["xlen"] / hdr["nx"] log.info("Using computed pixel size of %f Angstroms" % args.apix) if args.normalize: if args.diameter is not None: if args.diameter > 1.0: args.diameter /= args.apix * 2 # Convert Angstrom diameter to pixel radius. if args.reference is not None: ref, refhdr = read(args.reference, inc_header=True) final, mu, sigma = vop.normalize(data, ref=ref, return_stats=True, rmask=args.diameter) else: final, mu, sigma = vop.normalize(data, return_stats=True, rmask=args.diameter) log.info("Mean: %f, Standard deviation: %f" % (mu, sigma)) if args.apix_out is not None: if args.scale is not None: log.warn("--apix-out supersedes --scale") args.scale = args.apix / args.apix_out elif args.scale is not None: args.apix_out = args.apix / args.scale elif args.boxsize is not None: args.scale = box[0] / np.double(args.boxsize) if args.apix_out is None: args.apix_out = args.apix if args.boxsize is None: if args.scale is None: args.boxsize = box[0] args.scale = 1 else: args.boxsize = np.int(box[0] * args.scale) log.info("Volume will be scaled by %f to size %d @ %f A/px" % (args.scale, args.boxsize, args.apix_out)) if args.target and args.transform: log.warn( "Target pose transformation will be applied after explicit matrix") if args.euler is not None and (args.target is not None or args.transform is not None): log.warn( "Euler transformation will be applied after target pose transformation" ) if args.translate is not None and (args.euler is not None or args.target is not None or args.transform is not None): log.warn("Translation will be applied after other transformations") if args.origin is not None: try: args.origin = np.array( [np.double(tok) for tok in args.origin.split(",")]) / args.apix assert np.all(args.origin < box) except: log.error( "Origin must be comma-separated list of x,y,z coordinates and lie within the box" ) return 1 else: args.origin = center log.info("Origin set to box center, %s" % (args.origin * args.apix)) if not (args.target is None and args.euler is None and args.transform is None and args.boxsize is None) \ and vop.ismask(data) and args.spline_order != 0: log.warn( "Input looks like a mask, --spline-order 0 (nearest neighbor) is recommended" ) if args.transform is not None: try: args.transform = np.array(json.loads(args.transform)) except: log.error("Transformation matrix must be in JSON/Numpy format") return 1 r = args.transform[:, :3] if args.transform.shape[1] == 4: t = args.transform[:, -1] / args.apix t = r.dot(args.origin) + t - args.origin t = -r.T.dot(t) else: t = 0 log.debug("Final rotation: %s" % str(r).replace("\n", "\n" + " " * 16)) log.debug("Final translation: %s (%f px)" % (str(t), np.linalg.norm(t))) data = vop.resample_volume(data, r=r, t=t, ori=None, order=args.spline_order, invert=args.invert) if args.target is not None: try: args.target = np.array( [np.double(tok) for tok in args.target.split(",")]) / args.apix except: log.error( "Standard pose target must be comma-separated list of x,y,z coordinates" ) return 1 args.target -= args.origin args.target = np.where(np.abs(args.target) < 1, 0, args.target) ori = None if args.origin is center else args.origin - center r = vec2rot(args.target) t = np.linalg.norm(args.target) log.info("Euler angles are %s deg and shift is %f px" % (np.rad2deg(rot2euler(r)), t)) log.debug("Final rotation: %s" % str(r).replace("\n", "\n" + " " * 16)) log.debug("Final translation: %s (%f px)" % (str(t), np.linalg.norm(t))) data = vop.resample_volume(data, r=r, t=args.target, ori=ori, order=args.spline_order, invert=args.invert) if args.euler is not None: try: args.euler = np.deg2rad( np.array([np.double(tok) for tok in args.euler.split(",")])) except: log.error( "Eulers must be comma-separated list of phi,theta,psi angles") return 1 r = euler2rot(*args.euler) offset = args.origin - 0.5 offset = offset - r.T.dot(offset) data = affine_transform(data, r.T, offset=offset, order=args.spline_order) if args.translate is not None: try: args.translate = np.array( [np.double(tok) for tok in args.translate.split(",")]) / args.apix except: log.error( "Translation vector must be comma-separated list of x,y,z coordinates" ) return 1 args.translate -= args.origin data = shift(data, -args.translate, order=args.spline_order) if final is None: final = data if args.final_mask is not None: final_mask = read(args.final_mask) final *= final_mask if args.scale != 1 or args.boxsize != box[0]: final = vop.resample_volume(final, scale=args.scale, output_shape=args.boxsize, order=args.spline_order) write(args.output, final, psz=args.apix_out) return 0
def main(args): log = logging.getLogger(__name__) log.setLevel(logging.INFO) hdlr = logging.StreamHandler(sys.stdout) if args.quiet: hdlr.setLevel(logging.ERROR) elif args.verbose: hdlr.setLevel(logging.INFO) else: hdlr.setLevel(logging.WARN) log.addHandler(hdlr) data, hdr = read(args.input, inc_header=True) final = None box = np.array([hdr[a] for a in ["nx", "ny", "nz"]]) center = box // 2 if args.fft: data_ft = vop.vol_ft(data.T, threads=args.threads) np.save(args.output, data_ft) return 0 if args.transpose is not None: try: tax = [np.int64(a) for a in args.transpose.split(",")] data = np.transpose(data, axes=tax) except: log.error( "Transpose axes must be comma-separated list of three integers" ) return 1 if args.normalize: if args.reference is not None: ref, refhdr = read(args.reference, inc_header=True) final, mu, sigma = vop.normalize(data, ref=ref, return_stats=True) else: final, mu, sigma = vop.normalize(data, return_stats=True) final = (data - mu) / sigma if args.verbose: log.info("Mean: %f, Standard deviation: %f" % (mu, sigma)) if args.apix is None: args.apix = hdr["xlen"] / hdr["nx"] log.info("Using computed pixel size of %f Angstroms" % args.apix) if args.target and args.matrix: log.warn( "Target pose transformation will be applied after explicit matrix") if args.euler is not None and (args.target is not None or args.matrix is not None): log.warn( "Euler transformation will be applied after target pose transformation" ) if args.translate is not None and (args.euler is not None or args.target is not None or args.matrix is not None): log.warn("Translation will be applied after other transformations") if args.origin is not None: try: args.origin = np.array( [np.double(tok) for tok in args.origin.split(",")]) / args.apix assert np.all(args.origin < box) except: log.error( "Origin must be comma-separated list of x,y,z coordinates and lie within the box" ) return 1 else: args.origin = center log.info("Origin set to box center, %s" % (args.origin * args.apix)) if not (args.target is None and args.euler is None and args.matrix is None and args.boxsize is None) \ and vop.ismask(data) and args.spline_order != 0: log.warn( "Input looks like a mask, --spline-order 0 (nearest neighbor) is recommended" ) if args.matrix is not None: try: r = np.array(json.loads(args.matrix)) except: log.error("Matrix format is incorrect") return 1 data = vop.resample_volume(data, r=r, t=None, ori=None, order=args.spline_order) if args.target is not None: try: args.target = np.array( [np.double(tok) for tok in args.target.split(",")]) / args.apix except: log.error( "Standard pose target must be comma-separated list of x,y,z coordinates" ) return 1 args.target -= args.origin args.target = np.where(np.abs(args.target) < 1, 0, args.target) ori = None if args.origin is center else args.origin - args.center r = vec2rot(args.target) t = np.linalg.norm(args.target) log.info("Euler angles are %s deg and shift is %f px" % (np.rad2deg(rot2euler(r)), t)) data = vop.resample_volume(data, r=r, t=args.target, ori=ori, order=args.spline_order, invert=args.target_invert) if args.euler is not None: try: args.euler = np.deg2rad( np.array([np.double(tok) for tok in args.euler.split(",")])) except: log.error( "Eulers must be comma-separated list of phi,theta,psi angles") return 1 r = euler2rot(*args.euler) offset = args.origin - 0.5 offset = offset - r.T.dot(offset) data = affine_transform(data, r.T, offset=offset, order=args.spline_order) if args.translate is not None: try: args.translate = np.array( [np.double(tok) for tok in args.translate.split(",")]) / args.apix except: log.error( "Translation vector must be comma-separated list of x,y,z coordinates" ) return 1 args.translate -= args.origin data = shift(data, -args.translate, order=args.spline_order) if args.boxsize is not None: args.boxsize = np.double(args.boxsize) data = zoom(data, args.boxsize / box, order=args.spline_order) args.apix = args.apix * box[0] / args.boxsize if final is None: final = data if args.final_mask is not None: final_mask = read(args.final_mask) final *= final_mask write(args.output, final, psz=args.apix) return 0