Exemple #1
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 = map(
        lambda p: load_path(p, directory=directory), map(str, d["filenames"])
    )
    imageset = ImageSetFactory.new(filenames)[0]

    # 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:
            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"] is not "":
        path = load_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"] is not "":
        path = load_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 the imageset
    return imageset
Exemple #2
0
def imagesweep_from_dict(d, check_format=True, directory=None):
    """Construct and image sweep from the dictionary."""
    # 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
Exemple #3
0
def test_load_path():
    from dxtbx.serialize.filename import load_path
    os.environ['HELLO_WORLD'] = 'EXPANDED'
    new_path = os.path.join('~', '$HELLO_WORLD', 'path')
    path = load_path(new_path)
    assert path == os.path.join(os.path.expanduser('~'), 'EXPANDED', 'path')
    new_path = os.path.join('$HELLO_WORLD', 'path')
    path = load_path(new_path)
    assert path == os.path.abspath(os.path.join('EXPANDED', 'path'))
Exemple #4
0
def test_load_path():
    from dxtbx.serialize.filename import load_path

    os.environ["HELLO_WORLD"] = "EXPANDED"
    new_path = os.path.join("~", "$HELLO_WORLD", "path")
    path = load_path(new_path)
    assert path == os.path.join(os.path.expanduser("~"), "EXPANDED", "path")
    new_path = os.path.join("$HELLO_WORLD", "path")
    path = load_path(new_path)
    assert path == os.path.abspath(os.path.join("EXPANDED", "path"))
 def tst_load_path(self):
   import os
   from os.path import join, abspath, expanduser
   os.environ['HELLO_WORLD'] = 'EXPANDED'
   new_path = join('~', '$HELLO_WORLD', 'path')
   path = load_path(new_path)
   assert(path == join(expanduser('~'), 'EXPANDED', 'path'))
   new_path = join('$HELLO_WORLD', 'path')
   path = load_path(new_path)
   assert(path == abspath(join('EXPANDED', 'path')))
   print 'OK'
 def tst_load_path(self):
     import os
     from os.path import join, abspath, expanduser
     os.environ['HELLO_WORLD'] = 'EXPANDED'
     new_path = join('~', '$HELLO_WORLD', 'path')
     path = load_path(new_path)
     assert (path == join(expanduser('~'), 'EXPANDED', 'path'))
     new_path = join('$HELLO_WORLD', 'path')
     path = load_path(new_path)
     assert (path == abspath(join('EXPANDED', 'path')))
     print 'OK'
 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)
Exemple #8
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
Exemple #9
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')

    # 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 "":
        with open(d['mask']) as infile:
            sweep.external_lookup.mask.filename = d['mask']
            sweep.external_lookup.mask.data = ImageBool(pickle.load(infile))
    if 'gain' in d and d['gain'] is not None and d['gain'] is not "":
        with open(d['gain']) as infile:
            sweep.external_lookup.gain.filename = d['gain']
            sweep.external_lookup.gain.data = ImageDouble(pickle.load(infile))
    if 'pedestal' in d and d['pedestal'] is not None and d[
            'pedestal'] is not "":
        with open(d['pedestal']) as infile:
            sweep.external_lookup.pedestal.filename = d['pedestal']
            sweep.external_lookup.pedestal.data = ImageDouble(
                pickle.load(infile))

    # Return the sweep
    return sweep
Exemple #10
0
 def _from_file(filename, directory=None):
     """ Load a model dictionary from a file. """
     filename = load_path(filename, directory=directory)
     try:
         with open(filename, "r") as infile:
             return json.load(infile, object_hook=_decode_dict)
     except IOError:
         raise IOError("unable to read file, %s" % filename)
Exemple #11
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
Exemple #12
0
def basic_imageset_from_dict(d, directory=None):
    ''' Construct an ImageSet class from the dictionary.'''
    from dxtbx.model import BeamFactory, DetectorFactory
    from dxtbx.imageset import ImageSetFactory
    from dxtbx.serialize.filename import load_path

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

    # 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:
            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'] is not "":
        path = load_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'] is not "":
        path = load_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 the imageset
    return imageset
 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)
