Example #1
0
  def run(self):
    '''Execute the script.'''
    from dials.array_family import flex
    from dials.util.options import flatten_datablocks
    from dials.util.options import flatten_reflections
    from time import time
    from dials.util import log
    from logging import info, debug
    from libtbx.utils import Sorry
    start_time = time()

    # Parse the command line
    params, options = self.parser.parse_args(show_diff_phil=False)

    # Configure the logging
    log.config(
      params.verbosity,
      info=params.output.log,
      debug=params.output.debug_log)

    from dials.util.version import dials_version
    info(dials_version())

    # Log the diff phil
    diff_phil = self.parser.diff_phil.as_str()
    if diff_phil is not '':
      info('The following parameters have been modified:\n')
      info(diff_phil)

    # Ensure we have a data block
    datablocks = flatten_datablocks(params.input.datablock)
    reflections = flatten_reflections(params.input.reflections)
    if len(datablocks) == 0 and len(reflections) == 0:
      self.parser.print_help()
      return
    elif len(datablocks) != len(reflections):
      raise Sorry("Must have same number of datablocks and reflection tables")

    # Combine the datablocks and reflections
    datablock, reflections = combine(
      datablocks,
      reflections,
      params)

    # Save the reflections to file
    info('\n' + '-' * 80)
    reflections.as_pickle(params.output.reflections)
    info('Saved {0} reflections to {1}'.format(
        len(reflections), params.output.reflections))

    # Save the datablock
    from dxtbx.datablock import DataBlockDumper
    info('Saving datablocks to {0}'.format(
      params.output.datablock))
    dump = DataBlockDumper(datablocks)
    dump.as_file(params.output.datablock)


    # Print the time
    info("Time Taken: %f" % (time() - start_time))
Example #2
0
  def run(self):
    '''Execute the script.'''
    from dials.array_family import flex
    from dials.util.options import flatten_datablocks
    from dials.util.options import flatten_reflections
    from time import time
    from dials.util import log
    from libtbx.utils import Sorry
    start_time = time()

    # Parse the command line
    params, options = self.parser.parse_args(show_diff_phil=False)

    # Configure the logging
    log.config(
      params.verbosity,
      info=params.output.log,
      debug=params.output.debug_log)

    from dials.util.version import dials_version
    logger.info(dials_version())

    # Log the diff phil
    diff_phil = self.parser.diff_phil.as_str()
    if diff_phil is not '':
      logger.info('The following parameters have been modified:\n')
      logger.info(diff_phil)

    # Ensure we have a data block
    datablocks = flatten_datablocks(params.input.datablock)
    reflections = flatten_reflections(params.input.reflections)
    if len(datablocks) == 0 and len(reflections) == 0:
      self.parser.print_help()
      return
    elif len(datablocks) != len(reflections):
      raise Sorry("Must have same number of datablocks and reflection tables")

    # Combine the datablocks and reflections
    datablock, reflections = combine(
      datablocks,
      reflections,
      params)

    # Save the reflections to file
    logger.info('\n' + '-' * 80)
    reflections.as_pickle(params.output.reflections)
    logger.info('Saved {0} reflections to {1}'.format(
        len(reflections), params.output.reflections))

    # Save the datablock
    from dxtbx.datablock import DataBlockDumper
    logger.info('Saving datablocks to {0}'.format(
      params.output.datablock))
    dump = DataBlockDumper(datablocks)
    dump.as_file(params.output.datablock)


    # Print the time
    logger.info("Time Taken: %f" % (time() - start_time))
Example #3
0
  def write_datablocks(self, datablocks, params):
    '''
    Output the datablock to file.

    '''
    if params.output.datablock:
      logger.info("-" * 80)
      logger.info('Writing datablocks to %s' % params.output.datablock)
      dump = DataBlockDumper(datablocks)
      dump.as_file(params.output.datablock, compact=params.output.compact)
Example #4
0
  def write_datablocks(self, datablocks, params):
    '''
    Output the datablock to file.

    '''
    if params.output.datablock:
      logger.info("-" * 80)
      logger.info('Writing datablocks to %s' % params.output.datablock)
      dump = DataBlockDumper(datablocks)
      dump.as_file(params.output.datablock, compact=params.output.compact)
