Example #1
0
def work_helper_image_files():
  '''Test the static methods in scan_helper_image_files.'''

  helper = scan_helper_image_files()

  import dxtbx
  directory = os.path.join(os.path.split(dxtbx.__file__)[0], 'tests')

  template = 'image_###.dat'

  assert(len(scan_helper_image_files.template_directory_to_indices(
      template, directory)) == 20)

  assert(scan_helper_image_files.template_directory_index_to_image(
      template, directory, 1) == os.path.join(directory, 'image_001.dat'))

  assert(scan_helper_image_files.template_index_to_image(
      template, 1) == 'image_001.dat')

  assert(scan_helper_image_files.image_to_template_directory(
      os.path.join(directory, 'image_001.dat')) == (template, directory))

  assert(scan_helper_image_files.image_to_index('image_001.dat') == 1)

  assert(scan_helper_image_files.image_to_template(
      'image_001.dat') == 'image_###.dat')

  assert(scan_helper_image_files.image_to_index("image_6.8kev_1_001.cbf") == 1)

  return
Example #2
0
def test_helper_image_files():
    """Test the static methods in scan_helper_image_files."""

    helper = scan_helper_image_files()

    import dxtbx

    directory = os.path.join(os.path.split(dxtbx.__file__)[0], "tests")

    template = "image_###.dat"

    assert (len(
        scan_helper_image_files.template_directory_to_indices(
            template, directory)) == 20)

    assert scan_helper_image_files.template_directory_index_to_image(
        template, directory, 1) == os.path.join(directory, "image_001.dat")

    assert (scan_helper_image_files.template_index_to_image(
        template, 1) == "image_001.dat")

    assert scan_helper_image_files.image_to_template_directory(
        os.path.join(directory, "image_001.dat")) == (template, directory)

    assert scan_helper_image_files.image_to_index("image_001.dat") == 1

    assert scan_helper_image_files.image_to_template(
        "image_001.dat") == "image_###.dat"

    assert scan_helper_image_files.image_to_index(
        "image_6.8kev_1_001.cbf") == 1
Example #3
0
def test_helper_image_files(image_test_dir):
    """Test the static methods in scan_helper_image_files."""
    assert scan_helper_image_files()

    template = "image_###.dat"

    assert (
        len(
            scan_helper_image_files.template_directory_to_indices(
                template, image_test_dir
            )
        )
        == 20
    )

    assert scan_helper_image_files.template_directory_index_to_image(
        template, image_test_dir, 1
    ) == os.path.join(image_test_dir, "image_001.dat")

    assert (
        scan_helper_image_files.template_index_to_image(template, 1) == "image_001.dat"
    )

    assert scan_helper_image_files.image_to_template_directory(
        os.path.join(image_test_dir, "image_001.dat")
    ) == (template, image_test_dir)

    assert scan_helper_image_files.image_to_index("image_001.dat") == 1

    assert scan_helper_image_files.image_to_template("image_001.dat") == "image_###.dat"

    assert scan_helper_image_files.image_to_index("image_6.8kev_1_001.cbf") == 1
Example #4
0
def work_helper_image_files():
  '''Test the static methods in scan_helper_image_files.'''

  helper = scan_helper_image_files()

  import dxtbx
  directory = os.path.join(os.path.split(dxtbx.__file__)[0], 'tests')

  template = 'image_###.dat'

  assert(len(scan_helper_image_files.template_directory_to_indices(
      template, directory)) == 20)

  assert(scan_helper_image_files.template_directory_index_to_image(
      template, directory, 1) == os.path.join(directory, 'image_001.dat'))

  assert(scan_helper_image_files.template_index_to_image(
      template, 1) == 'image_001.dat')

  assert(scan_helper_image_files.image_to_template_directory(
      os.path.join(directory, 'image_001.dat')) == (template, directory))

  assert(scan_helper_image_files.image_to_index('image_001.dat') == 1)

  assert(scan_helper_image_files.image_to_template(
      'image_001.dat') == 'image_###.dat')

  assert(scan_helper_image_files.image_to_index("image_6.8kev_1_001.cbf") == 1)

  return
Example #5
0
    def imgCIF_H(cif_file, cbf_handle):
        """Initialize a scan model from an imgCIF file handle, where it is
        assumed that the file has already been read."""

        exposure = cbf_handle.get_integration_time()
        timestamp = cbf_handle.get_timestamp()[0]

        gonio = cbf_handle.construct_goniometer()
        try:
            angles = tuple(gonio.get_rotation_range())
        except Exception as e:
            if str(e).strip() == "CBFlib Error(s): CBF_NOTFOUND":
                # probaby a still shot -> no scan object
                return None
            raise

        # xia2-56 handle gracefully reverse turning goniometers - this assumes the
        # rotation axis is correctly inverted in the goniometer factory
        if angles[1] < 0:
            angles = -angles[0], -angles[1]

        index = scan_helper_image_files.image_to_index(cif_file)

        gonio.__swig_destroy__(gonio)

        return ScanFactory.make_scan((index, index), exposure, angles,
                                     {index: timestamp})
Example #6
0
 def single_file(filename, exposure_times, osc_start, osc_width, epoch):
     """Construct an scan instance for a single image."""
     index = scan_helper_image_files.image_to_index(
         os.path.split(filename)[-1])
     if epoch is None:
         epoch = 0.0
     return ScanFactory.make_scan((index, index), exposure_times,
                                  (osc_start, osc_width), {index: epoch})
