def sim_spots(F): a_real = (79, 0, 0) b_real = (0, 79, 0) c_real = (0, 0, 38) C = Crystal(a_real, b_real, c_real, 'P43212') nbcryst = NBcrystal(init_defaults=True) nbcryst.dxtbx_crystal = C # simulate ground truth nbcryst.thick_mm = 0.1 nbcryst.Ncells_abc = 10, 10, 10 nbcryst.miller_array = F print("Ground truth ncells = %f" % (nbcryst.Ncells_abc[0])) # ground truth detector from simtbx.nanoBragg.sim_data import SimData DET_gt = SimData.simple_detector(150, 0.177, (600, 600)) # initialize the simulator SIM = SimData(use_default_crystal=True) SIM.detector = DET_gt SIM.crystal = nbcryst SIM.instantiate_diffBragg(oversample=0) SIM.D.default_F = 0 SIM.D.progress_meter = False SIM.D.add_diffBragg_spots() SIM.D.F000 = 0 SPOTS = SIM.D.raw_pixels.as_numpy_array() SIM.D.free_all() SIM.D.free_Fhkl2() return SPOTS
def flexBeam_sim_colors(CRYSTAL, DETECTOR, BEAM, Famp, energies, fluxes, pids=None, cuda=False, oversample=0, Ncells_abc=(50, 50, 50), mos_dom=1, mos_spread=0, beamsize_mm=0.001, device_Id=0, omp=False, show_params=False, crystal_size_mm=0.01, printout_pix=None, time_panels=True, verbose=0, default_F=0, interpolate=0, recenter=True, profile="gauss", spot_scale_override=None, background_raw_pixels=None, include_noise=False, add_water=False, add_air=False, water_path_mm=0.005, air_path_mm=0, rois_perpanel=None, adc_offset=0, readout_noise=3, psf_fwhm=0, gain=1, mosaicity_random_seeds=None): """ :param CRYSTAL: dxtbx Crystal model :param DETECTOR: dxtbx detector model :param BEAM: dxtbx beam model :param Famp: cctbx miller array (amplitudes) :param energies: list of energies to simulate the scattering :param fluxes: list of pulse fluences per energy (same length as energies) :param pids: panel ids to simulate on (None means all panels) :param cuda: whether to use GPU (only works for nvidia builds) :param oversample: pixel oversample factor (0 means nanoBragg will decide) :param Ncells_abc: number of unit cells along each crystal direction in the mosaic block :param mos_dom: number of mosaic domains in used to sample mosaic spread (texture) :param mos_spread: mosaicity in degrees (spherical cap width) :param beamsize_mm: focal size of the beam :param device_Id: cuda device id (ignore if cuda=False) :param omp: whether to use open mp (required open MP build configuration) :param show_params: show the nanoBragg parameters :param crystal_size_mm: size of the crystal (increases the intensity of the spots) :param printout_pix: debug pixel position : tuple of (pixel_fast_coord, pixel_slow_coord) :param time_panels: show timing info :param verbose: verbosity level for nanoBragg (0-10), 0 is quiet :param default_F: default amplitude value for nanoBragg :param interpolate: whether to interpolate for small mosaic domains :param recenter: recenter for tilted cameras, deprecated :param profile: profile shape, can be : gauss, round, square, or tophat :param spot_scale_override: scale the simulated scattering bythis amounth (overrides value based on crystal thickness) :param background_raw_pixels: dictionary of {panel_id: raw_pixels}, add these background pixels to the simulated Bragg :param include_noise: add noise to simulated pattern :param add_water: add water to similated pattern :param add_air: add ait to simulated pattern :param water_path_mm: length of water the beam travels through :param air_path_mm: length of air the beam travels through :param rois_perpanel: regions of intererest on each panel :param adc_offset: add this value to each pixel in simulated pattern :param readout_noise: readout noise level (usually 3-5 ADU) :param psf_fwhm: point spread kernel FWHM :param gain: photon gain :param mosaicity_random_seeds: random seeds to simulating mosaic texture :return: list of [(panel_id0,simulated pattern0), (panel_id1, simulated_pattern1), ...] """ if pids is None: pids = range(len(DETECTOR)) if background_raw_pixels is None: background_raw_pixels = {pid: None for pid in pids} if rois_perpanel is None: rois_perpanel = {pid: None for pid in pids} nbBeam = NBbeam() nbBeam.size_mm = beamsize_mm nbBeam.unit_s0 = BEAM.get_unit_s0() wavelengths = ENERGY_CONV / np.array(energies) nbBeam.spectrum = list(zip(wavelengths, fluxes)) nbCrystal = NBcrystal() nbCrystal.dxtbx_crystal = CRYSTAL nbCrystal.miller_array = Famp nbCrystal.Ncells_abc = Ncells_abc nbCrystal.symbol = CRYSTAL.get_space_group().info().type().lookup_symbol() nbCrystal.thick_mm = crystal_size_mm nbCrystal.xtal_shape = profile nbCrystal.n_mos_domains = mos_dom nbCrystal.mos_spread_deg = mos_spread panel_images = [] tinit = time.time() S = SimData() S.detector = DETECTOR S.beam = nbBeam S.crystal = nbCrystal S.using_cuda = cuda S.using_omp = omp S.add_air = add_air S.air_path_mm = air_path_mm S.add_water = add_water S.water_path_mm = water_path_mm S.readout_noise = readout_noise S.gain = gain S.psf_fwhm = psf_fwhm S.include_noise = include_noise if mosaicity_random_seeds is not None: S.mosaic_seeds = mosaicity_random_seeds S.instantiate_nanoBragg(verbose=verbose, oversample=oversample, interpolate=interpolate, device_Id=device_Id, default_F=default_F, adc_offset=adc_offset) if printout_pix is not None: S.update_nanoBragg_instance("printout_pixel_fastslow", printout_pix) if spot_scale_override is not None: S.update_nanoBragg_instance("spot_scale", spot_scale_override) for pid in pids: t_panel = time.time() S.background_raw_pixels = background_raw_pixels[pid] S.panel_id = pid S.rois = rois_perpanel[pid] S.generate_simulated_image() if show_params: S.D.show_params() print('spot scale: %2.7g' % S.D.spot_scale) panel_image = S.D.raw_pixels.as_numpy_array() panel_images.append([pid, panel_image]) S.D.raw_pixels *= 0 if time_panels: tdone = time.time() - tinit t_panel = time.time() - t_panel print( 'Panel %d took %.4f seconds (Total sim time = %.4f seconds)' % (pid, t_panel, tdone)) S.D.free_all() return panel_images
def diffBragg_forward(CRYSTAL, DETECTOR, BEAM, Famp, energies, fluxes, oversample=0, Ncells_abc=(50, 50, 50), mos_dom=1, mos_spread=0, beamsize_mm=0.001, device_Id=0, show_params=True, crystal_size_mm=None, printout_pix=None, verbose=0, default_F=0, interpolate=0, profile="gauss", spot_scale_override=None, mosaicity_random_seeds=None, nopolar=False, diffuse_params=None, cuda=False, show_timings=False): if cuda: os.environ["DIFFBRAGG_USE_CUDA"] = "1" CRYSTAL, Famp = nanoBragg_utils.ensure_p1(CRYSTAL, Famp) nbBeam = NBbeam() nbBeam.size_mm = beamsize_mm nbBeam.unit_s0 = BEAM.get_unit_s0() wavelengths = utils.ENERGY_CONV / np.array(energies) nbBeam.spectrum = list(zip(wavelengths, fluxes)) nbCrystal = NBcrystal(init_defaults=False) nbCrystal.isotropic_ncells = False nbCrystal.dxtbx_crystal = CRYSTAL nbCrystal.miller_array = Famp nbCrystal.Ncells_abc = Ncells_abc nbCrystal.symbol = CRYSTAL.get_space_group().info().type().lookup_symbol() nbCrystal.thick_mm = crystal_size_mm nbCrystal.xtal_shape = profile nbCrystal.n_mos_domains = mos_dom nbCrystal.mos_spread_deg = mos_spread S = SimData() S.detector = DETECTOR npan = len(DETECTOR) nfast, nslow = DETECTOR[0].get_image_size() img_shape = npan, nslow, nfast S.beam = nbBeam S.crystal = nbCrystal if mosaicity_random_seeds is not None: S.mosaic_seeds = mosaicity_random_seeds S.instantiate_diffBragg(verbose=verbose, oversample=oversample, interpolate=interpolate, device_Id=device_Id, default_F=default_F, auto_set_spotscale=crystal_size_mm is not None and spot_scale_override is None) if spot_scale_override is not None: S.update_nanoBragg_instance("spot_scale", spot_scale_override) S.update_nanoBragg_instance("nopolar", nopolar) if show_params: S.D.show_params() print("Spot scale=%f" % S.D.spot_scale) if show_timings and verbose < 2: S.D.verbose = 2 S.D.record_time = True if diffuse_params is not None: S.D.use_diffuse = True S.D.gamma_miller_units = diffuse_params["gamma_miller_units"] S.D.diffuse_gamma = diffuse_params["gamma"] S.D.diffuse_sigma = diffuse_params["sigma"] S.D.add_diffBragg_spots_full() if show_timings: S.D.show_timings() t = time.time() data = S.D.raw_pixels_roi.as_numpy_array().reshape(img_shape) t = time.time() - t if show_timings: print("Took %f sec to recast and reshape" % t) if printout_pix is not None: S.D.raw_pixels_roi *= 0 p, f, s = printout_pix S.D.printout_pixel_fastslow = f, s S.D.show_params() S.D.add_diffBragg_spots(printout_pix) # free up memory S.D.free_all() S.D.free_Fhkl2() if S.D.gpu_free is not None: S.D.gpu_free() return data
# Setup the simulation and create a realistic image # with background and noise # <><><><><><><><><><><><><><><><><><><><><><><><><> nbcryst = NBcrystal(init_defaults=True) nbcryst.dxtbx_crystal = C # simulate ground truth nbcryst.thick_mm = 0.1 nbcryst.Ncells_abc = Ncells_gt # ground truth Ncells nbcryst.mos_spread_deg = MOS_SPREAD if args.aniso is not None: nbcryst.anisotropic_mos_spread_deg = ANISO_MOS_SPREAD assert nbcryst.has_anisotropic_mosaicity else: assert not nbcryst.has_anisotropic_mosaicity nbcryst.n_mos_domains = N_MOS_DOMAINS nbcryst.miller_array = miller_array_GT print("Ground truth ncells = %f" % (nbcryst.Ncells_abc[0])) # ground truth detector DET_gt = SimData.simple_detector(150, 0.177, (600, 600)) # initialize the simulator SIM = SimData() if args.aniso is None: SIM.Umats_method = 2 else: SIM.Umats_method = 3 SIM.detector = DET_gt SIM.crystal = nbcryst SIM.instantiate_diffBragg(oversample=1, verbose=0) SIM.D.refine(eta_diffBragg_id)
def flexBeam_sim_colors(CRYSTAL, DETECTOR, BEAM, Famp, energies, fluxes, pids=None, cuda=False, oversample=0, Ncells_abc=(50, 50, 50), mos_dom=1, mos_spread=0, beamsize_mm=0.001, device_Id=0, omp=False, show_params=False, crystal_size_mm=0.01, printout_pix=None, time_panels=True, verbose=0, default_F=0, interpolate=0, recenter=True, profile="gauss", spot_scale_override=None, background_raw_pixels=None, include_noise=False, add_water=False, add_air=False, water_path_mm=0.005, air_path_mm=0, rois_perpanel=None, adc_offset=0, readout_noise=3, psf_fwhm=0, gain=1, mosaicity_random_seeds=None): if pids is None: pids = range(len(DETECTOR)) if background_raw_pixels is None: background_raw_pixels = {pid: None for pid in pids} if rois_perpanel is None: rois_perpanel = {pid: None for pid in pids} nbBeam = NBbeam() nbBeam.size_mm = beamsize_mm nbBeam.unit_s0 = BEAM.get_unit_s0() wavelengths = ENERGY_CONV / np.array(energies) nbBeam.spectrum = list(zip(wavelengths, fluxes)) nbCrystal = NBcrystal() nbCrystal.dxtbx_crystal = CRYSTAL nbCrystal.miller_array = Famp nbCrystal.Ncells_abc = Ncells_abc nbCrystal.symbol = CRYSTAL.get_space_group().info().type().lookup_symbol() nbCrystal.thick_mm = crystal_size_mm nbCrystal.xtal_shape = profile nbCrystal.n_mos_domains = mos_dom nbCrystal.mos_spread_deg = mos_spread panel_images = [] for pid in pids: tinit = time.time() S = SimData() S.detector = DETECTOR S.beam = nbBeam S.crystal = nbCrystal S.panel_id = pid S.using_cuda = cuda S.using_omp = omp S.add_air = add_air S.air_path_mm = air_path_mm S.add_water = add_water S.water_path_mm = water_path_mm S.background_raw_pixels = background_raw_pixels[pid] S.rois = rois_perpanel[pid] S.readout_noise = readout_noise S.gain = gain S.psf_fwhm = psf_fwhm S.include_noise = include_noise if mosaicity_random_seeds is not None: S.mosaic_seeds = mosaicity_random_seeds S.instantiate_nanoBragg(verbose=verbose, oversample=oversample, interpolate=interpolate, device_Id=device_Id, default_F=default_F, adc_offset=adc_offset) if recenter: S.update_nanoBragg_instance( "beam_center_mm", DETECTOR[int(pid)].get_beam_centre(BEAM.get_s0())) if printout_pix is not None: S.update_nanoBragg_instance("printout_pixel_fastslow", printout_pix) if spot_scale_override is not None: S.update_nanoBragg_instance("spot_scale", spot_scale_override) S.generate_simulated_image() if show_params: S.D.show_params() print('spot scale: %2.7g' % S.D.spot_scale) panel_image = S.D.raw_pixels.as_numpy_array() panel_images.append([pid, panel_image]) S.D.free_all() if time_panels: tdone = time.time() - tinit print('Panel %d took %.4f seconds' % (pid, tdone)) del S.D return panel_images