Example #5
0
    def run(self):
        ''' Run the script. '''
        from dials.util.masking import MaskGenerator
        from dials.util.options import flatten_datablocks
        from libtbx.utils import Sorry
        import six.moves.cPickle as pickle
        from dials.util import log
        from dxtbx.format.image import ImageBool

        # Parse the command line arguments
        params, options = self.parser.parse_args(show_diff_phil=True)
        datablocks = flatten_datablocks(params.input.datablock)

        # Configure logging
        log.config()

        # Check number of args
        if len(datablocks) == 0:
            self.parser.print_help()
            return

        if len(datablocks) != 1:
            raise Sorry('exactly 1 datablock must be specified')
        datablock = datablocks[0]
        imagesets = datablock.extract_imagesets()
        if len(imagesets) != 1:
            raise Sorry('datablock must contain exactly 1 imageset')
        imageset = imagesets[0]

        # Generate the mask
        generator = MaskGenerator(params)
        mask = generator.generate(imageset)

        # Save the mask to file
        print("Writing mask to %s" % params.output.mask)
        with open(params.output.mask, "wb") as fh:
            pickle.dump(mask, fh)

        # Save the datablock
        if params.output.datablock is not None:
            imageset.external_lookup.mask.data = ImageBool(mask)
            imageset.external_lookup.mask.filename = params.output.mask
            from dxtbx.datablock import DataBlockDumper
            print('Saving datablocks to {0}'.format(params.output.datablock))
            dump = DataBlockDumper(datablocks)
            dump.as_file(params.output.datablock)
    def run(self):
        '''Execute the script.'''

        # Parse the command line
        params, options = self.parser.parse_args(show_diff_phil=True)

        try:
            assert len(params.input.reflections) == len(params.input.datablock)
        except AssertionError:
            raise Sorry(
                "The number of input reflections files does not match the "
                "number of input datablocks")

        datablocks = flatten_datablocks(params.input.datablock)
        reflections = flatten_reflections(params.input.reflections)

        if len(reflections):
            r = self.combine_reflections(reflections)
            # print number of reflections per imageset
            from libtbx.table_utils import simple_table
            max_id = max(r['id'])
            header = ["Imageset", "Nref"]
            nrefs_per_imset = [(r['id'] == i).count(True)
                               for i in range(max_id + 1)]
            rows = [(str(i), str(n)) for (i, n) in enumerate(nrefs_per_imset)]
            st = simple_table(rows, header)
            print(st.format())
            rf = params.output.reflections_filename
            print('Saving combined reflections to {0}'.format(rf))
            r.as_pickle(rf)

        if len(datablocks):
            db = self.combine_datablocks(datablocks)
            dbf = params.output.datablocks_filename
            print('Saving combined datablocks to {0}'.format(dbf))
            dump = DataBlockDumper(db)
            dump.as_file(dbf, compact=params.output.compact)

        return
Example #7
0
def write_expt(experiments, filename):
    from dxtbx.datablock import DataBlockDumper

    dump = DataBlockDumper(experiments)
    dump.as_file(filename)
