Ejemplo n.º 1
0
def refine_cell(data):

    spot_mask = spot_utils.strong_spot_mask(data['refl'], (1800, 1800))

    Patts = sim_utils.PatternFactory()
    Patts.adjust_mosaicity(2, 0.5)
    energy, fcalc = sim_utils.load_fcalc_file(data['fcalc_f'])

    crystal = data['crystal']
    a, b, c, _, _, _ = crystal.get_unit_cell().parameters()

    optX, optY = data['optX'], data['optY']
    optX_fine, optY_fine = data['optX_fine'], data['optY_fine']
    Op = optX_fine * optY_fine * optX * optY
    crystal.set_U(Op)

    overlaps = []
    imgs_all = []
    percs = np.arange(-0.005, 0.006, 0.001)
    crystals = []
    for i in percs:
        for j in percs:
            crystal2 = deepcopy(crystal)
            a2 = a + a * i
            c2 = c + c * j
            B2 = sqr((a2, 0, 0, 0, a2, 0, 0, 0, c2)).inverse()
            crystal2.set_B(B2)
            sim_patt = Patts.make_pattern2(
                crystal=crystal2,
                flux_per_en=[data['fracA'] * 1e14, data['fracB'] * 1e14],
                energies_eV=energy,
                fcalcs_at_energies=fcalc,
                mosaic_spread=None,
                mosaic_domains=None,
                ret_sum=True,
                Op=None)

            sim_sig_mask = sim_patt > 0
            overlaps.append(np.sum(sim_sig_mask * spot_mask))
            crystals.append(deepcopy(crystal2))
            imgs_all.append(sim_patt)
    refls_all = [data["refl"]] * len(imgs_all)
    utils.images_and_refls_to_simview("cell_refine", imgs_all, refls_all)
    return overlaps, crystals
Ejemplo n.º 2
0
def main():
    from cxid9114.sim import sim_utils
    from dxtbx.model.crystal import CrystalFactory
    from dxtbx_model_ext import flex_Beam
    from dxtbx.model.detector import DetectorFactory
    from dxtbx.model.beam import BeamFactory
    from simtbx.nanoBragg.tst_nanoBragg_basic import fcalc_from_pdb

    import numpy as np
    from cxid9114.parameters import ENERGY_CONV

    energies = np.arange(8920, 8930)
    fluxes = np.ones(len(energies)) * 5e11

    patt_args = {"Ncells_abc": (20, 20, 20), "profile": "square", "verbose": 0}

    beam_descr = {
        'direction': (0.0, 0.0, 1.0),
        'divergence': 0.0,
        'flux': 5e11,
        'polarization_fraction': 1.,
        'polarization_normal': (0.0, 1.0, 0.0),
        'sigma_divergence': 0.0,
        'transmission': 1.0,
        'wavelength': ENERGY_CONV / energies[0]
    }

    cryst_descr = {
        '__id__': 'crystal',
        'real_space_a': (79, 0, 0),
        'real_space_b': (0, 79, 0),
        'real_space_c': (0, 0, 38),
        'space_group_hall_symbol': '-P 4 2'
    }

    det_descr = {
        'panels': [{
            'fast_axis': (-1.0, 0.0, 0.0),
            'gain': 1.0,
            'identifier': '',
            'image_size': (196, 196),
            'mask': [],
            'material': '',
            'mu': 0.0,
            'name': 'Panel',
            'origin': (19.6, -19.6, -550),
            'pedestal': 0.0,
            'pixel_size': (0.1, 0.1),
            'px_mm_strategy': {
                'type': 'SimplePxMmStrategy'
            },
            'raw_image_offset': (0, 0),
            'slow_axis': (0.0, 1.0, 0.0),
            'thickness': 0.0,
            'trusted_range': (0.0, 65536.0),
            'type': ''
        }]
    }

    DET = DetectorFactory.from_dict(det_descr)
    BEAM = BeamFactory.from_dict(beam_descr)

    crystal = CrystalFactory.from_dict(cryst_descr)
    Patt = sim_utils.PatternFactory(crystal=crystal,
                                    detector=DET,
                                    beam=BEAM,
                                    **patt_args)

    img = None
    Fens = []
    xrbeams = flex_Beam()
    for fl, en in zip(fluxes, energies):
        wave = ENERGY_CONV / en
        F = fcalc_from_pdb(resolution=4, algorithm="fft", wavelength=wave)
        Patt.primer(crystal, energy=en, flux=fl, F=F)
        if img is None:
            img = Patt.sim_rois(reset=True)  # defaults to full detector
        else:
            img += Patt.sim_rois(reset=True)

        Fens.append(F)

        xrb = BeamFactory.from_dict(beam_descr)
        xrb.set_wavelength(wave *
                           1e-10)  # need to fix the necessity to do this..
        xrb.set_flux(fl)
        xrb.set_direction(BEAM.get_direction())

        xrbeams.append(xrb)

    #import pylab as plt
    #def plot_img(ax,img):
    #    m = img[img >0].mean()
    #    s = img[img > 0].std()
    #    vmax = m+5*s
    #    vmin = 0
    #    ax.imshow(img, vmin=vmin, vmax=vmax, cmap='gnuplot')

    print("\n\n\n")
    print("<><><><><><><><><><>")
    print("NEXT TRIAL")
    print("<><><><><><><><><><>")
    #

    patt2 = sim_utils.PatternFactory(crystal=crystal,
                                     detector=DET,
                                     beam=xrbeams,
                                     **patt_args)

    patt2.prime_multi_Fhkl(multisource_Fhkl=Fens)
    img2 = patt2.sim_rois(reset=True)

    #plt.figure()
    #ax1 = plt.gca()
    #plt.figure()
    #ax2 = plt.gca()
    #plot_img(ax1, img)
    #plot_img(ax2, img2)
    #plt.show()

    assert (np.allclose(img, img2))
