Ejemplo n.º 1
0
def exercise_multi_axis_goniometer():
  import libtbx.load_env
  from libtbx.test_utils import approx_equal
  import os

  if not libtbx.env.has_module("dials_regression"):
    print "Skipping tstFormatCBFFull.py: dials_regression not present"
    return

  data_dir = libtbx.env.find_in_repositories(
    relative_path="dials_regression/image_examples/dials-190",
    test=os.path.isdir)

  from dxtbx.imageset import ImageSetFactory
  imgset = ImageSetFactory.new(os.path.join(data_dir, "whatev1_01_00001.cbf"))[0]
  gonio = imgset.get_goniometer()
  assert approx_equal(gonio.get_fixed_rotation(), (1,0,0,0,1,0,0,0,1))
  assert approx_equal(gonio.get_setting_rotation(), (1,0,0,0,1,0,0,0,1))

  from dxtbx.imageset import ImageSetFactory
  imgset = ImageSetFactory.new(os.path.join(data_dir, "whatev1_02_00001.cbf"))[0]
  gonio = imgset.get_goniometer()
  assert approx_equal(gonio.get_fixed_rotation(), (1,0,0,0,1,0,0,0,1))
  assert approx_equal(
    gonio.get_setting_rotation(),
    (1, 0, 0, 0, -0.5, 0.866, 0.0, -0.866, -0.5), eps=1e-4)

  from dxtbx.imageset import ImageSetFactory
  imgset = ImageSetFactory.new(os.path.join(data_dir, "whatev1_03_00001.cbf"))[0]
  gonio = imgset.get_goniometer()
  assert approx_equal(gonio.get_fixed_rotation(), (1,0,0,0,1,0,0,0,1))
  assert approx_equal(
    gonio.get_setting_rotation(),
    (1, 0, 0, 0, 0.5, 0.866, 0.0, -0.866, 0.5), eps=1e-4)
Ejemplo n.º 2
0
def imagesweep_from_dict(d, check_format=True):
  '''Construct and image sweep from the dictionary.'''
  from dxtbx.imageset import ImageSetFactory
  from dxtbx.serialize import beam, detector, goniometer, scan
  from dxtbx.serialize.filename import load_path

  # Get the template (required)
  template = load_path(str(d['template']))

  # If the scan isn't set, find all available files
  scan_dict = d.get('scan')
  if scan_dict is None:
    image_range = None
  else:
    image_range = scan_dict.get('image_range')

  # Construct the sweep
  try:
    sweep = ImageSetFactory.from_template(
      template, image_range, check_format=check_format)[0]

    # Get the existing models as dictionaries
    beam_dict = beam.to_dict(sweep.get_beam())
    gonio_dict = goniometer.to_dict(sweep.get_goniometer())
    detector_dict = detector.to_dict(sweep.get_detector())
    scan_dict = scan.to_dict(sweep.get_scan())
  except Exception:
    indices = range(image_range[0], image_range[1] + 1)
    sweep = ImageSetFactory.make_sweep(
      template, indices, check_format=False)
    beam_dict = None
    gonio_dict = None
    detector_dict = None
    scan_dict = None

  # Set some external lookups
  if 'mask' in d and d['mask'] is not None:
    with open(d['mask']) as infile:
      sweep.external_lookup.mask.filename = d['mask']
      sweep.external_lookup.mask.data = pickle.load(infile)
  if 'gain' in d and d['gain'] is not None:
    with open(d['gain']) as infile:
      sweep.external_lookup.gain.filename = d['gain']
      sweep.external_lookup.gain.data = pickle.load(infile)
  if 'pedestal' in d and d['pedestal'] is not None:
    with open(d['pedestal']) as infile:
      sweep.external_lookup.pedestal.filename = d['pedestal']
      sweep.external_lookup.pedestal.data = pickle.load(infile)

  # Set the models with the exisiting models as templates
  sweep.set_beam(beam.from_dict(d.get('beam'), beam_dict))
  sweep.set_goniometer(goniometer.from_dict(d.get('goniometer'), gonio_dict))
  sweep.set_detector(detector.from_dict(d.get('detector'), detector_dict))
  sweep.set_scan(scan.from_dict(d.get('scan'), scan_dict))

  # Return the sweep
  return sweep
Ejemplo n.º 3
0
def print_sweep(list_of_images):

  from dxtbx.imageset import ImageSetFactory
  sweeps = ImageSetFactory.new(list_of_images)

  for s in sweeps:
    print s.get_detector()
    print s.get_beam()
    print s.get_goniometer()
    print s.get_scan()

    # compute the beam centre... in mm... w.r.t. fast, slow axis

    print 'Derived quantities:'

    from scitbx import matrix

    d = s.get_detector()[0]
    b = s.get_beam()

    o = matrix.col(d.get_origin())
    f = matrix.col(d.get_fast_axis())
    s = matrix.col(d.get_slow_axis())
    s0 = matrix.col(b.get_direction())

    n = f.cross(s)

    beam_offset = o - o.dot(s0) * s0
    print '    beam centre (mm, fast, slow): %.2f %.2f' % (- beam_offset.dot(f),
                                                            - beam_offset.dot(s))
Ejemplo n.º 4
0
def to_imageset(input_filename, extra_filename=None):
  '''Get an image set from the xds input filename plus an extra filename

  Params:
      input_filename The XDS.INP file
      extra_filename A (G)XPARM.XDS, INTGRATE.HKL or XDS_ASCII.HKL file

  Returns:
      The imageset

  '''
  from iotbx.xds import xds_inp
  from dxtbx.imageset import ImageSetFactory
  import dxtbx

  # Read the input filename
  handle = xds_inp.reader()
  handle.read_file(input_filename)

  # Get the template
  template = handle.name_template_of_data_frames[0].replace('?', '#')
  image_range = handle.data_range
  detector_name = handle.detector

  if extra_filename is not None:
    # we can get all the extra dxtbx models from extra_filename
    check_format = False
  else:
    # we need the image files present to get the dxtbx models
    check_format = True

  # Create the imageset
  imageset = ImageSetFactory.from_template(
    template, image_range=image_range, check_format=False)[0]

  # If an extra filename has been specified, try to load models
  if extra_filename:
    models = dxtbx.load(extra_filename)
    detector = models.get_detector()
    if detector_name.strip() == 'PILATUS':
      from dxtbx.model import ParallaxCorrectedPxMmStrategy
      from cctbx.eltbx import attenuation_coefficient
      table = attenuation_coefficient.get_table("Si")
      wavelength = models.get_beam().get_wavelength()
      mu = table.mu_at_angstrom(wavelength) / 10.0
      t0 = handle.sensor_thickness
      for panel in detector:
        panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0))
        panel.set_trusted_range(
          (handle.minimum_valid_pixel_value, handle.overload))
    imageset.set_beam(models.get_beam())
    imageset.set_detector(detector)
    imageset.set_goniometer(models.get_goniometer())
    # take the image range from XDS.INP
    scan = models.get_scan()
    scan.set_image_range(image_range)
    imageset.set_scan(scan)

  # Return the imageset
  return imageset
Ejemplo n.º 5
0
 def _make_stills(self, imageset):
   ''' Make a still imageset. '''
   from dxtbx.imageset import ImageSetFactory
   from dxtbx.serialize.filename import load_path
   filenames = [load_path(p) for p in imageset['images']]
   return ImageSetFactory.make_imageset(
     filenames, None, check_format=self._check_format)
  def tst_consistent(self):

    from dxtbx.imageset import ImageSetFactory
    from glob import glob
    from os.path import join
    from dxtbx.model import Scan

    # Create a sweep
    sweep_filenames = join(self.path, 'centroid_test_data', 'centroid*.cbf')
    sweep = ImageSetFactory.new(sorted(glob(sweep_filenames)))[0]

    # Create experiment with sweep and good scan
    e = Experiment(imageset=sweep, scan=sweep.get_scan())
    assert(e.is_consistent())

    # Create experiment with sweep and defective scan
    scan = sweep.get_scan()
    scan.set_image_range((1, 1))
    e = Experiment(imageset=sweep, scan=scan)
    #assert(not e.is_consistent()) # FIXME

    ## Create experiment with imageset and good scan
    #assert(e.is_consistent())

    ## Create experiment with imageset and non-still scan
    #assert(not e.is_consistent())

    ## Create experiment with imageset and scan with more than 1 image
    #assert(not e.is_consistent())

    ## Create experiment with imageset and defective scan
    #assert(not e.is_consistent())

    # Test passed
    print 'OK'
Ejemplo n.º 7
0
def basic_imageset_from_dict(d):
  ''' Construct an ImageSet class from the dictionary.'''
  from dxtbx.imageset import ImageSetFactory
  from dxtbx.serialize import beam, detector
  from dxtbx.serialize.filename import load_path

  # Get the filename list and create the imageset
  filenames = map(lambda p: load_path(p), map(str, d['filenames']))
  imageset = ImageSetFactory.new(filenames)[0]

  # Set some external lookups
  if 'mask' in d and d['mask'] is not None:
    with open(d['mask']) as infile:
      imageset.external_lookup.mask.filename = d['mask']
      imageset.external_lookup.mask.data = pickle.load(infile)
  if 'gain' in d and d['gain'] is not None:
    with open(d['gain']) as infile:
      imageset.external_lookup.gain.filename = d['gain']
      imageset.external_lookup.gain.data = pickle.load(infile)
  if 'pedestal' in d and d['pedestal'] is not None:
    with open(d['pedestal']) as infile:
      imageset.external_lookup.pedestal.filename = d['pedestal']
      imageset.external_lookup.pedestal.data = pickle.load(infile)

  # Get the existing models as dictionaries
  beam_dict = beam.to_dict(imageset.get_beam())
  detector_dict = detector.to_dict(imageset.get_detector())

  # Set models
  imageset.set_beam(beam.from_dict(d.get('beam'), beam_dict))
  imageset.set_detector(detector.from_dict(d.get('detector'), detector_dict))

  # Return the imageset
  return imageset
Ejemplo n.º 8
0
 def convert_stills_to_sweep(self, imageset):
   from dxtbx.model import Scan
   assert self.params.geometry.scan.oscillation is not None
   for i in range(len(imageset)):
     self.override_beam(
       imageset.get_beam(index=i),
       self.params.geometry.beam)
     self.override_detector(
       imageset.get_detector(index=i),
       self.params.geometry.detector)
     self.override_goniometer(
       imageset.get_goniometer(index=i),
       self.params.geometry.goniometer)
   beam = imageset.get_beam(index=0)
   detector = imageset.get_detector(index=0)
   goniometer = imageset.get_goniometer(index=0)
   for i in range(1, len(imageset)):
     assert beam.is_similar_to(
       imageset.get_beam(index=i),
       wavelength_tolerance            = self.params.input.tolerance.beam.wavelength,
       direction_tolerance             = self.params.input.tolerance.beam.direction,
       polarization_normal_tolerance   = self.params.input.tolerance.beam.polarization_normal,
       polarization_fraction_tolerance = self.params.input.tolerance.beam.polarization_fraction)
     assert detector.is_similar_to(
       imageset.get_detector(index=i),
       fast_axis_tolerance = self.params.input.tolerance.detector.fast_axis,
       slow_axis_tolerance = self.params.input.tolerance.detector.slow_axis,
       origin_tolerance    = self.params.input.tolerance.detector.origin)
     assert goniometer.is_similar_to(
       imageset.get_goniometer(index=i),
       rotation_axis_tolerance    = self.params.input.tolerance.goniometer.rotation_axis,
       fixed_rotation_tolerance   = self.params.input.tolerance.goniometer.fixed_rotation,
       setting_rotation_tolerance = self.params.input.tolerance.goniometer.setting_rotation)
   assert beam is not None
   assert detector is not None
   assert goniometer is not None
   if self.params.geometry.scan.image_range is not None:
     image_range = self.params.geometry.scan.image_range
   else:
     image_range = (1, len(imageset))
   oscillation = self.params.geometry.scan.oscillation
   scan = Scan(image_range=image_range, oscillation=oscillation)
   from dxtbx.sweep_filenames import template_regex
   from dxtbx.imageset import ImageSetFactory
   indices      = list(range(image_range[0], image_range[1]+1))
   template = template_regex(imageset.get_path(0))[0]
   if template is None:
     paths = [imageset.get_path(i) for i in range(len(imageset))]
     assert len(set(paths)) == 1
     template = paths[0]
   new_sweep = ImageSetFactory.make_sweep(
     template     = template,
     indices      = indices,
     format_class = imageset.reader().get_format_class(),
     beam         = beam,
     detector     = detector,
     goniometer   = goniometer,
     scan         = scan)
   return new_sweep
