<commit_before>import logging
from collections import defaultdict

import tmlib.models as tm
from tmlib.image_utils import mip
from tmlib.utils import notimplemented
from tmlib.utils import same_docstring_as
from tmlib.errors import NotSupportedError
from tmlib.workflow.align import registration as reg
from tmlib.workflow.api import ClusterRoutines
from tmlib.workflow.registry import api

logger = logging.getLogger(__name__)


@api('align')
class ImageRegistrator(ClusterRoutines):

    '''Class for registering and aligning images between cycles.
    '''

    def __init__(self, experiment_id, verbosity):
        '''
        Parameters
        ----------
        experiment_id: int
            ID of the processed experiment
        verbosity: int
            logging level
        '''
        super(ImageRegistrator, self).__init__(experiment_id, verbosity)

    def create_batches(self, args):
        '''Creates job descriptions for parallel computing.

        Parameters
        ----------
        args: tmlib.align.args.AlignInitArgs
            step-specific arguments

        Returns
        -------
        Dict[str, List[dict] or dict]
            job descriptions

        Raises
        ------
        tmlib.errors.NotSupportedError
            when a plate contains only one cycle
        ValueError
            when `args.ref_wavelength` does not exist across all cycles
        '''
        job_count = 0
        job_descriptions = dict()
        job_descriptions['run'] = list()

        with tm.utils.Session() as session:

            for plate in session.query(tm.Plate).\
                    filter_by(experiment_id=self.experiment_id):

                if not(len(plate.cycles) > 1):
                    raise NotSupportedError(
                        'Alignment requires more than one cycle.'
                    )

                sites = session.query(tm.Site).\
                    join(tm.Well).\
                    join(tm.Plate).\
                    filter(tm.Plate.id == plate.id).\
                    all()

                site_ids = [s.id for s in sites]
                batches = self._create_batches(site_ids, args.batch_size)
                for batch in batches:

                    job_count += 1
                    input_ids = {
                        'reference_file_ids': list(),
                        'target_file_ids': defaultdict(list)
                    }

                    for cycle in plate.cycles:

                        for s in batch:

                            files = session.query(
                                    tm.ChannelImageFile
                                ).\
                                join(tm.Site).\
                                join(tm.Cycle).\
                                join(tm.Channel).\<commit_msg>Change order of sites in batches of align step<commit_after>import logging
from collections import defaultdict

import tmlib.models as tm
from tmlib.image_utils import mip
from tmlib.utils import notimplemented
from tmlib.utils import same_docstring_as
from tmlib.errors import NotSupportedError
from tmlib.workflow.align import registration as reg
from tmlib.workflow.api import ClusterRoutines
from tmlib.workflow.registry import api

logger = logging.getLogger(__name__)


@api('align')
class ImageRegistrator(ClusterRoutines):

    '''Class for registering and aligning images between cycles.
    '''

    def __init__(self, experiment_id, verbosity):
        '''
        Parameters
        ----------
        experiment_id: int
            ID of the processed experiment
        verbosity: int
            logging level
        '''
        super(ImageRegistrator, self).__init__(experiment_id, verbosity)

    def create_batches(self, args):
        '''Creates job descriptions for parallel computing.

        Parameters
        ----------
        args: tmlib.align.args.AlignInitArgs
            step-specific arguments

        Returns
        -------
        Dict[str, List[dict] or dict]
            job descriptions

        Raises
        ------
        tmlib.errors.NotSupportedError
            when a plate contains only one cycle
        ValueError
            when `args.ref_wavelength` does not exist across all cycles
        '''
        job_count = 0
        job_descriptions = dict()
        job_descriptions['run'] = list()

        with tm.utils.Session() as session:

            for plate in session.query(tm.Plate).\
                    filter_by(experiment_id=self.experiment_id):

                if not(len(plate.cycles) > 1):
                    raise NotSupportedError(
                        'Alignment requires more than one cycle.'
                    )

                site_ids = session.query(tm.Site.id).\
                    join(tm.Well).\
                    join(tm.Plate).\
                    filter(tm.Plate.id == plate.id).\
                    order_by(tm.Site.id).\
                    all()

                batches = self._create_batches(site_ids, args.batch_size)
                for batch in batches:

                    job_count += 1
                    input_ids = {
                        'reference_file_ids': list(),
                        'target_file_ids': defaultdict(list)
                    }

                    for cycle in plate.cycles:

                        for s in batch:

                            files = session.query(
                                    tm.ChannelImageFile
                                ).\
                                join(tm.Site).\
                                join(tm.Cycle).\
                                join(tm.Channel).\
{'fraction_non_alphanumeric': 0.08231545406266595, 'fraction_numerical': 0.0010621348911311736, 'mean_word_length': 1.3669878508588187, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2991431', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1335, 'n_words': 358}