Ejemplo n.º 3
0
def jitter_panels(panel_ids,
                  crystal,
                  refls,
                  det,
                  beam,
                  FF,
                  en,
                  data_imgs,
                  flux,
                  ret_best=False,
                  scanX=None,
                  scanY=None,
                  scanZ=None,
                  mos_dom=1,
                  mos_spread=0.01,
                  szx=30,
                  szy=30,
                  **kwargs):
    """
    Helper function for doing fast refinements by rocking the U-matrix
    portion of the crystal A matrix
    NOTE, additional kwargs are passed  to the PatternFactor instantiator

    :param panel_ids: a list of panels ids where refinement
        if None we will refine using all panels with 1 or more reflections
    :param crystal: dxtbx crystal model
    :param refls: reflection table with strong spots
    :param det: dxtxb multi panel detector model
    :param beam: dxtbx beam model. Note this beam is only used to
        specify the beam direction but not the its energy, we will
        override the energy.
    :param FF: a cctbc miller array instance or a list of them
        corresponding to the energies list (same length)
    :param en: a float or list of floats specifying energies
    :param data_imgs: the data imgs corresponding to the
    :param flux: float or list of floats, the flux of each energy
        and it should be same length as en
    :param ret_best, whether to return the best Amatrix or all the data
        from each scan
    :param scanX: scan angles for the X-rotation matrix
        (same for scanY and Z, note these are lab frame notataions)
    :param mos_dom, number os mosaic domains in crystal
    :param mos_spread, angular spread of reflections from mosaicity (tangential component)
    :return: best A matrix , or else a bunch of data for each panel
        that can be used to select best A matrix
    """
    if isinstance(en, float) or isinstance(en, int):
        en = [en]
        FF = [FF]
        flux = [flux]
    else:
        # TODO: assert iterables
        assert (len(en) == len(FF) == len(flux))

    if scanX is None:
        scanX = np.arange(-0.3, 0.3, 0.025)
    if scanY is None:
        scanY = np.arange(-0.3, 0.3, 0.025)
    # leave Z scanning off by default

    R = spot_utils.refls_by_panelname(refls)
    out = {}
    for i_color in range(len(en)):
        for i_pid, (pid, dat) in enumerate(izip(panel_ids, data_imgs)):
            print i_color + 1, len(en), i_pid + 1, len(panel_ids)
            P = sim_utils.PatternFactory(detector=det,
                                         beam=beam,
                                         panel_id=pid,
                                         recenter=True,
                                         **kwargs)
            P.adjust_mosaicity(mos_dom, mos_spread)
            P.primer(crystal, en[i_color], flux[i_color], FF[i_color])
            JR = JitterFactory(crystal, P, R[pid], dat, szx=szx, szy=szy)
            if pid not in out:  # initialize
                out[pid] = JR.jitter_Amat(scanX, scanY, scanZ, plot=False)
            else:  # just add the overlay from the second color
                out_temp = JR.jitter_Amat(scanX, scanY, scanZ, plot=False)
                out[pid]['overlaps'] += out_temp['overlaps']

    if ret_best:
        max_overlay_pos = np.sum([out[i]['overlaps'] for i in out],
                                 axis=0).argmax()
        bestA = out[out.keys()[0]]["A_seq"][
            max_overlay_pos]  # its the same sequence for each panel

        return bestA
    else:
        return out
Ejemplo n.º 4
0
data = utils.open_flex(data_file)
C0 = data['crystal']
C1 = data['sim_indexed_crystals'][0]

if reset_unit_cell:
    print C1.get_unit_cell()
    #a,b,c,_,_,_ = C0.get_unit_cell().parameters()
    #B = sqr( (a,0,0,   0,b,0,  0,0,c)).inverse()
    A = sqr(C0.get_A())
    #B = sqr( C0.get_B())
    #C1.set_B(B)
    C1.set_A(A)
    print C1.get_unit_cell()

