def __call__(self, observed, predicted):
    '''Match the observed reflections with the predicted.

    Params:
        observed The list of observed reflections.
        predicted The list of predicted reflections.

    Returns:
        The list of matched reflections

    '''
    from dials.model.data import ReflectionList

    # Find the nearest neighbours and distances
    nn, dist = self._find_nearest_neighbours(observed, predicted)

    # Filter the matches by distance
    index = self._filter_by_distance(nn, dist)

    # Copy all of the reflection data for the matched reflections
    reflections = ReflectionList()
    for i in index:
      o = observed[i]
      p = predicted[nn[i]]
      o.miller_index = p.miller_index
      o.rotation_angle = p.rotation_angle
      o.beam_vector = p.beam_vector
      o.image_coord_px = p.image_coord_px
      o.image_coord_mm = p.image_coord_mm
      o.panel_number = p.panel_number
      o.frame_number = p.frame_number
      reflections.append(o)

    # Return the list of reflections
    return reflections
  def oneImage(self,framenumber):
    self.reporters[framenumber] = []

    from dxtbx.format.Registry import Registry
    filename = self.phil_params.distl.image[framenumber]
    reader = Registry.find(filename)
    img = reader(filename)

    detector = img.get_detector()
    beam = img.get_beam()
    S0 = beam.get_s0()
    data = img.get_raw_data()
    scan = img.get_scan()
    print scan
    if scan is None:
      print "No scan"
      RR = (0,1)
    else:
      print scan.get_oscillation()
      RR = scan.get_oscillation_range()

    from spotfinder.dxtbx_toolbox import Distl

    sfall = Distl(params = self.phil_params, detector = detector, beam = beam, data = data)

    resolutions = flex.double()
    spotlist = []
    from dials.model.data import ReflectionList,Reflection
    reflections = ReflectionList()


    for ip,panel in enumerate(detector):
      for spot in sfall.finderlist[ip].spots:
        resolutions.append( panel.get_resolution_at_pixel(S0, (spot.ctr_mass_x(), spot.ctr_mass_y())) )
        spotlist.append(spot)
        refl = Reflection()
        refl.panel_number = ip
        refl.centroid_position = (spot.ctr_mass_x(), spot.ctr_mass_y(),0.0)
        refl.centroid_variance = (0.5,0.5,0.0)
        reflections.append(refl)


    selection = (resolutions>0.0)
    if self.phil_params.distl.res.outer is not None:
      selection = (selection and (resolutions>self.phil_params.distl.res.outer))
    if self.phil_params.distl.res.inner is not None:
      selection = (selection and (resolutions<self.phil_params.distl.res.inner))

    reflections = reflections.select(selection)

    return dict(detector=detector, beam=beam, reflections=reflections, scan = scan,
                gonio = img.get_goniometer())
  def __call__(self):
    """Run the script."""
    from dials.model.serialize import load, dump
    from dials.model.data import ReflectionList
    import cPickle as pickle
    from dials.algorithms.spot_prediction import ray_intersection

    # Load the reflection list
    print 'Loading reflections from {0}'.format(self.reflections_filename)
    rlist = pickle.load(open(self.reflections_filename, 'r'))

    # Try to load the models
    print 'Loading models from {0}'.format(self.sweep_filename)

    sweep = load.sweep(open(self.sweep_filename, 'r'))
    beam = sweep.get_beam()
    wavelength = beam.get_wavelength()
    detector = sweep.get_detector()

    # get the intersections
    observations = ray_intersection(detector, rlist)

    if len(observations) != len(rlist):
      print "WARNING: not all reflections intersect the detector"

      # Why is this? Dump out the unique reflections to explore
      unique = ReflectionList()
      for r in rlist:
        try:
          obs = ray_intersection(detector, r)
        except RuntimeError:
          unique.append(r)

      unique_filename = "unique.pickle"
      print 'Those reflections that do not intersect have been saved' \
            ' to {0}'.format(unique_filename)
      pickle.dump(observations, open(unique_filename, 'wb'),
          pickle.HIGHEST_PROTOCOL)

    # update the centroid positions too
    for r in observations:
      r.centroid_position = r.image_coord_mm + (r.rotation_angle, )

    # Write out reflections
    if self.output_filename is not None:

      print 'Saving reflections to {0}'.format(self.output_filename)
      pickle.dump(observations, open(self.output_filename, 'wb'),
          pickle.HIGHEST_PROTOCOL)
    def __call__(self):
        """Run the script."""
        from dials.model.serialize import load, dump
        from dials.model.data import ReflectionList
        import cPickle as pickle
        from dials.algorithms.spot_prediction import ray_intersection

        # Load the reflection list
        print("Loading reflections from {0}".format(self.reflections_filename))
        rlist = pickle.load(open(self.reflections_filename, "r"))

        # Try to load the models
        print("Loading models from {0}".format(self.sweep_filename))

        sweep = load.sweep(open(self.sweep_filename, "r"))
        beam = sweep.get_beam()
        wavelength = beam.get_wavelength()
        detector = sweep.get_detector()

        # get the intersections
        observations = ray_intersection(detector, rlist)

        if len(observations) != len(rlist):
            print("WARNING: not all reflections intersect the detector")

            # Why is this? Dump out the unique reflections to explore
            unique = ReflectionList()
            for r in rlist:
                try:
                    obs = ray_intersection(detector, r)
                except RuntimeError:
                    unique.append(r)

            unique_filename = "unique.pickle"
            print("Those reflections that do not intersect have been saved"
                  " to {0}".format(unique_filename))
            pickle.dump(observations, open(unique_filename, "wb"),
                        pickle.HIGHEST_PROTOCOL)

        # update the centroid positions too
        for r in observations:
            r.centroid_position = r.image_coord_mm + (r.rotation_angle, )

        # Write out reflections
        if self.output_filename is not None:

            print("Saving reflections to {0}".format(self.output_filename))
            pickle.dump(observations, open(self.output_filename, "wb"),
                        pickle.HIGHEST_PROTOCOL)