Ejemplo n.º 9
0
  def run(self):
    from dxtbx.imageset import ImageSetFactory, ImageSweep

    filenames = self.get_file_list()

    sweep = ImageSetFactory.new(filenames)

    assert(isinstance(sweep[0], ImageSweep) == True)

    print 'OK'
Ejemplo n.º 10
0
  def _create_multi_file_imageset(self, format_class, records,
                                  format_kwargs=None):
    ''' Create a multi file sweep or imageset. '''
    from dxtbx.imageset import ImageSetFactory
    from os.path import abspath

    # Make either an imageset or sweep
    if len(records) == 1 and records[0].template is not None:

      # Get the image range
      image_range = records[0].scan.get_image_range()
      image_range = (image_range[0], image_range[1]+1)

      # Create the sweep
      imageset = ImageSetFactory.make_sweep(
        abspath(records[0].template), range(*image_range),
        format_class,
        records[0].beam, records[0].detector,
        records[0].goniometer, records[0].scan,
        format_kwargs=format_kwargs)

    else:

      # Get the filenames
      filenames = []
      for r in records:
        assert(r.template is None)
        filenames.append(r.filename)

      # make an imageset
      imageset = ImageSetFactory.make_imageset(
        map(abspath, filenames),
        format_class,
        format_kwargs=format_kwargs)
      for i, r in enumerate(records):
        imageset.set_beam(r.beam, i)
        imageset.set_detector(r.detector, i)
        imageset.set_goniometer(r.goniometer, i)
        imageset.set_scan(r.scan, i)

    # Return the imageset
    return imageset
Ejemplo n.º 11
0
def run(file_names):
  if len(file_names) == 1 and file_names[0].endswith('json'):
    from dxtbx.serialize import load
    datablock = load.datablock(file_names[0])
    assert(len(datablock) == 1)
    sweep = datablock[0].extract_sweeps()[0]
  else:
    from dxtbx.imageset import ImageSetFactory
    sweep = ImageSetFactory.new(file_names)[0]
  xsx = xds.to_xds(sweep)
  xsx.XDS_INP()
Ejemplo n.º 12
0
def run(file_names):
  if len(file_names) == 1 and file_names[0].endswith('json'):
    from dxtbx.serialize import load
    datablock = load.datablock(file_names[0])
    assert(len(datablock) == 1)
    sweep = datablock[0].extract_sweeps()[0]
  else:
    from dxtbx.imageset import ImageSetFactory
    sweep = ImageSetFactory.new(file_names)[0]
  detector = sweep.get_detector()
  beam = sweep.get_beam()
  print detector.get_ray_intersection(beam.get_s0())[1]
Ejemplo n.º 13
0
 def _make_stills(self, imageset, format_kwargs=None):
   ''' Make a still imageset. '''
   from dxtbx.imageset import ImageSetFactory
   from dxtbx.serialize.filename import load_path
   filenames = [load_path(p) for p in imageset['images']]
   indices = None
   if "single_file_indices" in imageset:
     indices = imageset['single_file_indices']
     assert len(indices) == len(filenames)
   return ImageSetFactory.make_imageset(
     filenames, None, check_format=self._check_format,
     single_file_indices=indices, format_kwargs=format_kwargs)
Ejemplo n.º 14
0
  def set_frame_wedge(self, start, end, apply_offset = True):
    '''Set the allowed range of images for processing.'''

    # XXX RJG Better to pass slice of imageset here?

    if apply_offset:
      start = start - self._fp_offset
      end = end - self._fp_offset

    self._fp_wedge = start, end

    if self._fp_matching_images:
      images = []
      for j in self._fp_matching_images:
        if j < start or j > end:
          continue
        images.append(j)
      self._fp_matching_images = images

      ## reload the header information as well - this will be
      ## for the old wedge...# read the image header
      ## XXX this shouldn't be needed

      from dxtbx.imageset import ImageSetFactory
      imageset = ImageSetFactory.new(self.get_image_name(start))[0]

      # print this to the debug channel
      Debug.write('Latest header information for image %d:' % start)
      print >> Debug, imageset.get_detector()
      print >> Debug, imageset.get_scan()
      print >> Debug, imageset.get_beam()
      print >> Debug, imageset.get_goniometer()

      # populate wavelength, beam etc from this

      if self._fp_wavelength_prov is None or \
                      self._fp_wavelength_prov == 'header':
        self._fp_wavelength_prov = 'header'

      if self._fp_distance_prov is None or \
                      self._fp_distance_prov == 'header':
        self._fp_distance_prov = 'header'

      if self._fp_beam_prov is None or \
            self._fp_beam_prov == 'header':
        self._fp_beam_prov = 'header'

    return
Ejemplo n.º 15
0
  def _make_sweep(self, imageset, scan):
    ''' Make an image sweep. '''
    from dxtbx.sweep_filenames import template_image_range
    from dxtbx.imageset import ImageSetFactory
    from dxtbx.serialize.filename import load_path

    # Get the template format
    template = load_path(imageset['template'])

    # Get the number of images (if no scan is given we'll try
    # to find all the images matching the template
    if scan is None:
      i0, i1 = template_image_range(template)
    else:
      i0, i1 = scan.get_image_range()

    # Make a sweep from the input data
    return ImageSetFactory.make_sweep(template,
      list(range(i0, i1+1)), None, check_format=self._check_format)
Ejemplo n.º 16
0
def exercise_to_xds():
  if not libtbx.env.has_module("dials_regression"):
    print "Skipping exercise_to_xds(): dials_regression not present"
    return

  data_dir = libtbx.env.find_in_repositories(
    relative_path="dials_regression/centroid_test_data",
    test=os.path.isdir)
  template = os.path.join(data_dir, "centroid_00*.cbf")
  file_names = glob.glob(template)
  sweep = ImageSetFactory.new(file_names)[0]
  to_xds = xds.to_xds(sweep)
  s1 = StringIO()
  to_xds.XDS_INP(out=s1)
  s2 = StringIO()
  real_space_a = (-5.327642, -39.034747, -4.988286)
  real_space_b = (-35.253495, 7.596265, -22.127661)
  real_space_c = (-22.673623, -1.486119, 35.793463)
  to_xds.xparm_xds(real_space_a, real_space_b, real_space_c, space_group=1, out=s2)
  # run coordinate frame converter on xparm.xds as a sanity check
  f = open_tmp_file(suffix="XPARM.XDS", mode="wb")
  s2.seek(0)
  f.writelines(s2.readlines())
  f.close()
  from rstbx.cftbx import coordinate_frame_helpers
  converter = coordinate_frame_helpers.import_xds_xparm(f.name)
  scan = sweep.get_scan()
  detector = sweep.get_detector()
  goniometer = sweep.get_goniometer()
  beam = sweep.get_beam()
  assert approx_equal(real_space_a, converter.get_real_space_a())
  assert approx_equal(real_space_b, converter.get_real_space_b())
  assert approx_equal(real_space_c, converter.get_real_space_c())
  assert approx_equal(goniometer.get_rotation_axis(),
                      converter.get_rotation_axis())
  assert approx_equal(
    beam.get_direction(), converter.get_sample_to_source().elems)
  assert approx_equal(detector[0].get_fast_axis(), converter.get_detector_fast())
  assert approx_equal(detector[0].get_slow_axis(), converter.get_detector_slow())
  assert approx_equal(detector[0].get_origin(), converter.get_detector_origin())
Ejemplo n.º 17
0
  def _create_imageset(self, format_class, template, filenames):
    ''' Create a multi file sweep or imageset. '''
    from dxtbx.imageset import ImageSetFactory
    from dxtbx.sweep_filenames import template_string_number_index

    # Get the image range
    index = slice(*template_string_number_index(template))
    first = int(filenames[0][index])
    last = int(filenames[-1][index])

    # Check all images in range are present
    numbers = [int(f[index]) for f in filenames]
    assert(all([x+1==y for x, y in zip(numbers, numbers[1:])]))

    # Read the image
    fmt = format_class(filenames[0])

    # Get the meta data from the format
    b = fmt.get_beam()
    d = fmt.get_detector()
    g = fmt.get_goniometer()
    s = fmt.get_scan()

    # Update the image range
    s.set_image_range((first, last))

    # Get the image range
    image_range = s.get_image_range()
    image_range = (image_range[0], image_range[1]+1)

    # Create the sweep
    imageset = ImageSetFactory.make_sweep(
      template, range(*image_range),
      format_class,
      b, d, g, s)

    # Return the imageset
    return imageset
Ejemplo n.º 18
0
    def _make_sequence(
        self,
        imageset,
        beam=None,
        detector=None,
        goniometer=None,
        scan=None,
        format_kwargs=None,
    ):
        """Make an image sequence."""
        # Get the template format
        template = resolve_path(imageset["template"], directory=self._directory)

        # Get the number of images (if no scan is given we'll try
        # to find all the images matching the template
        if scan is None:
            i0, i1 = template_image_range(template)
        else:
            i0, i1 = scan.get_image_range()

        format_class = None
        if self._check_format is False:
            if "single_file_indices" in imageset:
                format_class = FormatMultiImage

        # Make a sequence from the input data
        return ImageSetFactory.make_sequence(
            template,
            list(range(i0, i1 + 1)),
            format_class=format_class,
            check_format=self._check_format,
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
            format_kwargs=format_kwargs,
        )
Ejemplo n.º 19
0
  def run(self, experiments, reflections):
    from dials.util import log
    self.logger.log_step_time("INTEGRATE")

    logfile = os.path.splitext(self.logger.rank_log_file_path)[0] + "_integrate.log"
    log.config(logfile=logfile)
    processor = integrate_only_processor(self.params)

    # Re-generate the image sets using their format classes so we can read the raw data
    # Integrate the experiments one at a time to not use up memory
    all_integrated = flex.reflection_table()
    current_imageset = None
    current_imageset_path = None
    for expt_id, expt in enumerate(experiments):
      assert len(expt.imageset.paths()) == 1 and len(expt.imageset) == 1
      self.logger.log("Starting integration experiment %d"%expt_id)
      refls = reflections.select(reflections['exp_id'] == expt.identifier)
      if expt.imageset.paths()[0] != current_imageset_path:
        current_imageset_path = expt.imageset.paths()[0]
        current_imageset = ImageSetFactory.make_imageset(expt.imageset.paths())
      idx = expt.imageset.indices()[0]
      expt.imageset = current_imageset[idx:idx+1]
      idents = refls.experiment_identifiers()
      del idents[expt_id]
      idents[0] = expt.identifier
      refls['id'] = flex.int(len(refls), 0)

      integrated = processor.integrate(experiments[expt_id:expt_id+1], refls)

      idents = integrated.experiment_identifiers()
      del idents[0]
      idents[expt_id] = expt.identifier
      integrated['id'] = flex.int(len(integrated), expt_id)
      all_integrated.extend(integrated)

    self.logger.log("Integration done, %d experiments, %d reflections"%(len(experiments), len(all_integrated)))
    return experiments, all_integrated
