コード例 #1
0
  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
コード例 #2
0
  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())
コード例 #3
0
ファイル: fix_background.py プロジェクト: dials/dials_scratch
def generate_reflections(num, min_cts, max_cts, bg):
    from dials.algorithms.simulation.generate_test_reflections import (
        simple_gaussian_spots, )
    from random import randint
    from dials.model.data import ReflectionList
    from scitbx.array_family import flex
    from dials.algorithms.shoebox import MaskCode

    rlist = ReflectionList()
    expected = []
    for i in range(num):
        cts = randint(min_cts, max_cts)
        phil = create_phil(1, cts, bg)
        expected.append(cts)
        rlist.extend(simple_gaussian_spots(phil))

    #  background_inclined(rlist)
    background_xds(rlist)
    integrate_3d_summation(rlist)

    import pickle

    pickle.dump(rlist, open("test.pickle", "w"))

    #  for r in rlist:
    #    from math import sqrt, erf
    #    r.intensity *= 1.0 + (1.0 - erf(3.0 / sqrt(2.0)))

    for r, e in zip(rlist, expected):
        mask = r.shoebox_mask
        background = flex.bool([bool(m & MaskCode.Background) for m in mask])
        foreground = flex.bool([bool(m & MaskCode.Foreground) for m in mask])
        pixels = r.shoebox.select(foreground)
        print(e, r.intensity,
              flex.sum(pixels) - len(pixels) * 0, e / r.intensity)

    from math import sqrt

    I = [r.intensity for r in rlist]
    S = [sqrt(r.intensity_variance) for r in rlist]
    Z = [(i - e) / s for i, e, s in zip(I, expected, S)]
    meanz = sum(Z) / len(Z)
    sdevz = sqrt(sum((z - meanz)**2 for z in Z) / len(Z))
    print("MeanZ: %f, SDevZ: %f" % (meanz, sdevz))

    from matplotlib import pylab

    # pylab.ylim(0, 2)
    pylab.scatter(expected, [e / r.intensity for r, e in zip(rlist, expected)])
    pylab.show()
コード例 #4
0
    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 __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)
コード例 #6
0
def get_rmsds_obs_pred(observations, experiment):
    from dials.algorithms.spot_prediction import ray_intersection
    from dials.algorithms.indexing.indexer import master_params
    from dials.algorithms.refinement import RefinerFactory
    from dxtbx.model.experiment.experiment_list import ExperimentList

    master_params.refinement.reflections.close_to_spindle_cutoff = 0.001
    from dials.model.data import ReflectionList

    ref_list = ReflectionList.from_table(observations)
    ref_list = ray_intersection(experiment.detector, ref_list)
    ref_table = ref_list.to_table()
    import copy

    reflections = copy.deepcopy(observations)
    reflections["xyzcal.mm"] = ref_table["xyzcal.mm"]
    reflections["xyzcal.px"] = ref_table["xyzcal.px"]

    # XXX hack to make it work for a single lattice
    reflections["id"] = flex.int(len(reflections), 0)
    refine = RefinerFactory.from_parameters_data_experiments(master_params,
                                                             reflections,
                                                             ExperimentList(
                                                                 [experiment]),
                                                             verbosity=0)
    return refine.rmsds()
コード例 #7
0
ファイル: fix_background.py プロジェクト: dials/dials_scratch
def generate_reflections(num, min_cts, max_cts, bg):
  from dials.algorithms.simulation.generate_test_reflections import simple_gaussian_spots
  from random import randint
  from dials.model.data import ReflectionList
  from scitbx.array_family import flex
  from dials.algorithms.shoebox import MaskCode
  rlist = ReflectionList()
  expected = []
  for i in range(num):
    cts = randint(min_cts, max_cts)
    phil = create_phil(1, cts, bg)
    expected.append(cts)
    rlist.extend(simple_gaussian_spots(phil))

#  background_inclined(rlist)
  background_xds(rlist)
  integrate_3d_summation(rlist)

  import pickle
  pickle.dump(rlist, open("test.pickle", "w"))