Example #8
0
    def run(self):
        '''Execute the script.'''
        from dials.array_family import flex
        from dials.util.options import flatten_datablocks
        from time import time
        from dials.util import log
        from libtbx.utils import Sorry
        start_time = time()

        # Parse the command line
        params, options = self.parser.parse_args(show_diff_phil=False)

        # Configure the logging
        log.config(params.verbosity,
                   info=params.output.log,
                   debug=params.output.debug_log)

        from dials.util.version import dials_version
        logger.info(dials_version())

        # Log the diff phil
        diff_phil = self.parser.diff_phil.as_str()
        if diff_phil is not '':
            logger.info('The following parameters have been modified:\n')
            logger.info(diff_phil)

        # Ensure we have a data block
        datablocks = flatten_datablocks(params.input.datablock)
        if len(datablocks) == 0:
            self.parser.print_help()
            return
        elif len(datablocks) != 1:
            raise Sorry('only 1 datablock can be processed at a time')

        # Loop through all the imagesets and find the strong spots
        reflections = flex.reflection_table.from_observations(
            datablocks[0], params)

        # Delete the shoeboxes
        if not params.output.shoeboxes:
            del reflections['shoebox']

        # ascii spot count per image plot
        from dials.util.ascii_art import spot_counts_per_image_plot

        for i, imageset in enumerate(datablocks[0].extract_imagesets()):
            ascii_plot = spot_counts_per_image_plot(
                reflections.select(reflections['id'] == i))
            if len(ascii_plot):
                logger.info(
                    '\nHistogram of per-image spot count for imageset %i:' % i)
                logger.info(ascii_plot)

        # Save the reflections to file
        logger.info('\n' + '-' * 80)
        reflections.as_pickle(params.output.reflections)
        logger.info('Saved {0} reflections to {1}'.format(
            len(reflections), params.output.reflections))

        # Save the datablock
        if params.output.datablock:
            from dxtbx.datablock import DataBlockDumper
            logger.info('Saving datablocks to {0}'.format(
                params.output.datablock))
            dump = DataBlockDumper(datablocks)
            dump.as_file(params.output.datablock)

        # Print some per image statistics
        if params.per_image_statistics:
            from dials.algorithms.spot_finding import per_image_analysis
            from cStringIO import StringIO
            s = StringIO()
            for i, imageset in enumerate(datablocks[0].extract_imagesets()):
                print >> s, "Number of centroids per image for imageset %i:" % i
                stats = per_image_analysis.stats_imageset(
                    imageset,
                    reflections.select(reflections['id'] == i),
                    resolution_analysis=False)
                per_image_analysis.print_table(stats, out=s)
            logger.info(s.getvalue())

        # Print the time
        logger.info("Time Taken: %f" % (time() - start_time))
Example #9
0
        if not stills:
            num_stills = 0
        else:
            num_stills = len(stills)

        # Print some data block info
        print "-" * 80
        print "DataBlock %d" % i
        print "  format: %s" % str(datablock.format_class())
        print "  num images: %d" % datablock.num_images()
        print "  num sweeps: %d" % len(sweeps)
        print "  num stills: %d" % num_stills

        # Loop through all the sweeps
        if options.verbose > 1:
            for j, sweep in enumerate(sweeps):
                print ""
                print "Sweep %d" % j
                print "  length %d" % len(sweep)
                print sweep.get_beam()
                print sweep.get_goniometer()
                print sweep.get_detector()
                print sweep.get_scan()

    # Write the datablock to a JSON or pickle file
    if options.output:
        print "-" * 80
        print 'Writing datablocks to %s' % options.output
        dump = DataBlockDumper(datablocks)
        dump.as_file(options.output, compact=options.compact)