Пример #5
0
    def oneImage(self, framenumber):
        self.reporters[framenumber] = []

        import dxtbx.format.Registry
        filename = self.phil_params.distl.image[framenumber]
        reader = dxtbx.format.Registry.get_format_class_for_file(filename)
        img = reader(filename)

        detector = img.get_detector()
        beam = img.get_beam()
        S0 = beam.get_s0()
        data = img.get_raw_data()
        scan = img.get_scan()
        print(scan)
        if scan is None:
            print("No scan")
            RR = (0, 1)
        else:
            print(scan.get_oscillation())
            RR = scan.get_oscillation_range()

        from spotfinder.dxtbx_toolbox import Distl

        sfall = Distl(params=self.phil_params,
                      detector=detector,
                      beam=beam,
                      data=data)

        resolutions = flex.double()
        spotlist = []
        from dials.model.data import ReflectionList, Reflection
        reflections = ReflectionList()

        for ip, panel in enumerate(detector):
            for spot in sfall.finderlist[ip].spots:
                resolutions.append(
                    panel.get_resolution_at_pixel(
                        S0, (spot.ctr_mass_x(), spot.ctr_mass_y())))
                spotlist.append(spot)
                refl = Reflection()
                refl.panel_number = ip
                refl.centroid_position = (spot.ctr_mass_x(), spot.ctr_mass_y(),
                                          0.0)
                refl.centroid_variance = (0.5, 0.5, 0.0)
                reflections.append(refl)

        selection = (resolutions > 0.0)
        if self.phil_params.distl.res.outer is not None:
            selection = (selection
                         and (resolutions > self.phil_params.distl.res.outer))
        if self.phil_params.distl.res.inner is not None:
            selection = (selection
                         and (resolutions < self.phil_params.distl.res.inner))

        reflections = reflections.select(selection)

        return dict(detector=detector,
                    beam=beam,
                    reflections=reflections,
                    scan=scan,
                    gonio=img.get_goniometer())
