def __init__(self, psf_filename = None, **kwds): kwds["psf_filename"] = psf_filename super(CRPSFFn, self).__init__(**kwds) # Load the psf data. with open(psf_filename, 'rb') as fp: psf_data = pickle.load(fp) # Use C library to calculate the PSF and it's derivatives. psf = psf_data['psf'] self.psf_fft_c = psfFFTC.PSFFFT(psf_data['psf']) # Additional initializations. self.zmax = psf_data["zmax"] self.zmin = psf_data["zmin"] # I believe that this is the right way to scale the Z value based # on comparisons with the pupilfn equivalent of this class. This # is also the scaling that we use in psf_fn.py. self.scale_gSZ = (float(psf.shape[0]) - 1.0)/(self.zmax - self.zmin) # CR weights approximately every 25nm. self.n_zvals = int(round((self.zmax - self.zmin)/25.0)) self.delta_xy = self.pixel_size #self.delta_z = (self.getZMax() - self.getZMin())/float(self.n_zvals) self.delta_z = (self.zmax - self.zmin)/float(psf.shape[0])
def __init__(self, psf_filename=None, **kwds): super(PSFFn, self).__init__(**kwds) # Load the PSF data. with open(psf_filename, 'rb') as fp: psf_data = pickle.load(fp) # Initialize C library. psf = psf_data['psf'] self.psf_fft_c = psfFFTC.PSFFFT(psf) # Store some additional properties. self.pixel_size = psf_data["pixel_size"] self.psf_shape = psf.shape # These are in units of nanometers. self.zmax = psf_data["zmax"] self.zmin = psf_data["zmin"] self.scale_gSZ = (float(self.getZSize()) - 1.0) / (self.zmax - self.zmin) self.scale_rZ = 1.0e-3 * (self.zmax - self.zmin) / (float(self.getZSize()) - 1.0) # Sanity checks. assert ((psf.shape[0] % 2) == 1), "Z size must be an odd number." assert (psf.shape[1] == psf.shape[2]), "X/Y size must be the same." assert (self.zmax == -self.zmin), "z range must be symmetric."
def test_psf_fft3(): """ Test PSF dx calculation. """ dx = 0.2 [pf_psf, geo, pf] = makePSFAndPF(-0.4, 0.4, 0.05) pfft = psfFFTC.PSFFFT(pf_psf) pfft.translate(dx, 0.0, 0.0) dx_exact = pfft.getPSFdx() psf = pfft.getPSF() pfft.translate(dx + 1.0e-6, 0.0, 0.0) dx_calc = (pfft.getPSF() - psf) / 1.0e-6 if False: print(numpy.max(numpy.abs(dx_exact - dx_calc))) with tifffile.TiffWriter( storm_analysis.getPathOutputTest("test_psf_fft3.tif")) as tf: tf.save(dx_exact.astype(numpy.float32)) tf.save(dx_calc.astype(numpy.float32)) assert (numpy.max(numpy.abs(dx_exact - dx_calc))) < 1.0e-6 pfft.cleanup()
def test_psf_fft2(): """ Test translated PSF calculation. """ dx = 0.5 dy = 0.25 dz = 0.2 [pf_psf, geo, pf] = makePSFAndPF(-0.4, 0.4, 0.05) pfft = psfFFTC.PSFFFT(pf_psf) pfft.translate(dy, dx, dz * (pf_psf.shape[0] - 1) / 0.8) psf_fft = pfft.getPSF() defocused = geo.changeFocus(pf, dz) translated = geo.translatePf(defocused, dx, dy) psf_pf = pupilMath.intensity(pupilMath.toRealSpace(translated)) if False: print(numpy.max(numpy.abs(psf_fft - psf_pf))) with tifffile.TiffWriter( storm_analysis.getPathOutputTest("test_psf_fft2.tif")) as tf: tf.save(psf_fft.astype(numpy.float32)) tf.save(psf_pf.astype(numpy.float32)) assert (numpy.max(numpy.abs(psf_fft - psf_pf))) < 1.0e-10 pfft.cleanup()
def test_psf_fft7(): """ Test against the Python version, translation. """ dx = 0.5 dy = 0.25 dz = 0.2 [pf_psf, geo, pf] = makePSFAndPF(-0.4, 0.4, 0.05) pfft_c = psfFFTC.PSFFFT(pf_psf) pfft_py = psfFFTPy.PSFFFT(pf_psf) pfft_c.translate(dx, dy, dz) pfft_py.translate(dx, dy, dz) psf_c = pfft_c.getPSF() psf_py = pfft_py.getPSF() if False: print(numpy.max(numpy.abs(psf_c - psf_py))) with tifffile.TiffWriter( storm_analysis.getPathOutputTest("test_psf_fft7.tif")) as tf: tf.save(psf_c.astype(numpy.float32)) tf.save(psf_py.astype(numpy.float32)) assert (numpy.max(numpy.abs(psf_c - psf_py))) < 1.0e-6 pfft_c.cleanup()
def test_psf_fft1(): """ Test untranslated PSF calculation. """ [pf_psf, geo, pf] = makePSFAndPF(-0.4, 0.4, 0.05) pfft = psfFFTC.PSFFFT(pf_psf) psf_fft = pfft.getPSF() psf_pf = pupilMath.intensity(pupilMath.toRealSpace(pf)) if False: print(numpy.max(numpy.abs(psf_fft - psf_pf))) with tifffile.TiffWriter( storm_analysis.getPathOutputTest("test_psf_fft1.tif")) as tf: tf.save(psf_fft.astype(numpy.float32)) tf.save(psf_pf.astype(numpy.float32)) assert (numpy.max(numpy.abs(psf_fft - psf_pf))) < 1.0e-10 pfft.cleanup()
def test_psf_fft10(): """ Test against the Python version, dz. """ [pf_psf, geo, pf] = makePSFAndPF(-0.4, 0.4, 0.05) pfft_c = psfFFTC.PSFFFT(pf_psf) pfft_py = psfFFTPy.PSFFFT(pf_psf) psf_dz_c = pfft_c.getPSFdz() psf_dz_py = pfft_py.getPSFdz() if False: print(numpy.max(numpy.abs(psf_dz_c - psf_dz_py))) with tifffile.TiffWriter( storm_analysis.getPathOutputTest("test_psf_fft10.tif")) as tf: tf.save(psf_dz_c.astype(numpy.float32)) tf.save(psf_dz_py.astype(numpy.float32)) assert (numpy.max(numpy.abs(psf_dz_c - psf_dz_py))) < 1.0e-6 pfft_c.cleanup()