Ejemplo n.º 20
0
def basic_imageset_from_dict(d, directory=None):
    """Construct an ImageSet class from the dictionary."""
    # Get the filename list and create the imageset
    filenames = [
        resolve_path(str(p), directory=directory) for p in d["filenames"]
    ]
    imageset = ImageSetFactory.new(filenames)[0]

    # Set some external lookups
    if "mask" in d and d["mask"] is not None and d["mask"] != "":
        path = resolve_path(d["mask"], directory=directory)
        with open(path) as infile:
            imageset.external_lookup.mask.filename = path
            imageset.external_lookup.mask.data = ImageBool(pickle.load(infile))
    if "gain" in d and d["gain"] is not None and d["gain"] != "":
        path = resolve_path(d["gain"], directory=directory)
        with open(path) as infile:
            imageset.external_lookup.gain.filename = path
            imageset.external_lookup.gain.data = ImageDouble(
                pickle.load(infile))
    if "pedestal" in d and d["pedestal"] is not None and d["pedestal"] != "":
        path = resolve_path(d["pedestal"], directory=directory)
        with open(path) as infile:
            imageset.external_lookup.pedestal.filename = path
            imageset.external_lookup.pedestal.data = ImageDouble(
                pickle.load(infile))

    # Get the existing models as dictionaries
    beam_dict = imageset.get_beam(0).to_dict()
    detector_dict = imageset.get_detector(0).to_dict()

    # Set models
    imageset.set_beam(BeamFactory.from_dict(d.get("beam"), beam_dict))
    imageset.set_detector(
        DetectorFactory.from_dict(d.get("detector"), detector_dict))

    return imageset
Ejemplo n.º 21
0
    def run(self):
        from dxtbx.imageset import ImageSetFactory, ImageSweep
        from os.path import join

        filenames = self.get_file_list()

        sweep = ImageSetFactory.new(filenames)

        assert (isinstance(sweep[0], ImageSweep) == True)

        print 'OK'

        template = join(dials_regression, "centroid_test_data",
                        "centroid_####.cbf")
        image_range = (3, 6)

        sweep = ImageSetFactory.from_template(template, image_range)

        assert (isinstance(sweep[0], ImageSweep) == True)
        assert len(sweep[0]) == 4
        assert sweep[0].paths()[0].endswith("3.cbf")
        assert sweep[0].paths()[-1].endswith("6.cbf")

        print 'OK'

        imageset = ImageSetFactory.make_imageset(filenames)
        assert len(imageset) == 9

        print 'OK'

        imageset = ImageSetFactory.make_imageset(filenames, check_format=False)
        assert len(imageset) == 9

        print 'OK'

        sweep = ImageSetFactory.make_sweep(template, list(range(1, 9 + 1)))
        assert len(sweep) == 9

        print 'OK'

        sweep = ImageSetFactory.make_sweep(template, list(range(3, 6 + 1)))
        assert len(sweep) == 4

        print 'OK'
Ejemplo n.º 22
0
def test_pickle_imageset(centroid_files):
    from dxtbx.imageset import ImageSetFactory, ImageSweep

    sweep = ImageSetFactory.new(centroid_files)[0]

    # Read the 5th image
    image = sweep[4]

    # Pickle, then unpickle
    sweep2 = pickle.loads(pickle.dumps(sweep))

    assert sweep.get_template() == sweep2.get_template()
    assert sweep.get_array_range() == sweep2.get_array_range()
    assert sweep.get_beam() == sweep2.get_beam()
    assert sweep.get_goniometer() == sweep2.get_goniometer()
    assert sweep.get_scan() == sweep2.get_scan()
    assert sweep.paths() == sweep2.paths()
    assert sweep == sweep2

    # Check auxiliary methods after pickling
    sweep3 = sweep2[0:4]
    sweep4 = sweep3[0:2]
    sweep4.get_detectorbase(0)
    sweep4[0]
Ejemplo n.º 23
0
    def tst_consistent(self):

        from dxtbx.imageset import ImageSetFactory
        from glob import glob
        from os.path import join
        from dxtbx.model import Scan

        # Create a sweep
        sweep_filenames = join(self.path, 'centroid_test_data',
                               'centroid*.cbf')
        sweep = ImageSetFactory.new(sorted(glob(sweep_filenames)))[0]

        # Create experiment with sweep and good scan
        e = Experiment(imageset=sweep, scan=sweep.get_scan())
        assert (e.is_consistent())

        # Create experiment with sweep and defective scan
        scan = sweep.get_scan()
        scan.set_image_range((1, 1))
        e = Experiment(imageset=sweep, scan=scan)
        #assert(not e.is_consistent()) # FIXME

        ## Create experiment with imageset and good scan
        #assert(e.is_consistent())

        ## Create experiment with imageset and non-still scan
        #assert(not e.is_consistent())

        ## Create experiment with imageset and scan with more than 1 image
        #assert(not e.is_consistent())

        ## Create experiment with imageset and defective scan
        #assert(not e.is_consistent())

        # Test passed
        print 'OK'
Ejemplo n.º 24
0
  def tst_cspad_hierarchy(self):
    from dxtbx.serialize import dump, load
    from dxtbx.imageset import ImageSetFactory
    import libtbx.load_env
    import os
    from glob import glob
    try:
      path = libtbx.env.dist_path('dials_regression')
    except Exception:
      print "No dials_regression directory found"
      return

    # Get the imageset
    filename = os.path.join(path, "spotfinding_test_data", "idx*.cbf")
    imageset = ImageSetFactory.new(glob(filename))
    assert(len(imageset) == 1)
    imageset = imageset[0]

    # Dump and reload
    from uuid import uuid4
    filename = '%s.json' % uuid4().hex
    dump.imageset(imageset, filename)
    imageset2 = load.imageset(filename)

    # Check they're are the same
    assert(imageset2.get_beam() == imageset.get_beam())
    d1 = imageset.get_detector()
    d2 = imageset2.get_detector()
    assert(len(d1) == len(d2))
    for i, (p1, p2) in enumerate(zip(d1, d2)):
      assert(p1 == p2)
    assert(imageset2.get_detector() == imageset.get_detector())
    assert(imageset2 == imageset)

    # Test passed
    print 'OK'
Ejemplo n.º 25
0
  def _make_sweep(self, imageset, scan, format_kwargs=None):
    ''' Make an image sweep. '''
    from dxtbx.sweep_filenames import template_image_range
    from dxtbx.imageset import ImageSetFactory
    from dxtbx.serialize.filename import load_path

    # Get the template format
    template = load_path(imageset['template'])

    # Get the number of images (if no scan is given we'll try
    # to find all the images matching the template
    if scan is None:
      i0, i1 = template_image_range(template)
    else:
      i0, i1 = scan.get_image_range()

    # Make a sweep from the input data
    return ImageSetFactory.make_sweep(
      template,
      list(range(i0, i1+1)),
      None,
      check_format=self._check_format,
      scan=scan,
      format_kwargs=format_kwargs)
Ejemplo n.º 26
0
    def tst_cspad_hierarchy(self):
        from dxtbx.serialize import dump, load
        from dxtbx.imageset import ImageSetFactory
        import libtbx.load_env
        import os
        from glob import glob
        try:
            path = libtbx.env.dist_path('dials_regression')
        except Exception:
            print "No dials_regression directory found"
            return

        # Get the imageset
        filename = os.path.join(path, "spotfinding_test_data", "idx*.cbf")
        imageset = ImageSetFactory.new(glob(filename))
        assert (len(imageset) == 1)
        imageset = imageset[0]

        # Dump and reload
        from uuid import uuid4
        filename = '%s.json' % uuid4().hex
        dump.imageset(imageset, filename)
        imageset2 = load.imageset(filename)

        # Check they're are the same
        assert (imageset2.get_beam() == imageset.get_beam())
        d1 = imageset.get_detector()
        d2 = imageset2.get_detector()
        assert (len(d1) == len(d2))
        for i, (p1, p2) in enumerate(zip(d1, d2)):
            assert (p1 == p2)
        assert (imageset2.get_detector() == imageset.get_detector())
        assert (imageset2 == imageset)

        # Test passed
        print 'OK'
def do_import(filename):
    datablocks = DataBlockFactory.from_filenames([filename])
    if len(datablocks) == 0:
        try:
            datablocks = DataBlockFactory.from_json_file(filename)
        except ValueError:
            raise Abort("Could not load %s" % filename)

    if len(datablocks) == 0:
        raise Abort("Could not load %s" % filename)
    if len(datablocks) > 1:
        raise Abort("Got multiple datablocks from file %s" % filename)

    # Ensure the indexer and downstream applications treat this as set of stills
    reset_sets = []

    from dxtbx.imageset import ImageSetFactory
    for imageset in datablocks[0].extract_imagesets():
        image = ImageSetFactory.imageset_from_anyset(imageset)
        image.set_scan(None)
        image.set_goniometer(None)
        reset_sets.append(image)

    return DataBlockFactory.from_imageset(reset_sets)[0]
Ejemplo n.º 28
0
def imagesequence_from_dict(d, check_format=True, directory=None):
    """Construct and image sequence from the dictionary."""
    # Get the template (required)
    template = resolve_path(str(d["template"]), directory=directory)

    # If the scan isn't set, find all available files
    scan_dict = d.get("scan")
    if scan_dict is None:
        image_range = None
    else:
        image_range = scan_dict.get("image_range")

    # Set the models with the exisiting models as templates
    beam = BeamFactory.from_dict(d.get("beam"))
    goniometer = GoniometerFactory.from_dict(d.get("goniometer"))
    detector = DetectorFactory.from_dict(d.get("detector"))
    scan = ScanFactory.from_dict(d.get("scan"))

    # Construct the sequence
    try:
        sequence = ImageSetFactory.from_template(
            template,
            image_range,
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
            check_format=check_format,
        )[0]
    except Exception:
        indices = list(range(image_range[0], image_range[1] + 1))
        sequence = ImageSetFactory.make_sequence(
            template,
            indices,
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
            check_format=check_format,
        )

    # Set some external lookups
    if "mask" in d and d["mask"] is not None and d["mask"] != "":
        path = resolve_path(d["mask"], directory=directory)
        with open(path) as infile:
            sequence.external_lookup.mask.filename = path
            sequence.external_lookup.mask.data = ImageBool(pickle.load(infile))
    if "gain" in d and d["gain"] is not None and d["gain"] != "":
        path = resolve_path(d["gain"], directory=directory)
        with open(path) as infile:
            sequence.external_lookup.gain.filename = path
            sequence.external_lookup.gain.data = ImageDouble(
                pickle.load(infile))
    if "pedestal" in d and d["pedestal"] is not None and d["pedestal"] != "":
        path = resolve_path(d["pedestal"], directory=directory)
        with open(path) as infile:
            sequence.external_lookup.pedestal.filename = path
            sequence.external_lookup.pedestal.data = ImageDouble(
                pickle.load(infile))

    return sequence
def import_xds_as_still(xdsinp, xparm_in):
    #from dxtbx.serialize import xds
    from dxtbx.datablock import DataBlockFactory

    # Get the sweep from the XDS files
    #sweep = xds.to_imageset(xds_inp, xds_other)

    from iotbx.xds import xds_inp
    from dxtbx.imageset import ImageSetFactory
    import dxtbx

    # Read the input filename
    handle = xds_inp.reader()
    handle.read_file(xdsinp)

    # Get the template
    template = handle.name_template_of_data_frames[0]
    image_range = handle.data_range
    detector_name = handle.detector

    #assert image_range[0] == image_range[1]
    im_nr = int((image_range[1] - image_range[0] + 1) / 2)

    from yamtbx.dataproc.dataset import template_to_filenames

    # Create the imageset
    #imageset = ImageSetFactory.from_template(template, image_range=image_range, check_format=False)[0]
    imageset = ImageSetFactory.make_imageset(
        [os.path.realpath(template_to_filenames(template, im_nr, im_nr)[0])])

    models = dxtbx.load(xparm_in)
    detector = models.get_detector()
    if detector_name.strip() in ('PILATUS',
                                 'EIGER') or handle.silicon is not None:
        from dxtbx.model import ParallaxCorrectedPxMmStrategy
        from cctbx.eltbx import attenuation_coefficient
        if handle.silicon is None:
            table = attenuation_coefficient.get_table("Si")
            wavelength = models.get_beam().get_wavelength()
            mu = table.mu_at_angstrom(wavelength) / 10.0
        else:
            mu = handle.silicon
        t0 = handle.sensor_thickness
        for panel in detector:
            panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0))
            panel.set_trusted_range(
                (handle.minimum_valid_pixel_value, handle.overload))

    imageset.set_beam(models.get_beam())
    imageset.set_detector(detector)
    imageset.set_goniometer(None)
    imageset.set_scan(None)
    #imageset.set_goniometer(models.get_goniometer())
    # take the image range from XDS.INP
    #scan = models.get_scan()
    #scan.set_image_range(image_range)
    #imageset.set_scan(scan)

    from dxtbx.serialize import xds

    # Get the crystal from the XDS files
    crystal = xds.to_crystal(xparm_in)

    # Create the experiment list
    experiments = ExperimentListFactory.from_imageset_and_crystal(
        imageset, crystal)

    # Set the crystal in the experiment list
    assert (len(experiments) == 1)

    # Return the experiment list
    return experiments
