def process(work_params, spots, sampling_resolution_factor=0.5):
  import libtbx.load_env
  libtbx.env.require_module("labelit")
  spots_high_res = get_spots_high_resolution(
    work_params=work_params, spots=spots)
  if (spots_high_res is None):
    return
  uc = work_params.unit_cell
  uc_max_length = max(uc.parameters()[0:3])
  sampling = sampling_resolution_factor * spots_high_res / uc_max_length
  #
  from labelit.preferences import labelit_commands
  labelit_commands.model_refinement_minimum_N = 10
  labelit_commands.target_cell = uc
  print "labelit_commands.target_cell:", labelit_commands.target_cell
  #
  from scitbx.array_family import flex
  raw_spot_input = flex.vec3_double()
  dsx,dsy = work_params.detector.size
  dpx,dpy = work_params.detector.pixels
  sopx,sopy = dsx/dpx, dsy/dpy
  for spot in spots: # XXX C++
    x, y = spot.ctr_mass_x(), spot.ctr_mass_y()
    raw_spot_input.append((x*sopx+0.5, y*sopy+0.5, 0.0))
  #
  from labelit.dptbx import AutoIndexEngine, Parameters
  from iotbx.detectors.context.endstation import EndStation
  ai = AutoIndexEngine(EndStation(), sampling)
  ai.setData(raw_spot_input)
  ai_params = Parameters(
    xbeam=dsx/2,
    ybeam=dsy/2,
    distance=work_params.detector.distance,
    twotheta=0.)
  ai.setBase(ai_params)
  ai.setWavelength(work_params.wavelength)
  ai.setMaxcell(1.25*max(uc.parameters()[0:3]))
  ai.setDeltaphi(0.0)
  f = ai.film_to_camera()
  c = ai.camera_to_xyz()
  #
  from labelit.dptbx.sampling import HemisphereSampler
  hem_samp = HemisphereSampler(
    max_grid=sampling,
    characteristic_grid=sampling,
    quick_grid=0.016) # all grid parameters in radians
  hem_samp.hemisphere(
    ai=ai, size=30, cutoff_divisor=4.) # never change these parameters
  #
  from labelit.dptbx.basis_choice import SelectBasisMetaprocedure
  pd = {}
  sbm = SelectBasisMetaprocedure(
    input_index_engine=ai,
    input_dictionary=pd,
    opt_rawframes=False,
    opt_target=True,
    reduce_target=False)
  ai.fixsign()
  return ai
示例#2
0
    def analyze_one(self, solution):
        inputpd = self.Org.process()

        settings = pickle.load(open("LABELIT_possible", "rb"))
        setting = [
            setting for setting in settings if setting["counter"] == solution
        ][0]

        from labelit.preferences import labelit_commands as param

        pixel_size = float(inputpd['pixel_size'])
        self.pixel_size = pixel_size

        from labelit.dptbx import AutoIndexEngine, Parameters
        ai = AutoIndexEngine(inputpd['endstation'])

        P = Parameters(xbeam=setting["refined x beam"],
                       ybeam=setting["refined y beam"],
                       distance=setting["refined distance"],
                       twotheta=float(inputpd["twotheta"]))
        ai.setBase(P)
        ai.setWavelength(float(inputpd['wavelength']))
        ai.setMaxcell(float(inputpd['ref_maxcel']))
        print("Deltaphi is", float(inputpd['deltaphi']))
        ai.setDeltaphi(float(inputpd['deltaphi']) * math.pi / 180.)
        ai.setMosaicity(setting["mosaicity"])
        ai.setOrientation(setting["orient"])
        #why aren't hexagonal constraints applied here???
        print(inputpd["osc_start"])

        image_centers = [(math.pi / 180.) * float(x)
                         for x in inputpd["osc_start"].values()]

        limiting_resolution = param.distl_highres_limit
        print("Limiting resolution", limiting_resolution)

        #predict the spots
        spots = ai.predict_all(image_centers[0], limiting_resolution)
        pre2m = spots.vec3()
        self.pre2m = pre2m

        hkllist = spots.hkl()
        cell = ai.getOrientation().unit_cell()
        print(cell)
        for hkl in hkllist:
            #print "%25s %5.2f"%(str(hkl),cell.d(hkl))
            assert cell.d(hkl) >= limiting_resolution
        print("Number of hkls:", (hkllist).size(), end=' ')
        print("all inside the %4.2f Angstrom limiting sphere." %
              limiting_resolution)
        print("The unit cell is", cell)
        self.solution_setting_ai = ai
        self.solution_pd = inputpd
        self.image_centers = image_centers
        self.one_setting = setting
        return [ai.getOrientation().unit_cell(), hkllist]
