示例#1
0
文件: utils.py 项目: ssrl-px/iota
    def make_radial_average(self, num_bins=None, hires=None, lowres=None):
        from dials.algorithms.background import RadialAverage

        imageset = self.experiments.extract_imagesets()[0]
        beam = imageset.get_beam()
        detector = imageset.get_detector()
        scan_range = (0, len(imageset))

        summed_data = None
        summed_mask = None
        for i in range(*scan_range):
            data = imageset.get_raw_data(i)
            mask = imageset.get_mask(i)
            assert isinstance(data, tuple)
            assert isinstance(mask, tuple)
            if summed_data is None:
                summed_mask = mask
                summed_data = data
            else:
                summed_data = [
                    sd + d for sd, d in list(zip(summed_data, data))
                ]
                summed_mask = [
                    sm & m for sm, m in list(zip(summed_mask, mask))
                ]

        if num_bins is None:
            num_bins = int(sum(sum(p.get_image_size()) for p in detector) / 50)
        if lowres is not None:
            vmin = (1 / lowres)**2
        else:
            vmin = 0
        if hires is not None:
            vmax = (1 / hires)**2
        else:
            vmax = (1 / detector.get_max_resolution(beam.get_s0()))**2

        # Compute the radial average
        radial_average = RadialAverage(beam, detector, vmin, vmax, num_bins)
        for d, m in list(zip(summed_data, summed_mask)):
            radial_average.add(d.as_double() / (scan_range[1] - scan_range[0]),
                               m)
        mean = radial_average.mean()
        reso = radial_average.inv_d2()
        return mean, reso
示例#2
0
  def run(self):
    ''' Perform the integration. '''
    from dials.util.command_line import heading
    from dials.util.options import flatten_datablocks, flatten_experiments
    from dials.util import log
    from time import time
    from libtbx.utils import Sorry
    from dials.array_family import flex

    # Parse the command line
    params, options = self.parser.parse_args(show_diff_phil=False)
    experiments = flatten_experiments(params.input.experiments)
    datablocks = flatten_datablocks(params.input.datablock)
    if len(experiments) == 0 and len(datablocks) == 0:
      self.parser.print_help()
      return

    if len(datablocks) > 0:
      assert len(datablocks) == 1
      imagesets = datablocks[0].extract_imagesets()
      assert len(imagesets) == 1
      imageset = imagesets[0]
      beam = imageset.get_beam()
      detector = imageset.get_detector()
    else:
      assert len(experiments) == 1
      imageset = experiments[0].imageset
      beam = experiments[0].beam
      detector = experiments[0].detector

    # Configure logging
    log.config()

    # Set the scan range
    if params.scan_range is None:
      scan_range = (0, len(imageset))
    else:
      scan_range = params.scan_range
      i0, i1 = scan_range
      if i0 < 0 or i1 > len(imageset):
        raise RuntimeError('Scan range outside image range')
      if i0 >= i1:
        raise RuntimeError('Invalid scan range')

    summed_data = None
    summed_mask = None

    # Loop through images
    for i in range(*scan_range):
      logger.info("Reading image %d" % i)

      # Read image
      data = imageset.get_raw_data(i)
      mask = imageset.get_mask(i)
      assert isinstance(data, tuple)
      assert isinstance(mask, tuple)

      if summed_data is None:
        summed_mask = mask
        summed_data = data
      else:
        summed_data = [ sd + d for sd, d in zip(summed_data, data) ]
        summed_mask = [ sm & m for sm, m in zip(summed_mask, mask) ]

    # Compute min and max and num
    if params.num_bins is None:
      num_bins = sum(sum(p.get_image_size()) for p in detector)
    if params.d_max is None:
      vmin = 0
    else:
      vmin = (1.0 / d_max)**2
    if params.d_min is None:
      params.d_min = detector.get_max_resolution(beam.get_s0())
    vmax = (1.0 / params.d_min)**2

    # Print some info
    logger.info("Min 1/d^2: %f" % vmin)
    logger.info("Max 1/d^2: %f" % vmax)
    logger.info("Num bins:  %d" % num_bins)

    # Compute the radial average
    from dials.algorithms.background import RadialAverage
    radial_average = RadialAverage(beam, detector, vmin, vmax, num_bins)
    for d, m in zip(summed_data, summed_mask):
      radial_average.add(d.as_double(), m)
    mean = radial_average.mean()
    reso = radial_average.inv_d2()

    logger.info("Writing to %s" % params.output.filename)
    with open(params.output.filename, "w") as outfile:
      for r, m in zip(reso, mean):
        outfile.write("%f, %f\n" % (r, m))
示例#3
0
    def run(self):
        ''' Perform the integration. '''
        from dials.util.options import flatten_datablocks, flatten_experiments
        from dials.util import log

        # Parse the command line
        params, options = self.parser.parse_args(show_diff_phil=False)
        experiments = flatten_experiments(params.input.experiments)
        datablocks = flatten_datablocks(params.input.datablock)
        if len(experiments) == 0 and len(datablocks) == 0:
            self.parser.print_help()
            return

        if len(datablocks) > 0:
            assert len(datablocks) == 1
            imagesets = datablocks[0].extract_imagesets()
            assert len(imagesets) == 1
            imageset = imagesets[0]
            beam = imageset.get_beam()
            detector = imageset.get_detector()
        else:
            assert len(experiments) == 1
            imageset = experiments[0].imageset
            beam = experiments[0].beam
            detector = experiments[0].detector

        # Configure logging
        log.config()

        # Set the scan range
        if params.scan_range is None:
            scan_range = (0, len(imageset))
        else:
            scan_range = params.scan_range
            i0, i1 = scan_range
            if i0 < 0 or i1 > len(imageset):
                raise RuntimeError('Scan range outside image range')
            if i0 >= i1:
                raise RuntimeError('Invalid scan range')

        summed_data = None
        summed_mask = None

        # Loop through images
        for i in range(*scan_range):
            logger.info("Reading image %d" % i)

            # Read image
            data = imageset.get_raw_data(i)
            mask = imageset.get_mask(i)
            assert isinstance(data, tuple)
            assert isinstance(mask, tuple)

            if summed_data is None:
                summed_mask = mask
                summed_data = data
            else:
                summed_data = [sd + d for sd, d in zip(summed_data, data)]
                summed_mask = [sm & m for sm, m in zip(summed_mask, mask)]

        # Compute min and max and num
        if params.num_bins is None:
            num_bins = sum(sum(p.get_image_size()) for p in detector)
        if params.d_max is None:
            vmin = 0
        else:
            vmin = (1.0 / d_max)**2
        if params.d_min is None:
            params.d_min = detector.get_max_resolution(beam.get_s0())
        vmax = (1.0 / params.d_min)**2

        # Print some info
        logger.info("Min 1/d^2: %f" % vmin)
        logger.info("Max 1/d^2: %f" % vmax)
        logger.info("Num bins:  %d" % num_bins)

        # Compute the radial average
        from dials.algorithms.background import RadialAverage
        radial_average = RadialAverage(beam, detector, vmin, vmax, num_bins)
        for d, m in zip(summed_data, summed_mask):
            radial_average.add(d.as_double() / (scan_range[1] - scan_range[0]),
                               m)
        mean = radial_average.mean()
        reso = radial_average.inv_d2()

        logger.info("Writing to %s" % params.output.filename)
        with open(params.output.filename, "w") as outfile:
            for r, m in zip(reso, mean):
                outfile.write("%f, %f\n" % (r, m))