Пример #1
0
def configure(cal_file=None):

    # Create sCMOS calibration file if not specified.
    #
    if cal_file is None:
        cal_file = "calib.npy"
        offset = numpy.zeros(
            (settings.y_size, settings.x_size)) + settings.camera_offset
        variance = numpy.ones(
            (settings.y_size, settings.x_size)) * settings.camera_variance
        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)
    params.toXMLFile("scmos.xml", pretty=True)

    # Create localization on a grid file.
    #
    print("Creating gridded localization.")
    emittersOnGrid.emittersOnGrid("grid_list.hdf5", settings.nx, settings.ny,
                                  1.5, 20, 0.0, 0.0)

    # Create randomly located localizations file.
    #
    print("Creating random localization.")
    emittersUniformRandom.emittersUniformRandom("random_list.hdf5", 1.0, 10,
                                                settings.x_size,
                                                settings.y_size, 0.0)
Пример #2
0
def configure():

    # Create directory, if necessary.
    if not os.path.exists(settings.wdir):
        os.makedirs(settings.wdir)

    # Create parameters file for analysis.
    #
    print("Creating XML file.")
    params = testingParameters()
    params.toXMLFile("multiplane.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 sCMOS camera calibration files.
    #
    numpy.save(os.path.join(settings.wdir, "calib.npy"), [
        numpy.zeros(
            (settings.y_size, settings.x_size)) + settings.camera_offset,
        numpy.ones(
            (settings.y_size, settings.x_size)) * settings.camera_variance,
        numpy.ones((settings.y_size, settings.x_size)) * settings.camera_gain,
        numpy.ones((settings.y_size, settings.x_size)), 2
    ])
    shutil.copyfile(os.path.join(settings.wdir, "calib.npy"), "calib.npy")

    # Create mapping file.
    with open(os.path.join(settings.wdir, "map.map"), 'wb') as fp:
        pickle.dump(settings.mappings, fp)
    shutil.copyfile(os.path.join(settings.wdir, "map.map"), "map.map")

    # Create pupil functions for 'pupilfn'.
    print("Creating pupil functions.")
    for i in range(len(settings.z_planes)):
        fname = "c" + str(i + 1) + "_pupilfn.pfn"
        makePupilFn.makePupilFunction(os.path.join(settings.wdir, fname),
                                      settings.psf_size,
                                      settings.pixel_size * 1.0e-3,
                                      settings.pupil_fn,
                                      z_offset=-settings.z_planes[i])

        shutil.copyfile(os.path.join(settings.wdir, fname), fname)

    # Calculate Cramer-Rao weighting.
    #
    print("Calculating weights.")
    planeWeighting.runPlaneWeighting("multiplane.xml",
                                     os.path.join(settings.wdir,
                                                  "weights.npy"),
                                     [settings.photons[0]],
                                     settings.photons[1],
                                     no_plots=True)
Пример #3
0
def configure():
    # Create parameters file for analysis.
    #
    print("Creating XML file.")
    params = testingParameters()
    params.toXMLFile("dao.xml")

    # Create localization on a grid file.
    #
    print("Creating gridded localization.")
    emittersOnGrid.emittersOnGrid("grid_list.hdf5", settings.nx, settings.ny,
                                  1.5, 20, 0.0, 0.0)

    # Create randomly located localizations file.
    #
    print("Creating random localization.")
    emittersUniformRandom.emittersUniformRandom("random_list.hdf5", 1.0,
                                                settings.margin,
                                                settings.x_size,
                                                settings.y_size, 0.0)
Пример #4
0
def configure():
    # Create parameters file for analysis.
    #
    print("Creating XML file.")
    params = testingParameters()
    params.toXMLFile("multiplane.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 sCMOS camera calibration files.
    #
    numpy.save("calib.npy", [
        numpy.zeros(
            (settings.y_size, settings.x_size)) + settings.camera_offset,
        numpy.ones(
            (settings.y_size, settings.x_size)) * settings.camera_variance,
        numpy.ones((settings.y_size, settings.x_size)) * settings.camera_gain,
        numpy.ones((settings.y_size, settings.x_size)), 2
    ])

    # Create mapping file.
    with open("map.map", 'wb') as fp:
        pickle.dump(settings.mappings, fp)
Пример #5
0
def configure(cal_file = None):
    
    # Create parameters file for analysis.
    #
    print("Creating XML file.")
    params = testingParameters(cal_file = cal_file)
    params.toXMLFile("pupilfn.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 the pupil function.
    #
    print("Creating pupil function.")
    makePupilFn.makePupilFunction("pupil_fn.pfn",
                                  settings.pupil_size,
                                  settings.pixel_size * 1.0e-3,
                                  settings.zmn)
Пример #6
0
def createLocalizations():
    emittersOnGrid.emittersOnGrid("sim_locs.hdf5", nx, ny, 1.5, 20, 0.0, 0.0)
Пример #7
0
def configure():
    # Create analysis XML files.
    #
    print("Creating XML files.")
    params = testingParametersSCMOS()
    params.toXMLFile("scmos.xml")

    params = testingParametersMC()
    params.toXMLFile("multicolor.xml")

    # Useful variables
    aoi_size = int(settings.psf_size / 2) + 1

    # Create sCMOS data and HDF5 files we'll need for the simulation.
    #
    if True:

        # Create sCMOS camera calibration files.
        #
        numpy.save("calib.npy", [
            numpy.zeros(
                (settings.y_size, settings.x_size)) + settings.camera_offset,
            numpy.ones(
                (settings.y_size, settings.x_size)) * settings.camera_variance,
            numpy.ones(
                (settings.y_size, settings.x_size)) * settings.camera_gain,
            numpy.ones((settings.y_size, settings.x_size)), 2
        ])

        # Create localization on a grid file.
        #
        print("Creating gridded localizations.")
        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 (for STORM movies).
        #
        print("Creating random localizations.")
        emittersUniformRandom.emittersUniformRandom("random_storm.hdf5", 1.0,
                                                    settings.margin,
                                                    settings.x_size,
                                                    settings.y_size,
                                                    settings.test_z_range)

        # Create randomly located localizations file (for mapping measurement).
        #
        print("Creating random localizations.")
        emittersUniformRandom.emittersUniformRandom("random_map.hdf5", 0.0003,
                                                    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("psf_list.hdf5", 6, 3, 1.5, 40, 0.0, 0.0)

    ## This part makes / tests measuring the mapping.
    ##
    if True:
        print("Measuring mapping.")

        # Make localization files for simulations.
        #
        locs = saH5Py.loadLocalizations("random_map.hdf5")
        locs["z"][:] = 1.0e-3 * settings.z_planes[0]
        saH5Py.saveLocalizations("c1_random_map.hdf5", locs)
        for i in range(1, 4):
            locs["x"] += settings.dx
            locs["y"] += settings.dy
            locs["z"][:] = settings.z_planes[i]
            saH5Py.saveLocalizations("c" + str(i + 1) + "_random_map.hdf5",
                                     locs)

        # Make localization files for simulations.
        #
        locs = saH5Py.loadLocalizations("random_map.hdf5")
        locs["z"][:] = 1.0e-3 * settings.z_planes[0]
        saH5Py.saveLocalizations("c1_random_map.hdf5", locs)
        for i in range(1, 4):
            locs["x"] += settings.dx
            locs["y"] += settings.dy
            locs["z"][:] = settings.z_planes[i]
            saH5Py.saveLocalizations("c" + str(i + 1) + "_random_map.hdf5",
                                     locs)

        # Make simulated mapping data.
        #
        bg_f = lambda s, x, y, h5: background.UniformBackground(
            s, x, y, h5, photons=10)
        cam_f = lambda s, x, y, h5: camera.SCMOS(s, x, y, h5, "calib.npy")
        pp_f = lambda s, x, y, h5: photophysics.AlwaysOn(s, x, y, h5, 20000.0)
        psf_f = lambda s, x, y, i3: psf.GaussianPSF(s, x, y, i3, settings.
                                                    pixel_size)

        sim = simulate.Simulate(background_factory=bg_f,
                                camera_factory=cam_f,
                                photophysics_factory=pp_f,
                                psf_factory=psf_f,
                                x_size=settings.x_size,
                                y_size=settings.y_size)

        for i in range(4):
            sim.simulate("c" + str(i + 1) + "_map.dax",
                         "c" + str(i + 1) + "_random_map.hdf5", 1)

        # Analyze simulated mapping data
        #
        for i in range(4):
            h5_name = "c" + str(i + 1) + "_map.hdf5"
            if os.path.exists(h5_name):
                os.remove(h5_name)
            scmos.analyze("c" + str(i + 1) + "_map.dax", h5_name, "scmos.xml")

        # Measure mapping.
        #
        for i in range(3):
            micrometry.runMicrometry("c1_map.hdf5",
                                     "c" + str(i + 2) + "_map.hdf5",
                                     "c1_c" + str(i + 2) + "_map.map",
                                     min_size=5.0,
                                     max_size=100.0,
                                     max_neighbors=20,
                                     tolerance=1.0e-2,
                                     no_plots=True)

        # Merge mapping and save results.
        #
        merged_map = mergeMaps.mergeMaps(
            ["c1_c2_map.map", "c1_c3_map.map", "c1_c4_map.map"])

        with open("map.map", 'wb') as fp:
            pickle.dump(merged_map, fp)

        # Print mapping.
        #
        if True:
            print("Mapping is:")
            printMapping.printMapping("map.map")
            print("")

        # Check that mapping is close to what we expect (within 5%).
        #
        with open("map.map", 'rb') as fp:
            mappings = pickle.load(fp)

        for i in range(3):
            if not numpy.allclose(mappings["0_" + str(i + 1) + "_x"],
                                  numpy.array(
                                      [settings.dx * (i + 1), 1.0, 0.0]),
                                  rtol=0.05,
                                  atol=0.05):
                print("X mapping difference for channel", i + 1)
            if not numpy.allclose(mappings["0_" + str(i + 1) + "_y"],
                                  numpy.array(
                                      [settings.dy * (i + 1), 0.0, 1.0]),
                                  rtol=0.05,
                                  atol=0.05):
                print("Y mapping difference for channel", i + 1)

    ## This part measures / test the PSF measurement.
    ##
    if True:

        # 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.05,
                          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 measurements.
        #
        bg_f = lambda s, x, y, h5: background.UniformBackground(
            s, x, y, h5, photons=10)
        cam_f = lambda s, x, y, h5: camera.SCMOS(s, x, y, h5, "calib.npy")
        drift_f = lambda s, x, y, h5: drift.DriftFromFile(
            s, x, y, h5, "drift.txt")
        pp_f = lambda s, x, y, h5: photophysics.AlwaysOn(s, x, y, h5, 20000.0)
        psf_f = lambda s, x, y, h5: psf.PupilFunction(s, x, y, h5, settings.
                                                      pixel_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)

        if True:
            for i in range(4):
                sim.simulate("c" + str(i + 1) + "_zcal.dax",
                             "c" + str(i + 1) + "_random_map.hdf5", dz.size)

        # Get localizations to use for PSF measurement.
        #
        psfLocalizations.psfLocalizations("c1_map_ref.hdf5",
                                          "map.map",
                                          aoi_size=aoi_size)

        # Create PSF z stacks.
        #
        for i in range(4):
            psfZStack.psfZStack("c" + str(i + 1) + "_zcal.dax",
                                "c1_map_ref_c" + str(i + 1) + "_psf.hdf5",
                                "c" + str(i + 1) + "_zstack",
                                aoi_size=aoi_size)

        # Measure PSF.
        #
        for i in range(4):
            mpMeasurePSF.measurePSF("c" + str(i + 1) + "_zstack.npy",
                                    "z_offset.txt",
                                    "c" + str(i + 1) + "_psf_normed.psf",
                                    z_range=settings.psf_z_range,
                                    normalize=True)

    ## This part creates the splines.
    ##
    if True:
        print("Measuring Splines.")
        for i in range(4):
            psfToSpline.psfToSpline("c" + str(i + 1) + "_psf_normed.psf",
                                    "c" + str(i + 1) + "_psf.spline",
                                    int(settings.psf_size / 2))

    ## This part measures the Cramer-Rao weights.
    ##
    if True:
        print("Calculating weights.")
        planeWeighting.runPlaneWeighting("multicolor.xml",
                                         "weights.npy",
                                         [settings.photons[0][0]],
                                         settings.photons[0][1],
                                         no_plots=True)
def makeSampleData():
    # Create sample bead data for PSF measurement.
    #

    # Create sparser grid for PSF measurement.
    #
    print("Creating data for PSF measurement.")
    emittersOnGrid.emittersOnGrid("psf_locs.hdf5", 6, 3, 1.5, 40, 0.0, 0.0)

    # Create localization files for PSF measurement.
    #
    locs = saH5Py.loadLocalizations("psf_locs.hdf5")

    for i, z_offset in enumerate(z_planes):
        cx = mappings["0_" + str(i) + "_x"]
        cy = mappings["0_" + str(i) + "_y"]
        locs_temp = {"x" : locs["x"].copy(),
                     "y" : locs["y"].copy(),
                     "z" : locs["z"].copy()}
        xi = locs_temp["x"]
        yi = locs_temp["y"]
        xf = cx[0] + cx[1] * xi + cx[2] * yi
        yf = cy[0] + cy[1] * xi + cy[2] * yi
        locs_temp["x"] = xf
        locs_temp["y"] = yf
        locs_temp["z"][:] = z_offset
        
        saH5Py.saveLocalizations("c" + str(i+1) + "_psf.hdf5", locs_temp)

    # Create drift file, this is used to displace the localizations in the
    # PSF measurement movie.
    #
    dz = numpy.arange(-spline_z_range, 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 measurements.
    #
    bg_f = lambda s, x, y, h5 : background.UniformBackground(s, x, y, h5, photons = 10)
    cam_f = lambda s, x, y, h5 : camera.SCMOS(s, x, y, h5, "calib.npy")
    drift_f = lambda s, x, y, h5 : drift.DriftFromFile(s, x, y, h5, "drift.txt")
    pp_f = lambda s, x, y, h5 : photophysics.AlwaysOn(s, x, y, h5, 20000.0)
    psf_f = lambda s, x, y, h5 : psf.PupilFunction(s, x, y, h5, pixel_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 = x_size,
                            y_size = y_size)

    for i in range(len(z_planes)):
        sim.simulate("c" + str(i+1) + "_zcal.dax",
                     "c" + str(i+1) + "_psf.hdf5",
                     dz.size)
Пример #9
0
def createLocalizations():
    emittersOnGrid.emittersOnGrid("sim_locs.hdf5", nx, ny, 1.5, 20, 0.0, 0.0)
Пример #10
0
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)
Пример #11
0
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)
Пример #12
0
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,
                                  0.0,
                                  0.0)
    
    # Create randomly located localizations file.
    #
    print("Creating random localization.")
    emittersUniformRandom.emittersUniformRandom("random_list.hdf5",
                                                1.0,
                                                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_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 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.)
    pp_f = lambda s, x, y, i3 : photophysics.AlwaysOn(s, x, y, i3, 20000.0)
    psf_f = lambda s, x, y, i3 : psf.GaussianPSF(s, x, y, i3, settings.pixel_size)

    sim = simulate.Simulate(background_factory = bg_f,
                            camera_factory = cam_f,
                            photophysics_factory = pp_f,
                            psf_factory = psf_f,
                            dither = True,
                            x_size = settings.x_size,
                            y_size = settings.y_size)
                        
    sim.simulate("spline_2d.tif", "sparse_list.hdf5", 5)

    # Measure the PSF.
    #
    print("Measuring PSF.")
    psf_name = "psf.psf"
    measurePSF.measurePSF("spline_2d.tif",
                          "na",
                          "sparse_list.hdf5",
                          psf_name,
                          want2d = True,
                          aoi_size = int(settings.spline_size + 1),
                          pixel_size = settings.pixel_size * 1.0e-3)

    # Measure the Spline.
    #
    if True:
        print("Measuring Spline.")
        psfToSpline.psfToSpline(psf_name, "psf.spline", settings.spline_size)
Пример #13
0
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)
Пример #14
0
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)
Пример #15
0
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 makeSampleData():
    # Create sample bead data for PSF measurement.
    #

    # Create sparser grid for PSF measurement.
    #
    print("Creating data for PSF measurement.")
    emittersOnGrid.emittersOnGrid("psf_locs.hdf5", 6, 3, 1.5, 40, 0.0, 0.0)

    # Create localization files for PSF measurement.
    #
    locs = saH5Py.loadLocalizations("psf_locs.hdf5")

    for i, z_offset in enumerate(z_planes):
        cx = mappings["0_" + str(i) + "_x"]
        cy = mappings["0_" + str(i) + "_y"]
        locs_temp = {
            "x": locs["x"].copy(),
            "y": locs["y"].copy(),
            "z": locs["z"].copy()
        }
        xi = locs_temp["x"]
        yi = locs_temp["y"]
        xf = cx[0] + cx[1] * xi + cx[2] * yi
        yf = cy[0] + cy[1] * xi + cy[2] * yi
        locs_temp["x"] = xf
        locs_temp["y"] = yf
        locs_temp["z"][:] = z_offset

        saH5Py.saveLocalizations("c" + str(i + 1) + "_psf.hdf5", locs_temp)

    # Create drift file, this is used to displace the localizations in the
    # PSF measurement movie.
    #
    dz = numpy.arange(-spline_z_range, 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 measurements.
    #
    bg_f = lambda s, x, y, h5: background.UniformBackground(
        s, x, y, h5, photons=10)
    cam_f = lambda s, x, y, h5: camera.SCMOS(s, x, y, h5, "calib.npy")
    drift_f = lambda s, x, y, h5: drift.DriftFromFile(s, x, y, h5, "drift.txt")
    pp_f = lambda s, x, y, h5: photophysics.AlwaysOn(s, x, y, h5, 20000.0)
    psf_f = lambda s, x, y, h5: psf.PupilFunction(s, x, y, h5, pixel_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=x_size,
                            y_size=y_size)

    for i in range(len(z_planes)):
        sim.simulate("c" + str(i + 1) + "_zcal.dax",
                     "c" + str(i + 1) + "_psf.hdf5", dz.size)
Пример #17
0
def configure(psf_model, no_splines):
    # Create parameters file for analysis.
    #
    print("Creating XML file.")
    params = testingParameters(psf_model)
    params.toXMLFile("multiplane.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("psf_list.hdf5", 6, 3, 1.5, 40, 0.0, 0.0)

    # Create sCMOS camera calibration files.
    #
    numpy.save("calib.npy", [
        numpy.zeros(
            (settings.y_size, settings.x_size)) + settings.camera_offset,
        numpy.ones(
            (settings.y_size, settings.x_size)) * settings.camera_variance,
        numpy.ones((settings.y_size, settings.x_size)) * settings.camera_gain,
        numpy.ones((settings.y_size, settings.x_size)), 2
    ])

    # Create mapping file.
    with open("map.map", 'wb') as fp:
        pickle.dump(settings.mappings, fp)

    if no_splines:
        return

    # Create pupil functions for 'pupilfn'.
    if (psf_model == "pupilfn"):
        print("Creating pupil functions.")
        for i in range(len(settings.z_planes)):
            makePupilFn.makePupilFunction("c" + str(i + 1) + "_pupilfn.pfn",
                                          settings.psf_size,
                                          settings.pixel_size * 1.0e-3,
                                          settings.pupil_fn,
                                          z_offset=-settings.z_planes[i])

    # Both 'spline' and 'psf_fft' need measured PSFs.
    else:

        # Create localization files for PSF measurement.
        #
        locs = saH5Py.loadLocalizations("psf_list.hdf5")

        for i, z_offset in enumerate(settings.z_planes):
            cx = settings.mappings["0_" + str(i) + "_x"]
            cy = settings.mappings["0_" + str(i) + "_y"]
            locs_temp = {
                "x": locs["x"].copy(),
                "y": locs["y"].copy(),
                "z": locs["z"].copy()
            }
            xi = locs_temp["x"]
            yi = locs_temp["y"]
            xf = cx[0] + cx[1] * xi + cx[2] * yi
            yf = cy[0] + cy[1] * xi + cy[2] * yi
            locs_temp["x"] = xf
            locs_temp["y"] = yf
            locs_temp["z"][:] = z_offset

            saH5Py.saveLocalizations("c" + str(i + 1) + "_psf.hdf5", locs_temp)

        # 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 measurements.
        #
        bg_f = lambda s, x, y, h5: background.UniformBackground(
            s, x, y, h5, photons=10)
        cam_f = lambda s, x, y, h5: camera.SCMOS(s, x, y, h5, "calib.npy")
        drift_f = lambda s, x, y, h5: drift.DriftFromFile(
            s, x, y, h5, "drift.txt")
        pp_f = lambda s, x, y, h5: photophysics.AlwaysOn(s, x, y, h5, 20000.0)
        psf_f = lambda s, x, y, h5: psf.PupilFunction(
            s, x, y, h5, settings.pixel_size, settings.pupil_fn)

        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)

        for i in range(len(settings.z_planes)):
            sim.simulate("c" + str(i + 1) + "_zcal.dax",
                         "c" + str(i + 1) + "_psf.hdf5", dz.size)

        # Measure the PSF.
        #
        print("Measuring PSFs.")
        for i in range(len(settings.z_planes)):
            psfZStack.psfZStack("c" + str(i + 1) + "_zcal.dax",
                                "c" + str(i + 1) + "_psf.hdf5",
                                "c" + str(i + 1) + "_zstack",
                                aoi_size=int(settings.psf_size / 2 + 1))

    # Measure PSF and calculate spline for Spliner.
    #
    if (psf_model == "spline"):

        # PSFs are independently normalized.
        #
        if settings.independent_heights:
            for i in range(len(settings.z_planes)):
                mpMeasurePSF.measurePSF("c" + str(i + 1) + "_zstack.npy",
                                        "z_offset.txt",
                                        "c" + str(i + 1) + "_psf_normed.psf",
                                        z_range=settings.spline_z_range,
                                        normalize=True)

        # PSFs are normalized to each other.
        #
        else:
            for i in range(len(settings.z_planes)):
                mpMeasurePSF.measurePSF("c" + str(i + 1) + "_zstack.npy",
                                        "z_offset.txt",
                                        "c" + str(i + 1) + "_psf.psf",
                                        z_range=settings.spline_z_range)

            norm_args = ["c1_psf.psf"]
            for i in range(len(settings.z_planes) - 1):
                norm_args.append("c" + str(i + 2) + "_psf.psf")
            normalizePSFs.normalizePSFs(norm_args)

        # Measure the spline for Spliner.
        #
        print("Measuring Spline.")
        for i in range(len(settings.z_planes)):
            psfToSpline.psfToSpline("c" + str(i + 1) + "_psf_normed.psf",
                                    "c" + str(i + 1) + "_psf.spline",
                                    int(settings.psf_size / 2))

    # Measure PSF and downsample for PSF FFT.
    #
    elif (psf_model == "psf_fft"):

        # PSFs are independently normalized.
        #
        if settings.independent_heights:
            for i in range(len(settings.z_planes)):
                mpMeasurePSF.measurePSF("c" + str(i + 1) + "_zstack.npy",
                                        "z_offset.txt",
                                        "c" + str(i + 1) + "_psf_normed.psf",
                                        z_range=settings.spline_z_range,
                                        normalize=True)

        # PSFs are normalized to each other.
        #
        else:
            for i in range(len(settings.z_planes)):
                mpMeasurePSF.measurePSF("c" + str(i + 1) + "_zstack.npy",
                                        "z_offset.txt",
                                        "c" + str(i + 1) + "_psf.psf",
                                        z_range=settings.spline_z_range)

            norm_args = ["c1_psf.psf"]
            for i in range(len(settings.z_planes) - 1):
                norm_args.append("c" + str(i + 2) + "_psf.psf")
            normalizePSFs.normalizePSFs(norm_args)

    # Calculate Cramer-Rao weighting.
    #
    print("Calculating weights.")
    planeWeighting.runPlaneWeighting("multiplane.xml",
                                     "weights.npy", [settings.photons[0][0]],
                                     settings.photons[0][1],
                                     no_plots=True)