def subtract_outer(p1r, ptcl, submap_ft, refmap_ft, sx, sy, s, a, apix, coefs_method, r, nr, **kwargs): log = logging.getLogger('root') log.info("%d@%s Exp %f +/- %f" % (ptcl[star.UCSF.IMAGE_ORIGINAL_INDEX], ptcl[star.UCSF.IMAGE_ORIGINAL_PATH], np.mean(p1r), np.std(p1r))) ft = getattr(tls, 'ft', None) if ft is None: ft = rfft2(fftshift(p1r.copy()), threads=kwargs["fftthreads"], planner_effort="FFTW_ESTIMATE", overwrite_input=False, auto_align_input=True, auto_contiguous=True) tls.ft = ft if coefs_method >= 1: p1 = ft(p1r.copy(), np.zeros(ft.output_shape, dtype=ft.output_dtype)).copy() else: p1 = np.empty(ft.output_shape, ft.output_dtype) p1s = subtract(p1, submap_ft, refmap_ft, sx, sy, s, a, apix, ptcl[star.Relion.DEFOCUSU], ptcl[star.Relion.DEFOCUSV], ptcl[star.Relion.DEFOCUSANGLE], ptcl[star.Relion.PHASESHIFT], ptcl[star.Relion.VOLTAGE], ptcl[star.Relion.AC], ptcl[star.Relion.CS], ptcl[star.Relion.ANGLEROT], ptcl[star.Relion.ANGLETILT], ptcl[star.Relion.ANGLEPSI], ptcl[star.Relion.ORIGINX], ptcl[star.Relion.ORIGINY], coefs_method, r, nr) ift = getattr(tls, 'ift', None) if ift is None: ift = irfft2(p1s.copy(), threads=kwargs["fftthreads"], planner_effort="FFTW_ESTIMATE", auto_align_input=True, auto_contiguous=True) tls.ift = ift p1sr = fftshift(ift(p1s.copy(), np.zeros(ift.output_shape, dtype=ift.output_dtype)).copy()) log.info("%d@%s Exp %f +/- %f, Sub %f +/- %f" % (ptcl[star.UCSF.IMAGE_ORIGINAL_INDEX], ptcl[star.UCSF.IMAGE_ORIGINAL_PATH], np.mean(p1r), np.std(p1r), np.mean(p1sr), np.std(p1sr))) new_image = p1r - p1sr return new_image
def subtract_outer(p1r, p1rmask, ptcl, submap_ft, refmap_ft, sx, sy, s, a, apix, coefs_method, r, nr, **kwargs): log = logging.getLogger('root') log.debug("%d@%s Exp %f +/- %f" % (ptcl[star.UCSF.IMAGE_ORIGINAL_INDEX], ptcl[star.UCSF.IMAGE_ORIGINAL_PATH], np.mean(p1r), np.std(p1r))) ft = getattr(tls, 'ft', None) # Clip p1r to match to map dimensions p1r_clipped = clip_img(p1r, p1rmask.shape[0])*p1rmask if ft is None: ft = rfft2(fftshift(p1r_clipped.copy()), threads=kwargs["fftthreads"], planner_effort="FFTW_ESTIMATE", overwrite_input=False, auto_align_input=True, auto_contiguous=True) tls.ft = ft if coefs_method >= 1: p1 = ft(p1r_clipped.copy(), np.zeros(ft.output_shape, dtype=ft.output_dtype)).copy() else: p1 = np.empty(ft.output_shape, ft.output_dtype) p1s = subtract(p1, submap_ft, refmap_ft, sx, sy, s, a, apix, ptcl[star.Relion.DEFOCUSU], ptcl[star.Relion.DEFOCUSV], ptcl[star.Relion.DEFOCUSANGLE], ptcl[star.Relion.PHASESHIFT], ptcl[star.Relion.VOLTAGE], ptcl[star.Relion.AC], ptcl[star.Relion.CS], ptcl[star.Relion.ANGLEROT], ptcl[star.Relion.ANGLETILT], ptcl[star.Relion.ANGLEPSI], ptcl[star.Relion.ORIGINX], ptcl[star.Relion.ORIGINY], coefs_method, r, nr, kwargs["pfac"]) ift = getattr(tls, 'ift', None) if ift is None: ift = irfft2(p1s.copy(), threads=kwargs["fftthreads"], planner_effort="FFTW_ESTIMATE", auto_align_input=True, auto_contiguous=True) tls.ift = ift p1sr = fftshift(ift(p1s.copy(), np.zeros(ift.output_shape, dtype=ift.output_dtype)).copy()) # Zero pad p1sr p1sr = zero_pad(p1sr, p1r.shape[0]) log.debug("%d@%s Exp %f +/- %f, Sub %f +/- %f" % (ptcl[star.UCSF.IMAGE_ORIGINAL_INDEX], ptcl[star.UCSF.IMAGE_ORIGINAL_PATH], np.mean(p1r), np.std(p1r), np.mean(p1sr), np.std(p1sr))) new_image = p1r - p1sr if kwargs["crop"] is not None: orihalf = new_image.shape[0] // 2 newhalf = kwargs["crop"] // 2 x = orihalf - np.int(np.round(ptcl[star.Relion.ORIGINX])) y = orihalf - np.int(np.round(ptcl[star.Relion.ORIGINY])) new_image = new_image[y - newhalf:y + newhalf, x - newhalf:x + newhalf] return new_image
def calculateProjection( my_ndimage, my_rotation ): my_rotation = iseccFFT_v2.pyquat2scipy(my_rotation) my_rotmatrix = R.from_quat(my_rotation) """ Get random rotation for testing """ my_rotmatrix = R.random() my_rotmatrix = my_rotmatrix.as_matrix() #my_ndimage = iseccFFT_v2.swapAxes_ndimage( my_ndimage ) #my_2dsection = iseccFFT_v2.get2dsection( my_ndimage, my_rotation ) f3d = vop.vol_ft(my_ndimage, pfac=2 ) f2d = vop.interpolate_slice_numba(f3d, my_rotmatrix, pfac=2) ift = irfft2(f2d.copy(), planner_effort="FFTW_ESTIMATE", auto_align_input=True, auto_contiguous=True) proj = fftshift(ift(f2d.copy(), np.zeros(ift.output_shape, dtype=ift.output_dtype))) #iseccFFT_v2.plot2dimage( np.fft.ifftn(f2d).astype(np.float) ) #iseccFFT_v2.plot2dimage( proj ) return proj
def main(args): log = logging.getLogger('root') hdlr = logging.StreamHandler(sys.stdout) log.addHandler(hdlr) log.setLevel(logging.getLevelName(args.loglevel.upper())) df = star.parse_star(args.input, keep_index=False) star.augment_star_ucsf(df) maxshift = np.round(np.max(np.abs(df[star.Relion.ORIGINS].values))) if args.map is not None: if args.map.endswith(".npy"): log.info("Reading precomputed 3D FFT of volume") f3d = np.load(args.map) log.info("Finished reading 3D FFT of volume") if args.size is None: args.size = (f3d.shape[0] - 3) // args.pfac else: vol = mrc.read(args.map, inc_header=False, compat="relion") if args.mask is not None: mask = mrc.read(args.mask, inc_header=False, compat="relion") vol *= mask if args.size is None: args.size = vol.shape[0] if args.crop is not None and args.size // 2 < maxshift + args.crop // 2: log.error( "Some shifts are too large to crop (maximum crop is %d)" % (args.size - 2 * maxshift)) return 1 log.info("Preparing 3D FFT of volume") f3d = vop.vol_ft(vol, pfac=args.pfac, threads=args.threads) log.info("Finished 3D FFT of volume") else: log.error("Please supply a map") return 1 sz = (f3d.shape[0] - 3) // args.pfac apix = star.calculate_apix(df) * np.double(args.size) / sz sx, sy = np.meshgrid(np.fft.rfftfreq(sz), np.fft.fftfreq(sz)) s = np.sqrt(sx**2 + sy**2) a = np.arctan2(sy, sx) log.info("Projection size is %d, unpadded volume size is %d" % (args.size, sz)) log.info("Effective pixel size is %f A/px" % apix) if args.subtract and args.size != sz: log.error("Volume and projections must be same size when subtracting") return 1 if args.crop is not None and args.size // 2 < maxshift + args.crop // 2: log.error("Some shifts are too large to crop (maximum crop is %d)" % (args.size - 2 * maxshift)) return 1 ift = None with mrc.ZSliceWriter(args.output, psz=apix) as zsw: for i, p in df.iterrows(): f2d = project(f3d, p, s, sx, sy, a, pfac=args.pfac, apply_ctf=args.ctf, size=args.size, flip_phase=args.flip) if ift is None: ift = irfft2(f2d.copy(), threads=args.threads, planner_effort="FFTW_ESTIMATE", auto_align_input=True, auto_contiguous=True) proj = fftshift( ift(f2d.copy(), np.zeros(ift.output_shape, dtype=ift.output_dtype))) log.debug("%f +/- %f" % (np.mean(proj), np.std(proj))) if args.subtract: with mrc.ZSliceReader(p["ucsfImagePath"]) as zsr: img = zsr.read(p["ucsfImageIndex"]) log.debug("%f +/- %f" % (np.mean(img), np.std(img))) proj = img - proj if args.crop is not None: orihalf = args.size // 2 newhalf = args.crop // 2 x = orihalf - np.int(np.round(p[star.Relion.ORIGINX])) y = orihalf - np.int(np.round(p[star.Relion.ORIGINY])) proj = proj[y - newhalf:y + newhalf, x - newhalf:x + newhalf] zsw.write(proj) log.debug( "%d@%s: %d/%d" % (p["ucsfImageIndex"], p["ucsfImagePath"], i + 1, df.shape[0])) if args.star is not None: log.info("Writing output .star file") if args.crop is not None: df = star.recenter(df, inplace=True) if args.subtract: df[star.UCSF.IMAGE_ORIGINAL_PATH] = df[star.UCSF.IMAGE_PATH] df[star.UCSF.IMAGE_ORIGINAL_INDEX] = df[star.UCSF.IMAGE_INDEX] df[star.UCSF.IMAGE_PATH] = args.output df[star.UCSF.IMAGE_INDEX] = np.arange(df.shape[0]) star.simplify_star_ucsf(df) star.write_star(args.star, df) return 0
def main(args): log = logging.getLogger('root') hdlr = logging.StreamHandler(sys.stdout) log.addHandler(hdlr) log.setLevel(logging.getLevelName(args.loglevel.upper())) df = star.parse_star(args.input, keep_index=False) star.augment_star_ucsf(df) if args.map is not None: vol = mrc.read(args.map, inc_header=False, compat="relion") if args.mask is not None: mask = mrc.read(args.mask, inc_header=False, compat="relion") vol *= mask else: print("Please supply a map") return 1 f3d = vop.vol_ft(vol, pfac=args.pfac, threads=args.threads) sz = f3d.shape[0] // 2 - 1 sx, sy = np.meshgrid(np.fft.rfftfreq(sz), np.fft.fftfreq(sz)) s = np.sqrt(sx**2 + sy**2) a = np.arctan2(sy, sx) ift = None with mrc.ZSliceWriter(args.output) as zsw: for i, p in df.iterrows(): f2d = project(f3d, p, s, sx, sy, a, apply_ctf=args.ctf, size=args.size) if ift is None: ift = irfft2(f2d.copy(), threads=cpu_count(), planner_effort="FFTW_ESTIMATE", auto_align_input=True, auto_contiguous=True) proj = fftshift( ift(f2d.copy(), np.zeros(vol.shape[:-1], dtype=vol.dtype))) log.debug("%f +/- %f" % (np.mean(proj), np.std(proj))) if args.subtract: with mrc.ZSliceReader(p["ucsfImagePath"]) as zsr: img = zsr.read(p["ucsfImageIndex"]) log.debug("%f +/- %f" % (np.mean(img), np.std(img))) proj = img - proj zsw.write(proj) log.info( "%d@%s: %d/%d" % (p["ucsfImageIndex"], p["ucsfImagePath"], i + 1, df.shape[0])) if args.star is not None: if args.subtract: df[star.UCSF.IMAGE_ORIGINAL_PATH] = df[star.UCSF.IMAGE_PATH] df[star.UCSF.IMAGE_ORIGINAL_INDEX] = df[star.UCSF.IMAGE_INDEX] df[star.UCSF.IMAGE_PATH] = args.output df[star.UCSF.IMAGE_INDEX] = np.arange(df.shape[0]) star.simplify_star_ucsf(df) star.write_star(args.star, df) return 0