Example #1
0
    def test_create_multi_mappings(self):
        job_exploration_mappings = []
        job_exploration_mappings.append(
            classifier_domain.TrainingJobExplorationMapping(
                u'1', 1, 'Home', 'job_id1'))
        job_exploration_mappings.append(
            classifier_domain.TrainingJobExplorationMapping(
                u'1', 2, 'Home', 'job_id2'))

        mapping_ids = (classifier_models.TrainingJobExplorationMappingModel.
                       create_multi(job_exploration_mappings))
        self.assertEqual(len(mapping_ids), 2)

        mapping1 = (classifier_models.TrainingJobExplorationMappingModel.get(
            mapping_ids[0]))
        self.assertEqual(mapping1.exp_id, '1')
        self.assertEqual(mapping1.exp_version, 1)
        self.assertEqual(mapping1.job_id, 'job_id1')
        self.assertEqual(mapping1.state_name, 'Home')

        mapping2 = (classifier_models.TrainingJobExplorationMappingModel.get(
            mapping_ids[1]))
        self.assertEqual(mapping2.exp_id, '1')
        self.assertEqual(mapping2.exp_version, 2)
        self.assertEqual(mapping2.job_id, 'job_id2')
        self.assertEqual(mapping2.state_name, 'Home')
Example #2
0
def create_classifier_training_job_for_reverted_exploration(
        exploration, exploration_to_revert_to):
    """Create classifier training job model when an exploration is reverted.

    Args:
        exploration: Exploration. Exploration domain object.
        exploration_to_revert_to: Exploration. Exploration to revert to.
    """
    classifier_training_jobs_for_old_version = get_classifier_training_jobs(
        exploration.id, exploration_to_revert_to.version,
        list(exploration_to_revert_to.states.keys()))
    job_exploration_mappings = []
    state_names = list(exploration_to_revert_to.states.keys())
    for index, classifier_training_job in enumerate(
            classifier_training_jobs_for_old_version):
        if classifier_training_job is not None:
            state_name = state_names[index]
            job_exploration_mapping = (
                classifier_domain.TrainingJobExplorationMapping(
                    exploration.id, exploration.version + 1, state_name,
                    classifier_training_job.job_id))
            job_exploration_mapping.validate()
            job_exploration_mappings.append(job_exploration_mapping)

    classifier_models.TrainingJobExplorationMappingModel.create_multi(
        job_exploration_mappings)
def handle_non_retrainable_states(exploration, state_names, exp_versions_diff):
    """Creates new TrainingJobExplorationMappingModel instances for all the
    state names passed into the function. The mapping is created from the
    state in the new version of the exploration to the ClassifierTrainingJob of
    the state in the older version of the exploration. If there's been a change
    in the state name, we retrieve the old state name and create the mapping
    accordingly.
    This method is called only from exp_services._save_exploration() method and
    is never called from exp_services._create_exploration().
    In this method, the current_state_name refers to the name of the state in
    the current version of the exploration whereas the old_state_name refers to
    the name of the state in the previous version of the exploration.

    Args:
        exploration: Exploration. The Exploration domain object.
        state_names: list(str). List of state names.
        exp_versions_diff: ExplorationVersionsDiff. An instance of the
            exploration versions diff class.

    Raises:
        Exception. This method should not be called by exploration with version
            number 1.
    """
    exp_id = exploration.id
    current_exp_version = exploration.version
    old_exp_version = current_exp_version - 1
    if old_exp_version <= 0:
        raise Exception(
            'This method should not be called by exploration with version '
            'number %s' % (current_exp_version))

    state_names_to_retrieve = []
    for current_state_name in state_names:
        old_state_name = current_state_name
        if current_state_name in exp_versions_diff.new_to_old_state_names:
            old_state_name = exp_versions_diff.new_to_old_state_names[
                current_state_name]
        state_names_to_retrieve.append(old_state_name)
    classifier_training_jobs = get_classifier_training_jobs(
        exp_id, old_exp_version, state_names_to_retrieve)

    job_exploration_mappings = []
    for index, classifier_training_job in enumerate(classifier_training_jobs):
        if classifier_training_job is None:
            logging.error(
                'The ClassifierTrainingJobModel for the %s state of Exploration'
                ' with exp_id %s and exp_version %s does not exist.' %
                (state_names_to_retrieve[index], exp_id, old_exp_version))
            continue
        new_state_name = state_names[index]
        job_exploration_mapping = (
            classifier_domain.TrainingJobExplorationMapping(
                exp_id, current_exp_version, new_state_name,
                classifier_training_job.job_id))
        job_exploration_mapping.validate()
        job_exploration_mappings.append(job_exploration_mapping)

    classifier_models.TrainingJobExplorationMappingModel.create_multi(
        job_exploration_mappings)
