def setUp(self): super(NextJobHandlerTest, self).setUp() self.exp_id = 'exp_id1' self.title = 'Testing Classifier storing' self.category = 'Test' interaction_id = 'TextInput' self.algorithm_id = feconf.INTERACTION_CLASSIFIER_MAPPING[ interaction_id]['algorithm_id'] self.training_data = [{ u'answer_group_index': 1, u'answers': [u'a1', u'a2'] }, { u'answer_group_index': 2, u'answers': [u'a2', u'a3'] }] self.job_id = classifier_models.ClassifierTrainingJobModel.create( self.algorithm_id, interaction_id, self.exp_id, 1, datetime.datetime.utcnow(), self.training_data, 'Home', feconf.TRAINING_JOB_STATUS_NEW, 1) fs_services.save_classifier_data(self.exp_id, self.job_id, {}) self.expected_response = { u'job_id': self.job_id, u'training_data': self.training_data, u'algorithm_id': self.algorithm_id } self.payload = {} self.payload['vm_id'] = feconf.DEFAULT_VM_ID secret = feconf.DEFAULT_VM_SHARED_SECRET self.payload['message'] = json.dumps({}) self.payload['signature'] = classifier.generate_signature( python_utils.convert_to_bytes(secret), self.payload['message'])
def test_remove_classifier_data(self) -> None: """Test that classifier data is removed upon deletion.""" fs_services.save_classifier_data('exp_id', 'job_id', self.classifier_data_proto) self.assertTrue(self.fs.isfile('job_id-classifier-data.pb.xz')) fs_services.delete_classifier_data('exp_id', 'job_id') self.assertFalse(self.fs.isfile('job_id-classifier-data.pb.xz'))
def test_save_and_get_classifier_data(self) -> None: """Test that classifier data is stored and retrieved correctly.""" fs_services.save_classifier_data('exp_id', 'job_id', self.classifier_data_proto) filepath = 'job_id-classifier-data.pb.xz' fs = fs_services.GcsFileSystem(feconf.ENTITY_TYPE_EXPLORATION, 'exp_id') classifier_data = utils.decompress_from_zlib(fs.get(filepath)) classifier_data_proto = text_classifier_pb2.TextClassifierFrozenModel() classifier_data_proto.ParseFromString(classifier_data) self.assertEqual(classifier_data_proto.model_json, self.classifier_data_proto.model_json)
def _create_classifier_training_job(self, algorithm_id, interaction_id, exp_id, exp_version, next_scheduled_check_time, training_data, state_name, status, classifier_data, data_schema_version): """Creates a new classifier training job model and stores classfier data in a file. """ job_id = classifier_models.ClassifierTrainingJobModel.create( algorithm_id, interaction_id, exp_id, exp_version, next_scheduled_check_time, training_data, state_name, status, data_schema_version) fs_services.save_classifier_data(exp_id, job_id, classifier_data) return job_id
def _create_classifier_training_job( self, algorithm_id, interaction_id, exp_id, exp_version, next_scheduled_check_time, training_data, state_name, status, classifier_data, algorithm_version): """Creates a new classifier training job model and stores classfier data in a file. """ job_id = classifier_models.ClassifierTrainingJobModel.create( algorithm_id, interaction_id, exp_id, exp_version, next_scheduled_check_time, training_data, state_name, status, algorithm_version) classifier_data_proto = text_classifier_pb2.TextClassifierFrozenModel() classifier_data_proto.model_json = json.dumps(classifier_data) fs_services.save_classifier_data(exp_id, job_id, classifier_data_proto) return job_id
def setUp(self): super(ClassifierTrainingJobModelValidatorTests, self).setUp() self.signup(self.OWNER_EMAIL, self.OWNER_USERNAME) self.owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) explorations = [ exp_domain.Exploration.create_default_exploration( '%s' % i, title='title %d' % i, category='category%d' % i, ) for i in python_utils.RANGE(2) ] for exp in explorations: exp.add_states(['StateTest%s' % exp.id]) exp_services.save_new_exploration(self.owner_id, exp) next_scheduled_check_time = datetime.datetime.utcnow() classifier_data_proto = text_classifier_pb2.TextClassifierFrozenModel() classifier_data_proto.model_json = json.dumps( {'classifier_data': 'data'}) id0 = classifier_models.ClassifierTrainingJobModel.create( 'TextClassifier', 'TextInput', '0', 1, next_scheduled_check_time, [{ 'answer_group_index': 1, 'answers': ['a1', 'a2'] }], 'StateTest0', feconf.TRAINING_JOB_STATUS_NEW, 1) fs_services.save_classifier_data('TextClassifier', id0, classifier_data_proto) self.model_instance_0 = ( classifier_models.ClassifierTrainingJobModel.get_by_id(id0)) id1 = classifier_models.ClassifierTrainingJobModel.create( 'TextClassifier', 'TextInput', '1', 1, next_scheduled_check_time, [{ 'answer_group_index': 1, 'answers': ['a1', 'a2'] }], 'StateTest1', feconf.TRAINING_JOB_STATUS_NEW, 1) fs_services.save_classifier_data('TextClassifier', id1, classifier_data_proto) self.model_instance_1 = ( classifier_models.ClassifierTrainingJobModel.get_by_id(id1)) self.job_class = (prod_validation_jobs_one_off. ClassifierTrainingJobModelAuditOneOffJob)
def store_classifier_data(job_id, classifier_data_proto): """Checks for the existence of the model and then updates it. Args: job_id: str. ID of the ClassifierTrainingJob domain object. classifier_data_proto: FrozenModel. The frozen model protobuf object containing result of training job that needs to be stored. Raises: Exception. The ClassifierTrainingJobModel corresponding to the job_id of the ClassifierTrainingJob does not exist. """ classifier_training_job_model = ( classifier_models.ClassifierTrainingJobModel.get(job_id, strict=False)) if not classifier_training_job_model: raise Exception( 'The ClassifierTrainingJobModel corresponding to the job_id of the ' 'ClassifierTrainingJob does not exist.') classifier_training_job = get_classifier_training_job_from_model( classifier_training_job_model) classifier_training_job.validate() fs_services.save_classifier_data(classifier_training_job_model.exp_id, job_id, classifier_data_proto)
def store_classifier_data(job_id, classifier_data): """Checks for the existence of the model and then updates it. Args: job_id: str. ID of the ClassifierTrainingJob domain object. classifier_data: dict. The classification model which needs to be stored in the job. Raises: Exception. The ClassifierTrainingJobModel corresponding to the job_id of the ClassifierTrainingJob does not exist. """ classifier_training_job_model = ( classifier_models.ClassifierTrainingJobModel.get(job_id, strict=False)) if not classifier_training_job_model: raise Exception( 'The ClassifierTrainingJobModel corresponding to the job_id of the ' 'ClassifierTrainingJob does not exist.') classifier_training_job = get_classifier_training_job_from_model( classifier_training_job_model) classifier_training_job.update_classifier_data(classifier_data) classifier_training_job.validate() fs_services.save_classifier_data(classifier_training_job_model.exp_id, job_id, classifier_data)
def test_save_and_get_classifier_data(self): """Test that classifier data is stored and retrieved correctly.""" fs_services.save_classifier_data('exp_id', 'job_id', self.classifier_data) classifier_data = fs_services.read_classifier_data('exp_id', 'job_id') self.assertEqual(classifier_data, self.classifier_data)