#  for r in rlist:
#    from math import sqrt, erf
#    r.intensity *= 1.0 + (1.0 - erf(3.0 / sqrt(2.0)))


  for r, e in zip(rlist, expected):
    mask = r.shoebox_mask
    background = flex.bool([bool(m & MaskCode.Background) for m in mask])
    foreground = flex.bool([bool(m & MaskCode.Foreground) for m in mask])
    pixels = r.shoebox.select(foreground)
    print e, r.intensity, flex.sum(pixels) - len(pixels) * 0, e / r.intensity


  from math import sqrt
  I = [r.intensity for r in rlist]
  S = [sqrt(r.intensity_variance) for r in rlist]
  Z = [(i - e) / s for i, e, s in zip(I, expected, S)]
  meanz =sum(Z) / len(Z)
  sdevz =sqrt(sum((z - meanz)**2 for z in Z) / len(Z))
  print "MeanZ: %f, SDevZ: %f" % (meanz, sdevz)

  from matplotlib import pylab
  #pylab.ylim(0, 2)
  pylab.scatter(expected, [e / r.intensity for r, e in zip(rlist, expected)])
  pylab.show()
コード例 #8
0
ファイル: select_spots.py プロジェクト: dials/dials_scratch
    def _create_reflection_list(self, coords, values, spots, bbox, cpos, cvar,
                                index):
        """Create a reflection list from the spot data.

        Params:
            coords The pixel coordinates
            values The pixel values
            spots The pixel->spot mapping
            bbox The spot bounding boxes
            cpos The centroid position
            cvar The centroid variance
            index The list of valid indices

        Returns:
            A list of reflections

        """
        from dials.model.data import Reflection, ReflectionList
        from dials.algorithms.integration import allocate_reflection_profiles

        # Ensure the lengths are ok
        assert len(index) > 0
        assert len(spots) == len(bbox)
        assert len(spots) == len(cpos)
        assert len(spots) == len(cvar)

        # Create the reflection list
        reflection_list = ReflectionList(len(index))
        for i, r in zip(index, reflection_list):
            r.bounding_box = bbox[i]
            r.centroid_position = cpos[i]
            r.centroid_variance = cvar[i]

        # Allocate memory for the reflection profiles
        allocate_reflection_profiles(reflection_list,
                                     shoebox_default=0,
                                     shoebox_mask_default=0)

        # Set the pixel and mask values
        for i, r in zip(index, reflection_list):
            xs, xf, ys, yf, zs, zf = r.bounding_box
            for s in spots[i]:
                x, y, z = coords[s]
                x = x - xs
                y = y - ys
                z = z - zs
                r.shoebox[z, y, x] = values[s]
                r.shoebox_mask[z, y, x] = 1

        # Return the reflection list
        return reflection_list
コード例 #9
0
def get_rmsds_obs_pred(observations, experiment):
  from dials.algorithms.spot_prediction import ray_intersection
  from dials.algorithms.indexing.indexer import master_params
  from dials.algorithms.refinement import RefinerFactory
  from dxtbx.model.experiment.experiment_list import ExperimentList
  master_params.refinement.reflections.close_to_spindle_cutoff = 0.001
  from dials.model.data import ReflectionList
  ref_list = ReflectionList.from_table(observations)
  ref_list = ray_intersection(experiment.detector, ref_list)
  ref_table = ref_list.to_table()
  import copy
  reflections = copy.deepcopy(observations)
  reflections['xyzcal.mm'] = ref_table['xyzcal.mm']
  reflections['xyzcal.px'] = ref_table['xyzcal.px']

  # XXX hack to make it work for a single lattice
  reflections['id'] = flex.int(len(reflections), 0)
  refine = RefinerFactory.from_parameters_data_experiments(
    master_params, reflections, ExperimentList([experiment]), verbosity=0)
  return refine.rmsds()
コード例 #10
0
ファイル: simulate.py プロジェクト: kek-pf-mx/dials
    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)