Ejemplo n.º 30
0
from dxtbx.datablock import DataBlockFactory, DataBlockDumper
from dxtbx.imageset import ImageSetFactory, ImageSweep, ImageSetData
from dxtbx.model.goniometer import GoniometerFactory
from dxtbx.model.scan import ScanFactory
import glob, os, sys
"""
Modification of AB's prepare_sweep.py.
Usage: libtbx.python prepare_sweep.py img_dir start_img, end_img savefile
"""

root = sys.argv[1]
start, end = int(sys.argv[2]), int(sys.argv[3])

g = GoniometerFactory.single_axis()
s = ScanFactory.make_scan((start, end), 0, (0, 1), [0] * (end - start + 1))
sw = ImageSetFactory.from_template(template=os.path.join(
    root, "fft_frame_I_mf_####.cbf"),
                                   scan=s,
                                   goniometer=g,
                                   image_range=(start, end))
dump = DataBlockDumper(DataBlockFactory.from_imageset(sw))
dump.as_file(sys.argv[4])
Ejemplo n.º 31
0
def run(argv=None):
    if (argv is None):
        argv = sys.argv

    # XXX Could/should handle effective metrology the same way, except
    # it does not have a single scope.
    work_phil = phil.process_command_line(args=argv[1:],
                                          master_string=master_str + phil_str +
                                          additional_spotfinder_phil_defs)
    work_params = work_phil.work.extract()

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

    # show settings panel
    frame.OnShowSettings(None)
    frame.settings_frame.panel.center_ctrl.SetValue(work_params.beam_center)
    frame.settings_frame.panel.integ_ctrl.SetValue(
        work_params.show_integration_results)
    frame.settings_frame.panel.spots_ctrl.SetValue(
        work_params.show_spotfinder_results)
    frame.settings.show_effective_tiling = work_params.show_effective_tiling
    frame.settings_frame.panel.collect_values()

    if (work_params.effective_metrology is not None):
        from xfel.cftbx.detector.metrology import \
          master_phil, metrology_as_transformation_matrices

        stream = open(work_params.effective_metrology)
        metrology_phil = master_phil.fetch(sources=[phil.parse(stream.read())])
        stream.close()
        frame.metrology_matrices = metrology_as_transformation_matrices(
            metrology_phil.extract())

    # Update initial settings with values from the command line.  Needs
    # to be done before image is loaded (but after the frame is
    # instantiated).
    frame.params = work_params
    frame.init_pyslip()
    frame.pyslip.tiles.user_requests_antialiasing = work_params.anti_aliasing
    frame.pyslip.tiles.show_untrusted = frame.params.show_untrusted

    paths = work_phil.remaining_args
    if (len(paths) == 1 and os.path.basename(paths[0]) == "DISTL_pickle"):
        assert os.path.isfile(paths[0])
        frame.load_distl_output(paths[0])
    elif (len(paths) > 0):
        frame.CHOOSER_SIZE = 1500

        from dxtbx.imageset import ImageSetFactory
        from rstbx.slip_viewer.frame import chooser_wrapper
        sets = ImageSetFactory.new(paths)

        for imgset in sets:
            for idx in imgset.indices():
                frame.add_file_name_or_data(chooser_wrapper(imgset, idx))
        idx = sets[0].indices()[0]
        frame.load_image(chooser_wrapper(sets[0], idx))

    app.MainLoop()

    return 0
Ejemplo n.º 32
0
    def __call__(self, imageset):
        """
        Override the parameters
        """
        from copy import deepcopy

        from dxtbx.imageset import ImageSequence, ImageSetFactory
        from dxtbx.model import (
            BeamFactory,
            DetectorFactory,
            GoniometerFactory,
            ScanFactory,
        )

        if self.params.geometry.convert_sequences_to_stills:
            imageset = ImageSetFactory.imageset_from_anyset(imageset)
            for j in imageset.indices():
                imageset.set_scan(None, j)
                imageset.set_goniometer(None, j)
        if not isinstance(imageset, ImageSequence):
            if self.params.geometry.convert_stills_to_sequences:
                imageset = self.convert_stills_to_sequence(imageset)
        if isinstance(imageset, ImageSequence):
            beam = BeamFactory.from_phil(self.params.geometry, imageset.get_beam())
            detector = DetectorFactory.from_phil(
                self.params.geometry, imageset.get_detector(), beam
            )
            goniometer = GoniometerFactory.from_phil(
                self.params.geometry, imageset.get_goniometer()
            )
            scan = ScanFactory.from_phil(
                self.params.geometry, deepcopy(imageset.get_scan())
            )
            i0, i1 = scan.get_array_range()
            j0, j1 = imageset.get_scan().get_array_range()
            if i0 < j0 or i1 > j1:
                imageset = self.extrapolate_imageset(
                    imageset=imageset,
                    beam=beam,
                    detector=detector,
                    goniometer=goniometer,
                    scan=scan,
                )
            else:
                imageset.set_beam(beam)
                imageset.set_detector(detector)
                imageset.set_goniometer(goniometer)
                imageset.set_scan(scan)
        else:
            for i in range(len(imageset)):
                beam = BeamFactory.from_phil(self.params.geometry, imageset.get_beam(i))
                detector = DetectorFactory.from_phil(
                    self.params.geometry, imageset.get_detector(i), beam
                )
                goniometer = GoniometerFactory.from_phil(
                    self.params.geometry, imageset.get_goniometer(i)
                )
                scan = ScanFactory.from_phil(self.params.geometry, imageset.get_scan(i))
                imageset.set_beam(beam, i)
                imageset.set_detector(detector, i)
                imageset.set_goniometer(goniometer, i)
                imageset.set_scan(scan, i)
        return imageset
Ejemplo n.º 33
0
def factory(list_of_images):
    from dxtbx.imageset import ImageSetFactory

    sweeps = ImageSetFactory.new(list_of_images)
    assert len(sweeps) == 1
    return sweeps[0]
Ejemplo n.º 34
0
Archivo: xds.py Proyecto: hbrunie/dxtbx
def to_imageset(input_filename, extra_filename=None):
    """Get an image set from the xds input filename plus an extra filename

    Params:
        input_filename The XDS.INP file
        extra_filename A (G)XPARM.XDS, INTGRATE.HKL or XDS_ASCII.HKL file

    Returns:
        The imageset

    """
    from iotbx.xds import xds_inp
    from dxtbx.imageset import ImageSetFactory
    import dxtbx

    # Read the input filename
    handle = xds_inp.reader()
    handle.read_file(input_filename)

    # Get the template
    template = handle.name_template_of_data_frames[0].replace("?", "#")
    if template.endswith("h5"):
        template = template.replace("######", "master")
    image_range = handle.data_range
    detector_name = handle.detector

    if extra_filename is not None:
        # we can get all the extra dxtbx models from extra_filename
        check_format = False
    else:
        # we need the image files present to get the dxtbx models
        check_format = True

    # If an extra filename has been specified, try to load models
    if extra_filename:
        models = dxtbx.load(extra_filename)
        detector = models.get_detector()
        if detector_name.strip() in ("PILATUS",
                                     "EIGER") or handle.silicon is not None:
            from dxtbx.model import ParallaxCorrectedPxMmStrategy
            from cctbx.eltbx import attenuation_coefficient

            if handle.silicon is None:
                table = attenuation_coefficient.get_table("Si")
                wavelength = models.get_beam().get_wavelength()
                mu = table.mu_at_angstrom(wavelength) / 10.0
            else:
                mu = handle.silicon
            t0 = handle.sensor_thickness
            for panel in detector:
                panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0))
                panel.set_trusted_range(
                    (handle.minimum_valid_pixel_value, handle.overload))
        beam = models.get_beam()
        detector = models.get_detector()
        goniometer = models.get_goniometer()
        scan = models.get_scan()
        scan.set_image_range(image_range)
    else:
        beam = None
        detector = None
        goniometer = None
        scan = None

    # Create the imageset
    imageset = ImageSetFactory.from_template(
        template,
        image_range=image_range,
        check_format=check_format,
        beam=beam,
        detector=detector,
        goniometer=goniometer,
        scan=scan,
    )[0]

    # Return the imageset
    return imageset
Ejemplo n.º 35
0
  from optparse import OptionParser
  from dxtbx.serialize import dump
  from dxtbx.imageset import ImageSetFactory

  # Specify the command line options
  usage  = "usage: %prog [options] /path/to/image/files.ext"
  parser = OptionParser(usage)

  # Add a verbose option (False by default)
  parser.add_option('-o', '--output-file',
                    dest='output_file', type="string",
                    default="imageset.json",
                    help='Enter a destination filename for serialization')

  # Parse the arguments
  (options, args) = parser.parse_args()

  # Print help if no arguments specified, otherwise call spot prediction
  if len(args) == 0:
    print parser.print_help()

  else:
    imagesets = ImageSetFactory.new(args)
    if len(imagesets) == 0:
      print "Error: no imagesets to serialize."
    elif len(imagesets) > 1:
      print "Error: more than 1 imageset has been specified"
    else:
      dump.imageset(imagesets[0], options.output_file)
      print "Serialized imageset to {0}".format(options.output_file)
