Example #1
0
a2_real, b2_real, c2_real = sqr(
    uctbx.unit_cell(
        ucell2).orthogonalization_matrix()).transpose().as_list_of_lists()
C2 = Crystal(a2_real, b2_real, c2_real, symbol)
C2.rotate_around_origin(rot_axis, rot_ang)
assert np.allclose(C2.get_U(), C.get_U())
C2.rotate_around_origin(col(perturb_rot_axis), perturb_rot_ang)

# Setup the simulation and create a realistic image
# with background and noise
# <><><><><><><><><><><><><><><><><><><><><><><><><>
nbcryst = NBcrystal()
nbcryst.dxtbx_crystal = C  # simulate ground truth
nbcryst.thick_mm = 0.1
nbcryst.isotropic_ncells = False
if "eta" in args.perturb:
    nbcryst.n_mos_domains = 1000
    ETA_ABC_GT = args.eta
    nbcryst.anisotropic_mos_spread_deg = ETA_ABC_GT
    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:
Example #2
0
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