コード例 #1
0
ファイル: modeller.py プロジェクト: dials/dials
  def finalize(self, data, mask):
    '''
    Finalize the model

    :param data: The data array
    :param mask: The mask array

    '''
    from dials.algorithms.image.filter import median_filter, mean_filter
    from dials.algorithms.image.fill_holes import diffusion_fill
    from dials.algorithms.image.fill_holes import simple_fill
    from dials.array_family import flex

    # Print some image properties
    sub_data = data.as_1d().select(mask.as_1d())
    logger.info('Raw image statistics:')
    logger.info('  min:  %d' % int(flex.min(sub_data)))
    logger.info('  max:  %d' % int(flex.max(sub_data)))
    logger.info('  mean: %d' % int(flex.mean(sub_data)))
    logger.info('')

    # Transform to polar
    logger.info('Transforming image data to polar grid')
    result = self.transform.to_polar(data, mask)
    data = result.data()
    mask = result.mask()
    sub_data = data.as_1d().select(mask.as_1d())
    logger.info('Polar image statistics:')
    logger.info('  min:  %d' % int(flex.min(sub_data)))
    logger.info('  max:  %d' % int(flex.max(sub_data)))
    logger.info('  mean: %d' % int(flex.mean(sub_data)))
    logger.info('')

    # Filter the image to remove noise
    if self.kernel_size > 0:
      if self.filter_type == 'median':
        logger.info('Applying median filter')
        data = median_filter(data, mask, (self.kernel_size, 0))
        sub_data = data.as_1d().select(mask.as_1d())
        logger.info('Median polar image statistics:')
        logger.info('  min:  %d' % int(flex.min(sub_data)))
        logger.info('  max:  %d' % int(flex.max(sub_data)))
        logger.info('  mean: %d' % int(flex.mean(sub_data)))
        logger.info('')
      elif self.filter_type == 'mean':
        logger.info('Applying mean filter')
        mask_as_int = mask.as_1d().as_int()
        mask_as_int.reshape(mask.accessor())
        data = mean_filter(data, mask_as_int, (self.kernel_size, 0), 1)
        sub_data = data.as_1d().select(mask.as_1d())
        logger.info('Mean polar image statistics:')
        logger.info('  min:  %d' % int(flex.min(sub_data)))
        logger.info('  max:  %d' % int(flex.max(sub_data)))
        logger.info('  mean: %d' % int(flex.mean(sub_data)))
        logger.info('')
      else:
        raise RuntimeError('Unknown filter_type: %s' % self.filter_type)

    # Fill any remaining holes
    logger.info("Filling holes")
    data = simple_fill(data, mask)
    data = diffusion_fill(data, mask, self.niter)
    mask = flex.bool(data.accessor(), True)
    sub_data = data.as_1d().select(mask.as_1d())
    logger.info('Filled polar image statistics:')
    logger.info('  min:  %d' % int(flex.min(sub_data)))
    logger.info('  max:  %d' % int(flex.max(sub_data)))
    logger.info('  mean: %d' % int(flex.mean(sub_data)))
    logger.info('')

    # Transform back
    logger.info('Transforming image data from polar grid')
    result = self.transform.from_polar(data, mask)
    data = result.data()
    mask = result.mask()
    sub_data = data.as_1d().select(mask.as_1d())
    logger.info('Final image statistics:')
    logger.info('  min:  %d' % int(flex.min(sub_data)))
    logger.info('  max:  %d' % int(flex.max(sub_data)))
    logger.info('  mean: %d' % int(flex.mean(sub_data)))
    logger.info('')

    # Fill in any discontinuities
    # FIXME NEED TO HANDLE DISCONTINUITY
    # mask = ~self.transform.discontinuity()[:-1,:-1]
    # data = diffusion_fill(data, mask, self.niter)

    # Get and apply the mask
    mask = self.experiment.imageset.get_mask(0)[0]
    mask = mask.as_1d().as_int().as_double()
    mask.reshape(data.accessor())
    data *= mask

    # Return the result
    return data
