def makeData(cal_file="calib.npy", dither=False, verbosity=1): index = 1 for [bg, photons] in settings.photons: 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, cal_file) pp_f = lambda s, x, y, i3: photophysics.AlwaysOn(s, x, y, i3, photons) psf_f = lambda s, x, y, i3: psf.GaussianPSF(s, x, y, i3, settings. pixel_size) sim = simulate.Simulate(background_factory=bg_f, camera_factory=cam_f, photophysics_factory=pp_f, psf_factory=psf_f, dither=dither, x_size=settings.x_size, y_size=settings.y_size) sim.simulate(wdir + "/test.tif", "grid_list.hdf5", settings.n_frames, verbosity=verbosity) index += 1 makeDataCommon.makePeakFile(settings)
def makeSampleData(): # Create sample bead data for fiducial tracking. # # Create randomly located localizations file. # print("Creating random localizations.") sim_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/simulator/" subprocess.call([ "python", sim_path + "emitters_uniform_random.py", "--bin", "random.hdf5", "--density", str(density), "--margin", str(margin), "--sx", str(x_size), "--sy", str(y_size) ]) # Create X/Y/Z drift file. # dx = numpy.random.normal(loc=x_loc, scale=x_scale, size=n_frames) dy = numpy.random.normal(loc=y_loc, scale=y_scale, size=n_frames) # Integrate dx, dy for i in range(1, dx.size): dx[i] = dx[i - 1] + dx[i] dy[i] = dy[i - 1] + dy[i] drift_data = numpy.zeros((dx.size, 3)) drift_data[:, 0] = dx drift_data[:, 1] = dy numpy.savetxt("drift.txt", drift_data) # Create simulated data for fiducial tracking. # bg_f = lambda s, x, y, h5: background.UniformBackground( s, x, y, h5, photons=10) cam_f = lambda s, x, y, h5: camera.Ideal(s, x, y, h5, camera_offset) drift_f = lambda s, x, y, h5: drift.DriftFromFile(s, x, y, h5, "drift.txt") pp_f = lambda s, x, y, h5: photophysics.SimpleSTORM( s, x, y, h5, 4000, on_time=10.0, off_time=1.0) psf_f = lambda s, x, y, h5: psf.GaussianPSF(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) sim.simulate("fiducials.tif", "random.hdf5", n_frames)
def test_simulate_1(): """ No photo-physics, simple PSF, ideal camera. """ dax_name = storm_analysis.getPathOutputTest("test_sim1.dax") bin_name = storm_analysis.getData("test/data/test_sim.hdf5") sim = simulate.Simulate(background_factory = lambda settings, xs, ys, i3data : background.UniformBackground(settings, xs, ys, i3data), camera_factory = lambda settings, xs, ys, i3data : camera.Ideal(settings, xs, ys, i3data, 100.0), photophysics_factory = lambda settings, xs, ys, i3data : photophysics.AlwaysOn(settings, xs, ys, i3data, 1000.0), psf_factory = lambda settings, xs, ys, i3data : psf.GaussianPSF(settings, xs, ys, i3data, 160.0), x_size = 100, y_size = 75) sim.simulate(dax_name, bin_name, 5)
def createMovie(): bg_f = lambda s, x, y, i3: background.UniformBackground( s, x, y, i3, photons=bg) cam_f = lambda s, x, y, i3: camera.Ideal(s, x, y, i3, camera_offset) pp_f = lambda s, x, y, i3: photophysics.AlwaysOn(s, x, y, i3, signal) psf_f = lambda s, x, y, i3: psf.GaussianPSF(s, x, y, i3, 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) sim.simulate("test.tif", "sim_locs.hdf5", n_frames)
def createMovie(gain=1.0, offset=100.0): bg_f = lambda s, x, y, h5: background.GaussianBackground( s, x, y, h5, photons=bg) cam_f = lambda s, x, y, h5: camera.Ideal(s, x, y, h5, offset, gain=gain) pp_f = lambda s, x, y, h5: photophysics.SimpleSTORM( s, x, y, h5, photons=signal) psf_f = lambda s, x, y, h5: psf.GaussianPSF(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) sim.simulate("test.tif", "sim_locs.hdf5", n_frames)
def makeData(cal_file="calib.npy", dither=False): index = 1 if True: for [bg, photons] in settings.photons: 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, cal_file) pp_f = lambda s, x, y, i3: photophysics.AlwaysOn( s, x, y, i3, photons) psf_f = lambda s, x, y, i3: psf.GaussianPSF( s, x, y, i3, settings.pixel_size) sim = simulate.Simulate(background_factory=bg_f, camera_factory=cam_f, photophysics_factory=pp_f, psf_factory=psf_f, dither=dither, x_size=settings.x_size, y_size=settings.y_size) sim.simulate(wdir + "/test.tif", "grid_list.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_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']))))
def makeSampleData(): # Create sample bead data for fiducial tracking. # # Create randomly located localizations file. # print("Creating random localizations.") emittersUniformRandom.emittersUniformRandom("random.hdf5", density, margin, x_size, y_size, 0.0) # Create X/Y/Z drift file. # dx = numpy.random.normal(loc=x_loc, scale=x_scale, size=n_frames) dy = numpy.random.normal(loc=y_loc, scale=y_scale, size=n_frames) # Integrate dx, dy for i in range(1, dx.size): dx[i] = dx[i - 1] + dx[i] dy[i] = dy[i - 1] + dy[i] drift_data = numpy.zeros((dx.size, 3)) drift_data[:, 0] = dx drift_data[:, 1] = dy numpy.savetxt("drift.txt", drift_data) # Create simulated data for fiducial tracking. # bg_f = lambda s, x, y, h5: background.UniformBackground( s, x, y, h5, photons=10) cam_f = lambda s, x, y, h5: camera.Ideal(s, x, y, h5, camera_offset) drift_f = lambda s, x, y, h5: drift.DriftFromFile(s, x, y, h5, "drift.txt") pp_f = lambda s, x, y, h5: photophysics.SimpleSTORM( s, x, y, h5, 4000, on_time=10.0, off_time=1.0) psf_f = lambda s, x, y, h5: psf.GaussianPSF(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) sim.simulate("fiducials.tif", "random.hdf5", n_frames)
def createMovie(n_frames): bg_f = lambda s, x, y, i3: background.UniformBackground( s, x, y, i3, photons=bg) cam_f = lambda s, x, y, i3: camera.Ideal(s, x, y, i3, camera_offset) pp_f = lambda s, x, y, i3: photophysics.AlwaysOn(s, x, y, i3, signal) psf_f = lambda s, x, y, i3: psf.GaussianPSF(s, x, y, i3, 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) sim.simulate("test.tif", "sim_locs.hdf5", n_frames) # Also create file to use for peak locations. with saH5Py.SAH5Py("test_ref.hdf5") as h5: locs = h5.getLocalizationsInFrame(0) saH5Py.saveLocalizations("peak_locs.hdf5", locs)
def makeData(): index = 1 # Gaussian non-uniform background, STORM. if True: for elt in ["drift_xy.txt", "drift_xyz.txt"]: bg = settings.background photons = settings.photons 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.GaussianBackground( s, x, y, i3, photons=bg) cam_f = lambda s, x, y, i3: camera.Ideal(s, x, y, i3, settings. camera_offset) drift_f = lambda s, x, y, i3: drift.DriftFromFile(s, x, y, i3, elt) pp_f = lambda s, x, y, i3: photophysics.SimpleSTORM( s, x, y, i3, photons) 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, drift_factory=drift_f, photophysics_factory=pp_f, psf_factory=psf_f, x_size=settings.x_size, y_size=settings.y_size) sim.simulate(wdir + "/test.dax", "clusters_list.hdf5", settings.n_frames) #sim.simulate(wdir + "/test.dax", "lines_list.hdf5", settings.n_frames) index += 1
help="The length of the movie in frames.") parser.add_argument('--photons', dest='photons', type=float, required=True, help="The integral of a single emitter in photons.") args = parser.parse_args() sim = Simulate( lambda settings, xs, ys, h5data: background.UniformBackground( settings, xs, ys, h5data), lambda settings, xs, ys, h5data: camera. Ideal(settings, xs, ys, h5data, 100.0), lambda settings, xs, ys, h5data: photophysics.AlwaysOn( settings, xs, ys, h5data, args.photons), lambda settings, xs, ys, h5data: psf.GaussianPSF( settings, xs, ys, h5data, 160.0)) sim.simulate(args.dax_file, args.hdf5, args.frames) # # The MIT License # # Copyright (c) 2016 Zhuang Lab, Harvard University # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: #
bg = settings.background photons = settings.photons 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.GaussianBackground( s, x, y, i3, photons=bg) cam_f = lambda s, x, y, i3: camera.Ideal(s, x, y, i3, settings. camera_offset) drift_f = lambda s, x, y, i3: drift.DriftFromFile(s, x, y, i3, elt) pp_f = lambda s, x, y, i3: photophysics.SimpleSTORM( s, x, y, i3, photons) 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, drift_factory=drift_f, photophysics_factory=pp_f, psf_factory=psf_f, x_size=settings.x_size, y_size=settings.y_size) sim.simulate(wdir + "/test.dax", "clusters_list.hdf5", settings.n_frames) #sim.simulate(wdir + "/test.dax", "lines_list.hdf5", settings.n_frames) index += 1
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" ])
def makeData(): index = 1 # sCMOS calibration file. if settings.random_variance: variance = numpy.random.exponential(scale=settings.camera_variance, size=(settings.y_size, settings.x_size)) offset = settings.camera_offset + variance numpy.save("calib.npy", [ offset, variance, numpy.ones( (settings.y_size, settings.x_size)) * settings.camera_gain, 1 ]) else: 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 ]) # sCMOS camera movies. # # For these simulations we expect (approximately) these results: # # Analysis Summary: # Total analysis time 10.64 seconds # Recall 0.93726 # Noise 0.05972 # XY Error (nm): # test_01 14.44 14.53 # test_02 8.26 8.24 # # XY Width Error, Mean difference with truth, Standard deviation (pixels): # test_01 0.029 0.116 0.029 0.116 # test_02 0.017 0.105 0.017 0.105 # if True: for [bg, photons] in settings.photons: 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.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) sim.simulate(wdir + "/test.tif", "grid_list.hdf5", settings.n_frames) index += 1
def makeDataSpliner(): index = 1 # Non-uniform background, STORM. # if False: for [bg, photons] in settings.photons: 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.GaussianBackground( s, x, y, i3, photons=bg) cam_f = lambda s, x, y, i3: camera.Ideal(s, x, y, i3, settings. camera_offset) pp_f = lambda s, x, y, i3: photophysics.SimpleSTORM( s, x, y, i3, photons) 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) sim.simulate(wdir + "/test.dax", "random_list.hdf5", settings.n_frames) index += 1 # Ideal camera movies, PSF using the measured spline. # if False: for [bg, photons] in settings.photons: 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.Ideal(s, x, y, i3, settings. camera_offset) pp_f = lambda s, x, y, i3: photophysics.AlwaysOn( s, x, y, i3, photons) psf_f = lambda s, x, y, i3: psf.Spline(s, x, y, i3, settings. pixel_size, "psf.spline") 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.dax", "grid_list.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_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']))))
def configure(no_splines, cal_file = None): # Create sCMOS calibration file if requested. # if cal_file is not None: offset = numpy.zeros((settings.y_size, settings.x_size)) + settings.camera_offset variance = numpy.ones((settings.y_size, settings.x_size)) gain = numpy.ones((settings.y_size, settings.x_size)) * settings.camera_gain rqe = numpy.ones((settings.y_size, settings.x_size)) numpy.save(cal_file, [offset, variance, gain, rqe, 2]) # Create parameters file for analysis. # print("Creating XML file.") params = testingParameters(cal_file = cal_file) params.toXMLFile("spliner.xml") # Create localization on a grid file. # print("Creating gridded localization.") 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"]) # 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)]) # 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_list.hdf5", "--nx", "6", "--ny", "3", "--spacing", "40"]) if no_splines: return # Create simulated data for PSF measurement. # bg_f = lambda s, x, y, i3 : background.UniformBackground(s, x, y, i3, photons = 10) cam_f = lambda s, x, y, i3 : camera.Ideal(s, x, y, i3, 100.) pp_f = lambda s, x, y, i3 : photophysics.AlwaysOn(s, x, y, i3, 20000.0) psf_f = lambda s, x, y, i3 : psf.GaussianPSF(s, x, y, i3, settings.pixel_size) sim = simulate.Simulate(background_factory = bg_f, camera_factory = cam_f, photophysics_factory = pp_f, psf_factory = psf_f, dither = True, x_size = settings.x_size, y_size = settings.y_size) sim.simulate("spline_2d.tif", "sparse_list.hdf5", 5) # Measure the PSF. # print("Measuring PSF.") spliner_path = os.path.dirname(inspect.getfile(storm_analysis)) + "/spliner/" subprocess.call(["python", spliner_path + "measure_psf.py", "--movie", "spline_2d.tif", "--bin", "spline_2d_ref.hdf5", "--psf", "psf.psf", "--want2d", "--aoi_size", str(settings.spline_size+1)]) # Measure the Spline. # if True: print("Measuring Spline.") subprocess.call(["python", spliner_path + "psf_to_spline.py", "--psf", "psf.psf", "--spline", "psf.spline", "--spline_size", str(settings.spline_size)])
def configure(no_splines, cal_file = None): # Create sCMOS calibration file if requested. # if cal_file is not None: offset = numpy.zeros((settings.y_size, settings.x_size)) + settings.camera_offset variance = numpy.ones((settings.y_size, settings.x_size)) gain = numpy.ones((settings.y_size, settings.x_size)) * settings.camera_gain rqe = numpy.ones((settings.y_size, settings.x_size)) numpy.save(cal_file, [offset, variance, gain, rqe, 2]) # Create parameters file for analysis. # print("Creating XML file.") params = testingParameters(cal_file = cal_file) params.toXMLFile("spliner.xml") # Create localization on a grid file. # print("Creating gridded localization.") emittersOnGrid.emittersOnGrid("grid_list.hdf5", settings.nx, settings.ny, 1.5, 20, 0.0, 0.0) # Create randomly located localizations file. # print("Creating random localization.") emittersUniformRandom.emittersUniformRandom("random_list.hdf5", 1.0, settings.margin, settings.x_size, settings.y_size, 0.0) # Create sparser grid for PSF measurement. # print("Creating data for PSF measurement.") emittersOnGrid.emittersOnGrid("sparse_list.hdf5", 6, 3, 1.5, 40, 0.0, 0.0) if no_splines: return # Create beads.txt file for spline measurement. # with saH5Py.SAH5Py("sparse_list.hdf5") as h5: locs = h5.getLocalizations() numpy.savetxt("beads.txt", numpy.transpose(numpy.vstack((locs['x'], locs['y'])))) # Create simulated data for PSF measurement. # bg_f = lambda s, x, y, i3 : background.UniformBackground(s, x, y, i3, photons = 10) cam_f = lambda s, x, y, i3 : camera.Ideal(s, x, y, i3, 100.) pp_f = lambda s, x, y, i3 : photophysics.AlwaysOn(s, x, y, i3, 20000.0) psf_f = lambda s, x, y, i3 : psf.GaussianPSF(s, x, y, i3, settings.pixel_size) sim = simulate.Simulate(background_factory = bg_f, camera_factory = cam_f, photophysics_factory = pp_f, psf_factory = psf_f, dither = True, x_size = settings.x_size, y_size = settings.y_size) sim.simulate("spline_2d.tif", "sparse_list.hdf5", 5) # Measure the PSF. # print("Measuring PSF.") psf_name = "psf.psf" measurePSF.measurePSF("spline_2d.tif", "na", "sparse_list.hdf5", psf_name, want2d = True, aoi_size = int(settings.spline_size + 1), pixel_size = settings.pixel_size * 1.0e-3) # Measure the Spline. # if True: print("Measuring Spline.") psfToSpline.psfToSpline(psf_name, "psf.spline", settings.spline_size)
def makeData(dither = False): index = 1 # Gaussian PSF, uniform background. if True: for [bg, photons] in settings.photons: 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.Ideal(s, x, y, i3, settings.camera_offset) pp_f = lambda s, x, y, i3 : photophysics.AlwaysOn(s, x, y, i3, photons) psf_f = lambda s, x, y, i3 : psf.GaussianPSF(s, x, y, i3, settings.pixel_size) sim = simulate.Simulate(background_factory = bg_f, camera_factory = cam_f, photophysics_factory = pp_f, psf_factory = psf_f, dither = dither, x_size = settings.x_size, y_size = settings.y_size) sim.simulate(wdir + "/test.dax", "grid_list.hdf5", settings.n_frames) index += 1 # Pupil Function PSF. if False: for [bg, photons] in settings.photons: 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.Ideal(s, x, y, i3, settings.camera_offset) 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, []) 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.dax", "grid_list.hdf5", settings.n_frames) index += 1 # Gaussian non-uniform background, always on. if False: for [bg, photons] in settings.photons: 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.GaussianBackground(s, x, y, i3, photons = bg) cam_f = lambda s, x, y, i3 : camera.Ideal(s, x, y, i3, settings.camera_offset) pp_f = lambda s, x, y, i3 : photophysics.AlwaysOn(s, x, y, i3, photons) 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) sim.simulate(wdir + "/test.dax", "grid_list.hdf5", settings.n_frames) index += 1 # Uniform background, STORM. if False: for [bg, photons] in settings.photons: 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.Ideal(s, x, y, i3, settings.camera_offset) pp_f = lambda s, x, y, i3 : photophysics.SimpleSTORM(s, x, y, i3, photons) 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) sim.simulate(wdir + "/test.dax", "random_list.hdf5", settings.n_frames) index += 1 # Gaussian non-uniform background, STORM. if False: for [bg, photons] in settings.photons: 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.GaussianBackground(s, x, y, i3, photons = bg) cam_f = lambda s, x, y, i3 : camera.Ideal(s, x, y, i3, settings.camera_offset) pp_f = lambda s, x, y, i3 : photophysics.SimpleSTORM(s, x, y, i3, photons) 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) sim.simulate(wdir + "/test.dax", "random_list.hdf5", settings.n_frames) index += 1 # Sloped non-uniform background, always on. if False: for [bg, photons] in settings.photons: 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.SlopedBackground(s, x, y, i3, slope = 0.4, offset = 10) cam_f = lambda s, x, y, i3 : camera.Ideal(s, x, y, i3, settings.camera_offset) pp_f = lambda s, x, y, i3 : photophysics.AlwaysOn(s, x, y, i3, photons) psf_f = lambda s, x, y, i3 : psf.GaussianPSF(s, x, y, i3, settings.pixel_size) sim = simulate.Simulate(background_factory = bg_f, camera_factory = cam_f, photophysics_factory = pp_f, psf_factory = psf_f, dither = True, x_size = settings.x_size, y_size = settings.y_size) sim.simulate(wdir + "/test.dax", "grid_list.hdf5", settings.n_frames) index += 1 # Sine non-uniform background, always on. if False: for [bg, photons] in settings.photons: 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.SineBackground(s, x, y, i3, photons = bg, period = 45) cam_f = lambda s, x, y, i3 : camera.Ideal(s, x, y, i3, settings.camera_offset) pp_f = lambda s, x, y, i3 : photophysics.AlwaysOn(s, x, y, i3, photons) psf_f = lambda s, x, y, i3 : psf.GaussianPSF(s, x, y, i3, settings.pixel_size) sim = simulate.Simulate(background_factory = bg_f, camera_factory = cam_f, photophysics_factory = pp_f, psf_factory = psf_f, dither = True, x_size = settings.x_size, y_size = settings.y_size) sim.simulate(wdir + "/test.dax", "grid_list.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_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']))))
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)