def configure(no_splines): # Create parameters file for analysis. # print("Creating XML file.") params = testingParameters() params.toXMLFile("fdecon.xml") # Create localization on a grid file. # print("Creating gridded localization.") emittersOnGrid.emittersOnGrid("grid_list.hdf5", settings.nx, settings.ny, 1.5, 20, settings.test_z_range, settings.test_z_offset) # Create randomly located localizations file. # print("Creating random localization.") emittersUniformRandom.emittersUniformRandom("random_list.hdf5", 1.0, settings.margin, settings.x_size, settings.y_size, settings.test_z_range) # Create sparser grid for PSF measurement. # print("Creating data for PSF measurement.") emittersOnGrid.emittersOnGrid("sparse_list.hdf5", 6, 3, 1.5, 40, 0.0, 0.0) if no_splines: return # Create beads.txt file for spline measurement. # with saH5Py.SAH5Py("sparse_list.hdf5") as h5: locs = h5.getLocalizations() numpy.savetxt("beads.txt", numpy.transpose(numpy.vstack((locs['x'], locs['y'])))) # Create drift file, this is used to displace the localizations in the # PSF measurement movie. # dz = numpy.arange(-settings.spline_z_range, settings.spline_z_range + 0.001, 0.01) drift_data = numpy.zeros((dz.size, 3)) drift_data[:, 2] = dz numpy.savetxt("drift.txt", drift_data) # Also create the z-offset file. # z_offset = numpy.ones((dz.size, 2)) z_offset[:, 1] = dz numpy.savetxt("z_offset.txt", z_offset) # Create simulated data for PSF measurement. # bg_f = lambda s, x, y, i3: background.UniformBackground( s, x, y, i3, photons=10) cam_f = lambda s, x, y, i3: camera.Ideal(s, x, y, i3, 100.) drift_f = lambda s, x, y, i3: drift.DriftFromFile(s, x, y, i3, "drift.txt") pp_f = lambda s, x, y, i3: photophysics.AlwaysOn(s, x, y, i3, 20000.0) if settings.use_dh: psf_f = lambda s, x, y, i3: psf.DHPSF( s, x, y, i3, 100.0, z_range=settings.spline_z_range) else: psf_f = lambda s, x, y, i3: psf.PupilFunction(s, x, y, i3, 100.0, settings.zmn) sim = simulate.Simulate(background_factory=bg_f, camera_factory=cam_f, drift_factory=drift_f, photophysics_factory=pp_f, psf_factory=psf_f, x_size=settings.x_size, y_size=settings.y_size) sim.simulate("spline.dax", "sparse_list.hdf5", dz.size) # Measure the PSF. # print("Measuring PSF.") psf_name = "psf.psf" measurePSFBeads.measurePSFBeads("spline.dax", "z_offset.txt", "beads.txt", psf_name, aoi_size=int(settings.spline_size + 1), pixel_size=settings.pixel_size * 1.0e-3, z_range=settings.spline_z_range) # Measure the Spline. # if True: print("Measuring Spline.") psfToSpline.psfToSpline(psf_name, "psf.spline", settings.spline_size)
def configure(cal_file = None): # Create parameters file for analysis. # print("Creating XML file.") params = testingParameters(cal_file = cal_file) params.toXMLFile("psf_fft.xml") # Create localization on a grid file. # print("Creating gridded localization.") emittersOnGrid.emittersOnGrid("grid_list.hdf5", settings.nx, settings.ny, 1.5, 20, settings.test_z_range, settings.test_z_offset) # Create randomly located localizations file. # print("Creating random localization.") emittersUniformRandom.emittersUniformRandom("random_list.hdf5", 1.0, settings.margin, settings.x_size, settings.y_size, settings.test_z_range) # Create sparser grid for PSF measurement. # print("Creating data for PSF measurement.") emittersOnGrid.emittersOnGrid("sparse_list.hdf5", 6, 3, 1.5, 40, 0.0, 0.0) if False: # Create PSF using pupil functions directly. # print("Creating (theoritical) psf.") makePSFFromPF.makePSF("psf.psf", settings.psf_size, settings.pixel_size * 1.0e-3, settings.zmn, settings.psf_z_range, settings.z_step) else: # Create beads.txt file for PSF measurement. # with saH5Py.SAH5Py("sparse_list.hdf5") as h5: locs = h5.getLocalizations() numpy.savetxt("beads.txt", numpy.transpose(numpy.vstack((locs['x'], locs['y'])))) # Create drift file, this is used to displace the localizations in the # PSF measurement movie. # dz = numpy.arange(-settings.psf_z_range, settings.psf_z_range + 0.001, 0.010) drift_data = numpy.zeros((dz.size, 3)) drift_data[:,2] = dz numpy.savetxt("drift.txt", drift_data) # Also create the z-offset file. # z_offset = numpy.ones((dz.size, 2)) z_offset[:,1] = dz numpy.savetxt("z_offset.txt", z_offset) # Create simulated data for PSF measurement. # bg_f = lambda s, x, y, i3 : background.UniformBackground(s, x, y, i3, photons = 10) cam_f = lambda s, x, y, i3 : camera.Ideal(s, x, y, i3, 100.) drift_f = lambda s, x, y, i3 : drift.DriftFromFile(s, x, y, i3, "drift.txt") pp_f = lambda s, x, y, i3 : photophysics.AlwaysOn(s, x, y, i3, 20000.0) psf_f = lambda s, x, y, i3 : psf.PupilFunction(s, x, y, i3, 100.0, settings.zmn) sim = simulate.Simulate(background_factory = bg_f, camera_factory = cam_f, drift_factory = drift_f, photophysics_factory = pp_f, psf_factory = psf_f, x_size = settings.x_size, y_size = settings.y_size) sim.simulate("psf.dax", "sparse_list.hdf5", dz.size) # Measure the PSF using spliner/measure_psf_beads.py # print("Measuring PSF.") measurePSFBeads.measurePSFBeads("psf.dax", "z_offset.txt", "beads.txt", "psf.psf", aoi_size = int(settings.psf_size/2)+1, pixel_size = settings.pixel_size * 1.0e-3, z_range = settings.psf_z_range, z_step = settings.z_step)
def configure(no_splines, cal_file=None): # Create sCMOS calibration file if requested. # if cal_file is not None: offset = numpy.zeros( (settings.y_size, settings.x_size)) + settings.camera_offset variance = numpy.ones((settings.y_size, settings.x_size)) gain = numpy.ones( (settings.y_size, settings.x_size)) * settings.camera_gain rqe = numpy.ones((settings.y_size, settings.x_size)) numpy.save(cal_file, [offset, variance, gain, rqe, 2]) # Create parameters file for analysis. # print("Creating XML file.") params = testingParameters(cal_file=cal_file) params.toXMLFile("spliner.xml") # Create localization on a grid file. # print("Creating gridded localization.") emittersOnGrid.emittersOnGrid("grid_list.hdf5", settings.nx, settings.ny, 1.5, 20, settings.test_z_range, settings.test_z_offset) # Create randomly located localizations file. # print("Creating random localization.") emittersUniformRandom.emittersUniformRandom("random_list.hdf5", 1.0, settings.margin, settings.x_size, settings.y_size, settings.test_z_range) # Create sparser grid for PSF measurement. # print("Creating data for PSF measurement.") emittersOnGrid.emittersOnGrid("sparse_list.hdf5", 6, 3, 1.5, 40, 0.0, 0.0) if no_splines: return # Create beads.txt file for spline measurement. # with saH5Py.SAH5Py("sparse_list.hdf5") as h5: locs = h5.getLocalizations() numpy.savetxt("beads.txt", numpy.transpose(numpy.vstack((locs['x'], locs['y'])))) # Create drift file, this is used to displace the localizations in the # PSF measurement movie. # dz = numpy.arange(-settings.spline_z_range, settings.spline_z_range + 0.001, 0.01) drift_data = numpy.zeros((dz.size, 3)) drift_data[:, 2] = dz numpy.savetxt("drift.txt", drift_data) # Also create the z-offset file. # z_offset = numpy.ones((dz.size, 2)) z_offset[:, 1] = dz numpy.savetxt("z_offset.txt", z_offset) # Create simulated data for PSF measurement. # bg_f = lambda s, x, y, i3: background.UniformBackground( s, x, y, i3, photons=10) cam_f = lambda s, x, y, i3: camera.Ideal(s, x, y, i3, 100.) drift_f = lambda s, x, y, i3: drift.DriftFromFile(s, x, y, i3, "drift.txt") pp_f = lambda s, x, y, i3: photophysics.AlwaysOn(s, x, y, i3, 20000.0) psf_f = lambda s, x, y, i3: psf.PupilFunction(s, x, y, i3, 100.0, settings. zmn) sim = simulate.Simulate(background_factory=bg_f, camera_factory=cam_f, drift_factory=drift_f, photophysics_factory=pp_f, psf_factory=psf_f, x_size=settings.x_size, y_size=settings.y_size) sim.simulate("spline.dax", "sparse_list.hdf5", dz.size) # Measure the PSF. # print("Measuring PSF.") psf_name = "psf.psf" measurePSFBeads.measurePSFBeads("spline.dax", "z_offset.txt", "beads.txt", psf_name, aoi_size=int(settings.spline_size + 1), pixel_size=settings.pixel_size * 1.0e-3, z_range=settings.spline_z_range) # Smooth the PSF if requested. # if settings.smooth_psf: with open(psf_name, "rb") as fp: psf_data = pickle.load(fp) sm_psf = measurePSFUtils.smoothPSF(psf_data["psf"], xy_sigma=settings.smooth_psf_sigma, z_sigma=settings.smooth_psf_sigma) psf_data["psf"] = sm_psf psf_data["smoothed"] = True psf_name = "psf_smooth.psf" with open(psf_name, "wb") as fp: pickle.dump(psf_data, fp) # Measure the Spline. # if True: print("Measuring Spline.") psfToSpline.psfToSpline(psf_name, "psf.spline", settings.spline_size)
def measurePSF(): # Create sparse random localizations for PSF measurement. # print("Creating random localization.") emittersUniformRandom.emittersUniformRandom("sparse_random.hdf5", 0.0002, settings.margin, settings.x_size, settings.y_size, 0.0) # Create sparser grid for PSF measurement. # print("Creating data for PSF measurement.") emittersOnGrid.emittersOnGrid("sparse_grid.hdf5", 8, 3, 1.5, 40, 0.0, 0.0) # Create text files for PSF measurement. # locs = saH5Py.loadLocalizations("sparse_random.hdf5") [xf, yf] = iaUtilsC.removeNeighbors(locs["x"], locs["y"], 2.0 * ((settings.psf_size / 2) + 1)) numpy.savetxt("sparse_random.txt", numpy.transpose(numpy.vstack((xf, yf)))) locs = saH5Py.loadLocalizations("sparse_grid.hdf5") numpy.savetxt("sparse_grid.txt", numpy.transpose(numpy.vstack((locs['x'], locs['y'])))) # Create drift file, this is used to displace the localizations in the # PSF measurement movie. # dz = numpy.arange(-settings.psf_z_range, settings.psf_z_range + 0.001, 0.010) drift_data = numpy.zeros((dz.size, 3)) drift_data[:, 2] = dz numpy.savetxt("drift.txt", drift_data) # Also create the z-offset file. # z_offset = numpy.ones((dz.size, 2)) z_offset[:, 1] = dz numpy.savetxt("z_offset.txt", z_offset) z_offset[:, 0] = 0 numpy.savetxt("z_offset_none_valid.txt", z_offset) # Create simulated data for PSF measurement. # bg_f = lambda s, x, y, i3: background.UniformBackground( s, x, y, i3, photons=10) cam_f = lambda s, x, y, i3: camera.Ideal(s, x, y, i3, 100.) drift_f = lambda s, x, y, i3: drift.DriftFromFile(s, x, y, i3, "drift.txt") pp_f = lambda s, x, y, i3: photophysics.AlwaysOn(s, x, y, i3, 20000.0) psf_f = lambda s, x, y, i3: psf.PupilFunction(s, x, y, i3, 100.0, settings. zmn) sim = simulate.Simulate(background_factory=bg_f, camera_factory=cam_f, drift_factory=drift_f, photophysics_factory=pp_f, psf_factory=psf_f, x_size=settings.x_size, y_size=settings.y_size) if True: sim.simulate("sparse_grid.tif", "sparse_grid.hdf5", dz.size) sim.simulate("sparse_random.tif", "sparse_random.hdf5", dz.size) # Measure the PSF using spliner/measure_psf_beads.py and multiplane/measure_psf.py # diff_detected = False # Grid. if True: # Remove old results. for elt in [ "sparse_grid_beads.psf", "sparse_grid_hdf5_zo.psf", "sparse_grid_hdf5.psf", "sparse_grid_hdf5_mp_zo.psf" ]: if os.path.exists(elt): os.remove(elt) print("Measuring PSF (beads).") measurePSFBeads.measurePSFBeads("sparse_grid.tif", "z_offset.txt", "sparse_grid.txt", "sparse_grid_beads.psf", aoi_size=int(settings.psf_size / 2 + 1), z_range=settings.psf_z_range, z_step=settings.psf_z_step) print("Measuring PSF (HDF5, with zoffset).") spMeasurePSF.measurePSF("sparse_grid.tif", "z_offset.txt", "sparse_grid_ref.hdf5", "sparse_grid_hdf5_zo.psf", aoi_size=int(settings.psf_size / 2 + 1), z_range=settings.psf_z_range, z_step=settings.psf_z_step) print("Measuring PSF (HDF5).") spMeasurePSF.measurePSF("sparse_grid.tif", "", "sparse_grid_ref.hdf5", "sparse_grid_hdf5.psf", aoi_size=int(settings.psf_size / 2 + 1), z_range=settings.psf_z_range, z_step=settings.psf_z_step) multiplane_path = os.path.dirname( inspect.getfile(storm_analysis)) + "/multi_plane/" print("Measure PSF (multiplane).") psfZStack.psfZStack("sparse_grid.tif", "sparse_grid.hdf5", "sparse_grid_zstack", aoi_size=int(settings.psf_size / 2 + 1)) mpMeasurePSF.measurePSF("sparse_grid_zstack.npy", "z_offset.txt", "sparse_grid_hdf5_mp_zo.psf", z_range=settings.psf_z_range, z_step=settings.psf_z_step, normalize=True) # Check that the PSFs are the same. psf_beads = numpy.load("sparse_grid_beads.psf", allow_pickle=True)["psf"] psf_hdf5_zo = numpy.load("sparse_grid_hdf5_zo.psf", allow_pickle=True)["psf"] psf_hdf5 = numpy.load("sparse_grid_hdf5.psf", allow_pickle=True)["psf"] psf_hdf5_mp_zo = numpy.load("sparse_grid_hdf5_mp_zo.psf", allow_pickle=True)["psf"] diff_detected = diff_detected or psfDiffCheck(psf_beads, psf_hdf5_zo) diff_detected = diff_detected or psfDiffCheck(psf_beads, psf_hdf5) # Here we are only checking they are close. if (settings.psf_size >= 20): diff_detected = diff_detected or psfDiffCheck( psf_beads, psf_hdf5_mp_zo, atol=0.17, rtol=0.17) # Grid, no valid z offsets. These are supposed to fail. # if True: print("Measuring PSF (beads).") try: measurePSFBeads.measurePSFBeads( "sparse_grid.tif", "z_offset_none_valid.txt", "sparse_grid.txt", "sparse_grid_beads.psf", aoi_size=int(settings.psf_size / 2 + 1), z_range=settings.psf_z_range, z_step=settings.psf_z_step) except AssertionError: pass else: assert False, "spliner.measure_psf_beads did not fail!" print("Measuring PSF (HDF5, with zoffset).") try: spMeasurePSF.measurePSF("sparse_grid.tif", "z_offset_none_valid.txt", "sparse_grid_ref.hdf5", "sparse_grid_hdf5_zo.psf", aoi_size=int(settings.psf_size / 2 + 1), z_range=settings.psf_z_range, z_step=settings.psf_z_step) except AssertionError: pass else: assert False, "spliner.measure_psf did not fail!" print("Measure PSF (multiplane).") try: psfZStack.psfZStack("sparse_grid.tif", "sparse_grid.hdf5", "sparse_grid_zstack", aoi_size=int(settings.psf_size / 2 + 1)) mpMeasurePSF.measurePSF("sparse_grid_zstack.npy", "z_offset_none_valid.txt", "sparse_grid_hdf5_mp_zo.psf", z_range=settings.psf_z_range, z_step=settings.psf_z_step, normalize=True) except AssertionError: pass else: assert False, "multiplane PSF measurement did not fail!" # Random. if True: # Remove old results. for elt in [ "sparse_random_beads.psf", "sparse_random_hdf5_zo.psf", "sparse_random_hdf5.psf" ]: if os.path.exists(elt): os.remove(elt) print("Measuring PSF (beads).") measurePSFBeads.measurePSFBeads("sparse_random.tif", "z_offset.txt", "sparse_random.txt", "sparse_random_beads.psf", aoi_size=int(settings.psf_size / 2 + 1), z_range=settings.psf_z_range, z_step=settings.psf_z_step) print("Measuring PSF (HDF5, with zoffset).") spMeasurePSF.measurePSF("sparse_random.tif", "z_offset.txt", "sparse_random_ref.hdf5", "sparse_random_hdf5_zo.psf", aoi_size=int(settings.psf_size / 2 + 1), z_range=settings.psf_z_range, z_step=settings.psf_z_step) print("Measuring PSF (HDF5).") spMeasurePSF.measurePSF("sparse_random.tif", "", "sparse_random_ref.hdf5", "sparse_random_hdf5.psf", aoi_size=int(settings.psf_size / 2 + 1), z_range=settings.psf_z_range, z_step=settings.psf_z_step) psf_beads = numpy.load("sparse_random_beads.psf", allow_pickle=True)["psf"] psf_hdf5_zo = numpy.load("sparse_random_hdf5_zo.psf", allow_pickle=True)["psf"] psf_hdf5 = numpy.load("sparse_random_hdf5.psf", allow_pickle=True)["psf"] diff_detected = diff_detected or psfDiffCheck(psf_beads, psf_hdf5_zo) diff_detected = diff_detected or psfDiffCheck(psf_beads, psf_hdf5) if diff_detected: print("Difference detected in PSF measurements!") else: print("No differences detected, all good.") if False: with tifffile.TiffWriter("psf_diff.tif") as tf: for i in range(psf_beads.shape[0]): tf.save((psf_beads[i, :, :] - psf_hdf5_zo[i, :, :]).astype( numpy.float32))
def configure(): # Create PF for pupil function. # print("Creating pupil function.") pf_size = 2 * (settings.spline_size - 1) makePupilFn.makePupilFunction("pupilfn.pfn", pf_size, settings.pixel_size * 1.0e-3, settings.zmn, z_offset=settings.z_offset) # Create PSF using pupil functions directly. # if False: print("Creating (theoritical) psf.") makePSFFromPF.makePSF("psf_fft.psf", settings.spline_size, settings.pixel_size * 1.0e-3, settings.zmn, settings.psf_fft_z_range, settings.psf_fft_z_step) exit() # Localizations on a sparse parse grid for PSF # measurement for Spliner and PSF FFT. # print("Creating data for PSF measurement.") emittersOnGrid.emittersOnGrid("sparse_list.hdf5", 6, 3, 1.5, 40, 0.0, settings.z_offset) # Create beads.txt file for spline measurement. # with saH5Py.SAH5Py("sparse_list.hdf5") as h5: locs = h5.getLocalizations() numpy.savetxt("beads.txt", numpy.transpose(numpy.vstack((locs['x'], locs['y'])))) # Create drift file, this is used to displace the localizations in the # PSF measurement movie. # dz = numpy.arange(-settings.spline_z_range, settings.spline_z_range + 0.001, 0.01) drift_data = numpy.zeros((dz.size, 3)) drift_data[:, 2] = dz numpy.savetxt("drift.txt", drift_data) # Also create the z-offset file. # z_offset = numpy.ones((dz.size, 2)) z_offset[:, 1] = dz numpy.savetxt("z_offset.txt", z_offset) # Create simulated data for PSF measurement. # bg_f = lambda s, x, y, i3: background.UniformBackground( s, x, y, i3, photons=10) cam_f = lambda s, x, y, i3: camera.Ideal(s, x, y, i3, 100.0) drift_f = lambda s, x, y, i3: drift.DriftFromFile(s, x, y, i3, "drift.txt") pp_f = lambda s, x, y, i3: photophysics.AlwaysOn(s, x, y, i3, 20000.0) psf_f = lambda s, x, y, i3: psf.PupilFunction( s, x, y, i3, settings.pixel_size, settings.zmn, pf_size=pf_size) sim = simulate.Simulate(background_factory=bg_f, camera_factory=cam_f, drift_factory=drift_f, photophysics_factory=pp_f, psf_factory=psf_f, x_size=settings.x_size, y_size=settings.y_size) sim.simulate("psf.dax", "sparse_list.hdf5", dz.size) # Create spline for Spliner # # Measure the PSF for Spliner # print("Measuring PSF.") psf_name = "psf_spliner.psf" measurePSFBeads.measurePSFBeads("psf.dax", "z_offset.txt", "beads.txt", psf_name, aoi_size=int(settings.spline_size + 1), pixel_size=settings.pixel_size * 1.0e-3) # Measure the Spline. # # This is slow, sometimes you don't want to do it. if True: print("Measuring Spline.") psfToSpline.psfToSpline(psf_name, "psf.spline", settings.spline_size) # Create measured PSF for PSF FFT. # # Measure the PSF using spliner/measure_psf_beads.py # print("Measuring PSF.") measurePSFBeads.measurePSFBeads("psf.dax", "z_offset.txt", "beads.txt", "psf_fft.psf", aoi_size=int(settings.spline_size - 1), pixel_size=settings.pixel_size * 1.0e-3, z_range=settings.psf_fft_z_range, z_step=settings.psf_fft_z_step)