コード例 #2
0
ファイル: modeller.py プロジェクト: jbeilstenedmands/dials
    def finalize(self, data, mask):
        """
        Finalize the model

        :param data: The data array
        :param mask: The mask array
        """
        from dials.algorithms.image.filter import median_filter, mean_filter
        from dials.algorithms.image.fill_holes import diffusion_fill
        from dials.algorithms.image.fill_holes import simple_fill
        from dials.array_family import flex

        # Print some image properties
        sub_data = data.as_1d().select(mask.as_1d())
        logger.info("Raw image statistics:")
        logger.info("  min:  %d" % int(flex.min(sub_data)))
        logger.info("  max:  %d" % int(flex.max(sub_data)))
        logger.info("  mean: %d" % int(flex.mean(sub_data)))
        logger.info("")

        # Transform to polar
        logger.info("Transforming image data to polar grid")
        result = self.transform.to_polar(data, mask)
        data = result.data()
        mask = result.mask()
        sub_data = data.as_1d().select(mask.as_1d())
        logger.info("Polar image statistics:")
        logger.info("  min:  %d" % int(flex.min(sub_data)))
        logger.info("  max:  %d" % int(flex.max(sub_data)))
        logger.info("  mean: %d" % int(flex.mean(sub_data)))
        logger.info("")

        # Filter the image to remove noise
        if self.kernel_size > 0:
            if self.filter_type == "median":
                logger.info("Applying median filter")
                data = median_filter(data,
                                     mask, (self.kernel_size, 0),
                                     periodic=True)
                sub_data = data.as_1d().select(mask.as_1d())
                logger.info("Median polar image statistics:")
                logger.info("  min:  %d" % int(flex.min(sub_data)))
                logger.info("  max:  %d" % int(flex.max(sub_data)))
                logger.info("  mean: %d" % int(flex.mean(sub_data)))
                logger.info("")
            elif self.filter_type == "mean":
                logger.info("Applying mean filter")
                mask_as_int = mask.as_1d().as_int()
                mask_as_int.reshape(mask.accessor())
                data = mean_filter(data, mask_as_int, (self.kernel_size, 0), 1)
                sub_data = data.as_1d().select(mask.as_1d())
                logger.info("Mean polar image statistics:")
                logger.info("  min:  %d" % int(flex.min(sub_data)))
                logger.info("  max:  %d" % int(flex.max(sub_data)))
                logger.info("  mean: %d" % int(flex.mean(sub_data)))
                logger.info("")
            else:
                raise RuntimeError("Unknown filter_type: %s" %
                                   self.filter_type)

        # Fill any remaining holes
        logger.info("Filling holes")
        data = simple_fill(data, mask)
        data = diffusion_fill(data, mask, self.niter)
        mask = flex.bool(data.accessor(), True)
        sub_data = data.as_1d().select(mask.as_1d())
        logger.info("Filled polar image statistics:")
        logger.info("  min:  %d" % int(flex.min(sub_data)))
        logger.info("  max:  %d" % int(flex.max(sub_data)))
        logger.info("  mean: %d" % int(flex.mean(sub_data)))
        logger.info("")

        # Transform back
        logger.info("Transforming image data from polar grid")
        result = self.transform.from_polar(data, mask)
        data = result.data()
        mask = result.mask()
        sub_data = data.as_1d().select(mask.as_1d())
        logger.info("Final image statistics:")
        logger.info("  min:  %d" % int(flex.min(sub_data)))
        logger.info("  max:  %d" % int(flex.max(sub_data)))
        logger.info("  mean: %d" % int(flex.mean(sub_data)))
        logger.info("")

        # Fill in any discontinuities
        mask = ~self.transform.discontinuity()[:-1, :-1]
        data = diffusion_fill(data, mask, self.niter)

        # Get and apply the mask
        mask = self.experiment.imageset.get_mask(0)[0]
        mask = mask.as_1d().as_int().as_double()
        mask.reshape(data.accessor())
        data *= mask

        # Return the result
        return data