Ejemplo n.º 36
0
def test_to_xds(dials_regression, tmpdir):
    tmpdir.chdir()
    template = os.path.join(dials_regression, "centroid_test_data", "centroid_00*.cbf")
    file_names = glob.glob(template)
    sweep = ImageSetFactory.new(file_names)[0]
    to_xds = xds.to_xds(sweep)
    s1 = to_xds.XDS_INP()
    expected = """\
DETECTOR=PILATUS MINIMUM_VALID_PIXEL_VALUE=0 OVERLOAD=495976
SENSOR_THICKNESS= 0.320
!SENSOR_MATERIAL / THICKNESS Si 0.320
!SILICON= 3.960382
DIRECTION_OF_DETECTOR_X-AXIS= 1.00000 0.00000 0.00000
DIRECTION_OF_DETECTOR_Y-AXIS= 0.00000 1.00000 0.00000
NX=2463 NY=2527 QX=0.1720 QY=0.1720
DETECTOR_DISTANCE= 190.180000
ORGX= 1235.84 ORGY= 1279.58
ROTATION_AXIS= 1.00000 0.00000 0.00000
STARTING_ANGLE= 0.000
OSCILLATION_RANGE= 0.200
X-RAY_WAVELENGTH= 0.97950
INCIDENT_BEAM_DIRECTION= -0.000 -0.000 1.021
FRACTION_OF_POLARIZATION= 0.999
POLARIZATION_PLANE_NORMAL= 0.000 1.000 0.000
NAME_TEMPLATE_OF_DATA_FRAMES= %s
TRUSTED_REGION= 0.0 1.41
UNTRUSTED_RECTANGLE= 487 495 0 2528
UNTRUSTED_RECTANGLE= 981 989 0 2528
UNTRUSTED_RECTANGLE= 1475 1483 0 2528
UNTRUSTED_RECTANGLE= 1969 1977 0 2528
UNTRUSTED_RECTANGLE= 0 2464 195 213
UNTRUSTED_RECTANGLE= 0 2464 407 425
UNTRUSTED_RECTANGLE= 0 2464 619 637
UNTRUSTED_RECTANGLE= 0 2464 831 849
UNTRUSTED_RECTANGLE= 0 2464 1043 1061
UNTRUSTED_RECTANGLE= 0 2464 1255 1273
UNTRUSTED_RECTANGLE= 0 2464 1467 1485
UNTRUSTED_RECTANGLE= 0 2464 1679 1697
UNTRUSTED_RECTANGLE= 0 2464 1891 1909
UNTRUSTED_RECTANGLE= 0 2464 2103 2121
UNTRUSTED_RECTANGLE= 0 2464 2315 2333
DATA_RANGE= 1 9
JOB=XYCORR INIT COLSPOT IDXREF DEFPIX INTEGRATE CORRECT\
""" % os.path.join(
        dials_regression, "centroid_test_data", "centroid_????.cbf"
    )
    assert s1 == expected
    real_space_a = (-5.327642, -39.034747, -4.988286)
    real_space_b = (-35.253495, 7.596265, -22.127661)
    real_space_c = (-22.673623, -1.486119, 35.793463)
    s2 = to_xds.xparm_xds(real_space_a, real_space_b, real_space_c, space_group=1)
    # run coordinate frame converter on xparm.xds as a sanity check
    with open("xparm.xds", mode="wb") as fh:
        fh.write(s2.encode("ASCII"))
    from rstbx.cftbx import coordinate_frame_helpers

    converter = coordinate_frame_helpers.import_xds_xparm("xparm.xds")
    detector = sweep.get_detector()
    goniometer = sweep.get_goniometer()
    beam = sweep.get_beam()
    assert approx_equal(real_space_a, converter.get_real_space_a())
    assert approx_equal(real_space_b, converter.get_real_space_b())
    assert approx_equal(real_space_c, converter.get_real_space_c())
    assert approx_equal(goniometer.get_rotation_axis(), converter.get_rotation_axis())
    assert approx_equal(beam.get_direction(), converter.get_sample_to_source().elems)
    assert approx_equal(detector[0].get_fast_axis(), converter.get_detector_fast())
    assert approx_equal(detector[0].get_slow_axis(), converter.get_detector_slow())
    assert approx_equal(detector[0].get_origin(), converter.get_detector_origin())
Ejemplo n.º 37
0
  def _load_datablocks(self, obj, check_format=True):
    ''' Create the datablock from a dictionary. '''
    from libtbx.containers import OrderedDict
    from dxtbx.format.Registry import Registry
    from dxtbx.model import Beam, Detector, Scan
    from dxtbx.model import Detector
    from dxtbx.serialize.filename import load_path
    from dxtbx.imageset import ImageSetFactory, ImageGrid

    # If we have a list, extract for each dictionary in the list
    if isinstance(obj, list):
      return [self._load_datablocks(dd, check_format) for dd in obj]
    elif not isinstance(obj, dict):
      raise RuntimeError('unknown datablock dictionary type')
    assert(obj['__id__'] == 'DataBlock')

    # Get the list of models
    blist = obj.get('beam', [])
    dlist = obj.get('detector', [])
    glist = obj.get('goniometer', [])
    slist = obj.get('scan', [])

    def load_models(obj):
      try:
        beam = Beam.from_dict(blist[obj['beam']])
      except Exception:
        beam = None
      try:
        dobj = dlist[obj['detector']]
        detector = Detector.from_dict(dobj)
      except Exception:
        detector = None
      try:
        from dxtbx.serialize import goniometer
        gonio = goniometer.from_dict(glist[obj['goniometer']])
      except Exception:
        gonio = None
      try:
        scan = Scan.from_dict(slist[obj['scan']])
      except Exception:
        scan = None
      return beam, detector, gonio, scan

    # Loop through all the imagesets
    imagesets = []
    for imageset in obj['imageset']:
      ident = imageset['__id__']
      if "params" in imageset:
        format_kwargs = imageset['params']
      else:
        format_kwargs = {}
      if ident == 'ImageSweep':
        beam, detector, gonio, scan = load_models(imageset)
        if "template" in imageset:
          template = load_path(imageset['template'])
          i0, i1 = scan.get_image_range()
          iset = ImageSetFactory.make_sweep(
            template, range(i0, i1+1), None,
            beam, detector, gonio, scan, check_format,
            format_kwargs=format_kwargs)
          if 'mask' in imageset and imageset['mask'] is not None:
            imageset['mask'] = load_path(imageset['mask'])
            iset.external_lookup.mask.filename = imageset['mask']
            if check_format:
              with open(imageset['mask']) as infile:
                iset.external_lookup.mask.data = pickle.load(infile)
          if 'gain' in imageset and imageset['gain'] is not None:
            imageset['gain'] = load_path(imageset['gain'])
            iset.external_lookup.gain.filename = imageset['gain']
            if check_format:
              with open(imageset['gain']) as infile:
                iset.external_lookup.gain.data = pickle.load(infile)
          if 'pedestal' in imageset and imageset['pedestal'] is not None:
            imageset['pedestal'] = load_path(imageset['pedestal'])
            iset.external_lookup.pedestal.filename = imageset['pedestal']
            if check_format:
              with open(imageset['pedestal']) as infile:
                iset.external_lookup.pedestal.data = pickle.load(infile)
        elif "master" in imageset:
          template = load_path(imageset['master'])
          i0, i1 = scan.get_image_range()
          indices = imageset['images']
          assert min(indices) == i0-1 and max(indices) == i1-1
          iset = ImageSetFactory.make_sweep(
            template, range(i0, i1+1), None,
            beam, detector, gonio, scan, check_format,
            format_kwargs=format_kwargs)
          if 'mask' in imageset and imageset['mask'] is not None:
            imageset['mask'] = load_path(imageset['mask'])
            iset.external_lookup.mask.filename = imageset['mask']
            if check_format:
              with open(imageset['mask']) as infile:
                iset.external_lookup.mask.data = pickle.load(infile)
          if 'gain' in imageset and imageset['gain'] is not None:
            imageset['gain'] = load_path(imageset['gain'])
            iset.external_lookup.gain.filename = imageset['gain']
            if check_format:
              with open(imageset['gain']) as infile:
                iset.external_lookup.gain.data = pickle.load(infile)
          if 'pedestal' in imageset and imageset['pedestal'] is not None:
            imageset['pedestal'] = load_path(imageset['pedestal'])
            iset.external_lookup.pedestal.filename = imageset['pedestal']
            if check_format:
              with open(imageset['pedestal']) as infile:
                iset.external_lookup.pedestal.data = pickle.load(infile)
        imagesets.append(iset)
      elif ident == 'ImageSet' or ident == "ImageGrid":
        filenames = [image['filename'] for image in imageset['images']]
        indices = [image['image'] for image in imageset['images'] if 'image' in image]
        assert len(indices) == 0 or len(indices) == len(filenames)
        iset = ImageSetFactory.make_imageset(
          filenames, None, check_format, indices, format_kwargs=format_kwargs)
        if ident == "ImageGrid":
          grid_size = imageset['grid_size']
          iset = ImageGrid.from_imageset(iset, grid_size)
        for i, image in enumerate(imageset['images']):
          beam, detector, gonio, scan = load_models(image)
          iset.set_beam(beam, i)
          iset.set_detector(detector, i)
          iset.set_goniometer(gonio, i)
          iset.set_scan(scan, i)
        if 'mask' in imageset and imageset['mask'] is not None:
          imageset['mask'] = load_path(imageset['mask'])
          iset.external_lookup.mask.filename = imageset['mask']
          if check_format:
            with open(imageset['mask']) as infile:
              iset.external_lookup.mask.data = pickle.load(infile)
        if 'gain' in imageset and imageset['gain'] is not None:
          imageset['gain'] = load_path(imageset['gain'])
          iset.external_lookup.gain.filename = imageset['gain']
          if check_format:
            with open(imageset['gain']) as infile:
              iset.external_lookup.gain.data = pickle.load(infile)
        if 'pedestal' in imageset and imageset['pedestal'] is not None:
          imageset['pedestal'] = load_path(imageset['pedestal'])
          iset.external_lookup.pedestal.filename = imageset['pedestal']
          if check_format:
            with open(imageset['pedestal']) as infile:
              iset.external_lookup.pedestal.data = pickle.load(infile)
        imagesets.append(iset)
      else:
        raise RuntimeError('expected ImageSet/ImageSweep, got %s' % ident)

    # Return the datablock
    return DataBlock(imagesets)
Ejemplo n.º 38
0
def factory(list_of_images):
  from dxtbx.imageset import ImageSetFactory
  sweeps = ImageSetFactory.new(list_of_images)
  assert(len(sweeps) == 1)
  return sweeps[0]
Ejemplo n.º 39
0
      img = dxtbx.load(imgname)
      detector = img.get_detector()
      beam = img.get_beam()
      scan = img.get_scan()
      gonio = img.get_goniometer()

      print "s0 from ",imgname,": ",beam.get_s0()
      print "wavelength from ",imgname,": ",beam.get_wavelength()

      crystal = copy.deepcopy(experiments.crystals()[0])
      axis = gonio.get_rotation_axis()
      start_angle, delta_angle = scan.get_oscillation()
      crystal.rotate_around_origin(axis, start_angle + (delta_angle/2), deg=True)
      if (output_format == "json"):
        exp_list = ExperimentList()
        exp_list.append(Experiment(imageset=ImageSetFactory.make_imageset(list([imgname])),
                                   beam=beam,
                                   detector=detector,
                                   goniometer=gonio,
                                   scan=scan,
                                   crystal=crystal))
        if (add_background_images==True):
          if (len(bkglist) != 1):
            bkgname=bkglist[i]
            exp_list[0].imageset.external_lookup.pedestal.filename=os.path.abspath(bkgname)
        dump.experiment_list(exp_list,json_dir+"/experiments_for_lunus_{0:05d}.json".format(imnum))
      else:
        from scitbx import matrix
        A_matrix = matrix.sqr(crystal.get_A()).inverse()
        At = np.asarray(A_matrix.transpose()).reshape((3,3))
        print At
