<commit_before>#!/usr/bin/env python
#
# dials.algorithms.integration.reference_profile_factory.py
#
#  Copyright (C) 2013 Diamond Light Source
#
#  Author: <NAME>
#
#  This code is distributed under the BSD license, a copy of which is
#  included in the root directory of this package.

from __future__ import division


class ReferenceProfileCreator(object):
    ''' A class to learn reference profiles from strong spots. '''

    def __init__(self, learn=None):
        ''' Initialise the algorithm.

        Params:
            learn The profile learning strategy

        '''
        from dials.algorithms.integration import ReflectionPredictor
        from dials.algorithms.peak_finding import SpotMatcher

        # Create the reflection predictor
        self.predict = ReflectionPredictor()

        # Create the spot matcher
        self.match = SpotMatcher(max_separation=1)

        # Set the learning strategy
        self.learn = learn

    def __call__(self, sweep, crystal, strong, predicted=None):
        ''' Learn the reference profiles

        Params:
            sweep The sweep to process
            crystal The crystal to process
            strong The strong spots
            predicted The predicted spots

        Returns:
            The reference profile locator

        '''
        from dials.algorithms.integration import ReflectionExtractor

        # Predict the reflections
        if predicted == None:
            predicted = self.predict(sweep, crystal)

        # Match the predictions with the strong spots
        matches = self.match(strong, predicted)

        # Extract the reflections from the sweep
        extract = ReflectionExtractor(self.learn.bbox_nsigma)
        refl = extract(sweep, crystal, matches)

        # Learn the reference profiles
        return self.learn(sweep, crystal, refl)


class ProfileLearner(object):
    ''' A class to wrap the profile learning stuff. '''

    def __init__(self, bbox_nsigma, grid_size, threshold, frame_interval):
        ''' Configure the class.

        Params:
            bbox_nsigma The size of the bounding box
            grid_size The size of the transform grid size
            threshold The threshold level
            frame_interval The number of frames between references

        '''
        self.bbox_nsigma = bbox_nsigma
        self.frame_interval = frame_interval
        self.threshold = threshold
        self.grid_size = grid_size

    def __call__(self, sweep, crystal, reflections):
        ''' Learn the reference profiles

        Params:
            sweep The sweep class
            crystal The crystal class
            reflections The list of reflections to learn from

        Returns:
            The reference profile locator

        '''
        from dials.algorithms.reflection_basis import transform as rbt
        from dials.algorithms.integration.profile import XdsCircleSampler
        from dials.algorithms.integration.profile import XdsCircleReferenceLearner
        from dials.util.command_line import Command

        # Create the reference profile sampler
        image_size = sweep.get_detector().get_image_size()
        num_frames = sweep.get_scan().get_num_images()
        volume_size = image_size + (num_frames,)
        num_z = int(num_frames / self.frame_interval) + 1
        sampler = XdsCircleSampler(volume_size, num_z)

        # Configure the reference learner
        grid_size = (self.grid_size * 2 + 1,) * 3
        learner = XdsCircleReferenceLearner(sampler, grid_size, self.threshold)

        # Initialise the reciprocal space transform
        Command.start('Initialising reciprocal space transform')
        transform = rbt.Forward(sweep, crystal, self.bbox_nsigma, self.grid_size)
        Command.end('Initialised reciprocal space transform')

        # Transform the reflections to reciprocal space
        Command.start('Transforming reflections to reciprocal space')
        transform(reflections)
        Command.end('Transformed {0} reflections'.format(
            len([r for r in reflections if r.is_valid()])))

        # Learn the reference profiles
        Command.start('Learning reference profiles from {0} reflections'.format(
            len([r for r in reflections if r.is_valid()])))
        learner.learn(reflections)
        Command.end('Learnt {0} reference profiles from {1} reflections'.format(
            len(learner.locate()),
            len([r for r in reflections if r.is_valid()])))

#        zero_r = []
#        for r in reflections:
#            if r.is_valid():
#                locator = learner.locate()
#                idx = locator.index(r.image_coord_px + (r.frame_number,))
#                if idx == 0:
#                    zero_r.append(r)

#        from dials.model.serialize import dump
#        from dials.model.data import *
#        dump.reflections(ReflectionList(zero_r), 'zero_r.pickle')

        # Return the reference profile locator
        return learner.locate()