Exemple #14
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)
 def _from_file(filename, directory=None):
     ''' Load a model dictionary from a file. '''
     from dxtbx.serialize.load import _decode_dict
     from dxtbx.serialize.filename import load_path
     import json
     from os.path import dirname
     filename = load_path(filename, directory=directory)
     try:
         with open(filename, 'r') as infile:
             return json.load(infile, object_hook=_decode_dict)
     except IOError:
         raise IOError('unable to read file, %s' % filename)
 def _from_file(filename):
   ''' Load a model dictionary from a file. '''
   from dxtbx.serialize.load import _decode_dict
   from dxtbx.serialize.filename import load_path, temp_chdir
   import json
   from os.path import dirname
   filename = load_path(filename)
   try:
     with temp_chdir(dirname(filename)):
       with open(filename, 'r') as infile:
         return json.loads(infile.read(), object_hook=_decode_dict)
   except IOError, e:
     raise IOError('unable to read file, %s' % filename)
Exemple #17
0
 def _make_stills(self, imageset, format_kwargs=None):
     """ Make a still imageset. """
     filenames = [
         load_path(p, directory=self._directory) 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,
     )
  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)
  def _make_sweep(self,
                  imageset,
                  beam=None,
                  detector=None,
                  goniometer=None,
                  scan=None,
                  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
    from dxtbx.format.FormatMultiImage import FormatMultiImage

    # 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()

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

    # Make a sweep from the input data
    return ImageSetFactory.make_sweep(
      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)
Exemple #20
0
    def _load_pickle_path(self, imageset_data, param):
        # type: (Dict, str) -> Tuple[Optional[str], Any]
        """Read a filename from an imageset dict and load if required.

        Args:
            imageset_data: The dictionary holding imageset information
            param: The key name to lookup in the imageset dictionary

        Returns:
            A tuple of (filename, data) where data has been loaded from
            the pickle file. If there is no key entry then (None, None)
            is returned. If the configuration parameter check_format is
            False then (filename, None) will be returned.
        """
        if param not in imageset_data:
            return None, None

        filename = load_path(imageset_data[param], directory=self._directory)
        if self._check_format and filename:
            with open(filename, "rb") as fh:
                return filename, pickle.load(fh)
        return filename, None
Exemple #21
0
    def _make_sweep(
        self,
        imageset,
        beam=None,
        detector=None,
        goniometer=None,
        scan=None,
        format_kwargs=None,
    ):
        """ Make an image sweep. """
        # Get the template format
        template = load_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 sweep from the input data
        return ImageSetFactory.make_sweep(
            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,
        )
Exemple #22
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)
Exemple #23
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)
Exemple #24
0
def test_load_path_deprecated():
    os.environ["HELLO_WORLD"] = "EXPANDED"
    new_path = os.path.join("~", "$HELLO_WORLD", "path")
    with pytest.deprecated_call():
        path = load_path(new_path)
    assert path == resolve_path(new_path)
