def save_opt_det(phil_params, x, ref_params, SIM): opt_det = get_optimized_detector(x, ref_params, SIM) El = ExperimentList() E = Experiment() E.detector = opt_det El.append(E) El.as_file(phil_params.geometry.optimized_detector_name) print("Saved detector model to %s" % phil_params.geometry.optimized_detector_name)
from __future__ import division from simtbx.diffBragg import utils from simtbx.nanoBragg.tst_nanoBragg_basic import pdb_lines from simtbx.diffBragg.phil import hopper_phil, philz from libtbx.phil import parse from simtbx.nanoBragg import tst_nanoBragg_multipanel from dxtbx.model import Experiment import numpy as np # make a dummie experiment expt = Experiment() expt.detector = tst_nanoBragg_multipanel.whole_det expt.beam = tst_nanoBragg_multipanel.beam expt.crystal = tst_nanoBragg_multipanel.cryst # write a dummie PDB file PDB = "1234.pdb" o = open(PDB, "w") o.write(pdb_lines) o.close() # Create a miller array from on-disk PDB file F = utils.get_complex_fcalc_from_pdb(PDB, wavelength=expt.beam.get_wavelength(), dmin=2, dmax=20, k_sol=0.2, b_sol=20) F = F.as_amplitude_array() Fmap = {h: amp for h,amp in zip(F.indices(), F.data())} # Create a sim_data class instance as would be done for hopper_utils.refine for example phil_scope = parse(hopper_phil+philz)
P.init.Nabc = 20, 20, 20 else: P.init.Nabc = SIM.crystal.Ncells_abc if "detz_shift" in args.perturb: P.init.detz_shift = 1 else: P.init.detz_shift = 0 if "eta" in args.perturb: P.init.eta_abc = [0.12, 0.13, 0.14] P.simulator.crystal.num_mosaicity_samples = 250 # in practive, the number of mosaic domains we model should be smaller than whats in the crystal .. . P.simulator.crystal.has_isotropic_mosaicity = False P.fix.eta_abc = False E.detector = SIM.detector E.beam = SIM.D.beam E.imageset = make_imageset([img], E.beam, E.detector) #refls = utils.refls_from_sims([img], E.detector, E.beam, thresh=18) refls = utils.refls_from_sims([spots], E.detector, E.beam, thresh=18) print("%d REFLS" % len(refls)) utils.refls_to_q(refls, E.detector, E.beam, update_table=True) utils.refls_to_hkl(refls, E.detector, E.beam, E.crystal, update_table=True) P.roi.shoebox_size = 20 P.relative_tilt = False P.roi.fit_tilt = False P.roi.pad_shoebox_for_background_estimation = 10 P.roi.reject_edge_reflections = False P.refiner.sigma_r = SIM.D.readout_noise_adu P.refiner.adu_per_photon = SIM.D.quantum_gain
def update_detector(x, ref_params, SIM, save=None): """ Update the internal geometry of the diffBragg instance :param x: refinement parameters as seen by scipy.optimize (e.g. rescaled floats) :param ref_params: diffBragg.refiners.Parameters (dict of RangedParameters) :param SIM: SIM instance (instance of nanoBragg.sim_data.SimData) :param save: optional name to save the detector """ det = SIM.detector if save is not None: new_det = Detector() for pid in range(len(det)): panel = det[pid] panel_dict = panel.to_dict() group_id = SIM.panel_group_from_id[pid] if group_id not in SIM.panel_groups_refined: fdet = panel.get_fast_axis() sdet = panel.get_slow_axis() origin = panel.get_origin() else: Oang_p = ref_params["group%d_RotOrth" % group_id] Fang_p = ref_params["group%d_RotFast" % group_id] Sang_p = ref_params["group%d_RotSlow" % group_id] Xdist_p = ref_params["group%d_ShiftX" % group_id] Ydist_p = ref_params["group%d_ShiftY" % group_id] Zdist_p = ref_params["group%d_ShiftZ" % group_id] Oang = Oang_p.get_val(x[Oang_p.xpos]) Fang = Fang_p.get_val(x[Fang_p.xpos]) Sang = Sang_p.get_val(x[Sang_p.xpos]) Xdist = Xdist_p.get_val(x[Xdist_p.xpos]) Ydist = Ydist_p.get_val(x[Ydist_p.xpos]) Zdist = Zdist_p.get_val(x[Zdist_p.xpos]) origin_of_rotation = SIM.panel_reference_from_id[pid] SIM.D.reference_origin = origin_of_rotation SIM.D.update_dxtbx_geoms(det, SIM.beam.nanoBragg_constructor_beam, pid, Oang, Fang, Sang, Xdist, Ydist, Zdist, force=False) fdet = SIM.D.fdet_vector sdet = SIM.D.sdet_vector origin = SIM.D.get_origin() if save is not None: panel_dict["fast_axis"] = fdet panel_dict["slow_axis"] = sdet panel_dict["origin"] = origin new_det.add_panel(Panel.from_dict(panel_dict)) if save is not None and COMM.rank == 0: t = time.time() El = ExperimentList() E = Experiment() E.detector = new_det El.append(E) El.as_file(save) t = time.time() - t print("Saved detector model to %s (took %.4f sec)" % (save, t), flush=True)
def convert_crystfel_to_dxtbx(geom_filename, output_filename, detdist_override=None): """ :param geom_filename: a crystfel geometry file https://www.desy.de/~twhite/crystfel/manual-crystfel_geometry.html :param output_filename: filename for a dxtbx experiment containing a single detector model (this is a json file) :param detdist_override: alter the detector distance stored in the crystfel geometry to this value (in millimeters) """ geom = load_crystfel_geometry(geom_filename) dxtbx_det = Detector() for panel_name in geom['panels'].keys(): P = geom['panels'][panel_name] FAST = P['fsx'], P['fsy'], P['fsz'] SLOW = P['ssx'], P['ssy'], P['ssz'] # dxtbx uses millimeters pixsize = 1 / P['res'] # meters pixsize_mm = pixsize * 1000 detdist = P['coffset'] + P['clen'] # meters detdist_mm = detdist * 1000 if detdist_override is not None: detdist_mm = detdist_override # dxtbx and crystfel both identify the outer corner of the first pixel in memory as the origin of the panel origin = P['cnx'] * pixsize_mm, P[ 'cny'] * pixsize_mm, -detdist_mm # dxtbx assumes crystal as at point 0,0,0 num_fast_pix = P["max_fs"] - P['min_fs'] + 1 num_slow_pix = P["max_ss"] - P['min_ss'] + 1 panel_description = { 'fast_axis': FAST, 'gain': 1.0, # I dont think nanoBragg cares about this parameter 'identifier': '', 'image_size': (num_fast_pix, num_slow_pix), 'mask': [], 'material': 'Si', 'mu': 0, # NOTE for a thick detector set this to appropriate value 'name': panel_name, 'origin': origin, 'pedestal': 0.0, # I dont think nanoBragg cares about this parameter 'pixel_size': (pixsize_mm, pixsize_mm), 'px_mm_strategy': { 'type': 'SimplePxMmStrategy' }, 'raw_image_offset': (0, 0), # not sure what this is 'slow_axis': SLOW, 'thickness': 0, # note for a thick detector set this to appropriate value 'trusted_range': (-1.0, 1e6), # set as you wish 'type': 'SENSOR_PAD' } dxtbx_node = Panel.from_dict(panel_description) dxtbx_det.add_panel(dxtbx_node) E = Experiment() E.detector = dxtbx_det El = ExperimentList() El.append(E) El.as_file(output_filename) # this can be loaded into nanoBragg
path = iset.get_path(0) d = df2_filt0.query("imgpaths=='%s'" % path) if has_master: master_index = iset.indices()[0] d = d.query("master_indices==%d" % master_index) if len(d) != 1: continue A = d.Amats.values[0] #break C = deepcopy(crystals[i]) #C.set_A(A) Ex = Experiment() Ex.crystal = C Ex.imageset = iset Ex.beam = beams[i] Ex.detector = D El2.append(Ex) Rsel = R.select(R['id']==i) nref = len(Rsel) Rsel['id'] = flex.int(nref, new_id) R2.extend(Rsel) new_id += 1 print (new_id) el_file = "%s.expt" % args.tag R_file = "%s.refl" % args.tag El2.as_file(el_file) print("Saved experiment %s" % el_file ) R2.as_file(R_file) print("Saved refls %s" % R_file)