Ejemplo n.º 40
0
def combine(datablock_list, reflections_list, params):
  '''
  Combine the found spots.

  '''
  from dxtbx.datablock import BeamComparison
  from dxtbx.datablock import DetectorComparison
  from dxtbx.datablock import GoniometerComparison
  from dxtbx.datablock import DataBlock
  from dxtbx.imageset import ImageSetFactory
  from dials.algorithms.spot_finding import StrongSpotCombiner
  from logging import info
  from dials.array_family import flex
  assert len(datablock_list) == len(reflections_list)

  # Get a list of imagesets
  imageset_list = []
  for db in datablock_list:
    iset = db.extract_imagesets()
    assert len(iset) == 1
    imageset_list.append(iset[0])

  compare_beam = BeamComparison(
    wavelength_tolerance=params.input.tolerance.beam.wavelength,
    direction_tolerance=params.input.tolerance.beam.direction,
    polarization_normal_tolerance=params.input.tolerance.beam.polarization_normal,
    polarization_fraction_tolerance=params.input.tolerance.beam.polarization_fraction)
  compare_detector = DetectorComparison(
    fast_axis_tolerance=params.input.tolerance.detector.fast_axis,
    slow_axis_tolerance=params.input.tolerance.detector.slow_axis,
    origin_tolerance=params.input.tolerance.detector.origin)
  compare_goniometer = GoniometerComparison(
    rotation_axis_tolerance=params.input.tolerance.goniometer.rotation_axis,
    fixed_rotation_tolerance=params.input.tolerance.goniometer.fixed_rotation,
    setting_rotation_tolerance=params.input.tolerance.goniometer.setting_rotation)
  scan_tolerance = params.input.tolerance.scan.oscillation

  # The initial models
  format_class = imageset_list[0].reader().get_format_class()
  beam = imageset_list[0].get_beam()
  detector = imageset_list[0].get_detector()
  goniometer = imageset_list[0].get_goniometer()
  scan = imageset_list[0].get_scan()
  template = imageset_list[0].get_template()

  # Check all the models
  for imageset in imageset_list[1:]:
    b = imageset.get_beam()
    d = imageset.get_detector()
    g = imageset.get_goniometer()
    s = imageset.get_scan()
    if not imageset.reader().get_format_class() == format_class:
      raise RuntimeError('Format classes do not match')
    if not imageset.get_template() == template:
      raise RuntimeError('Templates do not match')
    if not compare_beam(beam, b):
      raise RuntimeError('Beam models are too dissimilar')
    if not compare_detector(detector, d):
      raise RuntimeError('Detector models are too dissimilar')
    if not compare_goniometer(goniometer, g):
      raise RuntimeError('Goniometer models are too dissimilar')
    try:
      scan.append(s, scan_tolerance=scan_tolerance)
    except Exception:
      raise RuntimeError('Scans do not match')

  # Get the image range
  image_range = scan.get_image_range()
  image_range = (image_range[0], image_range[1]+1)

  # Create the sweep
  imageset = ImageSetFactory.make_sweep(
    template, range(*image_range),
    format_class,
    beam, detector,
    goniometer, scan)

  # Combine spots
  combiner = StrongSpotCombiner()
  for index, rlist in enumerate(reflections_list, start=1):
    assert rlist['id'].all_eq(0)
    info("Combining %d reflections from reflection list %d" % (
      len(rlist),
      index))
    combiner.add(rlist['shoebox'])
  shoeboxes = combiner.shoeboxes()

  # Calculate the spot centroids and intensities
  info('Combined into %d reflections' % len(shoeboxes))
  centroid = shoeboxes.centroid_valid()
  info('Calculated {0} spot centroids'.format(len(shoeboxes)))
  intensity = shoeboxes.summed_intensity()
  info('Calculated {0} spot intensities'.format(len(shoeboxes)))

  # Construct the reflection table
  reflections = flex.reflection_table(
    flex.observation(
      shoeboxes.panels(),
      centroid,
      intensity),
    shoeboxes)
  reflections['id'] = flex.int(len(reflections), 0)
  reflections.set_flags(
    flex.size_t_range(len(reflections)),
    reflections.flags.strong)

  # Return the datablock and reflections
  return DataBlock([imageset]), reflections
Ejemplo n.º 41
0
      img = dxtbx.load(imgname)
#      detector = img.get_detector()
#      beam = img.get_beam()
      scan = img.get_scan()
      gonio = img.get_goniometer()

      print "s0 from ",imgname,": ",beam.get_s0()
      print "wavelength from ",imgname,": ",beam.get_wavelength()

      crystal = copy.deepcopy(experiments.crystals()[0])
      axis = gonio.get_rotation_axis()
      start_angle, delta_angle = scan.get_oscillation()
      crystal.rotate_around_origin(axis, start_angle + (delta_angle/2), deg=True)
      if (output_format == "json"):
        exp_list = ExperimentList()
        exp_list.append(Experiment(imageset=ImageSetFactory.make_imageset(list([imgname])),
                                   beam=beam,
                                   detector=detector,
                                   goniometer=gonio,
                                   scan=scan,
                                   crystal=crystal))
        if (add_background_images==True):
          if (len(bkglist) != 1):
            bkgname=bkglist[i]
          else:
            bkgname=bkglist[0]
          exp_list[0].imageset.external_lookup.pedestal.filename=os.path.abspath(bkgname)
        dump.experiment_list(exp_list,json_dir+"/experiments_for_lunus_{0:05d}.json".format(imnum))
      else:
        from scitbx import matrix
        A_matrix = matrix.sqr(crystal.get_A()).inverse()
Ejemplo n.º 42
0
def factory(list_of_images):
    sequences = ImageSetFactory.new(list_of_images)
    assert len(sequences) == 1
    return sequences[0]
Ejemplo n.º 43
0
def index(experiments, reflections, params):
    """
    Index the input experiments and reflections.

    Args:
        experiments: The experiments to index
        reflections (list): A list of reflection tables containing strong spots
        params: An instance of the indexing phil scope

    Returns:
        (tuple): tuple containing:
            experiments: The indexed experiment list
            reflections (dials.array_family.flex.reflection_table):
                The indexed reflections

    """
    if experiments.crystals()[0] is not None:
        known_crystal_models = experiments.crystals()
    else:
        known_crystal_models = None

    if len(reflections) == 0:
        raise ValueError("No reflection lists found in input")
    elif len(reflections) == 1:
        reflections[0]["imageset_id"] = reflections[0]["id"]
    elif len(reflections) > 1:
        assert len(reflections) == len(experiments)
        for i in range(len(reflections)):
            reflections[i]["imageset_id"] = flex.int(len(reflections[i]), i)
            if i > 0:
                reflections[0].extend(reflections[i])
    reflections = reflections[0]

    # If there are scan and goniometer objects present but the oscillation angle is zero
    # then expt.scan and expt.goniometer to None, as the behaviour of some downstream
    # algorithms depend on the presence/absence of these objects
    for expt in experiments:
        if (expt.goniometer is not None and expt.scan is not None
                and expt.scan.is_still()):
            expt.imageset = ImageSetFactory.imageset_from_anyset(expt.imageset)
            expt.goniometer = None
            expt.scan = None

    if params.indexing.image_range:
        reflections = slice_reflections(reflections,
                                        params.indexing.image_range)

    if len(experiments) == 1 or params.indexing.joint_indexing:
        indexed_experiments, indexed_reflections = _index_experiments(
            experiments,
            reflections,
            copy.deepcopy(params),
            known_crystal_models=known_crystal_models,
        )
    else:
        indexed_experiments = ExperimentList()
        indexed_reflections = flex.reflection_table()

        with concurrent.futures.ProcessPoolExecutor(
                max_workers=params.indexing.nproc) as pool:
            futures = []
            for i_expt, expt in enumerate(experiments):
                refl = reflections.select(reflections["imageset_id"] == i_expt)
                refl["imageset_id"] = flex.size_t(len(refl), 0)
                futures.append(
                    pool.submit(
                        _index_experiments,
                        ExperimentList([expt]),
                        refl,
                        copy.deepcopy(params),
                        known_crystal_models=known_crystal_models,
                    ))

            for future in concurrent.futures.as_completed(futures):
                try:
                    idx_expts, idx_refl = future.result()
                except Exception as e:
                    print(e)
                else:
                    if idx_expts is None:
                        continue
                    for j_expt, _ in enumerate(idx_expts):
                        sel = idx_refl["id"] == j_expt
                        idx_refl["id"].set_selected(
                            sel,
                            len(indexed_experiments) + j_expt)
                    idx_refl["imageset_id"] = flex.size_t(
                        len(idx_refl), i_expt)
                    indexed_reflections.extend(idx_refl)
                    indexed_experiments.extend(idx_expts)
    return indexed_experiments, indexed_reflections
Ejemplo n.º 44
0
def exercise_to_xds():
    if not libtbx.env.has_module("dials"):
        print "Skipping test: dials not present"
        return
    if not libtbx.env.has_module("dials_regression"):
        print "Skipping exercise_to_xds(): dials_regression not present"
        return

    data_dir = libtbx.env.find_in_repositories(
        relative_path="dials_regression/centroid_test_data",
        test=os.path.isdir)
    template = os.path.join(data_dir, "centroid_*.cbf")
    file_names = glob.glob(template)

    expected_output = """\
DETECTOR=PILATUS MINIMUM_VALID_PIXEL_VALUE=0 OVERLOAD=495976
SENSOR_THICKNESS= 0.320
DIRECTION_OF_DETECTOR_X-AXIS= 1.00000 0.00000 0.00000
DIRECTION_OF_DETECTOR_Y-AXIS= 0.00000 1.00000 0.00000
NX=2463 NY=2527 QX=0.1720 QY=0.1720
DETECTOR_DISTANCE= 190.180
ORGX= 1235.84 ORGY= 1279.58
ROTATION_AXIS= 1.00000 0.00000 0.00000
STARTING_ANGLE= 0.000
OSCILLATION_RANGE= 0.200
X-RAY_WAVELENGTH= 0.97950
INCIDENT_BEAM_DIRECTION= -0.000 -0.000 1.021
FRACTION_OF_POLARIZATION= 0.999
POLARIZATION_PLANE_NORMAL= 0.000 1.000 0.000
NAME_TEMPLATE_OF_DATA_FRAMES= %s
TRUSTED_REGION= 0.0 1.41
UNTRUSTED_RECTANGLE= 487 495 0 2528
UNTRUSTED_RECTANGLE= 981 989 0 2528
UNTRUSTED_RECTANGLE= 1475 1483 0 2528
UNTRUSTED_RECTANGLE= 1969 1977 0 2528
UNTRUSTED_RECTANGLE= 0 2464 195 213
UNTRUSTED_RECTANGLE= 0 2464 407 425
UNTRUSTED_RECTANGLE= 0 2464 619 637
UNTRUSTED_RECTANGLE= 0 2464 831 849
UNTRUSTED_RECTANGLE= 0 2464 1043 1061
UNTRUSTED_RECTANGLE= 0 2464 1255 1273
UNTRUSTED_RECTANGLE= 0 2464 1467 1485
UNTRUSTED_RECTANGLE= 0 2464 1679 1697
UNTRUSTED_RECTANGLE= 0 2464 1891 1909
UNTRUSTED_RECTANGLE= 0 2464 2103 2121
UNTRUSTED_RECTANGLE= 0 2464 2315 2333
DATA_RANGE= 1 9
JOB=XYCORR INIT COLSPOT IDXREF DEFPIX INTEGRATE CORRECT\
""" % (template.replace("*", "????"))

    cmd = " ".join(["dxtbx.to_xds"] + file_names)
    result = easy_run.fully_buffered(cmd)
    # allow extra lines to have been added (these may be comments)
    for record in expected_output.split('\n'):
        assert record.strip() in "\n".join(result.stdout_lines), record

    # now test reading from a json file
    sweep = ImageSetFactory.new(file_names)[0]
    f = open_tmp_file(suffix="sweep.json", mode="wb")
    dump.imageset(sweep, f)
    f.close()
    cmd = " ".join(["dxtbx.to_xds", f.name])
    print cmd
    result = easy_run.fully_buffered(cmd)

    # allow extra lines to have been added (these may be comments)
    for record in expected_output.split('\n'):
        assert record.strip() in "\n".join(result.stdout_lines), record
Ejemplo n.º 45
0
def imagesweep_from_dict(d, check_format=True, directory=None):
    '''Construct and image sweep from the dictionary.'''
    from dxtbx.imageset import ImageSetFactory
    from dxtbx.model import BeamFactory, DetectorFactory, GoniometerFactory, ScanFactory
    from dxtbx.serialize.filename import load_path

    # Get the template (required)
    template = load_path(str(d['template']), directory=directory)

    # If the scan isn't set, find all available files
    scan_dict = d.get('scan')
    if scan_dict is None:
        image_range = None
    else:
        image_range = scan_dict.get('image_range')

    # Set the models with the exisiting models as templates
    beam = BeamFactory.from_dict(d.get('beam'))
    goniometer = GoniometerFactory.from_dict(d.get('goniometer'))
    detector = DetectorFactory.from_dict(d.get('detector'))
    scan = ScanFactory.from_dict(d.get('scan'))

    # Construct the sweep
    try:
        sweep = ImageSetFactory.from_template(template,
                                              image_range,
                                              beam=beam,
                                              detector=detector,
                                              goniometer=goniometer,
                                              scan=scan,
                                              check_format=check_format)[0]
    except Exception:
        indices = range(image_range[0], image_range[1] + 1)
        sweep = ImageSetFactory.make_sweep(template,
                                           indices,
                                           beam=beam,
                                           detector=detector,
                                           goniometer=goniometer,
                                           scan=scan,
                                           check_format=check_format)

    # Set some external lookups
    if 'mask' in d and d['mask'] is not None and d['mask'] is not "":
        path = load_path(d['mask'], directory=directory)
        with open(path) as infile:
            sweep.external_lookup.mask.filename = path
            sweep.external_lookup.mask.data = ImageBool(pickle.load(infile))
    if 'gain' in d and d['gain'] is not None and d['gain'] is not "":
        path = load_path(d['gain'], directory=directory)
        with open(path) as infile:
            sweep.external_lookup.gain.filename = path
            sweep.external_lookup.gain.data = ImageDouble(pickle.load(infile))
    if 'pedestal' in d and d['pedestal'] is not None and d[
            'pedestal'] is not "":
        path = load_path(d['pedestal'], directory=directory)
        with open(path) as infile:
            sweep.external_lookup.pedestal.filename = path
            sweep.external_lookup.pedestal.data = ImageDouble(
                pickle.load(infile))

    # Return the sweep
    return sweep