Пример #6
0
    def main(self):
        # FIXME import simulation code
        import six.moves.cPickle as pickle
        import math
        from dials.util.command_line import Importer
        from dials.algorithms.integration import ReflectionPredictor
        from libtbx.utils import Sorry

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

        importer = Importer(args)
        if len(importer.imagesets) == 0 and len(importer.crystals) == 0:
            self.config().print_help()
            return
        if len(importer.imagesets) != 1:
            raise Sorry('need 1 sweep: %d given' % len(importer.imagesets))
        if len(importer.crystals) != 1:
            raise Sorry('need 1 crystal: %d given' % len(importer.crystals))
        sweep = importer.imagesets[0]
        crystal = importer.crystals[0]

        # generate predictions for possible reflections => generate a
        # reflection list

        predict = ReflectionPredictor()
        predicted = predict(sweep, crystal)

        # sort with James's reflection table: should this not go somewhere central?
        from dials.scratch.jmp.container.reflection_table import ReflectionTable

        # calculate shoebox sizes: take parameters from params & transform
        # from reciprocal space to image space to decide how big a shoe box to use

        table = ReflectionTable()
        table['miller_index'] = predicted.miller_index()
        indexer = table.index_map('miller_index')

        candidates = []

        unique = sorted(indexer)

        for h, k, l in unique:

            try:
                for _h in h - 1, h + 1:
                    if not indexer[(_h, k, l)]:
                        raise ValueError('missing')
                for _k in k - 1, k + 1:
                    if not indexer[(h, _k, l)]:
                        raise ValueError('missing')
                for _l in l - 1, l + 1:
                    if not indexer[(h, k, _l)]:
                        raise ValueError('missing')
                candidates.append((h, k, l))
            except ValueError:
                continue

        from dials.algorithms.simulation.utils import build_prediction_matrix

        from dials.algorithms.simulation.generate_test_reflections import \
         master_phil
        from libtbx.phil import command_line
        cmd = command_line.argument_interpreter(master_params=master_phil)
        working_phil = cmd.process_and_fetch(args=args[2:])
        params = working_phil.extract()

        node_size = params.rs_node_size
        window_size = params.rs_window_size
        reference = params.integrated_data_file
        scale = params.integrated_data_file_scale

        if reference:
            counts_database = {}
            from iotbx import mtz
            m = mtz.object(reference)
            mi = m.extract_miller_indices()
            i = m.extract_reals('IMEAN').data
            s = m.space_group().build_derived_point_group()
            for j in range(len(mi)):
                for op in s.all_ops():
                    hkl = tuple(map(int, op * mi[j]))
                    counts = max(0, int(math.floor(i[j] * scale)))
                    counts_database[hkl] = counts
                    counts_database[(-hkl[0], -hkl[1], -hkl[2])] = counts
        else:

            def constant_factory(value):
                import itertools
                return itertools.repeat(value).next

            from collections import defaultdict
            counts_database = defaultdict(constant_factory(params.counts))

        from dials.model.data import ReflectionList

        useful = ReflectionList()
        d_matrices = []

        for h, k, l in candidates:
            hkl = predicted[indexer[(h, k, l)][0]]
            _x = hkl.image_coord_px[0]
            _y = hkl.image_coord_px[1]
            _z = hkl.frame_number

            # build prediction matrix
            mhkl = predicted[indexer[(h - 1, k, l)][0]]
            phkl = predicted[indexer[(h + 1, k, l)][0]]
            hmkl = predicted[indexer[(h, k - 1, l)][0]]
            hpkl = predicted[indexer[(h, k + 1, l)][0]]
            hkml = predicted[indexer[(h, k, l - 1)][0]]
            hkpl = predicted[indexer[(h, k, l + 1)][0]]
            d = build_prediction_matrix(hkl, mhkl, phkl, hmkl, hpkl, hkml,
                                        hkpl)
            d_matrices.append(d)

            # construct the shoebox parameters: outline the ellipsoid
            x, y, z = [], [], []

            for dh in (1, 0, 0), (0, 1, 0), (0, 0, 1):
                dxyz = -1 * window_size * d * dh
                x.append(dxyz[0] + _x)
                y.append(dxyz[1] + _y)
                z.append(dxyz[2] + _z)
                dxyz = window_size * d * dh
                x.append(dxyz[0] + _x)
                y.append(dxyz[1] + _y)
                z.append(dxyz[2] + _z)

            hkl.bounding_box = (int(math.floor(min(x))),
                                int(math.floor(max(x)) + 1),
                                int(math.floor(min(y))),
                                int(math.floor(max(y)) + 1),
                                int(math.floor(min(z))),
                                int(math.floor(max(z)) + 1))
            try:
                counts = counts_database[hkl.miller_index]
                useful.append(hkl)
            except KeyError:
                continue

        from dials.algorithms import shoebox
        shoebox.allocate(useful)

        from dials.util.command_line import ProgressBar
        p = ProgressBar(title='Generating shoeboxes')

        # now for each reflection perform the simulation
        for j, refl in enumerate(useful):
            p.update(j * 100.0 / len(useful))
            d = d_matrices[j]

            from scitbx.random import variate, normal_distribution
            g = variate(normal_distribution(mean=0, sigma=node_size))
            counts = counts_database[refl.miller_index]
            dhs = g(counts)
            dks = g(counts)
            dls = g(counts)
            self.map_to_image_space(refl, d, dhs, dks, dls)

        p.finished('Generated %d shoeboxes' % len(useful))

        # now for each reflection add background
        from dials.algorithms.simulation.generate_test_reflections import \
         random_background_plane

        p = ProgressBar(title='Generating background')
        for j, refl in enumerate(useful):
            p.update(j * 100.0 / len(useful))
            if params.background:
                random_background_plane(refl.shoebox, params.background, 0.0,
                                        0.0, 0.0)
            else:
                random_background_plane(refl.shoebox, params.background_a,
                                        params.background_b,
                                        params.background_c,
                                        params.background_d)

        p.finished('Generated %d backgrounds' % len(useful))
        if params.output.all:
            with open(params.output.all, 'wb') as fh:
                pickle.dump(useful, fh, pickle.HIGHEST_PROTOCOL)