コード例 #11
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())
コード例 #12
0
ファイル: simulate.py プロジェクト: cctbx-xfel/dials
class Script(object):

  def __init__(self):
    from dials.util.options import OptionParser
    from libtbx.phil import parse

    usage  = "usage: %prog [options] [param.phil] " \
             "sweep.json crystal.json intensities.mtz"

    phil_scope = parse('''
      output = simulated.pickle
        .type = str
        .help = "The filename for the simulated reflections"
    ''')

    # Create the parser
    self.parser = OptionParser(
      usage=usage,
      phil=self.phil_scope())

  @staticmethod
  def map_to_image_space(refl, d, dhs, dks, dls):
    from scitbx.array_family import flex
    d_elems = d.elems
    bb = refl.bounding_box
    dxs = d_elems[0] * dhs + d_elems[1] * dks + d_elems[2] * dls
    dys = d_elems[3] * dhs + d_elems[4] * dks + d_elems[5] * dls
    dzs = d_elems[6] * dhs + d_elems[7] * dks + d_elems[8] * dls
    xs = flex.floor(dxs + refl.image_coord_px[0]).iround() - bb[0]
    ys = flex.floor(dys + refl.image_coord_px[1]).iround() - bb[2]
    zs = flex.floor(dzs + refl.frame_number).iround() - bb[4]
    xyz = flex.vec3_int(zs, ys, xs)
    xyz = xyz.select((xs >= 0 and xs < (bb[1] - bb[0])) &
                     (ys >= 0 and ys < (bb[3] - bb[2])) &
                     (zs >= 0 and zs < (bb[5] - bb[4])))
    for _xyz in xyz:
      refl.shoebox[_xyz] += 1

    return

  def main(self):
    # FIXME import simulation code
    import 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, e:
        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, e:
        continue
コード例 #13
0
ファイル: test_problem.py プロジェクト: dials/dials_scratch
reference = load.reference("/home/upc86896/Data/TRP_M1S3_2_/reference.pickle")
# from dials.algorithms.integration import ReflectionExtractor

# extract = ReflectionExtractor(3)
# refl = extract(sweep, crystal)
# index = 104786
# print refl[index].miller_index

import pickle

# pickle.dump(refl[index], open('test_reflection', 'w'))
refl = pickle.load(open("test_reflection", "r"))

from dials.model.data import ReflectionList

ref_list = ReflectionList(1)
ref_list[0] = refl

from dials.algorithms.background import XdsSubtractor

background = XdsSubtractor(min_data=10, n_sigma=3)
background(sweep, crystal, ref_list)

r = ref_list[0]
print(r.bounding_box)

shoebox = r.shoebox
background = r.shoebox_background
mask = r.shoebox_mask

diff = shoebox - background
コード例 #14
0
        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)
コード例 #15
0
import sys
from dials.model.data import ReflectionList

filename1 = sys.argv[1]
filename2 = sys.argv[2]

from dials.model.serialize import load
from cctbx.array_family import flex

refl1 = load.reflections(filename1)
refl2 = load.reflections(filename2)

print("Length: ", len(refl1), len(refl2))
# assert(len(refl1) == len(refl2))

refl1 = ReflectionList([r for r in refl1 if r.is_valid()])
refl2 = ReflectionList([r for r in refl2 if r.is_valid()])

num_valid1 = len([r for r in refl1 if r.is_valid()])
num_valid2 = len([r for r in refl2 if r.is_valid()])
print("Valid: ", num_valid1, num_valid2)
assert num_valid1 == num_valid2
from scitbx import matrix

print("Checking")
for r1, r2 in zip(refl1, refl2):
    equal(r1.is_valid(), r2.is_valid())
    equal(r1.miller_index, r2.miller_index)
    almost_equal(r1.rotation_angle, r2.rotation_angle)
    almost_equal(matrix.col(r1.beam_vector), matrix.col(r2.beam_vector))
    almost_equal(matrix.col(r1.image_coord_px), matrix.col(r2.image_coord_px))
コード例 #16
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)]
コード例 #17
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)])
コード例 #18
0
  for col in range(3):
    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)