Example #1
0
def collateRQE(dirs, settings):
    """
    Results collation for RQE correction.
    """
    for a_dir in dirs:
        print("Processing", a_dir)

        t_locs = saH5Py.loadLocalizations("grid_list.hdf5", fields=["x", "y"])

        t_locs_found = numpy.zeros_like(t_locs["x"])

        n_frames = 0
        with saH5Py.SAH5Py(os.path.join(a_dir, "test.hdf5")) as h5:
            for i in range(h5.getMovieLength()):
                n_frames += 1

                m_locs = h5.getLocalizationsInFrame(i, fields=["x", "y"])
                dist = iaUtilsC.peakToPeakDistAndIndex(t_locs['x'],
                                                       t_locs['y'],
                                                       m_locs['x'],
                                                       m_locs['y'],
                                                       max_distance=3)[0]

                for j in range(dist.size):
                    if (dist[j] > -0.1):
                        t_locs_found[j] += 1

        # Check results against the binomial distribution.
        p = numpy.sum(t_locs_found) / (n_frames * t_locs["x"].size)
        print("  Mean P found     : {0:.3f}".format(p))
        print("  Expected variance: {0:.3f}".format(n_frames * p * (1 - p)))
        print("  Actual variance  : {0:.3f}".format(numpy.var(t_locs_found)))
        print()
Example #2
0
def makeData():
    index = 1

    # Create 'tracked' files with different numbers of localizations
    # randomly pulled from the clusters file.
    #
    locs = saH5Py.loadLocalizations("clusters_list.hdf5", fields = ["x", "y"])
    i_arr = numpy.arange(locs["x"].size)

    for reps in settings.n_reps:
    
        wdir = "test_{0:02d}".format(index)
        print(wdir)
        if not os.path.exists(wdir):
            os.makedirs(wdir)
        
        with saH5Py.SAH5Py(wdir + "/test.hdf5", is_existing = False, overwrite = True) as h5:
            h5.setMovieInformation(settings.x_size, settings.y_size, 1, "")
            h5.setPixelSize(settings.pixel_size)
            for i in range(reps):
                track_id = numpy.arange(i * settings.n_tracks_in_group,
                                        (i+1) * settings.n_tracks_in_group,
                                        dtype = numpy.int64)
                numpy.random.shuffle(i_arr)

                lx = locs["x"][i_arr]
                ly = locs["y"][i_arr]
            
                h5.addTracks({"x" : lx[:settings.n_tracks_in_group],
                              "y" : ly[:settings.n_tracks_in_group],
                              "track_id" : track_id})

        index += 1
Example #3
0
def makeData():
    index = 1

    # Create HDF5 files for each plane.
    #
    for elt in ["grid_list.hdf5", "random_storm.hdf5"]:
        locs = saH5Py.loadLocalizations(elt)
        locs["color"] = numpy.random.randint(4, size=locs["x"].size)
        zo = locs["z"].copy()

        locs["z"][:] = zo + 1.0e-3 * settings.z_planes[0]
        saH5Py.saveLocalizations("sim_input_c1_" + elt, locs)
        for i in range(1, 4):
            locs["x"] += settings.dx
            locs["y"] += settings.dy
            locs["z"][:] = zo + 1.0e-3 * settings.z_planes[i]
            saH5Py.saveLocalizations("sim_input_c" + str(i + 1) + "_" + elt,
                                     locs)

    if True:

        # Create a movie for each plane.
        for [bg, photons] in settings.photons:

            # Adjust photons by the number of planes.
            photons = photons / float(len(settings.z_planes))

            wdir = "test_{0:02d}".format(index)
            print(wdir)
            if not os.path.exists(wdir):
                os.makedirs(wdir)

            for i in range(4):
                bg_f = lambda s, x, y, i3: background.UniformBackground(
                    s, x, y, i3, photons=bg)
                cam_f = lambda s, x, y, i3: camera.SCMOS(
                    s, x, y, i3, "calib.npy")
                pp_f = lambda s, x, y, i3: photophysics.AlwaysOnMC(
                    s, x, y, i3, color=i, photons=photons)
                psf_f = lambda s, x, y, i3: psf.PupilFunction(
                    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)

                sim.simulate(wdir + "/test_c" + str(i + 1) + ".dax",
                             "sim_input_c" + str(i + 1) + "_grid_list.hdf5",
                             settings.n_frames)

            index += 1
Example #4
0
def collate():

    dirs = sorted(glob.glob("test*"))

    if (len(dirs) == 0):
        print("No test directories found.")
        exit()

    # Load reference localizations.
    ref = saH5Py.loadLocalizations("sim_input_c1_grid_list.hdf5",
                                   fields=["color", "x", "y"])

    for a_dir in dirs:

        # Check color correspondence.
        #
        # Note: Currently only works for gridded localizations.
        #
        exp = saH5Py.loadTracks(a_dir + "/test.hdf5",
                                fields=["x", "y", "km_color"])

        # Identify corresponding peaks with 2 pixel maximum radius.
        p_index = iaUtilsC.peakToPeakDistAndIndex(exp["x"],
                                                  exp["y"],
                                                  ref["x"],
                                                  ref["y"],
                                                  max_distance=2.0)[1]

        # Create array for checking category correspondence.
        c_size = numpy.count_nonzero(p_index > -1)
        categories = numpy.zeros((c_size, 2), dtype=numpy.int32)
        i = 0
        for j in range(p_index.size):
            if (p_index[j] < 0):
                continue
            categories[i, 0] = int(ref["color"][p_index[j]])
            categories[i, 1] = int(exp["km_color"][j])
            i += 1

        # Measure fraction of experimental localizations assigned correctly. This
        # is complicated by the k-mean category being arbitrary.
        for i in range(8):
            ref_mask = (categories[:, 0] == i)
            if (numpy.count_nonzero(ref_mask) > 0):
                exp_cat = int(numpy.median(categories[ref_mask, 1]))
                exp_mask = (categories[:, 1] == exp_cat)

                total = numpy.count_nonzero(exp_mask)
                matched = numpy.count_nonzero(
                    numpy.logical_and(ref_mask, exp_mask))
                mismatched = total - matched
                print(
                    "Color {0:0d}, matching fraction is {1:.2f}, unmatched fraction is {2:.2f}, total {3:0d}"
                    .format(i, matched / total, mismatched / total, total))
Example #5
0
def collate():

    dirs = sorted(glob.glob("test*"))

    if(len(dirs) == 0):
        print("No test directories found.")
        exit()

    # Load reference localizations.
    ref = saH5Py.loadLocalizations("sim_input_c1_grid_list.hdf5", fields = ["color", "x", "y"])
    
    for a_dir in dirs:
    
        # Check color correspondence.
        #
        # Note: Currently only works for gridded localizations.
        #
        exp = saH5Py.loadTracks(a_dir + "/test.hdf5", fields = ["x", "y", "km_color"])

        # Identify corresponding peaks with 2 pixel maximum radius.
        p_index = iaUtilsC.peakToPeakDistAndIndex(exp["x"], exp["y"], ref["x"], ref["y"],
                                                  max_distance = 2.0)[1]

        # Create array for checking category correspondence.
        c_size = numpy.count_nonzero(p_index > -1)
        categories = numpy.zeros((c_size, 2), dtype = numpy.int32)
        i = 0
        for j in range(p_index.size):
            if (p_index[j] < 0):
                continue
            categories[i,0] = int(ref["color"][p_index[j]])
            categories[i,1] = int(exp["km_color"][j])
            i += 1

        # Measure fraction of experimental localizations assigned correctly. This
        # is complicated by the k-mean category being arbitrary.
        for i in range(8):
            ref_mask = (categories[:,0] == i)
            if(numpy.count_nonzero(ref_mask) > 0):
                exp_cat = int(numpy.median(categories[ref_mask,1]))
                exp_mask = (categories[:,1] == exp_cat)

                total = numpy.count_nonzero(exp_mask)
                matched = numpy.count_nonzero(numpy.logical_and(ref_mask, exp_mask))
                mismatched = total - matched
                print("Color {0:0d}, matching fraction is {1:.2f}, unmatched fraction is {2:.2f}, total {3:0d}".format(i, matched/total, mismatched/total, total))
