def main(): from simtbx.nanoBragg.sim_data import SimData import sys if "--cuda" in sys.argv: import os os.environ["DIFFBRAGG_USE_CUDA"] = "1" S = SimData(use_default_crystal=True) S.instantiate_diffBragg() S.D.nopolar = True S.D.oversample = 3 S.D.add_diffBragg_spots() S._add_background() diff_img = S.D.raw_pixels.as_numpy_array() S.D.raw_pixels *= 0 S.D.add_nanoBragg_spots() S._add_background() nano_img = S.D.raw_pixels.as_numpy_array() import numpy as np assert np.allclose(diff_img, nano_img, atol=1e-9)
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
NCELLS_GT = 12, 12, 11 else: NCELLS_GT = 12, 12, 11 nbcryst.Ncells_abc = NCELLS_GT SIM = SimData(use_default_crystal=True) #SIM.detector = SimData.simple_detector(150, 0.1, (513, 512)) if "eta" in args.perturb: shape = 513 * 3, 512 * 3 #detdist = 70 else: shape = 513, 512 detdist = 150 SIM.detector = SimData.simple_detector(detdist, 0.1, shape) SIM.crystal = nbcryst SIM.instantiate_diffBragg(oversample=0, auto_set_spotscale=True) SIM.D.default_F = 0 SIM.D.F000 = 0 SIM.D.progress_meter = False SIM.water_path_mm = 0.005 SIM.air_path_mm = 0.1 SIM.add_air = True SIM.add_Water = True SIM.include_noise = True SIM.D.verbose = 2 SIM.D.add_diffBragg_spots() SIM.D.verbose = 0 spots = SIM.D.raw_pixels.as_numpy_array() SIM._add_background() SIM.D.readout_noise_adu = args.readout SIM._add_noise()
# make a nanoBragg crystal to pass to diffBragg nbcryst = NBcrystal() nbcryst.dxtbx_crystal = C nbcryst.n_mos_domains = 1 nbcryst.thick_mm = 0.01 nbcryst.Ncells_abc = (7, 7, 7) # make an instance of diffBRagg, use the simData wrapper SIM = SimData(use_default_crystal=True) # overwrite the default detector with a smaller pixels one SIM.detector = SimData.simple_detector(220, 0.1, (1000, 1000)) SIM.crystal = nbcryst SIM.instantiate_diffBragg(oversample=0, verbose=0, interpolate=0, default_F=1e3, auto_set_spotscale=True) # D is an instance of diffBragg with reasonable parameters # and our dxtbx crystal created above D = SIM.D if args.curvatures: D.compute_curvatures = True # STEP 1: simulate the un-perturbed image: D.refine(rot_idx) D.initialize_managers() D.set_value(rot_idx, 0) D.use_cuda = args.cuda #D.printout_pixel_fastslow = 786, 567 D.add_diffBragg_spots()
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
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) SIM.D.initialize_managers() SIM.D.spot_scale = 100000 SIM.D.default_F = 0 SIM.D.progress_meter = False SIM.water_path_mm = 0.15 SIM.air_path_mm = 0.1 SIM.add_air = True SIM.add_water = True SIM.include_noise = True SIM.D.use_cuda = args.cuda SIM.D.compute_curvatures = args.curvatures SIM.D.add_diffBragg_spots() if args.aniso is None:
# make a nanoBragg crystal to pass to diffBragg nbcryst = NBcrystal(init_defaults=True) nbcryst.dxtbx_crystal = C nbcryst.n_mos_domains = 1 nbcryst.thick_mm = 0.01 nbcryst.Ncells_abc = (7, 7, 7) # make an instance of diffBRagg, use the simData wrapper SIM = SimData(use_default_crystal=True) # overwrite the default detector with a smaller pixels one SIM.detector = SimData.simple_detector(300, 0.1, (700, 700)) SIM.crystal = nbcryst Fcell = 1e6 SIM.instantiate_diffBragg(oversample=0, verbose=0, interpolate=0, default_F=Fcell) # D is an instance of diffBragg with reasonable parameters # and our dxtbx crystal created above D = SIM.D D.progress_meter = True # initialize the derivative manager for Fcell fcell = 11 # internal index of fcell manager within diffBragg D.refine(fcell) D.initialize_managers() #roi = ((0, 699), (0, 699)) #rX = slice(roi[0][0], roi[0][1], 1) #rY = slice(roi[1][0], roi[1][1], 1)