Ejemplo n.º 46
0
def combine(datablock_list, reflections_list, params):
  '''
  Combine the found spots.

  '''
  from dxtbx.datablock import BeamComparison
  from dxtbx.datablock import DetectorComparison
  from dxtbx.datablock import GoniometerComparison
  from dxtbx.datablock import DataBlock
  from dxtbx.imageset import ImageSetFactory
  from dials.algorithms.spot_finding import StrongSpotCombiner
  from dials.array_family import flex
  assert len(datablock_list) == len(reflections_list)

  # Get a list of imagesets
  imageset_list = []
  for db in datablock_list:
    iset = db.extract_imagesets()
    assert len(iset) == 1
    imageset_list.append(iset[0])

  compare_beam = BeamComparison(
    wavelength_tolerance=params.input.tolerance.beam.wavelength,
    direction_tolerance=params.input.tolerance.beam.direction,
    polarization_normal_tolerance=params.input.tolerance.beam.polarization_normal,
    polarization_fraction_tolerance=params.input.tolerance.beam.polarization_fraction)
  compare_detector = DetectorComparison(
    fast_axis_tolerance=params.input.tolerance.detector.fast_axis,
    slow_axis_tolerance=params.input.tolerance.detector.slow_axis,
    origin_tolerance=params.input.tolerance.detector.origin)
  compare_goniometer = GoniometerComparison(
    rotation_axis_tolerance=params.input.tolerance.goniometer.rotation_axis,
    fixed_rotation_tolerance=params.input.tolerance.goniometer.fixed_rotation,
    setting_rotation_tolerance=params.input.tolerance.goniometer.setting_rotation)
  scan_tolerance = params.input.tolerance.scan.oscillation

  # The initial models
  format_class = imageset_list[0].get_format_class()
  beam = imageset_list[0].get_beam()
  detector = imageset_list[0].get_detector()
  goniometer = imageset_list[0].get_goniometer()
  scan = imageset_list[0].get_scan()
  template = imageset_list[0].get_template()

  # Check all the models
  for imageset in imageset_list[1:]:
    b = imageset.get_beam()
    d = imageset.get_detector()
    g = imageset.get_goniometer()
    s = imageset.get_scan()
    if not imageset.get_format_class() == format_class:
      raise RuntimeError('Format classes do not match')
    if not imageset.get_template() == template:
      raise RuntimeError('Templates do not match')
    if not compare_beam(beam, b):
      raise RuntimeError('Beam models are too dissimilar')
    if not compare_detector(detector, d):
      raise RuntimeError('Detector models are too dissimilar')
    if not compare_goniometer(goniometer, g):
      raise RuntimeError('Goniometer models are too dissimilar')
    try:
      scan.append(s, scan_tolerance=scan_tolerance)
    except Exception:
      raise RuntimeError('Scans do not match')

  # Get the image range
  image_range = scan.get_image_range()
  image_range = (image_range[0], image_range[1]+1)

  # Create the sweep
  imageset = ImageSetFactory.make_sweep(
    template, range(*image_range),
    format_class,
    beam, detector,
    goniometer, scan)

  # Combine spots
  combiner = StrongSpotCombiner()
  for index, rlist in enumerate(reflections_list, start=1):
    assert rlist['id'].all_eq(0)
    logger.info("Combining %d reflections from reflection list %d" % (
      len(rlist),
      index))
    combiner.add(rlist['shoebox'])
  shoeboxes = combiner.shoeboxes()

  # Calculate the spot centroids and intensities
  logger.info('Combined into %d reflections' % len(shoeboxes))
  centroid = shoeboxes.centroid_valid()
  logger.info('Calculated {0} spot centroids'.format(len(shoeboxes)))
  intensity = shoeboxes.summed_intensity()
  logger.info('Calculated {0} spot intensities'.format(len(shoeboxes)))

  # Construct the reflection table
  reflections = flex.reflection_table(
    flex.observation(
      shoeboxes.panels(),
      centroid,
      intensity),
    shoeboxes)
  reflections['id'] = flex.int(len(reflections), 0)
  reflections.set_flags(
    flex.size_t_range(len(reflections)),
    reflections.flags.strong)

  # Return the datablock and reflections
  return DataBlock([imageset]), reflections
Ejemplo n.º 47
0
def imagesweep_from_dict(d, check_format=True):
    '''Construct and image sweep from the dictionary.'''
    from dxtbx.imageset import ImageSetFactory
    from dxtbx.model import BeamFactory, DetectorFactory, GoniometerFactory, ScanFactory
    from dxtbx.serialize.filename import load_path

    # Get the template (required)
    template = load_path(str(d['template']))

    # If the scan isn't set, find all available files
    scan_dict = d.get('scan')
    if scan_dict is None:
        image_range = None
    else:
        image_range = scan_dict.get('image_range')

    # Construct the sweep
    try:
        sweep = ImageSetFactory.from_template(template,
                                              image_range,
                                              check_format=check_format)[0]

        # Get the existing models as dictionaries
        beam_dict = sweep.get_beam().to_dict()
        gonio_dict = sweep.get_goniometer().to_dict()
        detector_dict = sweep.get_detector().to_dict()
        scan_dict = sweep.get_scan().to_dict()
    except Exception:
        indices = range(image_range[0], image_range[1] + 1)
        sweep = ImageSetFactory.make_sweep(template,
                                           indices,
                                           check_format=False)
        beam_dict = None
        gonio_dict = None
        detector_dict = None
        scan_dict = None

    # Set some external lookups
    if 'mask' in d and d['mask'] is not None:
        with open(d['mask']) as infile:
            sweep.external_lookup.mask.filename = d['mask']
            sweep.external_lookup.mask.data = pickle.load(infile)
    if 'gain' in d and d['gain'] is not None:
        with open(d['gain']) as infile:
            sweep.external_lookup.gain.filename = d['gain']
            sweep.external_lookup.gain.data = pickle.load(infile)
    if 'pedestal' in d and d['pedestal'] is not None:
        with open(d['pedestal']) as infile:
            sweep.external_lookup.pedestal.filename = d['pedestal']
            sweep.external_lookup.pedestal.data = pickle.load(infile)

    # Set the models with the exisiting models as templates
    sweep.set_beam(BeamFactory.from_dict(d.get('beam'), beam_dict))
    sweep.set_goniometer(
        GoniometerFactory.from_dict(d.get('goniometer'), gonio_dict))
    sweep.set_detector(
        DetectorFactory.from_dict(d.get('detector'), detector_dict))
    sweep.set_scan(ScanFactory.from_dict(d.get('scan'), scan_dict))

    # Return the sweep
    return sweep
Ejemplo n.º 48
0
def run(argv=None):
  if (argv is None):
    argv = sys.argv

  # XXX Could/should handle effective metrology the same way, except
  # it does not have a single scope.
  work_phil = phil.process_command_line(
    args=argv[1:],
    master_string=master_str + phil_str + additional_spotfinder_phil_defs)
  work_params = work_phil.work.extract()

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

  # show settings panel
  frame.OnShowSettings(None)
  frame.settings_frame.panel.center_ctrl.SetValue(
    work_params.beam_center)
  frame.settings_frame.panel.integ_ctrl.SetValue(
    work_params.show_integration_results)
  frame.settings_frame.panel.spots_ctrl.SetValue(
    work_params.show_spotfinder_results)
  frame.settings.show_effective_tiling = work_params.show_effective_tiling
  frame.settings_frame.panel.collect_values()

  if (work_params.effective_metrology is not None):
    from xfel.cftbx.detector.metrology import \
      master_phil, metrology_as_transformation_matrices

    stream = open(work_params.effective_metrology)
    metrology_phil = master_phil.fetch(sources=[phil.parse(stream.read())])
    stream.close()
    frame.metrology_matrices = metrology_as_transformation_matrices(
      metrology_phil.extract())

  # Update initial settings with values from the command line.  Needs
  # to be done before image is loaded (but after the frame is
  # instantiated).
  frame.params = work_params
  frame.init_pyslip()
  frame.pyslip.tiles.user_requests_antialiasing = work_params.anti_aliasing
  frame.pyslip.tiles.show_untrusted = frame.params.show_untrusted

  paths = work_phil.remaining_args
  if (len(paths) == 1 and os.path.basename(paths[0]) == "DISTL_pickle"):
    assert os.path.isfile(paths[0])
    frame.load_distl_output(paths[0])
  elif (len(paths) > 0):
    frame.CHOOSER_SIZE = 1500

    from dxtbx.imageset import ImageSetFactory
    from rstbx.slip_viewer.frame import chooser_wrapper
    sets = ImageSetFactory.new(paths)

    for imgset in sets:
      for idx in imgset.indices():
        frame.add_file_name_or_data(chooser_wrapper(imgset, idx))
    idx = sets[0].indices()[0]
    frame.load_image(chooser_wrapper(sets[0],idx))

  app.MainLoop()

  return 0
Ejemplo n.º 49
0
  from glob import glob
  from dxtbx.imageset import ImageSetFactory

  # Check dials_regression is configured
  try:
    path = libtbx.env.dist_path('dials_regression')
  except KeyError, e:
    print 'FAIL: dials_regression not configured'
    raise

  # Find the filenames
  template = os.path.join(path, 'centroid_test_data', 'centroid_*.cbf')
  filenames = glob(template)

  # Create the sweep
  sweep = ImageSetFactory.new(filenames)
  assert(len(sweep) == 1)
  sweep = sweep[0]

  # Get the models
  beam = sweep.get_beam()
  detector = sweep.get_detector()
  gonio = sweep.get_goniometer()
  scan = sweep.get_scan()
  print beam
  print detector
  print gonio
  print scan

  print "sweep: ", sweep
  print "sweep indices: ", sweep.indices()