Exemple #25
0
  def _extract_experiments(self):
    ''' Helper function. Extract the experiments. '''
    from dxtbx.imageset import ImageSweep, ImageSet, ImageGrid
    from dxtbx.serialize.filename import load_path
    import cPickle as pickle

    # Map of imageset/scan pairs
    imagesets = {}

    # For every experiment, use the given input to create
    # a sensible experiment.
    el = ExperimentList()
    for eobj in self._obj['experiment']:

      # Get the models
      beam = ExperimentListDict.model_or_none(self._blist, eobj, 'beam')
      detector = ExperimentListDict.model_or_none(self._dlist, eobj, 'detector')
      goniometer = ExperimentListDict.model_or_none(self._glist, eobj, 'goniometer')
      scan = ExperimentListDict.model_or_none(self._slist, eobj, 'scan')
      crystal = ExperimentListDict.model_or_none(self._clist, eobj, 'crystal')
      profile = ExperimentListDict.model_or_none(self._plist, eobj, 'profile')
      key = (eobj.get('imageset'), eobj.get('scan'))
      try:
        imageset = imagesets[key]
      except Exception:
        imageset = ExperimentListDict.model_or_none(self._ilist, eobj, 'imageset')

        # Create the imageset from the input data
        if imageset is not None:
          if 'params' in imageset:
            format_kwargs = imageset['params']
          else:
            format_kwargs = {}
          if 'mask' in imageset and imageset['mask'] is not None:
            mask_filename = load_path(imageset['mask'])
            if self._check_format:
              mask = pickle.load(open(mask_filename))
            else:
              mask = None
          else:
            mask_filename = None
            mask = None
          if 'gain' in imageset and imageset['gain'] is not None:
            gain_filename = load_path(imageset['gain'])
            if self._check_format:
              gain = pickle.load(open(gain_filename))
            else:
              gain = None
          else:
            gain_filename = None
            gain = None
          if 'pedestal' in imageset and imageset['pedestal'] is not None:
            pedestal_filename = load_path(imageset['pedestal'])
            if self._check_format:
              pedestal = pickle.load(open(pedestal_filename))
            else:
              pedestal = None
          else:
            pedestal_filename = None
            pedestal = None
          if imageset['__id__'] == 'ImageSet':
            imageset = self._make_stills(imageset, format_kwargs=format_kwargs)
          elif imageset['__id__'] == 'ImageGrid':
            imageset = self._make_grid(imageset, format_kwargs=format_kwargs)
          elif imageset['__id__'] == 'ImageSweep':
            imageset = self._make_sweep(imageset, scan, format_kwargs=format_kwargs)
          elif imageset['__id__'] == 'MemImageSet':
            imageset = self._make_mem_imageset(imageset)
          else:
            raise RuntimeError('Unknown imageset type')

          # Set the external lookup
          if imageset is not None:
            imageset.external_lookup.mask.data = mask
            imageset.external_lookup.mask.filename = mask_filename
            imageset.external_lookup.gain.data = gain
            imageset.external_lookup.gain.filename = gain_filename
            imageset.external_lookup.pedestal.data = pedestal
            imageset.external_lookup.pedestal.filename = pedestal_filename

          # Update the imageset models
          if isinstance(imageset, ImageSweep):
            imageset.set_beam(beam)
            imageset.set_detector(detector)
            imageset.set_goniometer(goniometer)
            imageset.set_scan(scan)
          elif isinstance(imageset, ImageSet):
            for i in range(len(imageset)):
              imageset.set_beam(beam, i)
              imageset.set_detector(detector, i)
              imageset.set_goniometer(goniometer, i)
              imageset.set_scan(scan, i)
          elif isinstance(imageset, ImageGrid):
            for i in range(len(imageset)):
              imageset.set_beam(beam, i)
              imageset.set_detector(detector, i)
              imageset.set_goniometer(goniometer, i)
              imageset.set_scan(scan, i)
          else:
            pass

        # Add the imageset to the dict
        imagesets[key] = imageset

      # Append the experiment
      el.append(Experiment(
        imageset=imageset,
        beam=beam,
        detector=detector,
        goniometer=goniometer,
        scan=scan,
        crystal=crystal,
        profile=profile))

    # Return the experiment list
    return el
  def _extract_experiments(self):
    ''' Helper function. Extract the experiments. '''
    from dxtbx.imageset import ImageSweep, ImageSet
    from dxtbx.serialize.filename import load_path
    import cPickle as pickle

    # Map of imageset/scan pairs
    imagesets = {}

    # For every experiment, use the given input to create
    # a sensible experiment.
    el = ExperimentList()
    for eobj in self._obj['experiment']:

      # Get the models
      beam = ExperimentListDict.model_or_none(self._blist, eobj, 'beam')
      detector = ExperimentListDict.model_or_none(self._dlist, eobj, 'detector')
      goniometer = ExperimentListDict.model_or_none(self._glist, eobj, 'goniometer')
      scan = ExperimentListDict.model_or_none(self._slist, eobj, 'scan')
      crystal = ExperimentListDict.model_or_none(self._clist, eobj, 'crystal')
      profile = ExperimentListDict.model_or_none(self._plist, eobj, 'profile')
      key = (eobj.get('imageset', None), eobj.get('scan',None))
      try:
        imageset = imagesets[key]
      except Exception:
        imageset = ExperimentListDict.model_or_none(self._ilist, eobj, 'imageset')

        # Create the imageset from the input data
        if imageset is not None:
          if 'mask' in imageset and imageset['mask'] is not None:
            mask_filename = load_path(imageset['mask'])
            if self._check_format:
              mask = pickle.load(open(mask_filename))
            else:
              mask = None
          else:
            mask_filename = None
            mask = None
          if 'gain' in imageset and imageset['gain'] is not None:
            gain_filename = load_path(imageset['gain'])
            if self._check_format:
              gain = pickle.load(open(gain_filename))
            else:
              gain = None
          else:
            gain_filename = None
            gain = None
          if 'pedestal' in imageset and imageset['pedestal'] is not None:
            pedestal_filename = load_path(imageset['pedestal'])
            if self._check_format:
              pedestal = pickle.load(open(pedestal_filename))
            else:
              pedestal = None
          else:
            pedestal_filename = None
            pedestal = None
          if imageset['__id__'] == 'ImageSet':
            imageset = self._make_stills(imageset)
          elif imageset['__id__'] == 'ImageSweep':
            imageset = self._make_sweep(imageset, scan)
          elif imageset['__id__'] == 'MemImageSet':
            imageset = self._make_mem_imageset(imageset)
          else:
            raise RuntimeError('Unknown imageset type')

          # Fill in any models if they aren't already there
          if imageset is not None:
            imageset.external_lookup.mask.data = mask
            imageset.external_lookup.mask.filename = mask_filename
            imageset.external_lookup.gain.data = gain
            imageset.external_lookup.gain.filename = gain_filename
            imageset.external_lookup.pedestal.data = pedestal
            imageset.external_lookup.pedestal.filename = pedestal_filename
            if beam is None:
              beam = imageset.get_beam()
            if detector is None:
              detector = imageset.get_detector()
            if goniometer is None:
              goniometer = imageset.get_goniometer()
            if scan is None:
              scan = imageset.get_scan()

          # Update the imageset models
          if isinstance(imageset, ImageSweep):
            imageset.set_beam(beam)
            imageset.set_detector(detector)
            imageset.set_goniometer(goniometer)
            imageset.set_scan(scan)
          elif isinstance(imageset, ImageSet):
            for i in range(len(imageset)):
              imageset.set_beam(beam, i)
              imageset.set_detector(detector, i)
              imageset.set_goniometer(goniometer, i)
              imageset.set_scan(scan, i)
          else:
            pass

        # Add the imageset to the dict
        imagesets[key] = imageset

      # Append the experiment
      el.append(Experiment(
        imageset=imageset,
        beam=beam,
        detector=detector,
        goniometer=goniometer,
        scan=scan,
        crystal=crystal,
        profile=profile))

    # Return the experiment list
    return el
    def _extract_experiments(self):
        ''' Helper function. Extract the experiments. '''
        from dxtbx.imageset import ImageSweep, ImageSet, ImageGrid
        from dxtbx.serialize.filename import load_path
        import six.moves.cPickle as pickle
        from dxtbx.format.image import ImageBool, ImageDouble

        # Map of imageset/scan pairs
        imagesets = {}

        # For every experiment, use the given input to create
        # a sensible experiment.
        el = ExperimentList()
        for eobj in self._obj['experiment']:

            # Get the models
            identifier = eobj.get("identifier", "")
            beam = ExperimentListDict.model_or_none(self._blist, eobj, 'beam')
            detector = ExperimentListDict.model_or_none(
                self._dlist, eobj, 'detector')
            goniometer = ExperimentListDict.model_or_none(
                self._glist, eobj, 'goniometer')
            scan = ExperimentListDict.model_or_none(self._slist, eobj, 'scan')
            crystal = ExperimentListDict.model_or_none(self._clist, eobj,
                                                       'crystal')
            profile = ExperimentListDict.model_or_none(self._plist, eobj,
                                                       'profile')
            scaling_model = ExperimentListDict.model_or_none(
                self._scalelist, eobj, 'scaling_model')
            key = (eobj.get('imageset'), eobj.get('scan'))
            try:
                imageset = imagesets[key]
            except Exception:
                imageset = ExperimentListDict.model_or_none(
                    self._ilist, eobj, 'imageset')

                # Create the imageset from the input data
                if imageset is not None:
                    if 'params' in imageset:
                        format_kwargs = imageset['params']
                    else:
                        format_kwargs = {}
                    if 'mask' in imageset and imageset['mask'] is not None:
                        mask_filename = load_path(imageset['mask'],
                                                  directory=self._directory)
                        if self._check_format and mask_filename is not "":
                            with open(mask_filename, 'rb') as fh:
                                mask = pickle.load(fh)
                        else:
                            mask = None
                    else:
                        mask_filename = None
                        mask = None
                    if 'gain' in imageset and imageset['gain'] is not None:
                        gain_filename = load_path(imageset['gain'],
                                                  directory=self._directory)
                        if self._check_format and gain_filename is not "":
                            with open(gain_filename, 'rb') as fh:
                                gain = pickle.load(fh)
                        else:
                            gain = None
                    else:
                        gain_filename = None
                        gain = None
                    if 'pedestal' in imageset and imageset[
                            'pedestal'] is not None:
                        pedestal_filename = load_path(
                            imageset['pedestal'], directory=self._directory)
                        if self._check_format and pedestal_filename is not "":
                            with open(pedestal_filename, 'rb') as fh:
                                pedestal = pickle.load(fh)
                        else:
                            pedestal = None
                    else:
                        pedestal_filename = None
                        pedestal = None
                    if 'dx' in imageset and imageset['dx'] is not None:
                        dx_filename = load_path(imageset['dx'],
                                                directory=self._directory)
                        if dx_filename is not "":
                            with open(dx_filename, 'rb') as fh:
                                dx = pickle.load(fh)
                        else:
                            dx = None
                    else:
                        dx_filename = None
                        dx = None
                    if 'dy' in imageset and imageset['dy'] is not None:
                        dy_filename = load_path(imageset['dy'],
                                                directory=self._directory)
                        if dy_filename is not "":
                            with open(dy_filename, 'rb') as fh:
                                dy = pickle.load(fh)
                        else:
                            dy = None
                    else:
                        dy_filename = None
                        dy = None
                    if imageset['__id__'] == 'ImageSet':
                        imageset = self._make_stills(
                            imageset, format_kwargs=format_kwargs)
                    elif imageset['__id__'] == 'ImageGrid':
                        imageset = self._make_grid(imageset,
                                                   format_kwargs=format_kwargs)
                    elif imageset['__id__'] == 'ImageSweep':
                        imageset = self._make_sweep(
                            imageset,
                            beam=beam,
                            detector=detector,
                            goniometer=goniometer,
                            scan=scan,
                            format_kwargs=format_kwargs)
                    elif imageset['__id__'] == 'MemImageSet':
                        imageset = self._make_mem_imageset(imageset)
                    else:
                        raise RuntimeError('Unknown imageset type')

                    # Set the external lookup
                    if imageset is not None:
                        if mask_filename is None:
                            mask_filename = ""
                        if gain_filename is None:
                            gain_filename = ""
                        if pedestal_filename is None:
                            pedestal_filename = ""
                        if dx_filename is None:
                            dx_filename = ""
                        if dy_filename is None:
                            dy_filename = ""
                        if mask is None:
                            mask = ImageBool()
                        else:
                            mask = ImageBool(mask)
                        if gain is None:
                            gain = ImageDouble()
                        else:
                            gain = ImageDouble(gain)
                        if pedestal is None:
                            pedestal = ImageDouble()
                        else:
                            pedestal = ImageDouble(pedestal)
                        if dx is None:
                            dx = ImageDouble()
                        else:
                            dx = ImageDouble(dx)
                        if dy is None:
                            dy = ImageDouble()
                        else:
                            dy = ImageDouble(dy)
                        imageset.external_lookup.mask.data = mask
                        imageset.external_lookup.mask.filename = mask_filename
                        imageset.external_lookup.gain.data = gain
                        imageset.external_lookup.gain.filename = gain_filename
                        imageset.external_lookup.pedestal.data = pedestal
                        imageset.external_lookup.pedestal.filename = pedestal_filename
                        imageset.external_lookup.dx.data = dx
                        imageset.external_lookup.dx.filename = dx_filename
                        imageset.external_lookup.dy.data = dy
                        imageset.external_lookup.dy.filename = dy_filename

                    # Update the imageset models
                    if isinstance(imageset, ImageSweep):
                        imageset.set_beam(beam)
                        imageset.set_detector(detector)
                        imageset.set_goniometer(goniometer)
                        imageset.set_scan(scan)
                    elif isinstance(imageset, ImageSet):
                        for i in range(len(imageset)):
                            imageset.set_beam(beam, i)
                            imageset.set_detector(detector, i)
                            imageset.set_goniometer(goniometer, i)
                            imageset.set_scan(scan, i)
                    elif isinstance(imageset, ImageGrid):
                        for i in range(len(imageset)):
                            imageset.set_beam(beam, i)
                            imageset.set_detector(detector, i)
                            imageset.set_goniometer(goniometer, i)
                            imageset.set_scan(scan, i)
                    else:
                        pass

                    if imageset is not None:
                        imageset.update_detector_px_mm_data()

                # Add the imageset to the dict
                imagesets[key] = imageset

            # Append the experiment
            el.append(
                Experiment(imageset=imageset,
                           beam=beam,
                           detector=detector,
                           goniometer=goniometer,
                           scan=scan,
                           crystal=crystal,
                           profile=profile,
                           scaling_model=scaling_model,
                           identifier=identifier))

        # Return the experiment list
        return el