<commit_msg>Using new transform code in reference profile creation.<commit_after>#!/usr/bin/env python
#
# dials.algorithms.integration.reference_profile_factory.py
#
#  Copyright (C) 2013 Diamond Light Source
#
#  Author: <NAME>
#
#  This code is distributed under the BSD license, a copy of which is
#  included in the root directory of this package.

from __future__ import division


class ReferenceProfileCreator(object):
    ''' A class to learn reference profiles from strong spots. '''

    def __init__(self, learn=None):
        ''' Initialise the algorithm.

        Params:
            learn The profile learning strategy

        '''
        from dials.algorithms.integration import ReflectionPredictor
        from dials.algorithms.peak_finding import SpotMatcher

        # Create the reflection predictor
        self.predict = ReflectionPredictor()

        # Create the spot matcher
        self.match = SpotMatcher(max_separation=1)

        # Set the learning strategy
        self.learn = learn

    def __call__(self, sweep, crystal, strong, predicted=None):
        ''' Learn the reference profiles

        Params:
            sweep The sweep to process
            crystal The crystal to process
            strong The strong spots
            predicted The predicted spots

        Returns:
            The reference profile locator

        '''
        from dials.algorithms.integration import ReflectionExtractor

        # Predict the reflections
        if predicted == None:
            predicted = self.predict(sweep, crystal)

        # Match the predictions with the strong spots
        matches = self.match(strong, predicted)

        # Extract the reflections from the sweep
        extract = ReflectionExtractor(self.learn.bbox_nsigma)
        refl = extract(sweep, crystal, matches)

        # Learn the reference profiles
        return self.learn(sweep, crystal, refl)


class ProfileLearner(object):
    ''' A class to wrap the profile learning stuff. '''

    def __init__(self, bbox_nsigma, grid_size, threshold, frame_interval):
        ''' Configure the class.

        Params:
            bbox_nsigma The size of the bounding box
            grid_size The size of the transform grid size
            threshold The threshold level
            frame_interval The number of frames between references

        '''
        self.bbox_nsigma = bbox_nsigma
        self.frame_interval = frame_interval
        self.threshold = threshold
        self.grid_size = grid_size

    def __call__(self, sweep, crystal, reflections):
        ''' Learn the reference profiles

        Params:
            sweep The sweep class
            crystal The crystal class
            reflections The list of reflections to learn from

        Returns:
            The reference profile locator

        '''
        from dials.algorithms.reflection_basis import transform as rbt
        from dials.algorithms.integration.profile import XdsCircleSampler
        from dials.algorithms.integration.profile import XdsCircleReferenceLearner
        from dials.util.command_line import Command

        # Create the reference profile sampler
        image_size = sweep.get_detector().get_image_size()
        num_frames = sweep.get_scan().get_num_images()
        volume_size = image_size + (num_frames,)
        num_z = int(num_frames / self.frame_interval) + 1
        sampler = XdsCircleSampler(volume_size, num_z)

        # Configure the reference learner
        grid_size = (self.grid_size * 2 + 1,) * 3
        learner = XdsCircleReferenceLearner(sampler, grid_size, self.threshold)

        # Initialise the reciprocal space transform
        Command.start('Initialising reciprocal space transform')
        spec = rbt.TransformSpec(sweep, crystal, self.bbox_nsigma, self.grid_size)
        Command.end('Initialised reciprocal space transform')

        # Transform the reflections to reciprocal space
        Command.start('Transforming reflections to reciprocal space')
        rbt.forward_batch(spec, reflections, False)
        Command.end('Transformed {0} reflections'.format(
            len([r for r in reflections if r.is_valid()])))

        # Learn the reference profiles
        Command.start('Learning reference profiles from {0} reflections'.format(
            len([r for r in reflections if r.is_valid()])))
        learner.learn(reflections)
        Command.end('Learnt {0} reference profiles from {1} reflections'.format(
            len(learner.locate()),
            len([r for r in reflections if r.is_valid()])))

        # Return the reference profile locator
        return learner.locate()

{'fraction_non_alphanumeric': 0.07154742096505824, 'fraction_numerical': 0.002807820299500832, 'mean_word_length': 2.474349710982659, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '670644', 'n_tokens_mistral': 2556, 'n_tokens_neox': 2384, 'n_words': 788}