if sim_new_cryst:
    Patts = sim_utils.PatternFactory(crystal=C1)
    en, fcalc = sim_utils.load_fcalc_file("../sim/fcalc_slim.pkl")
    flux = [data['fracA'] * 1e14, data['fracB'] * 1e14]
    imgA, imgB = Patts.make_pattern2(C1, flux, en, fcalc, 20, 0.1, False)

else:
    orig_file = os.path.splitext(data_file)[0] + ".h5"
    imgA = dxtbx.load(orig_file).get_raw_data(0).as_numpy_array()

threshA = threshB = 0
labimgA, nlabA = ndimage.label(imgA > threshA)
out = ndimage.center_of_mass(imgA, labimgA, range(1, nlabA))
yA, xA = map(np.array, zip(*out))

xdata, ydata, _ = map(np.array,
                      spot_utils.xyz_from_refl(data['sim_indexed_refls']))
Ejemplo n.º 5
0
import sys

"""
This script is used to analyze the isr.py data files..
"""

data_file = sys.argv[1]

data = utils.open_flex(data_file)
C0 = data['crystal']
C1 = data['sim_indexed_crystals'][0]

import os
orig_file = os.path.splitext( data_file)[0] + ".h5"
Patts = sim_utils.PatternFactory()
en, fcalc = sim_utils.load_fcalc_file("../sim/fcalc_slim.pkl")
flux= [ data['fracA']*1e14, data['fracB']*1e14]
sim_patt = Patts.make_pattern2(C1, flux, en, fcalc, 20,0.1, False)
imgA,imgB = sim_patt
threshA = threshB = 0

labimgA, nlabA = ndimage.label(imgA > threshA)
out = ndimage.center_of_mass(imgA, labimgA, range(1, nlabA))
yA, xA = map(np.array, zip(*out))

labimgB, nlabB = ndimage.label(imgB > threshB)
out = ndimage.center_of_mass(imgB, labimgB, range(1, nlabB))
yB, xB = map(np.array, zip(*out))

xAB, yAB = np.hstack((xA, xB)), np.hstack((yA, yB))
Ejemplo n.º 6
0
def xyscan(crystal,
           fcalcs_energies,
           fcalcs,
           fracA,
           fracB,
           strong,
           rotxy_series,
           jid,
           mos_dom=1,
           mos_spread=0.05,
           flux=1e14,
           use_weights=False,
           raw_image=None):
    """
    :param crystal:
    :param fcalcs_energies:
    :param fcalcs:
    :param fracA:
    :param fracB:
    :param strong:
    :param rotxy_series:
    :param jid:
    :param mos_dom:
    :param mos_spread:
    :param flux:
    :param use_weights:
    :return:
    """
    Patts = sim_utils.PatternFactory()
    Patts.adjust_mosaicity(mos_dom, mos_spread)  # defaults
    flux_per_en = [fracA * flux, fracB * flux]

    img_size = Patts.detector.to_dict()['panels'][0]['image_size']
    found_spot_mask = spot_utils.strong_spot_mask(strong, img_size)
    #if use_weights:
    #    if raw_image is None:
    #        raise ValueError("Cant use weights if raw image is None")
    #    spot_signals = raw_image * found_spot_mask
    #    weights = spot_signals /  spot_signals.max()

    overlaps = []
    for rots in rotxy_series:
        if len(rots) == 2:
            Op = rots[0] * rots[1]
        elif len(rots) == 3:
            Op = rots[0] * rots[1] * rots[2]
        elif len(rots == 1):
            Op = rots[0]
        else:
            raise ValueError("rotxy_series should be list of 1,2, or 3-tuples")
        sim_patt = Patts.make_pattern2(crystal=deepcopy(crystal),
                                       flux_per_en=flux_per_en,
                                       energies_eV=fcalcs_energies,
                                       fcalcs_at_energies=fcalcs,
                                       mosaic_spread=None,
                                       mosaic_domains=None,
                                       ret_sum=True,
                                       Op=Op)

        if use_weights:
            dblock = utils.datablock_from_numpyarrays(image=sim_patt,
                                                      detector=Patts.detector,
                                                      beam=Patts.beam)
            sim_refl = flex.reflection_table.from_observations(
                dblock, params=find_spot_params)
            sim_spot_mask = spot_utils.strong_spot_mask(
                sim_refl, sim_patt.shape)
            overlaps.append(np.sum(sim_spot_mask * found_spot_mask))
            #weights2 = sim_patt / sim_patt.max()
            #overlaps.append( np.sum(sim_sig_mask * found_spot_mask * weights * weights2))
        else:
            sim_sig_mask = sim_patt > 0
            overlaps.append(np.sum(sim_sig_mask * found_spot_mask))
        print "JOB %d" % jid
    return overlaps