def test_elliptical_distortion(tmpdir):
    """Create distortion maps for elliptical distortion using a dummy datablock
  with a small detector, for speed. Check those maps seem sensible"""

    tmpdir.chdir()

    # Make a detector model
    d = make_detector()

    # The beam is also essential for a datablock to be serialisable
    b = Beam((0, 0, 1), 1.0)

    # Create and write out a datablock
    imageset = ImageSet(
        ImageSetData(Reader(["non-existent.cbf"]),
                     Masker(["non-existent.cbf"])))
    imageset.set_detector(d)
    imageset.set_beam(b)
    datablocks = DataBlockFactory.from_imageset(imageset)
    dump = DataBlockDumper(datablocks)
    dump.as_file("dummy_datablock.json")

    # Centre of distortion will be the far corner from the origin of the first
    # panel
    centre_xy = d[0].get_image_size_mm()

    # Generate distortion maps
    cmd = ("dials.generate_distortion_maps dummy_datablock.json "
           "mode=ellipse centre_xy={},{} "
           "phi=0 l1=1.0 l2=0.95").format(*centre_xy)
    result = easy_run.fully_buffered(command=cmd).raise_if_errors()

    # Load the maps
    with open("dx.pickle", "rb") as f:
        dx = pickle.load(f)
    with open("dy.pickle", "rb") as f:
        dy = pickle.load(f)

    # Check there are 4 maps each
    assert len(dx) == len(dy) == 4

    # Ellipse has phi=0, so all correction is in the dy map
    for arr in dx:
        assert min(arr) == max(arr) == 0.0

    # The ellipse correction is centred at the middle of the detector and all in
    # the Y direction. Therefore we expect a few things from the dy maps:
    #
    # (1) Within each panel the columns of the array are identical.
    # (2) The two upper panels should be the same
    # (3) The two lower panels should be the same.
    # (4) One column from an upper panel is a negated, reversed column from a
    #     lower panel.
    #
    # All together expect the 4 dy maps to look something like this:
    #
    # /-----------\ /-----------\
    # |-3 -3 -3 -3| |-3 -3 -3 -3|
    # |-2 -2 -2 -2| |-2 -2 -2 -2|
    # |-1 -1 -1 -1| |-1 -1 -1 -1|
    # | 0  0  0  0| | 0  0  0  0|
    # \-----------/ \-----------/
    # /-----------\ /-----------\
    # | 0  0  0  0| | 0  0  0  0|
    # | 1  1  1  1| | 1  1  1  1|
    # | 2  2  2  2| | 2  2  2  2|
    # | 3  3  3  3| | 3  3  3  3|
    # \-----------/ \-----------/

    # So the fundamental data is all in the first column of first panel's map
    col0 = dy[0].matrix_copy_column(0)

    # The correction should be 5% of the distance from the ellipse centre to a
    # corrected pixel (l2 = 0.95 above) along the slow axis. Check that is the
    # case (for the first pixel at least)
    vec_centre_to_first_px = (matrix.col(d[0].get_pixel_lab_coord(
        (0.5, 0.5))) - matrix.col(d[0].get_lab_coord(centre_xy)))
    dist_centre_to_first_px = vec_centre_to_first_px.dot(
        matrix.col(d[0].get_slow_axis()))
    corr_mm = dist_centre_to_first_px * 0.05
    corr_px = corr_mm / d[0].get_pixel_size()[1]
    assert col0[0] == pytest.approx(corr_px)

    # Test (1) from above list for panel 0
    for i in range(1, 50):
        assert (col0 == dy[0].matrix_copy_column(i)).all_eq(True)

    # Test (2)
    assert (dy[0] == dy[1]).all_eq(True)

    # Test (3)
    assert (dy[2] == dy[3]).all_eq(True)

    # Test (4)
    assert col0 == pytest.approx(-1.0 * dy[2].matrix_copy_column(0).reversed())

    # Test (1) for panel 2 as well, which then covers everything needed
    col0 = dy[2].matrix_copy_column(0)
    for i in range(1, 50):
        assert (col0 == dy[2].matrix_copy_column(i)).all_eq(True)