Example #7
0
    def single_file(filename, exposure_times, osc_start, osc_width, epoch):
        """Construct an scan instance for a single image."""
        index = scan_helper_image_files.image_to_index(
            os.path.split(filename)[-1])
        if epoch is None:
            epoch = 0.0

        # if the oscillation width is negative at this stage it is almost
        # certainly an artefact of the omega end being 0 when the omega start
        # angle was ~ 360 so it would be ~ -360 - see dxtbx#378
        if osc_width < -180:
            osc_width += 360

        return ScanFactory.make_scan((index, index), exposure_times,
                                     (osc_start, osc_width), {index: epoch})
Example #8
0
class ScanFactory:
  '''A factory for scan instances, to help with constructing the classes
  in a set of common circumstances.'''

  @staticmethod
  def from_phil(params, reference=None):
    '''
    Generate a scan model from phil parameters

    '''
    if reference is None:
      if params.scan.image_range is None and params.scan.oscillation is None:
        return None
      if params.scan.image_range is None:
        raise RuntimeError('No image range set')
      if params.scan.oscillation is None:
        raise RuntimeError('No oscillation set')
      scan = Scan(
        params.scan.image_range,
        params.scan.oscillation)
    else:
      scan = reference

      if params.scan.image_range is not None:
        most_recent_image_index = scan.get_image_range()[1] - scan.get_image_range()[0]
        scan.set_oscillation(
          scan.get_image_oscillation(
            params.scan.image_range[0]))
        scan.set_image_range(params.scan.image_range)
        if params.scan.extrapolate_scan and \
            (params.scan.image_range[1] - params.scan.image_range[0]) > most_recent_image_index:
          exposure_times = scan.get_exposure_times()
          epochs = scan.get_epochs()
          exposure_time = exposure_times[most_recent_image_index]
          epoch_correction = epochs[most_recent_image_index]
          for i in range(most_recent_image_index + 1, \
              params.scan.image_range[1] - params.scan.image_range[0] + 1):
            exposure_times[i] = exposure_time
            epoch_correction += exposure_time
            epochs[i] = epoch_correction
          scan.set_epochs(epochs)
          scan.set_exposure_times(exposure_times)
      if params.scan.oscillation is not None:
        scan.set_oscillation(params.scan.oscillation)

    # Return the model
    return scan

  @staticmethod
  def from_dict(d, t=None):
    ''' Convert the dictionary to a scan model

    Params:
        d The dictionary of parameters
        t The template dictionary to use

    Returns:
        The scan model

    '''
    from dxtbx.model import Scan
    from scitbx.array_family import flex # import dependency

    # If None, return None
    if d == None:
      if t == None: return None
      else: return from_dict(t, None)
    elif t != None:
      d = dict(t.items() + d.items())
    if not isinstance(d['exposure_time'], list):
      d['exposure_time'] = [d['exposure_time']]

    # Create the model from the dictionary
    return Scan.from_dict(d)

  @staticmethod
  def make_scan(image_range, exposure_times, oscillation, epochs, deg=True):
    from scitbx.array_family import flex

    if not isinstance(exposure_times, list):
      num_images = image_range[1] - image_range[0] + 1
      exposure_times = [exposure_times for i in range(num_images)]
    else:
      num_images = image_range[1] - image_range[0] + 1
      num_exp = len(exposure_times)
      if num_exp != num_images:
        if (num_exp == 0):
          exposure_times = [0 for i in range(num_images)]
        else:
          exposure_times = exposure_times.extend(
            [exposure_times[-1] for i in range(num_images - num_exp)])

    epoch_list = [epochs[j] for j in sorted(epochs)]

    return Scan(
        tuple(map(int, image_range)),
        tuple(map(float, oscillation)),
        flex.double(list(map(float, exposure_times))),
        flex.double(list(map(float, epoch_list))),
        deg)

  @staticmethod
  def single(filename, format, exposure_times, osc_start, osc_width, epoch):
    '''Construct an scan instance for a single image.'''

    import os
    index = scan_helper_image_files.image_to_index(os.path.split(filename)[-1])
    if epoch is None:
      epoch = 0.0
    return ScanFactory.make_scan(
                (index, index), exposure_times, (osc_start, osc_width),
                {index:epoch})

  @staticmethod
  def imgCIF(cif_file):
    '''Initialize a scan model from an imgCIF file.'''

    cbf_handle = pycbf.cbf_handle_struct()
    cbf_handle.read_file(cif_file, pycbf.MSG_DIGEST)

    return ScanFactory.imgCIF_H(cif_file, cbf_handle)

  @staticmethod
  def imgCIF_H(cif_file, cbf_handle):
    '''Initialize a scan model from an imgCIF file handle, where it is
    assumed that the file has already been read.'''

    exposure = cbf_handle.get_integration_time()
    timestamp = cbf_handle.get_timestamp()[0]

    gonio = cbf_handle.construct_goniometer()
    try:
      angles = tuple(gonio.get_rotation_range())
    except Exception, e:
      if str(e).strip() == 'CBFlib Error(s): CBF_NOTFOUND':
        # probaby a still shot -> no scan object
        return None
      raise

    # xia2-56 handle gracefully reverse turning goniometers - this assumes the
    # rotation axis is correctly inverted in the goniometer factory
    if angles[1] < 0:
      angles = -angles[0], -angles[1]

    index = scan_helper_image_files.image_to_index(cif_file)

    gonio.__swig_destroy__(gonio)

    return ScanFactory.make_scan(
        (index, index), exposure, angles, {index:timestamp})