示例#3
0
    def __init__(self, pd, horizons_phil):
        #initialVolumeFactor = 1.8 # Legacy value=1.5 prior to 7/17/07
        initialVolumeFactor = horizons_phil.integration.initial_volume_factor
        self.subsequentVolumeFactor = 1.5  # this value not used by stats_mtz; see stats_mtz code
        if horizons_phil.mosflm_integration_reslimit_override != None:
            self.initial_limit = horizons_phil.mosflm_integration_reslimit_override
            self.outer_limit = self.initial_limit
        else:
            #resolution_inspection from DISTL is a conservative estimate
            #increase the reciprocal space volume by factor of 1.5 for trial integration
            from math import pow
            trial_reslimit = float(pd["resolution_inspection"]) / pow(
                initialVolumeFactor, 1.0 / 3.0)

            from labelit.dptbx import AutoIndexEngine, Parameters
            ai = AutoIndexEngine(pd['endstation'],
                                 horizons_phil.model_refinement_minimum_N
                                 )  #Just a container for a few parameters
            base = Parameters(xbeam=float(pd['xbeam']),
                              ybeam=float(pd['ybeam']),
                              distance=float(pd['distance']),
                              twotheta=0.0)
            ai.setBase(base)
            ai.setWavelength(float(pd['wavelength']))

            safety = SafetyLimits(pd, ai).safety_limit(
                algorithm=horizons_phil.mosflm_safety_algorithm)

            self.outer_limit = safety
            self.initial_limit = max((trial_reslimit, safety))
            """synopsis:
      LABELIT integrates to determine the best-estimate of
      dataset resolution.  After integration out to the image corner, an
      intensity log-plot is used to linearly extrapolate out to an I/sigma
      of 0.75, based on all partials and fulls.  Ana Gonzalez requested that
      this be changed to the image edge, because of MOSFLM problems in cases
      where the diffraction is of low quality or low resolution.  Unfortunately,
      using a cutoff at the edge is counter-productive in cases where diffraction
      extends past the corners.  In those cases, the log-linear extrapolation
      invariably leads to a resolution estimate that is more optimistic (lower
      Angstrom cutoff) than when all of the data is used out to the image corner.
      Edge cutoff is therefore not recommended.  However, it is now available as
      a configurable choice using the command line argument:
      mosflm_safety_algorithm=[corner|edge]  (default = corner).
      #print pd['file'][pd['file'].keys()[0]]
      #print "corner: %.2f"%(max((trial_reslimit,interface.SafetyLimits(pd,ai).safety_limit(algorithm="corner"))))
      #print "  edge: %.2f"%(max((trial_reslimit,interface.SafetyLimits(pd,ai).safety_limit(algorithm="edge"))))
      """
        self.current_limit = self.initial_limit