Example #11
0
  def run(self):
    '''Execute the script.'''
    from dials.array_family import flex
    from dials.util.options import flatten_datablocks
    from time import time
    from dials.util import log
    from libtbx.utils import Sorry
    start_time = time()

    # Parse the command line
    params, options = self.parser.parse_args(show_diff_phil=False)

    # Configure the logging
    log.config(
      params.verbosity,
      info=params.output.log,
      debug=params.output.debug_log)

    from dials.util.version import dials_version
    logger.info(dials_version())

    # Log the diff phil
    diff_phil = self.parser.diff_phil.as_str()
    if diff_phil is not '':
      logger.info('The following parameters have been modified:\n')
      logger.info(diff_phil)

    # Ensure we have a data block
    datablocks = flatten_datablocks(params.input.datablock)
    if len(datablocks) == 0:
      self.parser.print_help()
      return
    elif len(datablocks) != 1:
      raise Sorry('only 1 datablock can be processed at a time')

    # Loop through all the imagesets and find the strong spots
    reflections = flex.reflection_table.from_observations(
      datablocks[0], params)

    # Delete the shoeboxes
    if not params.output.shoeboxes:
      del reflections['shoebox']

    # ascii spot count per image plot
    from dials.util.ascii_art import spot_counts_per_image_plot

    for i, imageset in enumerate(datablocks[0].extract_imagesets()):
      ascii_plot = spot_counts_per_image_plot(
        reflections.select(reflections['id'] == i))
      if len(ascii_plot):
        logger.info('\nHistogram of per-image spot count for imageset %i:' %i)
        logger.info(ascii_plot)

    # Save the reflections to file
    logger.info('\n' + '-' * 80)
    reflections.as_pickle(params.output.reflections)
    logger.info('Saved {0} reflections to {1}'.format(
        len(reflections), params.output.reflections))

    # Save the datablock
    if params.output.datablock:
      from dxtbx.datablock import DataBlockDumper
      logger.info('Saving datablocks to {0}'.format(
        params.output.datablock))
      dump = DataBlockDumper(datablocks)
      dump.as_file(params.output.datablock)

    # Print some per image statistics
    if params.per_image_statistics:
      from dials.algorithms.spot_finding import per_image_analysis
      from cStringIO import StringIO
      s = StringIO()
      for i, imageset in enumerate(datablocks[0].extract_imagesets()):
        print >> s, "Number of centroids per image for imageset %i:" %i
        stats = per_image_analysis.stats_imageset(
          imageset, reflections.select(reflections['id'] == i),
          resolution_analysis=False)
        per_image_analysis.print_table(stats, out=s)
      logger.info(s.getvalue())

    # Print the time
    logger.info("Time Taken: %f" % (time() - start_time))
Example #12
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])
Example #13
0
from __future__ import absolute_import, division, print_function
from dxtbx.datablock import DataBlockFactory, DataBlockDumper
import sys
from six.moves import range

fin = sys.argv[1]
px_size = 0.05  # in mm
dpx = [0, 0, 1, 1, 0, 1, 1, 0]  # in px
dpy = [0, 0, 1, 1, 0, 1, 1, 0]  # in px

db = DataBlockFactory.from_json_file(fin)
db0 = db[0]
dd = db0.to_dict()
for i in range(8):
    x = dd['detector'][0]['panels'][i]['origin'][0] + dpx[i] * px_size
    y = dd['detector'][0]['panels'][i]['origin'][1] + dpy[i] * px_size
    dd['detector'][0]['panels'][i]['origin'] = (x, y, 0)
xx = DataBlockFactory.from_dict(dd)
yy = DataBlockDumper(xx)
yy.as_file('new_detector_geom.json')
Example #14
0
def datablock(obj, outfile, **kwargs):
    """Dump the given object to file."""
    dump = DataBlockDumper(obj)
    dump.as_file(outfile, **kwargs)
Example #15
0
def datablock(obj, outfile, **kwargs):
  ''' Dump the given object to file. '''
  from dxtbx.datablock import DataBlockDumper
  dump = DataBlockDumper(obj)
  dump.as_file(outfile, **kwargs)
    if not stills:
      num_stills = 0
    else:
      num_stills = len(stills)

    # Print some data block info
    print "-" * 80
    print "DataBlock %d" % i
    print "  format: %s" % str(datablock.format_class())
    print "  num images: %d" % datablock.num_images()
    print "  num sweeps: %d" % len(sweeps)
    print "  num stills: %d" % num_stills

    # Loop through all the sweeps
    if options.verbose > 1:
      for j, sweep in enumerate(sweeps):
        print ""
        print "Sweep %d" % j
        print "  length %d" % len(sweep)
        print sweep.get_beam()
        print sweep.get_goniometer()
        print sweep.get_detector()
        print sweep.get_scan()

  # Write the datablock to a JSON or pickle file
  if options.output:
    print "-" * 80
    print 'Writing datablocks to %s' % options.output
    dump = DataBlockDumper(datablocks)
    dump.as_file(options.output, compact=options.compact)
Example #17
0
def datablock(obj, outfile, **kwargs):
    """ Dump the given object to file. """
    from dxtbx.datablock import DataBlockDumper

    dump = DataBlockDumper(obj)
    dump.as_file(outfile, **kwargs)