<commit_before>#===============================================================================
# Copyright 2012 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================

#============= enthought library imports =======================
from traits.api import Any, on_trait_change, Int, List, Bool, Instance
from pyface.timer.do_later import do_later
#============= standard library imports ========================

#============= local library imports  ==========================
from pychron.experiment.queue.base_queue import BaseExperimentQueue
from pychron.experiment.utilities.identifier import make_runid
from pychron.experiment.utilities.human_error_checker import HumanErrorChecker
from pychron.experiment.queue.experiment_queue_action import ExperimentQueueAction
from pychron.experiment.utilities.uv_human_error_checker import UVHumanErrorChecker
from pychron.core.ui.gui import invoke_in_main_thread


class ExperimentQueue(BaseExperimentQueue):
#     current_run = Any
    selected = Any
    executed_selected = Any
    dclicked = Any
    database_identifier = Int
    executed_runs = List
    executed_runs_scroll_to_row = Int
    automated_runs_scroll_to_row = Int
    linked_copy_cache = List
    queue_actions = List

    executed = Bool(False)

    human_error_checker = Instance(HumanErrorChecker, ())

    def select_run_idx(self, idx):
        if self.automated_runs:
            self.selected = self.automated_runs[idx:idx + 1]

    def reset(self):
        ans = self.automated_runs
        ens = self.executed_runs
        self.no_update = True

        finished = len(ans) == 0

        for ei in reversed(ens):
            ei.state = 'not run'
            if not ei.is_step_heat():
                ei.aliquot = 0
            elif finished:
                ei.aliquot += 1

            ei.clear_step()
            ans.insert(0, ei)

        self.executed_runs = []
        self.executed = False
        self.no_update = False

    def set_run_inprogress(self, aid):
        run = self._find_run(aid)

        # using the no_update ctx manager was not working
        # have to manually toggle _no_update

        self._no_update = True
        if run is not None:
            self.automated_runs.remove(run)
            self.executed_runs.append(run)
            idx = len(self.executed_runs) - 1
            invoke_in_main_thread(do_later, lambda: self.trait_set(executed_runs_scroll_to_row=idx))
            self.debug('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ set ex scroll to {}'.format(idx))
        else:
            self.debug('Problem removing {}'.format(aid))

        self._no_update = False

    def _find_run(self, aid):
        return next((a for a in self.automated_runs
                     if make_runid(a.labnumber, a.aliquot, a.step) == aid), None)

    def executed_paste_function(self, obj):
        return

    def paste_function(self, obj):

        ci = obj.clone_traits()
        ci.state = 'not run'
        if obj.user_defined_aliquot:
            ci.aliquot = obj.aliquot
        else:
            ci.aliquot = 0
        return ci

    @on_trait_change('automated_runs[]')
    def _refresh_info(self, new):
        if new:
            idx = self.automated_runs.index(new[-1])
            self.debug('SSSSSSSSSSSSSS set AR scroll to {}'.format(idx))
            invoke_in_main_thread(do_later, lambda: self.trait_set(automated_runs_scroll_to_row=idx))

    @on_trait_change('automated_runs:state')
    def _refresh_table1(self):
        self.refresh_table_needed = True

    def _load_meta(self, meta):
        super(ExperimentQueue, self)._load_meta(meta)
        if 'actions' in meta:
            self.queue_actions = [ExperimentQueueAction(astr)
                                  for astr in meta['actions']]

        else:
            self.debug('no actions provided for this queue')

    def _load_actions(self):<commit_msg>updated reset queues. if experiment finished increment the aliquot.<commit_after>#===============================================================================
# Copyright 2012 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================

#============= enthought library imports =======================
from traits.api import Any, on_trait_change, Int, List, Bool, Instance
from pyface.timer.do_later import do_later
#============= standard library imports ========================

#============= local library imports  ==========================
from pychron.experiment.queue.base_queue import BaseExperimentQueue
from pychron.experiment.utilities.identifier import make_runid
from pychron.experiment.utilities.human_error_checker import HumanErrorChecker
from pychron.experiment.queue.experiment_queue_action import ExperimentQueueAction
from pychron.experiment.utilities.uv_human_error_checker import UVHumanErrorChecker
from pychron.core.ui.gui import invoke_in_main_thread


class ExperimentQueue(BaseExperimentQueue):
#     current_run = Any
    selected = Any
    executed_selected = Any
    dclicked = Any
    database_identifier = Int
    executed_runs = List
    executed_runs_scroll_to_row = Int
    automated_runs_scroll_to_row = Int
    linked_copy_cache = List
    queue_actions = List

    executed = Bool(False)

    human_error_checker = Instance(HumanErrorChecker, ())

    def select_run_idx(self, idx):
        if self.automated_runs:
            self.selected = self.automated_runs[idx:idx + 1]

    def reset(self):
        """
            clear the step from the run. increment the aliquot if a step heat and experiment completed.
        """

        ens = self.executed_runs

        self._no_update = True

        finished = len(self.automated_runs) == 0
        nans = []
        # for ei in reversed(ens):
        for ei in ens:
            ei.state = 'not run'
            if not ei.is_step_heat():
                ei.aliquot = 0
            elif finished and ei.user_defined_aliquot:
                ei.user_defined_aliquot += 1

            ei.clear_step()
            nans.append(ei)
            # nans.insert(0, ei)

        nans.extend(self.automated_runs)
        self.automated_runs = nans

        self.executed_runs = []
        self.executed = False
        self._no_update = False

    def set_run_inprogress(self, aid):
        run = self._find_run(aid)

        # using the no_update ctx manager was not working
        # have to manually toggle _no_update

        self._no_update = True
        if run is not None:
            self.automated_runs.remove(run)
            self.executed_runs.append(run)
            idx = len(self.executed_runs) - 1
            invoke_in_main_thread(do_later, lambda: self.trait_set(executed_runs_scroll_to_row=idx))
            self.debug('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ set ex scroll to {}'.format(idx))
        else:
            self.debug('Problem removing {}'.format(aid))

        self._no_update = False

    def _find_run(self, aid):
        return next((a for a in self.automated_runs
                     if make_runid(a.labnumber, a.aliquot, a.step) == aid), None)

    def executed_paste_function(self, obj):
        return

    def paste_function(self, obj):

        ci = obj.clone_traits()
        ci.state = 'not run'
        if obj.user_defined_aliquot:
            ci.aliquot = obj.aliquot
        else:
            ci.aliquot = 0
        return ci

    @on_trait_change('automated_runs[]')
    def _refresh_info(self, new):
        if new and not self._no_update:
            idx = self.automated_runs.index(new[-1])
            self.debug('SSSSSSSSSSSSSS set AR scroll to {}'.format(idx))
            invoke_in_main_thread(do_later, lambda: self.trait_set(automated_runs_scroll_to_row=idx))

    @on_trait_change('automated_runs:state')
    def _refresh_table1(self):
        self.refresh_table_needed = True

    def _load_meta(self, meta):
        super(ExperimentQueue, self)._load_meta(meta)
        if 'actions' in meta:
            self.queue_actions = [ExperimentQueueAction(astr)
                                  for astr in meta['actions']]

        else:
            self.debug('no actions provided for this queue')

    def _load_actions(self):
{'fraction_non_alphanumeric': 0.14201183431952663, 'fraction_numerical': 0.003657880580957504, 'mean_word_length': 2.904241915161697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3621461', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2642, 'n_words': 683}