Example #6
0
def makeData():
    index = 1

    # Create 'tracked' files with different numbers of localizations
    # randomly pulled from the clusters file.
    #
    locs = saH5Py.loadLocalizations("clusters_list.hdf5", fields=["x", "y"])
    i_arr = numpy.arange(locs["x"].size)

    for reps in settings.n_reps:

        wdir = "test_{0:02d}".format(index)
        print(wdir)
        if not os.path.exists(wdir):
            os.makedirs(wdir)

        with saH5Py.SAH5Py(os.path.join(wdir, "test.hdf5"),
                           is_existing=False,
                           overwrite=True) as h5:
            h5.setMovieInformation(settings.x_size, settings.y_size, 1, "")
            h5.setPixelSize(settings.pixel_size)
            for i in range(reps):
                track_id = numpy.arange(i * settings.n_tracks_in_group,
                                        (i + 1) * settings.n_tracks_in_group,
                                        dtype=numpy.int64)
                numpy.random.shuffle(i_arr)

                lx = locs["x"][i_arr]
                ly = locs["y"][i_arr]

                h5.addTracks({
                    "x": lx[:settings.n_tracks_in_group],
                    "y": ly[:settings.n_tracks_in_group],
                    "track_id": track_id
                })

        index += 1
Example #7
0
def test_zcal_5():
    """
    Test that the z_calibration and fitz_c agree on the parameters.
    """
    pixel_size = 100.0
    zv = numpy.arange(-0.6, 0.601, 0.01)
    wx_params = [3.0, 0.3, 0.5]
    wx = zCalibration.zcalib0(wx_params, zv)
    wy_params = [3.0, -0.3, 0.5]
    wy = zCalibration.zcalib0(wy_params, zv)

    # Write HDF5 file.
    h5_name = storm_analysis.getPathOutputTest("test_3ddao_zcal.hdf5")
    with saH5Py.SAH5Py(h5_name, is_existing = False, overwrite = True) as h5:
        h5.setMovieInformation(100, 100, wx.size, "")
        h5.setPixelSize(pixel_size)
        for i in range(wx.size):
            locs = {"x" : numpy.array([1.0]),
                    "y" : numpy.array([1.0]),
                    "xsigma" : numpy.array([0.5 * wx[i]]),
                    "ysigma" : numpy.array([0.5 * wy[i]])}
            h5.addLocalizations(locs, i)

    # Format parameters for fitzC.
    #
    wx_params = zCalibration.convertUnits(wx_params, pixel_size)
    wy_params = zCalibration.convertUnits(wy_params, pixel_size)

    # Fit z values.
    #
    fitzC.fitz(h5_name, 1.0, wx_params, wy_params, -0.7, 0.7)

    # Check results.
    fz = saH5Py.loadLocalizations(h5_name, fields = ["z"])["z"]

    assert(numpy.allclose(zv, fz, atol = 0.01, rtol = 0.01))