Exemple #28
0
  def _load_datablocks(self, obj, check_format=True, directory=None):
    ''' Create the datablock from a dictionary. '''
    from dxtbx.format.Registry import Registry
    from dxtbx.model import BeamFactory, DetectorFactory
    from dxtbx.model import GoniometerFactory, ScanFactory
    from dxtbx.serialize.filename import load_path
    from dxtbx.format.image import ImageBool, ImageDouble
    from dxtbx.format.FormatMultiImage import FormatMultiImage

    # If we have a list, extract for each dictionary in the list
    if isinstance(obj, list):
      return [self._load_datablocks(dd, check_format, directory) for dd in obj]
    elif not isinstance(obj, dict):
      raise InvalidDataBlockError("Unexpected datablock type {} instead of dict".format(type(obj)))
    # Make sure the id signature is correct
    if not obj.get("__id__") == "DataBlock":
      raise InvalidDataBlockError(
        "Expected __id__ 'DataBlock', but found {}".format(repr(obj.get("__id__"))))

    # 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 = BeamFactory.from_dict(blist[obj['beam']])
      except Exception:
        beam = None
      try:
        dobj = dlist[obj['detector']]
        detector = DetectorFactory.from_dict(dobj)
      except Exception:
        detector = None
      try:
        gonio = GoniometerFactory.from_dict(glist[obj['goniometer']])
      except Exception:
        gonio = None
      try:
        scan = ScanFactory.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'], directory=directory)
          i0, i1 = scan.get_image_range()
          iset = dxtbx.imageset.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'], directory=directory)
            iset.external_lookup.mask.filename = imageset['mask']
            if check_format:
              with open(imageset['mask']) as infile:
                iset.external_lookup.mask.data = ImageBool(pickle.load(infile))
          if 'gain' in imageset and imageset['gain'] is not None:
            imageset['gain'] = load_path(imageset['gain'], directory=directory)
            iset.external_lookup.gain.filename = imageset['gain']
            if check_format:
              with open(imageset['gain']) as infile:
                iset.external_lookup.gain.data = ImageDouble(pickle.load(infile))
          if 'pedestal' in imageset and imageset['pedestal'] is not None:
            imageset['pedestal'] = load_path(imageset['pedestal'], directory=directory)
            iset.external_lookup.pedestal.filename = imageset['pedestal']
            if check_format:
              with open(imageset['pedestal']) as infile:
                iset.external_lookup.pedestal.data = ImageDouble(pickle.load(infile))
          if 'dx' in imageset and imageset['dx'] is not None:
            imageset['dx'] = load_path(imageset['dx'], directory=directory)
            iset.external_lookup.dx.filename = imageset['dx']
            with open(imageset['dx']) as infile:
              iset.external_lookup.dx.data = ImageDouble(pickle.load(infile))
          if 'dy' in imageset and imageset['dy'] is not None:
            imageset['dy'] = load_path(imageset['dy'], directory=directory)
            iset.external_lookup.dy.filename = imageset['dy']
            with open(imageset['dy']) as infile:
              iset.external_lookup.dy.data = ImageDouble(pickle.load(infile))
          iset.update_detector_px_mm_data()
        elif "master" in imageset:
          template = load_path(imageset['master'], directory=directory)
          i0, i1 = scan.get_image_range()
          indices = imageset['images']
          if check_format == False:
            format_class = FormatMultiImage
          else:
            format_class = None
          iset = dxtbx.imageset.ImageSetFactory.make_sweep(
            template,
            list(range(i0, i1+1)),
            format_class  = format_class,
            beam          = beam,
            detector      = detector,
            goniometer    = gonio,
            scan          = scan,
            check_format  = check_format,
            format_kwargs = format_kwargs)
          if 'mask' in imageset and imageset['mask'] is not None:
            imageset['mask'] = load_path(imageset['mask'], directory)
            iset.external_lookup.mask.filename = imageset['mask']
            if check_format:
              with open(imageset['mask']) as infile:
                iset.external_lookup.mask.data = ImageBool(pickle.load(infile))
          if 'gain' in imageset and imageset['gain'] is not None:
            imageset['gain'] = load_path(imageset['gain'], directory)
            iset.external_lookup.gain.filename = imageset['gain']
            if check_format:
              with open(imageset['gain']) as infile:
                iset.external_lookup.gain.data = ImageDouble(pickle.load(infile))
          if 'pedestal' in imageset and imageset['pedestal'] is not None:
            imageset['pedestal'] = load_path(imageset['pedestal'], directory)
            iset.external_lookup.pedestal.filename = imageset['pedestal']
            if check_format:
              with open(imageset['pedestal']) as infile:
                iset.external_lookup.pedestal.data = ImageDouble(pickle.load(infile))
          if 'dx' in imageset and imageset['dx'] is not None:
            imageset['dx'] = load_path(imageset['dx'], directory)
            iset.external_lookup.dx.filename = imageset['dx']
            with open(imageset['dx']) as infile:
              iset.external_lookup.dx.data = ImageDouble(pickle.load(infile))
          if 'dy' in imageset and imageset['dy'] is not None:
            imageset['dy'] = load_path(imageset['dy'], directory)
            iset.external_lookup.dy.filename = imageset['dy']
            with open(imageset['dy']) as infile:
              iset.external_lookup.dy.data = ImageDouble(pickle.load(infile))
          iset.update_detector_px_mm_data()
        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 = dxtbx.imageset.ImageSetFactory.make_imageset(
          filenames, None, check_format, indices, format_kwargs=format_kwargs)
        if ident == "ImageGrid":
          grid_size = imageset['grid_size']
          iset = dxtbx.imageset.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'], directory)
          iset.external_lookup.mask.filename = imageset['mask']
          if check_format:
            with open(imageset['mask']) as infile:
              iset.external_lookup.mask.data = ImageBool(pickle.load(infile))
        if 'gain' in imageset and imageset['gain'] is not None:
          imageset['gain'] = load_path(imageset['gain'], directory)
          iset.external_lookup.gain.filename = imageset['gain']
          if check_format:
            with open(imageset['gain']) as infile:
              iset.external_lookup.gain.data = ImageDouble(pickle.load(infile))
        if 'pedestal' in imageset and imageset['pedestal'] is not None:
          imageset['pedestal'] = load_path(imageset['pedestal'], directory)
          iset.external_lookup.pedestal.filename = imageset['pedestal']
          if check_format:
            with open(imageset['pedestal']) as infile:
              iset.external_lookup.pedestal.data = ImageDouble(pickle.load(infile))
        if 'dx' in imageset and imageset['dx'] is not None:
          imageset['dx'] = load_path(imageset['dx'], directory)
          iset.external_lookup.dx.filename = imageset['dx']
          with open(imageset['dx']) as infile:
            iset.external_lookup.dx.data = ImageDouble(pickle.load(infile))
        if 'dy' in imageset and imageset['dy'] is not None:
          imageset['dy'] = load_path(imageset['dy'], directory)
          iset.external_lookup.dy.filename = imageset['dy']
          with open(imageset['dy']) as infile:
            iset.external_lookup.dy.data = ImageDouble(pickle.load(infile))
          iset.update_detector_px_mm_data()
        imagesets.append(iset)
      else:
        raise RuntimeError('expected ImageSet/ImageSweep, got %s' % ident)

    # Return the datablock
    return DataBlock(imagesets)
Exemple #29
0
def test_load_path_deprecated(monkeypatch):
    monkeypatch.setenv("HELLO_WORLD", "EXPANDED")
    new_path = os.path.join("~", "$HELLO_WORLD", "path")
    with pytest.deprecated_call():
        path = load_path(new_path)
    assert path == resolve_path(new_path)
Exemple #30
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)