Ejemplo n.º 50
0
  def _load_datablocks(self, obj, check_format=True):
    ''' Create the datablock from a dictionary. '''
    from libtbx.containers import OrderedDict
    from dxtbx.format.Registry import Registry
    from dxtbx.model import Beam, Detector, Goniometer, Scan
    from dxtbx.model import HierarchicalDetector
    from dxtbx.serialize.filename import load_path
    from dxtbx.imageset import ImageSetFactory
    import pickle

    # If we have a list, extract for each dictionary in the list
    if isinstance(obj, list):
      return [self._load_datablocks(dd, check_format) for dd in obj]
    elif not isinstance(obj, dict):
      raise RuntimeError('unknown datablock dictionary type')
    assert(obj['__id__'] == 'DataBlock')

    # Get the list of models
    blist = obj.get('beam', [])
    dlist = obj.get('detector', [])
    glist = obj.get('goniometer', [])
    slist = obj.get('scan', [])

    def load_models(obj):
      try:
        beam = Beam.from_dict(blist[obj['beam']])
      except Exception:
        beam = None
      try:
        dobj = dlist[obj['detector']]
        if 'hierarchy' in dobj:
          detector = HierarchicalDetector.from_dict(dobj)
        else:
          detector = Detector.from_dict(dobj)
      except Exception:
        detector = None
      try:
        gonio = Goniometer.from_dict(glist[obj['goniometer']])
      except Exception:
        gonio = None
      try:
        scan = Scan.from_dict(slist[obj['scan']])
      except Exception:
        scan = None
      return beam, detector, gonio, scan

    # Loop through all the imagesets
    imagesets = []
    for imageset in obj['imageset']:
      ident = imageset['__id__']
      if ident == 'ImageSweep':
        beam, detector, gonio, scan = load_models(imageset)
        template = load_path(imageset['template'])
        i0, i1 = scan.get_image_range()
        iset = ImageSetFactory.make_sweep(
          template, range(i0, i1+1), None,
          beam, detector, gonio, scan, check_format)
        if 'mask' in imageset and imageset['mask'] is not None:
          imageset['mask'] = load_path(imageset['mask'])
          iset.external_lookup.mask.filename = imageset['mask']
          if check_format:
            with open(imageset['mask']) as infile:
              iset.external_lookup.mask.data = pickle.load(infile)
        if 'gain' in imageset and imageset['gain'] is not None:
          imageset['gain'] = load_path(imageset['gain'])
          iset.external_lookup.gain.filename = imageset['gain']
          if check_format:
            with open(imageset['gain']) as infile:
              iset.external_lookup.gain.data = pickle.load(infile)
        if 'pedestal' in imageset and imageset['pedestal'] is not None:
          imageset['pedestal'] = load_path(imageset['pedestal'])
          iset.external_lookup.pedestal.filename = imageset['pedestal']
          if check_format:
            with open(imageset['pedestal']) as infile:
              iset.external_lookup.pedestal.data = pickle.load(infile)
        imagesets.append(iset)
      elif ident == 'ImageSet':
        filenames = [image['filename'] for image in imageset['images']]
        iset = ImageSetFactory.make_imageset(
          filenames, None, check_format)
        for i, image in enumerate(imageset['images']):
          beam, detector, gonio, scan = load_models(image)
          iset.set_beam(beam, i)
          iset.set_detector(detector, i)
          if gonio:
            iset.set_goniometer(gonio, i)
          if scan:
            iset.set_scan(scan, i)
        if 'mask' in imageset and imageset['mask'] is not None:
          imageset['mask'] = load_path(imageset['mask'])
          iset.external_lookup.mask.filename = imageset['mask']
          if check_format:
            with open(imageset['mask']) as infile:
              iset.external_lookup.mask.data = pickle.load(infile)
        if 'gain' in imageset and imageset['gain'] is not None:
          imageset['gain'] = load_path(imageset['gain'])
          iset.external_lookup.gain.filename = imageset['gain']
          if check_format:
            with open(imageset['gain']) as infile:
              iset.external_lookup.gain.data = pickle.load(infile)
        if 'pedestal' in imageset and imageset['pedestal'] is not None:
          imageset['pedestal'] = load_path(imageset['pedestal'])
          iset.external_lookup.pedestal.filename = imageset['pedestal']
          if check_format:
            with open(imageset['pedestal']) as infile:
              iset.external_lookup.pedestal.data = pickle.load(infile)
        imagesets.append(iset)
      else:
        raise RuntimeError('expected ImageSet/ImageSweep, got %s' % ident)

    # Return the datablock
    return DataBlock(imagesets)
Ejemplo n.º 51
0
    def convert_stills_to_sweep(self, imageset):
        from dxtbx.model import Scan

        assert self.params.geometry.scan.oscillation is not None
        beam = imageset.get_beam(index=0)
        detector = imageset.get_detector(index=0)
        goniometer = imageset.get_goniometer(index=0)
        for i in range(1, len(imageset)):
            b_i = imageset.get_beam(i)
            d_i = imageset.get_detector(i)
            g_i = imageset.get_goniometer(i)
            assert (beam is None and b_i is None) or beam.is_similar_to(
                imageset.get_beam(index=i),
                wavelength_tolerance=self.params.input.tolerance.beam.
                wavelength,
                direction_tolerance=self.params.input.tolerance.beam.direction,
                polarization_normal_tolerance=self.params.input.tolerance.beam.
                polarization_normal,
                polarization_fraction_tolerance=self.params.input.tolerance.
                beam.polarization_fraction,
            )
            assert (detector is None
                    and d_i is None) or detector.is_similar_to(
                        imageset.get_detector(index=i),
                        fast_axis_tolerance=self.params.input.tolerance.
                        detector.fast_axis,
                        slow_axis_tolerance=self.params.input.tolerance.
                        detector.slow_axis,
                        origin_tolerance=self.params.input.tolerance.detector.
                        origin,
                    )
            assert (goniometer is None
                    and g_i is None) or goniometer.is_similar_to(
                        imageset.get_goniometer(index=i),
                        rotation_axis_tolerance=self.params.input.tolerance.
                        goniometer.rotation_axis,
                        fixed_rotation_tolerance=self.params.input.tolerance.
                        goniometer.fixed_rotation,
                        setting_rotation_tolerance=self.params.input.tolerance.
                        goniometer.setting_rotation,
                    )
        oscillation = self.params.geometry.scan.oscillation
        from dxtbx.sweep_filenames import template_regex_from_list
        from dxtbx.imageset import ImageSetFactory

        template, indices = template_regex_from_list(imageset.paths())
        image_range = (min(indices), max(indices))
        assert (image_range[1] + 1 - image_range[0]) == len(indices)
        scan = Scan(image_range=image_range, oscillation=oscillation)
        if template is None:
            paths = [imageset.get_path(i) for i in range(len(imageset))]
            assert len(set(paths)) == 1
            template = paths[0]
        new_sweep = ImageSetFactory.make_sweep(
            template=template,
            indices=indices,
            format_class=imageset.reader().get_format_class(),
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
        )
        return new_sweep
Ejemplo n.º 52
0
def exercise_to_xds():
  if not libtbx.env.has_module("dials_regression"):
    print "Skipping exercise_to_xds(): dials_regression not present"
    return

  data_dir = libtbx.env.find_in_repositories(
    relative_path="dials_regression/centroid_test_data",
    test=os.path.isdir)
  template = os.path.join(data_dir, "centroid_*.cbf")
  file_names = glob.glob(template)

  expected_output = """\
DETECTOR=PILATUS MINIMUM_VALID_PIXEL_VALUE=0 OVERLOAD=495976
SENSOR_THICKNESS= 0.320
DIRECTION_OF_DETECTOR_X-AXIS= 1.00000 0.00000 0.00000
DIRECTION_OF_DETECTOR_Y-AXIS= 0.00000 1.00000 0.00000
NX=2463 NY=2527 QX=0.1720 QY=0.1720
DETECTOR_DISTANCE= 190.180
ORGX= 1235.84 ORGY= 1279.58
ROTATION_AXIS= 1.00000 0.00000 0.00000
STARTING_ANGLE= 0.000
OSCILLATION_RANGE= 0.200
X-RAY_WAVELENGTH= 0.97950
INCIDENT_BEAM_DIRECTION= -0.000 -0.000 1.021
FRACTION_OF_POLARIZATION= 0.999
POLARIZATION_PLANE_NORMAL= 0.000 1.000 0.000
NAME_TEMPLATE_OF_DATA_FRAMES= %s
TRUSTED_REGION= 0.0 1.41
UNTRUSTED_RECTANGLE= 487 495 0 2528
UNTRUSTED_RECTANGLE= 981 989 0 2528
UNTRUSTED_RECTANGLE= 1475 1483 0 2528
UNTRUSTED_RECTANGLE= 1969 1977 0 2528
UNTRUSTED_RECTANGLE= 0 2464 195 213
UNTRUSTED_RECTANGLE= 0 2464 407 425
UNTRUSTED_RECTANGLE= 0 2464 619 637
UNTRUSTED_RECTANGLE= 0 2464 831 849
UNTRUSTED_RECTANGLE= 0 2464 1043 1061
UNTRUSTED_RECTANGLE= 0 2464 1255 1273
UNTRUSTED_RECTANGLE= 0 2464 1467 1485
UNTRUSTED_RECTANGLE= 0 2464 1679 1697
UNTRUSTED_RECTANGLE= 0 2464 1891 1909
UNTRUSTED_RECTANGLE= 0 2464 2103 2121
UNTRUSTED_RECTANGLE= 0 2464 2315 2333
DATA_RANGE= 1 9
JOB=XYCORR INIT COLSPOT IDXREF DEFPIX INTEGRATE CORRECT\
""" %(template.replace("*", "????"))

  cmd = " ".join(["dxtbx.to_xds"] + file_names)
  result = easy_run.fully_buffered(cmd)
  # allow extra lines to have been added (these may be comments)
  for record in expected_output.split('\n'):
    assert record.strip() in "\n".join(result.stdout_lines), record

  # now test reading from a json file
  sweep = ImageSetFactory.new(file_names)[0]
  f = open_tmp_file(suffix="sweep.json", mode="wb")
  dump.imageset(sweep, f)
  f.close()
  cmd = " ".join(["dxtbx.to_xds", f.name])
  result = easy_run.fully_buffered(cmd)

  # allow extra lines to have been added (these may be comments)
  for record in expected_output.split('\n'):
    assert record.strip() in "\n".join(result.stdout_lines), record
Ejemplo n.º 53
0
# Print out the contents of the dxtbx understanding of a bunch of images to
# an example XDS.INP file. This should illustrate the usage of the dxtbx
# classes.

import sys
from dxtbx.serialize import xds

def run(file_names):
  if len(file_names) == 1 and file_names[0].endswith('json'):
    from dxtbx.serialize import load
    try:
      datablock = load.datablock(file_names[0])
      assert len(datablock) == 1
      sweep = datablock[0].extract_sweeps()[0]
    except ValueError, e:
      if str(e) == '"__id__" does not equal "imageset"':
        experiments = load.experiment_list(file_names[0])
        assert len(experiments) == 1
        sweep = experiments[0].imageset
      else:
        raise
  else:
    from dxtbx.imageset import ImageSetFactory
    sweep = ImageSetFactory.new(file_names)[0]
  xsx = xds.to_xds(sweep)
  xsx.XDS_INP()


if __name__ == '__main__':
  run(sys.argv[1:])
Ejemplo n.º 54
0
def to_imageset(input_filename, extra_filename=None):
    '''Get an image set from the xds input filename plus an extra filename

  Params:
      input_filename The XDS.INP file
      extra_filename A (G)XPARM.XDS, INTGRATE.HKL or XDS_ASCII.HKL file

  Returns:
      The imageset

  '''
    from iotbx.xds import xds_inp
    from dxtbx.imageset import ImageSetFactory
    import dxtbx

    # Read the input filename
    handle = xds_inp.reader()
    handle.read_file(input_filename)

    # Get the template
    template = handle.name_template_of_data_frames[0].replace('?', '#')
    image_range = handle.data_range
    detector_name = handle.detector

    if extra_filename is not None:
        # we can get all the extra dxtbx models from extra_filename
        check_format = False
    else:
        # we need the image files present to get the dxtbx models
        check_format = True

    # Create the imageset
    imageset = ImageSetFactory.from_template(template,
                                             image_range=image_range,
                                             check_format=False)[0]

    # If an extra filename has been specified, try to load models
    if extra_filename:
        models = dxtbx.load(extra_filename)
        detector = models.get_detector()
        if detector_name.strip() == 'PILATUS':
            from dxtbx.model import ParallaxCorrectedPxMmStrategy
            from cctbx.eltbx import attenuation_coefficient
            table = attenuation_coefficient.get_table("Si")
            wavelength = models.get_beam().get_wavelength()
            mu = table.mu_at_angstrom(wavelength) / 10.0
            t0 = handle.sensor_thickness
            for panel in detector:
                panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0))
                panel.set_trusted_range(
                    (handle.minimum_valid_pixel_value, handle.overload))
        imageset.set_beam(models.get_beam())
        imageset.set_detector(detector)
        imageset.set_goniometer(models.get_goniometer())
        # take the image range from XDS.INP
        scan = models.get_scan()
        scan.set_image_range(image_range)
        imageset.set_scan(scan)

    # Return the imageset
    return imageset