Пример #7
0
from __future__ import division
from __future__ import print_function

from scitbx.array_family import flex
from dials.model.data import Reflection, ReflectionList
from dials.algorithms import shoebox

rl = ReflectionList()
r1 = Reflection()
r1.shoebox = (10, 20, 10, 20, 10, 20)
r2 = Reflection()
r2.shoebox = (15, 25, 15, 25, 15, 25)
r3 = Reflection()
r3.shoebox = (20, 30, 20, 30, 20, 30)
rl.append(r1)
rl.append(r2)
rl.append(r3)
overlapping = shoebox.find_overlapping(rl)

for e in overlapping.edges():
    print("Edge: ", overlapping.edge_vertices(e))

for v in overlapping.vertices():
    print("Vertex: ", v, " => ", [a for a in overlapping.adjacent_vertices(v)])
    data2d[0,row, col] += row * 2
    data2d[0,row, col] += col * 2

mask2d = flex.int(flex.grid(1, 3, 3),3)
mask2d[0, 1, 1] = 5

background2d = flex.double(flex.grid(1, 3, 3),0)
from dials.model.data import Reflection, ReflectionList
from scitbx.array_family import flex
r = Reflection()
r.shoebox = data2d
r.shoebox_mask = mask2d
r.shoebox_background = background2d

rlist = ReflectionList()
rlist.append(r)

#from dials.algorithms.background.flat_background_subtractor \
# import layering_and_background_avg
#layering_and_background_avg(rlist)

#from dials.algorithms.background.curved_background_subtractor \
# import layering_and_background_modl
#layering_and_background_modl(rlist)

from dials.algorithms.background.inclined_background_subtractor \
 import layering_and_background_plane
layering_and_background_plane(rlist)


from dials.algorithms.integration.summation2d \
Пример #9
0
from __future__ import division

from scitbx.array_family import flex
from dials.model.data import Reflection, ReflectionList
from dials.algorithms import shoebox

rl = ReflectionList()
r1 = Reflection()
r1.shoebox = (10, 20, 10, 20, 10, 20)
r2 = Reflection()
r2.shoebox = (15, 25, 15, 25, 15, 25)
r3 = Reflection()
r3.shoebox = (20, 30, 20, 30, 20, 30)
rl.append(r1)
rl.append(r2)
rl.append(r3)
overlapping = shoebox.find_overlapping(rl)

for e in overlapping.edges():
    print "Edge: ", overlapping.edge_vertices(e)

for v in overlapping.vertices():
    print "Vertex: ", v, " => ", [a for a in overlapping.adjacent_vertices(v)]
        data2d[0, row, col] += col * 2

mask2d = flex.int(flex.grid(1, 3, 3), 3)
mask2d[0, 1, 1] = 5

background2d = flex.double(flex.grid(1, 3, 3), 0)
from dials.model.data import Reflection, ReflectionList
from scitbx.array_family import flex

r = Reflection()
r.shoebox = data2d
r.shoebox_mask = mask2d
r.shoebox_background = background2d

rlist = ReflectionList()
rlist.append(r)

# from dials.algorithms.background.flat_background_subtractor \
# import layering_and_background_avg
# layering_and_background_avg(rlist)

# from dials.algorithms.background.curved_background_subtractor \
# import layering_and_background_modl
# layering_and_background_modl(rlist)

from dials.algorithms.background.inclined_background_subtractor import (
    layering_and_background_plane, )

layering_and_background_plane(rlist)

from dials.algorithms.integration.summation2d import flex_2d_layering_n_integrating