Example #8
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.")
    sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/"
    subprocess.call([
        "python", sim_path + "emitters_on_grid.py", "--bin", "grid_list.hdf5",
        "--nx",
        str(settings.nx), "--ny",
        str(settings.ny), "--spacing", "20", "--zrange",
        str(settings.test_z_range), "--zoffset",
        str(settings.test_z_offset)
    ])

    # Create randomly located localizations file.
    #
    print("Creating random localization.")
    subprocess.call([
        "python", sim_path + "emitters_uniform_random.py", "--bin",
        "random_list.hdf5", "--density", "1.0", "--margin",
        str(settings.margin), "--sx",
        str(settings.x_size), "--sy",
        str(settings.y_size), "--zrange",
        str(settings.test_z_range)
    ])

    # Create sparser grid for PSF measurement.
    #
    print("Creating data for PSF measurement.")
    sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/"
    subprocess.call([
        "python", sim_path + "emitters_on_grid.py", "--bin", "psf_list.hdf5",
        "--nx", "6", "--ny", "3", "--spacing", "40"
    ])

    # 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

    multiplane_path = os.path.dirname(
        inspect.getfile(storm_analysis)) + "/multi_plane/"

    # Create pupil functions for 'pupilfn'.
    if (psf_model == "pupilfn"):
        pupilfn_path = os.path.dirname(
            inspect.getfile(storm_analysis)) + "/pupilfn/"
        print("Creating pupil functions.")
        for i in range(len(settings.z_planes)):
            subprocess.call([
                "python", pupilfn_path + "make_pupil_fn.py", "--filename",
                "c" + str(i + 1) + "_pupilfn.pfn", "--size",
                str(settings.psf_size), "--pixel-size",
                str(settings.pixel_size), "--zmn",
                str(settings.pupil_fn), "--z-offset",
                str(-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.")
        psf_fft_path = os.path.dirname(
            inspect.getfile(storm_analysis)) + "/psf_fft/"
        spliner_path = os.path.dirname(
            inspect.getfile(storm_analysis)) + "/spliner/"
        for i in range(len(settings.z_planes)):
            subprocess.call([
                "python", multiplane_path + "psf_zstack.py", "--movie",
                "c" + str(i + 1) + "_zcal.dax", "--bin",
                "c" + str(i + 1) + "_psf.hdf5", "--zstack",
                "c" + str(i + 1) + "_zstack", "--scmos_cal", "calib.npy",
                "--aoi_size",
                str(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)):
                subprocess.call([
                    "python", multiplane_path + "measure_psf.py", "--zstack",
                    "c" + str(i + 1) + "_zstack.npy", "--zoffsets",
                    "z_offset.txt", "--psf_name",
                    "c" + str(i + 1) + "_psf_normed.psf", "--z_range",
                    str(settings.spline_z_range), "--normalize", "True"
                ])

        # PSFs are normalized to each other.
        #
        else:
            for i in range(len(settings.z_planes)):
                subprocess.call([
                    "python", multiplane_path + "measure_psf.py", "--zstack",
                    "c" + str(i + 1) + "_zstack.npy", "--zoffsets",
                    "z_offset.txt", "--psf_name",
                    "c" + str(i + 1) + "_psf.psf", "--z_range",
                    str(settings.spline_z_range)
                ])

            norm_args = [
                "python", multiplane_path + "normalize_psfs.py", "--psfs",
                "c1_psf.psf"
            ]
            for i in range(len(settings.z_planes) - 1):
                norm_args.append("c" + str(i + 2) + "_psf.psf")
            subprocess.call(norm_args)

        # Measure the spline for Spliner.
        #
        print("Measuring Spline.")
        for i in range(len(settings.z_planes)):
            subprocess.call([
                "python", spliner_path + "psf_to_spline.py", "--psf",
                "c" + str(i + 1) + "_psf_normed.psf", "--spline",
                "c" + str(i + 1) + "_psf.spline", "--spline_size",
                str(settings.psf_size)
            ])

    # 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)):
                subprocess.call([
                    "python", multiplane_path + "measure_psf.py", "--zstack",
                    "c" + str(i + 1) + "_zstack.npy", "--zoffsets",
                    "z_offset.txt", "--psf_name",
                    "c" + str(i + 1) + "_psf_normed.psf", "--z_range",
                    str(settings.psf_z_range), "--z_step",
                    str(settings.psf_z_step), "--normalize", "True"
                ])

        # PSFs are normalized to each other.
        #
        else:
            for i in range(len(settings.z_planes)):
                subprocess.call([
                    "python", multiplane_path + "measure_psf.py", "--zstack",
                    "c" + str(i + 1) + "_zstack.npy", "--zoffsets",
                    "z_offset.txt", "--psf_name",
                    "c" + str(i + 1) + "_psf.psf", "--z_range",
                    str(settings.psf_z_range), "--z_step",
                    str(settings.psf_z_step)
                ])

            norm_args = [
                "python", multiplane_path + "normalize_psfs.py", "--psfs",
                "c1_psf.psf"
            ]
            for i in range(len(settings.z_planes) - 1):
                norm_args.append("c" + str(i + 2) + "_psf.psf")
            subprocess.call(norm_args)

        # Downsample the PSF to 1x for PSF FFT.
        print("Downsampling PSF.")
        for i in range(len(settings.z_planes)):
            subprocess.call([
                "python", psf_fft_path + "downsample_psf.py", "--spliner_psf",
                "c" + str(i + 1) + "_psf_normed.psf", "--psf",
                "c" + str(i + 1) + "_psf_fft.psf", "--pixel-size",
                str(settings.pixel_size)
            ])

    # Calculate Cramer-Rao weighting.
    #
    print("Calculating weights.")
    subprocess.call([
        "python", multiplane_path + "plane_weighting.py", "--background",
        str(settings.photons[0][0]), "--photons",
        str(settings.photons[0][1]), "--output", "weights.npy", "--xml",
        "multiplane.xml", "--no_plots"
    ])
Example #9
0
def configure():
    # Get relevant paths.
    mm_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/micrometry/"
    mp_path = os.path.dirname(
        inspect.getfile(storm_analysis)) + "/multi_plane/"
    sp_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/spliner/"

    # 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, 1
        ])

        # Create localization on a grid file.
        #
        print("Creating gridded localizations.")
        sim_path = os.path.dirname(
            inspect.getfile(storm_analysis)) + "/simulator/"
        subprocess.call([
            "python", sim_path + "emitters_on_grid.py", "--bin",
            "grid_list.hdf5", "--nx",
            str(settings.nx), "--ny",
            str(settings.ny), "--spacing", "20", "--zrange",
            str(settings.test_z_range), "--zoffset",
            str(settings.test_z_offset)
        ])

        # Create randomly located localizations file (for STORM movies).
        #
        print("Creating random localizations.")
        subprocess.call([
            "python", sim_path + "emitters_uniform_random.py", "--bin",
            "random_storm.hdf5", "--density", "1.0", "--margin",
            str(settings.margin), "--sx",
            str(settings.x_size), "--sy",
            str(settings.y_size), "--zrange",
            str(settings.test_z_range)
        ])

        # Create randomly located localizations file (for mapping measurement).
        #
        print("Creating random localizations.")
        subprocess.call([
            "python", sim_path + "emitters_uniform_random.py", "--bin",
            "random_map.hdf5", "--density", "0.0003", "--margin",
            str(settings.margin), "--sx",
            str(settings.x_size), "--sy",
            str(settings.y_size)
        ])

        # Create sparser grid for PSF measurement.
        #
        print("Creating data for PSF measurement.")
        sim_path = os.path.dirname(
            inspect.getfile(storm_analysis)) + "/simulator/"
        subprocess.call([
            "python", sim_path + "emitters_on_grid.py", "--bin",
            "psf_list.hdf5", "--nx", "6", "--ny", "3", "--spacing", "40"
        ])

    ## 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):
            scmos.analyze("c" + str(i + 1) + "_map.dax",
                          "c" + str(i + 1) + "_map.hdf5", "scmos.xml")

        # Measure mapping.
        #
        for i in range(3):
            subprocess.call([
                "python", mm_path + "micrometry.py", "--locs1", "c1_map.hdf5",
                "--locs2", "c" + str(i + 2) + "_map.hdf5", "--results",
                "c1_c" + str(i + 2) + "_map.map", "--no_plots"
            ])

        # Merge mapping.
        #
        subprocess.call([
            "python", mm_path + "merge_maps.py", "--results", "map.map",
            "--maps", "c1_c2_map.map", "c1_c3_map.map", "c1_c4_map.map"
        ])

        # Print mapping.
        #
        if True:
            print("Mapping is:")
            subprocess.call([
                "python", mp_path + "print_mapping.py", "--mapping", "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.
        #
        subprocess.call([
            "python", mp_path + "psf_localizations.py", "--bin",
            "c1_map_ref.hdf5", "--map", "map.map", "--aoi_size",
            str(aoi_size)
        ])

        # Create PSF z stacks.
        #
        for i in range(4):
            subprocess.call([
                "python", mp_path + "psf_zstack.py", "--movie",
                "c" + str(i + 1) + "_zcal.dax", "--bin",
                "c1_map_ref_c" + str(i + 1) + "_psf.hdf5", "--zstack",
                "c" + str(i + 1) + "_zstack", "--scmos_cal", "calib.npy",
                "--aoi_size",
                str(aoi_size)
            ])

        # Measure PSF.
        #
        for i in range(4):
            subprocess.call([
                "python", mp_path + "measure_psf.py", "--zstack",
                "c" + str(i + 1) + "_zstack.npy", "--zoffsets", "z_offset.txt",
                "--psf_name", "c" + str(i + 1) + "_psf_normed.psf",
                "--z_range",
                str(settings.psf_z_range), "--normalize"
            ])

    ## This part creates the splines.
    ##
    if True:
        print("Measuring Splines.")
        for i in range(4):
            subprocess.call([
                "python", sp_path + "psf_to_spline.py", "--psf",
                "c" + str(i + 1) + "_psf_normed.psf", "--spline",
                "c" + str(i + 1) + "_psf.spline", "--spline_size",
                str(settings.psf_size)
            ])

    ## This part measures the Cramer-Rao weights.
    ##
    if True:
        print("Calculating weights.")
        subprocess.call([
            "python", mp_path + "plane_weighting.py", "--background",
            str(settings.photons[0][0]), "--photons",
            str(settings.photons[0][1]), "--output", "weights.npy", "--xml",
            "multicolor.xml", "--no_plots"
        ])
Example #10
0
def configure():
    # Get relevant paths.
    mm_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/micrometry/"
    mp_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/multi_plane/"
    sp_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/spliner/"

    # 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.")
        sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/"
        subprocess.call(["python", sim_path + "emitters_on_grid.py",
                         "--bin", "grid_list.hdf5",
                         "--nx", str(settings.nx),
                         "--ny", str(settings.ny),
                         "--spacing", "20",
                         "--zrange", str(settings.test_z_range),
                         "--zoffset", str(settings.test_z_offset)])

        # Create randomly located localizations file (for STORM movies).
        #
        print("Creating random localizations.")
        subprocess.call(["python", sim_path + "emitters_uniform_random.py",
                         "--bin", "random_storm.hdf5",
                         "--density", "1.0",
                         "--margin", str(settings.margin),
                         "--sx", str(settings.x_size),
                         "--sy", str(settings.y_size),
                         "--zrange", str(settings.test_z_range)])

        # Create randomly located localizations file (for mapping measurement).
        #
        print("Creating random localizations.")
        subprocess.call(["python", sim_path + "emitters_uniform_random.py",
                         "--bin", "random_map.hdf5",
                         "--density", "0.0003",
                         "--margin", str(settings.margin),
                         "--sx", str(settings.x_size),
                         "--sy", str(settings.y_size)])

        # Create sparser grid for PSF measurement.
        #
        print("Creating data for PSF measurement.")
        sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/"
        subprocess.call(["python", sim_path + "emitters_on_grid.py",
                         "--bin", "psf_list.hdf5",
                         "--nx", "6",
                         "--ny", "3",
                         "--spacing", "40"])


    ## 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):
            scmos.analyze("c" + str(i+1) + "_map.dax", "c" + str(i+1) + "_map.hdf5", "scmos.xml")

        # Measure mapping.
        #
        for i in range(3):
            subprocess.call(["python", mm_path + "micrometry.py",
                             "--locs1", "c1_map.hdf5",
                             "--locs2", "c" + str(i+2) + "_map.hdf5",
                             "--results", "c1_c" + str(i+2) + "_map.map",
                             "--no_plots"])

        # Merge mapping.
        #
        subprocess.call(["python", mm_path + "merge_maps.py",
                         "--results", "map.map",
                         "--maps", "c1_c2_map.map", "c1_c3_map.map", "c1_c4_map.map"])
        
        # Print mapping.
        #
        if True:
            print("Mapping is:")
            subprocess.call(["python", mp_path + "print_mapping.py",
                             "--mapping", "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.
        #
        subprocess.call(["python", mp_path + "psf_localizations.py",
                         "--bin", "c1_map_ref.hdf5",
                         "--map", "map.map",
                         "--aoi_size", str(aoi_size)])
    
        # Create PSF z stacks.
        #
        for i in range(4):
            subprocess.call(["python", mp_path + "psf_zstack.py",
                             "--movie", "c" + str(i+1) + "_zcal.dax",
                             "--bin", "c1_map_ref_c" + str(i+1) + "_psf.hdf5",
                             "--zstack", "c" + str(i+1) + "_zstack",
                             "--scmos_cal", "calib.npy",
                             "--aoi_size", str(aoi_size)])

        # Measure PSF.
        #
        for i in range(4):
            subprocess.call(["python", mp_path + "measure_psf.py",
                             "--zstack", "c" + str(i+1) + "_zstack.npy",
                             "--zoffsets", "z_offset.txt",
                             "--psf_name", "c" + str(i+1) + "_psf_normed.psf",
                             "--z_range", str(settings.psf_z_range),
                             "--normalize"])


    ## This part creates the splines.
    ##
    if True:
        print("Measuring Splines.")
        for i in range(4):
            subprocess.call(["python", sp_path + "psf_to_spline.py",
                             "--psf", "c" + str(i+1) + "_psf_normed.psf",
                             "--spline", "c" + str(i+1) + "_psf.spline",
                             "--spline_size", str(int(settings.psf_size/2))])
        
            
    ## This part measures the Cramer-Rao weights.
    ##
    if True:
        print("Calculating weights.")
        subprocess.call(["python", mp_path + "plane_weighting.py",
                         "--background", str(settings.photons[0][0]),
                         "--photons", str(settings.photons[0][1]),
                         "--output", "weights.npy",
                         "--xml", "multicolor.xml",
                         "--no_plots"])
def makeSampleData(mappings = None):
    # Create sample bead data for mapping measurement.
    #

    # Create randomly located localizations file (for STORM movies).
    #
    print("Creating random localizations.")
    emittersUniformRandom.emittersUniformRandom("random.hdf5", density, margin, x_size, y_size, 0.0)

    # Create mapping, if not specified.
    #
    if mappings is None:
        mappings = {"0_0_x" : numpy.array([0.0, 1.0, 0.0]),
                    "0_0_y" : numpy.array([0.0, 0.0, 1.0]),
                    "0_1_x" : numpy.array([2.0, 1.0, 0.0]),
                    "0_1_y" : numpy.array([5.0, 0.0, 1.0]),
                    "1_0_x" : numpy.array([-2.0, 1.0, 0.0]),
                    "1_0_y" : numpy.array([-5.0, 0.0, 1.0])}

    # Figure out number of planes in the mapping.
    #
    n_planes = 0
    for elt in mappings:
        [i, j] = map(int, elt.split("_")[:2])
        if (i > n_planes):
            n_planes = i

    n_planes += 1
    print(n_planes)
        
    # Create localization files for PSF measurement.
    #
    locs = saH5Py.loadLocalizations("random.hdf5")

    for i in range(n_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
        
        saH5Py.saveLocalizations("c" + str(i+1) + "_map.hdf5", locs_temp)

    # 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")
    pp_f = lambda s, x, y, h5 : photophysics.AlwaysOn(s, x, y, h5, 10000.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,
                            photophysics_factory = pp_f,
                            psf_factory = psf_f,
                            x_size = x_size,
                            y_size = y_size)

    for i in range(n_planes):
        sim.simulate("c" + str(i+1) + "_map.dax", "c" + str(i+1) + "_map.hdf5", 2)
Example #12
0
def makeData():
    index = 1

    if True:

        # Create .bin files for each plane.
        h5_locs = saH5Py.loadLocalizations("grid_list.hdf5")

        # Load channel to channel mapping file.
        with open("map.map", 'rb') as fp:
            mappings = pickle.load(fp)

        for i, z_plane in enumerate(settings.z_planes):
            cx = mappings["0_" + str(i) + "_x"]
            cy = mappings["0_" + str(i) + "_y"]
            xi = h5_locs["x"].copy()
            yi = h5_locs["y"].copy()
            zi = h5_locs["z"].copy()
            xf = cx[0] + cx[1] * xi + cx[2] * yi
            yf = cy[0] + cy[1] * xi + cy[2] * yi
            zf = zi + z_plane
            h5_temp = {"x": xf, "y": yf, "z": zf}
            saH5Py.saveLocalizations("sim_input_c" + str(i + 1) + ".hdf5",
                                     h5_temp)

        # Create a movie for each plane.
        for [bg, photons] in settings.photons:

            # Adjust photons by the number of planes.
            photons = photons / float(len(settings.z_planes))

            wdir = "test_{0:02d}".format(index)
            print(wdir)
            if not os.path.exists(wdir):
                os.makedirs(wdir)

            bg_f = lambda s, x, y, i3: background.UniformBackground(
                s, x, y, i3, photons=bg)
            cam_f = lambda s, x, y, i3: camera.SCMOS(s, x, y, i3, "calib.npy")
            pp_f = lambda s, x, y, i3: photophysics.AlwaysOn(
                s, x, y, i3, photons)
            psf_f = lambda s, x, y, i3: psf.PupilFunction(
                s, x, y, i3, settings.pixel_size, settings.pupil_fn)

            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(len(settings.z_planes)):
                sim.simulate(wdir + "/test_c" + str(i + 1) + ".dax",
                             "sim_input_c" + str(i + 1) + ".hdf5",
                             settings.n_frames)

            index += 1

    # Create "peak_locations" file if needed.
    #
    if hasattr(settings, "peak_locations") and (settings.peak_locations
                                                is not None):
        with saH5Py.SAH5Py("test_01/test_c1_ref.hdf5") as h5:
            locs = h5.getLocalizationsInFrame(0)

        if settings.peak_locations.endswith(".hdf5"):
            saH5Py.saveLocalizations(settings.peak_locations, locs)
        else:
            numpy.savetxt(
                settings.peak_locations,
                numpy.transpose(
                    numpy.vstack((locs['x'], locs['y'], locs['height'],
                                  locs['background']))))
Example #13
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.")
    sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/"
    subprocess.call(["python", sim_path + "emitters_on_grid.py",
                     "--bin", "grid_list.hdf5",
                     "--nx", str(settings.nx),
                     "--ny", str(settings.ny),
                     "--spacing", "20",
                     "--zrange", str(settings.test_z_range),
                     "--zoffset", str(settings.test_z_offset)])

    # Create randomly located localizations file.
    #
    print("Creating random localization.")
    subprocess.call(["python", sim_path + "emitters_uniform_random.py",
                     "--bin", "random_list.hdf5",
                     "--density", "1.0",
                     "--margin", str(settings.margin),
                     "--sx", str(settings.x_size),
                     "--sy", str(settings.y_size),
                     "--zrange", str(settings.test_z_range)])

    # Create sparser grid for PSF measurement.
    #
    print("Creating data for PSF measurement.")
    sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/"
    subprocess.call(["python", sim_path + "emitters_on_grid.py",
                     "--bin", "psf_list.hdf5",
                     "--nx", "6",
                     "--ny", "3",
                     "--spacing", "40"])

    # 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

    multiplane_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/multi_plane/"

    # Create pupil functions for 'pupilfn'.
    if (psf_model == "pupilfn"):
        pupilfn_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/pupilfn/"
        print("Creating pupil functions.")
        for i in range(len(settings.z_planes)):
            subprocess.call(["python", pupilfn_path + "make_pupil_fn.py",
                             "--filename", "c" + str(i+1) + "_pupilfn.pfn",
                             "--size", str(settings.psf_size),
                             "--pixel-size", str(settings.pixel_size),
                             "--zmn", str(settings.pupil_fn),
                             "--z-offset", str(-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.")
        psf_fft_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/psf_fft/"
        spliner_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/spliner/"
        for i in range(len(settings.z_planes)):
            subprocess.call(["python", multiplane_path + "psf_zstack.py",
                             "--movie", "c" + str(i+1) + "_zcal.dax",
                             "--bin", "c" + str(i+1) + "_psf.hdf5",
                             "--zstack", "c" + str(i+1) + "_zstack",
                             "--scmos_cal", "calib.npy",
                             "--aoi_size", str(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)):
                subprocess.call(["python", multiplane_path + "measure_psf.py",
                                 "--zstack", "c" + str(i+1) + "_zstack.npy",
                                 "--zoffsets", "z_offset.txt",
                                 "--psf_name", "c" + str(i+1) + "_psf_normed.psf",
                                 "--z_range", str(settings.spline_z_range),
                                 "--normalize", "True"])

        # PSFs are normalized to each other.
        #
        else:
            for i in range(len(settings.z_planes)):
                subprocess.call(["python", multiplane_path + "measure_psf.py",
                                 "--zstack", "c" + str(i+1) + "_zstack.npy",
                                 "--zoffsets", "z_offset.txt",
                                 "--psf_name", "c" + str(i+1) + "_psf.psf",
                                 "--z_range", str(settings.spline_z_range)])

            norm_args = ["python", multiplane_path + "normalize_psfs.py",
                         "--psfs", "c1_psf.psf"]
            for i in range(len(settings.z_planes)-1):
                norm_args.append("c" + str(i+2) + "_psf.psf")
            subprocess.call(norm_args)

        # Measure the spline for Spliner.
        #
        print("Measuring Spline.")
        for i in range(len(settings.z_planes)):
            subprocess.call(["python", spliner_path + "psf_to_spline.py",
                             "--psf", "c" + str(i+1) + "_psf_normed.psf",
                             "--spline", "c" + str(i+1) + "_psf.spline",
                             "--spline_size", str(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)):
                subprocess.call(["python", multiplane_path + "measure_psf.py",
                                 "--zstack", "c" + str(i+1) + "_zstack.npy",
                                 "--zoffsets", "z_offset.txt",
                                 "--psf_name", "c" + str(i+1) + "_psf_normed.psf",
                                 "--z_range", str(settings.psf_z_range),
                                 "--z_step", str(settings.psf_z_step),
                                 "--normalize", "True"])

        # PSFs are normalized to each other.
        #
        else:
            for i in range(len(settings.z_planes)):
                subprocess.call(["python", multiplane_path + "measure_psf.py",
                                 "--zstack", "c" + str(i+1) + "_zstack.npy",
                                 "--zoffsets", "z_offset.txt",
                                 "--psf_name", "c" + str(i+1) + "_psf.psf",
                                 "--z_range", str(settings.psf_z_range),
                                 "--z_step", str(settings.psf_z_step)])

            norm_args = ["python", multiplane_path + "normalize_psfs.py",
                         "--psfs", "c1_psf.psf"]
            for i in range(len(settings.z_planes)-1):
                norm_args.append("c" + str(i+2) + "_psf.psf")
            subprocess.call(norm_args)

    # Calculate Cramer-Rao weighting.
    #
    print("Calculating weights.")
    subprocess.call(["python", multiplane_path + "plane_weighting.py",
                     "--background", str(settings.photons[0][0]),
                     "--photons", str(settings.photons[0][1]),
                     "--output", "weights.npy",
                     "--xml", "multiplane.xml",
                     "--no_plots"])
def makeSampleData(mappings=None):
    # Create sample bead data for mapping measurement.
    #

    # Create randomly located localizations file (for STORM movies).
    #
    print("Creating random localizations.")
    emittersUniformRandom.emittersUniformRandom("random.hdf5", density, margin,
                                                x_size, y_size, 0.0)

    # Create mapping, if not specified.
    #
    if mappings is None:
        mappings = {
            "0_0_x": numpy.array([0.0, 1.0, 0.0]),
            "0_0_y": numpy.array([0.0, 0.0, 1.0]),
            "0_1_x": numpy.array([2.0, 1.0, 0.0]),
            "0_1_y": numpy.array([5.0, 0.0, 1.0]),
            "1_0_x": numpy.array([-2.0, 1.0, 0.0]),
            "1_0_y": numpy.array([-5.0, 0.0, 1.0])
        }

    # Figure out number of planes in the mapping.
    #
    n_planes = 0
    for elt in mappings:
        [i, j] = map(int, elt.split("_")[:2])
        if (i > n_planes):
            n_planes = i

    n_planes += 1
    print(n_planes)

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

    for i in range(n_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

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

    # 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")
    pp_f = lambda s, x, y, h5: photophysics.AlwaysOn(s, x, y, h5, 10000.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,
                            photophysics_factory=pp_f,
                            psf_factory=psf_f,
                            x_size=x_size,
                            y_size=y_size)

    for i in range(n_planes):
        sim.simulate("c" + str(i + 1) + "_map.dax",
                     "c" + str(i + 1) + "_map.hdf5", 2)
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)
Example #16
0
def makeData():
    index = 1

    if True:

        # Create localization files for each plane.
        h5_locs = saH5Py.loadLocalizations("grid_list.hdf5")
        
        # Load channel to channel mapping file.
        with open("map.map", 'rb') as fp:
            mappings = pickle.load(fp)

        for i, z_plane in enumerate(settings.z_planes):
            cx = mappings["0_" + str(i) + "_x"]
            cy = mappings["0_" + str(i) + "_y"]
            xi = h5_locs["x"].copy()
            yi = h5_locs["y"].copy()
            zi = h5_locs["z"].copy()
            xf = cx[0] + cx[1] * xi + cx[2] * yi
            yf = cy[0] + cy[1] * xi + cy[2] * yi
            zf = zi + z_plane
            h5_temp = {"x" : xf,
                       "y" : yf,
                       "z" : zf}
            saH5Py.saveLocalizations("sim_input_c" + str(i+1) + ".hdf5", h5_temp)
        
        # Create a movie for each plane.
        for [bg, photons] in settings.photons:

            # Adjust photons by the number of planes.
            photons = photons/float(len(settings.z_planes))

            wdir = "test_{0:02d}".format(index)
            print(wdir)
            if not os.path.exists(wdir):
                os.makedirs(wdir)

            bg_f = lambda s, x, y, i3 : background.UniformBackground(s, x, y, i3, photons = bg)
            cam_f = lambda s, x, y, i3 : camera.SCMOS(s, x, y, i3, "calib.npy")
            pp_f = lambda s, x, y, i3 : photophysics.AlwaysOn(s, x, y, i3, photons)
            psf_f = lambda s, x, y, i3 : psf.PupilFunction(s, x, y, i3, settings.pixel_size, settings.pupil_fn)
            
            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(len(settings.z_planes)):
                sim.simulate(wdir + "/test_c" + str(i+1) + ".dax",
                             "sim_input_c" + str(i+1) + ".hdf5",
                             settings.n_frames)
        
            index += 1


    # Create "peak_locations" file if needed.
    #
    if hasattr(settings, "peak_locations") and (settings.peak_locations is not None):
        with saH5Py.SAH5Py("test_01/test_c1_ref.hdf5") as h5:
            locs = h5.getLocalizationsInFrame(0)
        
        if settings.peak_locations.endswith(".hdf5"):
            saH5Py.saveLocalizations(settings.peak_locations, locs)
        else:
            numpy.savetxt(settings.peak_locations,
                          numpy.transpose(numpy.vstack((locs['x'],
                                                        locs['y'],
                                                        locs['height'],
                                                        locs['background']))))
Example #17
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)
Example #19
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)
Example #20
0
Hazen 01/18
"""
import numpy
import os

import storm_analysis.sa_library.sa_h5py as saH5Py

import settings

index = 1

# Create 'tracked' files with different numbers of localizations
# randomly pulled from the clusters file.
#
locs = saH5Py.loadLocalizations("clusters_list.hdf5", fields=["x", "y"])
i_arr = numpy.arange(locs["x"].size)

for reps in settings.n_reps:

    wdir = "test_{0:02d}".format(index)
    print(wdir)
    if not os.path.exists(wdir):
        os.makedirs(wdir)

    with saH5Py.SAH5Py(wdir + "/test.hdf5", is_existing=False,
                       overwrite=True) as h5:
        h5.setMovieInformation(settings.x_size, settings.y_size, 1, "")
        h5.setPixelSize(settings.pixel_size)
        for i in range(reps):
            track_id = numpy.arange(i * settings.n_tracks_in_group,
Example #21
0
def makeData():

    # Create .bin files for each plane.
    h5_locs = saH5Py.loadLocalizations("grid_list.hdf5")
        
    # Load channel to channel mapping file.
    with open("map.map", 'rb') as fp:
        mappings = pickle.load(fp)

    # Add z offset to reference localizations.
    x = h5_locs["x"].copy()
    y = h5_locs["y"].copy()
    z = h5_locs["z"].copy() + settings.z_planes[0]
    h5_temp = {"x" : x,
               "y" : y,
               "z" : z}
    saH5Py.saveLocalizations("sim_input_c1.hdf5", h5_temp)


    # Create a movie for first plane.
    [bg, photons] = settings.photons

    # Adjust photons by the number of planes.
    photons = photons/float(len(settings.z_planes))

    bg_f = lambda s, x, y, i3 : background.UniformBackground(s, x, y, i3, photons = bg)
    cam_f = lambda s, x, y, i3 : camera.SCMOS(s, x, y, i3, "calib.npy")
    pp_f = lambda s, x, y, i3 : photophysics.SimpleSTORM(s, x, y, i3,
                                                         photons = photons,
                                                         on_time = settings.on_time,
                                                         off_time = settings.off_time)
    psf_f = lambda s, x, y, i3 : psf.PupilFunction(s, x, y, i3, settings.pixel_size, settings.pupil_fn)

    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)

    sim.simulate(os.path.join(settings.wdir, "test_c1.dax"),
                 "sim_input_c1.hdf5",
                 settings.n_frames)

    # Create other movies.
    for i in range(1, len(settings.z_planes)):
        cx = mappings["0_" + str(i) + "_x"]
        cy = mappings["0_" + str(i) + "_y"]
        z_offset = settings.z_planes[i] - settings.z_planes[0]
            
        pp_f = lambda s, x, y, i3 : photophysics.Duplicate(s, x, y, i3,
                                                           h5_name = os.path.join(settings.wdir, "test_c1_ref.hdf5"),
                                                           cx = cx, cy = cy, z_offset = z_offset)

        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)

        sim.simulate(os.path.join(settings.wdir, "test_c" + str(i+1) + ".dax"),
                     "sim_input_c1.hdf5", # This is not actually used.
                     settings.n_frames)            

    # Remove any old XML files.
    for elt in glob.glob(os.path.join(settings.wdir, "job*.xml")):
        os.remove(elt)
            
    # Make analysis XML files.
    splitAnalysisXML.splitAnalysisXML(settings.wdir, "multiplane.xml", 0, settings.n_frames, settings.divisions)
Example #22
0
import storm_analysis.sa_library.sa_h5py as saH5Py

import storm_analysis.simulator.background as background
import storm_analysis.simulator.camera as camera
import storm_analysis.simulator.photophysics as photophysics
import storm_analysis.simulator.psf as psf
import storm_analysis.simulator.simulate as simulate

import settings

index = 1

if True:

    # Create .bin files for each plane.
    h5_locs = saH5Py.loadLocalizations("grid_list.hdf5")

    # Load channel to channel mapping file.
    with open("map.map", 'rb') as fp:
        mappings = pickle.load(fp)

    for i, z_plane in enumerate(settings.z_planes):
        cx = mappings["0_" + str(i) + "_x"]
        cy = mappings["0_" + str(i) + "_y"]
        xi = h5_locs["x"].copy()
        yi = h5_locs["y"].copy()
        zi = h5_locs["z"].copy()
        xf = cx[0] + cx[1] * xi + cx[2] * yi
        yf = cy[0] + cy[1] * xi + cy[2] * yi
        zf = zi + z_plane
        h5_temp = {"x": xf, "y": yf, "z": zf}
Example #23
0
def measurePSF():
    
    # Create sparse random localizations for PSF measurement.
    #
    print("Creating random localization.")
    sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/"
    subprocess.call(["python", sim_path + "emitters_uniform_random.py",
                     "--bin", "sparse_random.hdf5",
                     "--density", "0.0002",
                     "--margin", str(settings.margin),
                     "--sx", str(settings.x_size),
                     "--sy", str(settings.y_size)])

    # Create sparser grid for PSF measurement.
    #
    print("Creating data for PSF measurement.")
    sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/"
    subprocess.call(["python", sim_path + "emitters_on_grid.py",
                     "--bin", "sparse_grid.hdf5",
                     "--nx", "8",
                     "--ny", "3",
                     "--spacing", "40"])

    # 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.dax", "sparse_grid.hdf5", dz.size)
        sim.simulate("sparse_random.dax", "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:
        print("Measuring PSF (beads).")
        spliner_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/spliner/"
        subprocess.call(["python", spliner_path + "measure_psf_beads.py",
                         "--movie", "sparse_grid.dax",
                         "--zoffset", "z_offset.txt",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--beads", "sparse_grid.txt",
                         "--psf", "sparse_grid_beads.psf",
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])

        print("Measuring PSF (HDF5, with zoffset).")
        subprocess.call(["python", spliner_path + "measure_psf.py",
                         "--movie", "sparse_grid.dax",
                         "--bin", "sparse_grid_ref.hdf5",
                         "--psf", "sparse_grid_hdf5_zo.psf",
                         "--zoffset", "z_offset.txt",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])

        print("Measuring PSF (HDF5).")
        subprocess.call(["python", spliner_path + "measure_psf.py",
                         "--movie", "sparse_grid.dax",
                         "--bin", "sparse_grid_ref.hdf5",
                         "--psf", "sparse_grid_hdf5.psf",
                         "--zoffset", "",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])

        multiplane_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/multi_plane/"
        print("Measure PSF (multiplane).")
        subprocess.call(["python", multiplane_path + "psf_zstack.py",
                         "--movie", "sparse_grid.dax",
                         "--bin", "sparse_grid.hdf5",
                         "--zstack", "sparse_grid_zstack",
                         "--aoi_size", str(int(settings.psf_size/2)+1)])

        subprocess.call(["python", multiplane_path + "measure_psf.py",
                         "--zstack", "sparse_grid_zstack.npy",
                         "--zoffsets", "z_offset.txt",
                         "--psf_name", "sparse_grid_hdf5_mp_zo.psf",
                         "--z_range", str(settings.psf_z_range),
                         "--z_step", str(settings.psf_z_step),
                         "--normalize", "True"])

        # Check that the PSFs are the same.
        psf_beads = numpy.load("sparse_grid_beads.psf")["psf"]
        psf_hdf5_zo = numpy.load("sparse_grid_hdf5_zo.psf")["psf"]
        psf_hdf5 = numpy.load("sparse_grid_hdf5.psf")["psf"]
        psf_hdf5_mp_zo = numpy.load("sparse_grid_hdf5_mp_zo.psf")["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.
    if True:
        print("Measuring PSF (beads).")
        spliner_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/spliner/"
        try:
            subprocess.check_output(["python", spliner_path + "measure_psf_beads.py",
                                     "--movie", "sparse_grid.dax",
                                     "--zoffset", "z_offset_none_valid.txt",
                                     "--aoi_size", str(int(settings.psf_size/2)+1),
                                     "--beads", "sparse_grid.txt",
                                     "--psf", "sparse_grid_beads.psf",
                                     "--zrange", str(settings.psf_z_range),
                                     "--zstep", str(settings.psf_z_step)])
        except subprocess.CalledProcessError:
            pass
        else:
            assert False, "spliner.measure_psf_beads did not fail!"

        print("Measuring PSF (HDF5, with zoffset).")
        try:
            subprocess.check_output(["python", spliner_path + "measure_psf.py",
                                     "--movie", "sparse_grid.dax",
                                     "--bin", "sparse_grid_ref.hdf5",
                                     "--psf", "sparse_grid_hdf5_zo.psf",
                                     "--zoffset", "z_offset_none_valid.txt",
                                     "--aoi_size", str(int(settings.psf_size/2)+1),
                                     "--zrange", str(settings.psf_z_range),
                                     "--zstep", str(settings.psf_z_step)])
        except subprocess.CalledProcessError:
            pass
        else:
            assert False, "spliner.measure_psf did not fail!"            

        multiplane_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/multi_plane/"
        print("Measure PSF (multiplane).")
        try:
            subprocess.check_output(["python", multiplane_path + "psf_zstack.py",
                                    "--movie", "sparse_grid.dax",
                                     "--bin", "sparse_grid.hdf5",
                                     "--zstack", "sparse_grid_zstack",
                                     "--aoi_size", str(int(settings.psf_size/2)+1)])

            subprocess.check_output(["python", multiplane_path + "measure_psf.py",
                                     "--zstack", "sparse_grid_zstack.npy",
                                     "--zoffsets", "z_offset_none_valid.txt",
                                     "--psf_name", "sparse_grid_hdf5_mp_zo.psf",
                                     "--z_range", str(settings.psf_z_range),
                                     "--z_step", str(settings.psf_z_step),
                                     "--normalize", "True"])
        except subprocess.CalledProcessError:
            pass
        else:
            assert False, "multiplane PSF measurement did not fail!"

    # Random.
    if True:
        print("Measuring PSF (beads).")
        spliner_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/spliner/"
        subprocess.call(["python", spliner_path + "measure_psf_beads.py",
                         "--movie", "sparse_random.dax",
                         "--zoffset", "z_offset.txt",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--beads", "sparse_random.txt",
                         "--psf", "sparse_random_beads.psf",
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])

        print("Measuring PSF (HDF5, with zoffset).")
        subprocess.call(["python", spliner_path + "measure_psf.py",
                         "--movie", "sparse_random.dax",
                         "--bin", "sparse_random_ref.hdf5",
                         "--psf", "sparse_random_hdf5_zo.psf",
                         "--zoffset", "z_offset.txt",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])

        print("Measuring PSF (HDF5).")
        subprocess.call(["python", spliner_path + "measure_psf.py",
                         "--movie", "sparse_random.dax",
                         "--bin", "sparse_random_ref.hdf5",
                         "--psf", "sparse_random_hdf5.psf",
                         "--zoffset", "",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])    

        psf_beads = numpy.load("sparse_random_beads.psf")["psf"]
        psf_hdf5_zo = numpy.load("sparse_random_hdf5_zo.psf")["psf"]
        psf_hdf5 = numpy.load("sparse_random_hdf5.psf")["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))
Example #24
0
def measurePSF():
    
    # Create sparse random localizations for PSF measurement.
    #
    print("Creating random localization.")
    sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/"
    subprocess.call(["python", sim_path + "emitters_uniform_random.py",
                     "--bin", "sparse_random.hdf5",
                     "--density", "0.0002",
                     "--margin", str(settings.margin),
                     "--sx", str(settings.x_size),
                     "--sy", str(settings.y_size)])

    # Create sparser grid for PSF measurement.
    #
    print("Creating data for PSF measurement.")
    sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/"
    subprocess.call(["python", sim_path + "emitters_on_grid.py",
                     "--bin", "sparse_grid.hdf5",
                     "--nx", "8",
                     "--ny", "3",
                     "--spacing", "40"])

    # 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).")
        spliner_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/spliner/"
        subprocess.call(["python", spliner_path + "measure_psf_beads.py",
                         "--movie", "sparse_grid.tif",
                         "--zoffset", "z_offset.txt",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--beads", "sparse_grid.txt",
                         "--psf", "sparse_grid_beads.psf",
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])

        print("Measuring PSF (HDF5, with zoffset).")
        subprocess.call(["python", spliner_path + "measure_psf.py",
                         "--movie", "sparse_grid.tif",
                         "--bin", "sparse_grid_ref.hdf5",
                         "--psf", "sparse_grid_hdf5_zo.psf",
                         "--zoffset", "z_offset.txt",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])

        print("Measuring PSF (HDF5).")
        subprocess.call(["python", spliner_path + "measure_psf.py",
                         "--movie", "sparse_grid.tif",
                         "--bin", "sparse_grid_ref.hdf5",
                         "--psf", "sparse_grid_hdf5.psf",
                         "--zoffset", "",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])

        multiplane_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/multi_plane/"
        print("Measure PSF (multiplane).")
        subprocess.call(["python", multiplane_path + "psf_zstack.py",
                         "--movie", "sparse_grid.tif",
                         "--bin", "sparse_grid.hdf5",
                         "--zstack", "sparse_grid_zstack",
                         "--aoi_size", str(int(settings.psf_size/2)+1)])

        subprocess.call(["python", multiplane_path + "measure_psf.py",
                         "--zstack", "sparse_grid_zstack.npy",
                         "--zoffsets", "z_offset.txt",
                         "--psf_name", "sparse_grid_hdf5_mp_zo.psf",
                         "--z_range", str(settings.psf_z_range),
                         "--z_step", str(settings.psf_z_step),
                         "--normalize"])

        # 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).")
        spliner_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/spliner/"
        try:
            subprocess.check_output(["python", spliner_path + "measure_psf_beads.py",
                                     "--movie", "sparse_grid.tif",
                                     "--zoffset", "z_offset_none_valid.txt",
                                     "--aoi_size", str(int(settings.psf_size/2)+1),
                                     "--beads", "sparse_grid.txt",
                                     "--psf", "sparse_grid_beads.psf",
                                     "--zrange", str(settings.psf_z_range),
                                     "--zstep", str(settings.psf_z_step)])
        except subprocess.CalledProcessError:
            pass
        else:
            assert False, "spliner.measure_psf_beads did not fail!"

        print("Measuring PSF (HDF5, with zoffset).")
        try:
            subprocess.check_output(["python", spliner_path + "measure_psf.py",
                                     "--movie", "sparse_grid.tif",
                                     "--bin", "sparse_grid_ref.hdf5",
                                     "--psf", "sparse_grid_hdf5_zo.psf",
                                     "--zoffset", "z_offset_none_valid.txt",
                                     "--aoi_size", str(int(settings.psf_size/2)+1),
                                     "--zrange", str(settings.psf_z_range),
                                     "--zstep", str(settings.psf_z_step)])
        except subprocess.CalledProcessError:
            pass
        else:
            assert False, "spliner.measure_psf did not fail!"            

        multiplane_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/multi_plane/"
        print("Measure PSF (multiplane).")
        try:
            subprocess.check_output(["python", multiplane_path + "psf_zstack.py",
                                    "--movie", "sparse_grid.tif",
                                     "--bin", "sparse_grid.hdf5",
                                     "--zstack", "sparse_grid_zstack",
                                     "--aoi_size", str(int(settings.psf_size/2)+1)])

            subprocess.check_output(["python", multiplane_path + "measure_psf.py",
                                     "--zstack", "sparse_grid_zstack.npy",
                                     "--zoffsets", "z_offset_none_valid.txt",
                                     "--psf_name", "sparse_grid_hdf5_mp_zo.psf",
                                     "--z_range", str(settings.psf_z_range),
                                     "--z_step", str(settings.psf_z_step),
                                     "--normalize"])
        except subprocess.CalledProcessError:
            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).")
        spliner_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/spliner/"
        subprocess.call(["python", spliner_path + "measure_psf_beads.py",
                         "--movie", "sparse_random.tif",
                         "--zoffset", "z_offset.txt",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--beads", "sparse_random.txt",
                         "--psf", "sparse_random_beads.psf",
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])

        print("Measuring PSF (HDF5, with zoffset).")
        subprocess.call(["python", spliner_path + "measure_psf.py",
                         "--movie", "sparse_random.tif",
                         "--bin", "sparse_random_ref.hdf5",
                         "--psf", "sparse_random_hdf5_zo.psf",
                         "--zoffset", "z_offset.txt",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(settings.psf_z_step)])

        print("Measuring PSF (HDF5).")
        subprocess.call(["python", spliner_path + "measure_psf.py",
                         "--movie", "sparse_random.tif",
                         "--bin", "sparse_random_ref.hdf5",
                         "--psf", "sparse_random_hdf5.psf",
                         "--zoffset", "",
                         "--aoi_size", str(int(settings.psf_size/2)+1),
                         "--zrange", str(settings.psf_z_range),
                         "--zstep", str(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))
Example #25
0
def getPeakLocations(peak_filename, margin, pixel_size, sigma):
    """
    This is for if you already know where your want fitting to happen, as
    for example in a bead calibration movie and you just want to use the
    approximate locations as inputs for fitting.

    There are two choices for peak_locations file format:

    1. A text file with the peak x, y, height and background values as 
       white spaced columns (x and y positions are in pixels as determined 
       using visualizer).

       1.0 2.0 1000.0 100.0
       10.0 5.0 2000.0 200.0
       ...

    2. An HDF5 format localization file. This is treated in a similar
       fashion to the text file in that all of the locations are loaded.
       If the fields 'xsigma' or 'ysigma' exist they will be used for
       the initial X/Y sigma values of the localization.
    """
    if os.path.exists(peak_filename):
        print("Using peak starting locations specified in", peak_filename)
    elif os.path.exists(os.path.basename(peak_filename)):
        peak_filename = os.path.basename(peak_filename)
        print("Using peak starting locations specified in", peak_filename)

    # Check if the file is a storm-analysis HDF5 file.
    #
    if saH5Py.isSAHDF5(peak_filename):
        peak_locations_type = "hdf5"
        peak_locations = saH5Py.loadLocalizations(peak_filename)
        if not "ysigma" in peak_locations:
            if not "xsigma" in peak_locations:
                peak_locations["xsigma"] = numpy.ones(
                    peak_locations["x"].size) * sigma
            peak_locations["ysigma"] = peak_locations["xsigma"].copy()

    else:
        peak_locations_type = "text"

        # Load peak x,y locations.
        peak_locs = numpy.loadtxt(peak_filename, ndmin=2)

        # Create peak dictionary.
        peak_locations = {
            "background": peak_locs[:, 3],
            "height": peak_locs[:, 2],
            "x": peak_locs[:, 0],
            "y": peak_locs[:, 1]
        }

        peak_locations["xsigma"] = numpy.ones(peak_locations["x"].size) * sigma
        peak_locations["ysigma"] = numpy.ones(peak_locations["x"].size) * sigma
        peak_locations["z"] = numpy.zeros(peak_locations["x"].size)

    # Adjust positions for finding/fitting margin.
    peak_locations["x"] += margin
    peak_locations["y"] += margin

    print("Loaded", peak_locations["x"].size, "peak locations")
    #
    # We return is_text as the caller might want to do different things if
    # the file is text, like initialize the Z value.
    #
    return [peak_locations, peak_locations_type]
Example #26
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)