示例#4
0
  def integrate_one_character(self,setting,integration_limit):
    #from libtbx.development.timers import Profiler
    #P = Profiler("Preliminary")
    import copy
    local = copy.deepcopy(self.process_dictionary)
    local['cell']=cellstr(setting)

    print("Cell in setting",setting["counter"],local["cell"])

    frames = list(sorted(self.spotfinder_results.pd['osc_start'].keys()))

    local['maxcel']='0'
    local['xbeam']="%f"%setting['minimizer'].new['xbeam']
    local['ybeam']="%f"%setting['minimizer'].new['ybeam']
    local['distance']="%f"%setting['minimizer'].new['distance']
    local["resolution"]= "%f"%integration_limit

    from labelit.steps import primaries
    local['spacegroup'] = primaries[setting['bravais']]

    local['procstart'] = local['procend'] = "%d"%frames[0]

    self.pixel_size = float(local['pixel_size'])

    from labelit.dptbx import AutoIndexEngine, Parameters
    ai = AutoIndexEngine(local['endstation'])

    P = Parameters(xbeam=setting["refined x beam"],ybeam=setting["refined y beam"],
             distance=setting["refined distance"],twotheta=float(local["twotheta"]))
    ai.setBase(P)
    ai.setWavelength(float(local['wavelength']))
    ai.setMaxcell(float(local['ref_maxcel']))
    print("Deltaphi is",float(local['deltaphi']))
    ai.setDeltaphi(float(local['deltaphi'])*math.pi/180.)
    ai.setMosaicity(setting["mosaicity"])
    ai.setOrientation(setting["orient"])
    refimage = self.files.images[0]
    ai.set_active_areas(self.horizons_phil,
                        beam=(int(refimage.beamx/refimage.pixel_size),
                              int(refimage.beamy/refimage.pixel_size)))

    image_centers = [(math.pi/180.)*float(x) for x in local["osc_start"].values()]

    print("Limiting resolution",integration_limit)
    local["results"] = []
    for i in range(len(frames)):
      print("---------BEGIN Integrate one frame %d %s" % \
          (frames[i], os.path.split(self.files.filenames()[i])[-1]))
      #P = Profiler("worker")
      if self.horizons_phil.integration.combine_sym_constraints_and_3D_target and setting["counter"]>1:
        from rstbx.apps.stills.dials_refinement_preceding_integration import integrate_one_frame
        integrate_worker = integrate_one_frame(self.triclinic["integration"]["results"][0])
      else:
        from rstbx.apps.stills.deltapsi_refinement_preceding_integration import integrate_one_frame
        integrate_worker = integrate_one_frame()
      integrate_worker.inputai = ai

      integrate_worker.inputpd = dict(masks=local["masks"],
                                      size1=local["size1"],
                                      size2=local["size2"],
                                      symmetry=setting["best_subsym"])
        # carefully select only the data items needed for integrate_worker
        # avoid giving the whole process dictionary; reference to "local"
        # is a circular reference creating memory leak, while copying the
        # whole thing is a big performance hit.
      integrate_worker.frame_numbers = frames
      integrate_worker.imagefiles = self.files
      integrate_worker.spotfinder = self.spotfinder_results
      integrate_worker.image_centers = image_centers

      integrate_worker.limiting_resolution = integration_limit
      integrate_worker.setting_id = setting["counter"]
      integrate_worker.pixel_size = self.pixel_size
      integrate_worker.set_pixel_size(self.pixel_size)
      integrate_worker.set_detector_size(int(local["size1"]),int(local["size2"]))

      integrate_worker.set_detector_saturation(refimage.saturation)
      integrate_worker.set_up_mask_focus()
      integrate_worker.initialize_increments(i)
      integrate_worker.horizons_phil = self.horizons_phil
      if self.horizons_phil.indexing.verbose_cv:
        print("EFFECTIVE TILING"," ".join(
          ["%d"%z for z in refimage.get_tile_manager(self.horizons_phil).effective_tiling_as_flex_int()]))
      integrate_worker.integration_concept(image_number = i,
        cb_op_to_primitive = setting["cb_op_inp_best"].inverse(),
        verbose_cv = self.horizons_phil.indexing.verbose_cv,
        background_factor = self.horizons_phil.integration.background_factor,
        )
      #P = Profiler("proper")
      integrate_worker.integration_proper()
      local["results"].append(integrate_worker)
      local["r_xbeam"]=ai.xbeam()
      local["r_ybeam"]=ai.ybeam()
      local["r_distance"]=ai.distance()
      local["r_wavelength"]=ai.wavelength
      local["r_residual"]=integrate_worker.r_residual
      local["r_mosaicity"]=setting["mosaicity"]
      try:
        local["ewald_proximal_volume"]=integrate_worker.ewald_proximal_volume
      except Exception as e:
        local["ewald_proximal_volume"]=None

      if (self.horizons_phil.indexing.open_wx_viewer):
       if True: #use updated slip viewer
        try:
          import wx
          from rstbx.slip_viewer.frame import XrayFrame as SlipXrayFrame
          from rstbx.command_line.slip_viewer import master_str as slip_params
          from iotbx import phil
          from spotfinder import phil_str
          from spotfinder.command_line.signal_strength import additional_spotfinder_phil_defs

          work_phil = phil.process_command_line("",master_string=slip_params + phil_str + additional_spotfinder_phil_defs)
          work_params = work_phil.work.extract()

          app = wx.App(0)
          wx.SystemOptions.SetOption("osx.openfiledialog.always-show-types", "1")
          frame = SlipXrayFrame(None, -1, "X-ray image display", size=(800,720))
          frame.Show()

          # Update initial settings with values from the command line.  Needs
          # to be done before image is loaded (but after the frame is
          # instantiated).
          frame.inherited_params = integrate_worker.horizons_phil
          frame.params = work_params

          if (frame.pyslip is None):
            frame.init_pyslip()
          if (frame.settings_frame is None):
            frame.OnShowSettings(None)
          frame.Layout()

          frame.pyslip.tiles.user_requests_antialiasing = work_params.anti_aliasing
          frame.settings_frame.panel.center_ctrl.SetValue(True)
          frame.settings_frame.panel.integ_ctrl.SetValue(True)
          frame.settings_frame.panel.spots_ctrl.SetValue(False)
          frame.settings.show_effective_tiling = work_params.show_effective_tiling
          frame.settings_frame.panel.collect_values()
          paths = work_phil.remaining_args

          frame.user_callback = integrate_worker.slip_callback
          frame.load_image(self.files.filenames()[i])

          app.MainLoop()
          del app
        except Exception:
          pass # must use phenix.wxpython for wx display

       elif False : #original wx viewer
        try:
          from rstbx.viewer.frame import XrayFrame
          import wx
          from rstbx.viewer import display
          display.user_callback = integrate_worker.user_callback

          app = wx.App(0)
          frame = XrayFrame(None, -1, "X-ray image display", size=(1200,1080))
          frame.settings.show_spotfinder_spots = False
          frame.settings.show_integration = False
          #frame.settings.enable_collect_values = False
          frame.SetSize((1024,780))
          frame.load_image(self.files.filenames()[i])
          frame.Show()
          app.MainLoop()
          del app
        except Exception:
          pass # must use phenix.wxpython for wx display

      # for the wx image viewer
      filename = self.horizons_phil.indexing.indexing_pickle
      if filename != None:
        filename = "%s_%d_%d.pkl"%(filename,setting["counter"],keys[i])

        SIO = StringIO()
        table_raw = show_observations(integrate_worker.get_obs(
          local["spacegroup"]),out=SIO)
        limitobject = ResolutionAnalysisMetaClass(local, self.horizons_phil)
        info = dict(table = SIO.getvalue(),
          table_raw = table_raw,
          xbeam = setting["refined x beam"],
          ybeam = setting["refined y beam"],
          distance = setting["refined distance"],
          residual = integrate_worker.r_residual,
          resolution = limitobject.value, # FIXME not reliable?
          mosaicity = setting["mosaicity"],
          pointgroup = local["spacegroup"],
          hkllist = integrate_worker.hkllist,
          predictions = (1./integrate_worker.pixel_size)*integrate_worker.predicted,
          mapped_predictions = integrate_worker.detector_xy,
          integration_masks_xy = integrate_worker.integration_masks_as_xy_tuples(),
          background_masks_xy = integrate_worker.background_masks_as_xy_tuples()
        )
        assert info["predictions"].size() >= info["mapped_predictions"].size()
        assert info["predictions"].size() == info["hkllist"].size()
        G = open(filename,"wb")
        pickle.dump(info,G,pickle.HIGHEST_PROTOCOL)
      print("---------END Integrate one frame",frames[i])

    return local
    def get_predictions_accounting_for_centering(self, experiments,
                                                 reflections,
                                                 cb_op_to_primitive, **kwargs):
        # interface requires this function to set current_orientation
        # in the actual setting used for Miller index calculation
        detector = experiments[0].detector
        crystal = experiments[0].crystal

        if self.horizons_phil.integration.model == "user_supplied":

            lower_limit_domain_size = math.pow(
                crystal.get_unit_cell().volume(), 1. / 3.
            ) * self.horizons_phil.integration.mosaic.domain_size_lower_limit  # default 10-unit cell block size minimum reasonable domain
            actual_used_domain_size = kwargs.get("domain_size_ang",
                                                 lower_limit_domain_size)

            self.block_counter += 1
            rot_mat = matrix.sqr(
                cb_op_to_primitive.c().r().as_double()).transpose()

            from cctbx.crystal_orientation import crystal_orientation, basis_type
            centered_orientation = crystal_orientation(crystal.get_A(),
                                                       basis_type.reciprocal)
            self.current_orientation = centered_orientation
            self.current_cb_op_to_primitive = cb_op_to_primitive
            primitive_orientation = centered_orientation.change_basis(rot_mat)

            self.inputai.setOrientation(primitive_orientation)
            from cxi_user import pre_get_predictions
            if self.block_counter < 2:
                KLUDGE = self.horizons_phil.integration.mosaic.kludge1  # bugfix 1 of 2 for protocol 6, equation 2
                self.inputai.setMosaicity(KLUDGE * self.inputai.getMosaicity())

            oldbase = self.inputai.getBase()

            #print oldbase.xbeam, oldbase.ybeam
            newbeam = detector[0].get_beam_centre(experiments[0].beam.get_s0())
            newdistance = -detector[0].get_beam_centre_lab(
                experiments[0].beam.get_s0())[2]

            from labelit.dptbx import Parameters
            base = Parameters(
                xbeam=newbeam[0],
                ybeam=newbeam[1],  #oldbase.xbeam, ybeam = oldbase.ybeam,
                distance=newdistance,
                twotheta=0.0)
            self.inputai.setBase(base)
            self.inputai.setWavelength(experiments[0].beam.get_wavelength())

            self.bp3_wrapper = pre_get_predictions(
                self.inputai,
                self.horizons_phil,
                raw_image=self.imagefiles.images[self.image_number],
                imageindex=self.frame_numbers[self.image_number],
                spotfinder=self.spotfinder,
                limiting_resolution=self.limiting_resolution,
                domain_size_ang=actual_used_domain_size,
            )

            BPpredicted = self.bp3_wrapper.ucbp3.selected_predictions_labelit_format(
            )
            BPhkllist = self.bp3_wrapper.ucbp3.selected_hkls()

            self.actual = actual_used_domain_size
            primitive_hkllist = BPhkllist
            #not sure if matrix needs to be transposed first for outputting HKL's???:
            self.hkllist = cb_op_to_primitive.inverse().apply(
                primitive_hkllist)

            if self.horizons_phil.integration.spot_prediction == "dials":
                from dials.algorithms.spot_prediction import StillsReflectionPredictor
                predictor = StillsReflectionPredictor(experiments[0])
                Rcalc = flex.reflection_table.empty_standard(len(self.hkllist))
                Rcalc['miller_index'] = self.hkllist
                predictor.for_reflection_table(Rcalc, crystal.get_A())
                self.predicted = Rcalc['xyzcal.mm']
                self.dials_spot_prediction = Rcalc
                self.dials_model = experiments

            elif self.horizons_phil.integration.spot_prediction == "ucbp3":
                self.predicted = BPpredicted

            self.inputai.setOrientation(centered_orientation)
            if self.inputai.active_areas != None:
                self.predicted, self.hkllist = self.inputai.active_areas(
                    self.predicted, self.hkllist, self.pixel_size)
            if self.block_counter < 2:
                down = self.inputai.getMosaicity() / KLUDGE
                print "Readjusting mosaicity back down to ", down
                self.inputai.setMosaicity(down)
            return