Example #4
0
    def _get_mapping_from_dict(self, mapping_dict):
        """Returns the TrainingJobExplorationMapping object after receiving the
        content from the mapping_dict.
        """
        mapping = classifier_domain.TrainingJobExplorationMapping(
            mapping_dict['exp_id'], mapping_dict['exp_version'],
            mapping_dict['state_name'], mapping_dict['job_id'])

        return mapping
Example #5
0
def handle_trainable_states(exploration, state_names):
    """Creates ClassifierTrainingJobModel instances for all the state names
    passed into the function. If this function is called with version number 1,
    we are creating jobs for all trainable states in the exploration. Otherwise,
    a new job is being created for the states where retraining is required.

    Args:
        exploration: Exploration. The Exploration domain object.
        state_names: list(str). List of state names.
    """
    job_dicts_list = []
    exp_id = exploration.id
    exp_version = exploration.version
    for state_name in state_names:
        state = exploration.states[state_name]
        training_data = state.get_training_data()
        interaction_id = state.interaction.id
        algorithm_id = feconf.INTERACTION_CLASSIFIER_MAPPING[interaction_id][
            'algorithm_id']
        next_scheduled_check_time = datetime.datetime.utcnow()
        classifier_data = None
        data_schema_version = 1

        # Validate the job.
        dummy_classifier_training_job = classifier_domain.ClassifierTrainingJob(
            'job_id_dummy', algorithm_id, interaction_id, exp_id, exp_version,
            next_scheduled_check_time, state_name,
            feconf.TRAINING_JOB_STATUS_NEW, training_data, classifier_data,
            data_schema_version)
        dummy_classifier_training_job.validate()

        job_dicts_list.append({
            'algorithm_id': algorithm_id,
            'interaction_id': interaction_id,
            'exp_id': exp_id,
            'exp_version': exp_version,
            'next_scheduled_check_time': next_scheduled_check_time,
            'state_name': state_name,
            'training_data': training_data,
            'status': feconf.TRAINING_JOB_STATUS_NEW,
            'classifier_data': classifier_data,
            'data_schema_version': data_schema_version
        })

    # Create all the classifier training jobs.
    job_ids = classifier_models.ClassifierTrainingJobModel.create_multi(
        job_dicts_list)

    # Create mapping for each job. For TrainingJobExplorationMapping, we can
    # append Domain objects to send to the job_exploration_mappings dict because
    # we know all the attributes required for creating the Domain object unlike
    # ClassifierTrainingJob class where we don't know the job_id.
    job_exploration_mappings = []
    for job_id_index, job_id in enumerate(job_ids):
        job_exploration_mapping = (
            classifier_domain.TrainingJobExplorationMapping(
                job_dicts_list[job_id_index]['exp_id'],
                job_dicts_list[job_id_index]['exp_version'],
                job_dicts_list[job_id_index]['state_name'], job_id))
        job_exploration_mapping.validate()
        job_exploration_mappings.append(job_exploration_mapping)

    classifier_models.TrainingJobExplorationMappingModel.create_multi(
        job_exploration_mappings)
Example #6
0
 def _get_model_domain_object_instance(cls, item):
     return classifier_domain.TrainingJobExplorationMapping(
         item.exp_id, item.exp_version, item.state_name, item.job_id)
    def _get_mapping_from_dict(self, mapping_dict):
        mapping = classifier_domain.TrainingJobExplorationMapping(
            mapping_dict['exp_id'], mapping_dict['exp_version'],
            mapping_dict['state_name'], mapping_dict['job_id'])

        return mapping