def save_annotation_task(self, instance): # self.initial_data is the JSON received from the frontend print('save_annotation_task - start') logger.info('save_annotation_task - start') # mainly saving an annotations units array self.reset_current_task(instance) remote_units_array = [] instance.user_comment = self.initial_data['user_comment'] # save the new tokens instead of the old ones tokens, old_to_new_token_id_map= self.save_tokens(instance) all_tree_ids = [] # a list of all tree_ids by their order in the input annotation_unit_map = {} # tree_id -> annotation_unit object for au in self.initial_data['annotation_units']: annotation_unit = Annotation_Units() if is_correct_format_tree_id(au['tree_id']): annotation_unit.tree_id = au['tree_id'] all_tree_ids.append(au['tree_id']) annotation_unit_map[annotation_unit.tree_id] = annotation_unit annotation_unit.task_id = instance if au['type'] in [x[0] for x in Constants.ANNOTATION_UNIT_TYPES]: annotation_unit.type = au['type'] annotation_unit.comment = au['comment'] annotation_unit.cluster = au['cluster'] annotation_unit.is_remote_copy = au['is_remote_copy'] # parent_id = get_object_or_404(Annotation_Units, tree_id=au['parent_tree_id'],task_id=instance.id) parent_id = annotation_unit_map[au['parent_tree_id']] if au['parent_tree_id'] else None annotation_unit.parent_id = parent_id annotation_unit.gui_status = au['gui_status'] if annotation_unit.is_remote_copy: annotation_unit.remote_categories = get_value_or_none('categories', au) if au['cloned_from_tree_id']: annotation_unit.cloned_from_tree_id = au['cloned_from_tree_id'] remote_units_array.append(annotation_unit) else: # not a remote unit instance.annotation_units_set.add(annotation_unit, bulk=False) self.save_children_tokens(annotation_unit, get_value_or_none('children_tokens', au), old_to_new_token_id_map) self.save_annotation_categories(annotation_unit, get_value_or_none('categories', au)) for annotation_unit in remote_units_array: remote_unit = self.save_annotation_remote_unit(annotation_unit) self.save_remote_annotation_categories(remote_unit, annotation_unit.remote_categories) instance.save() print('save_annotation_task - end') logger.info('save_annotation_task - end')
def create(self, validated_data): ownerUser = self.initial_data['created_by'] validated_data['created_by'] = ownerUser project = get_object_or_404(Projects, pk=self.initial_data['project']['id']) annotator = get_object_or_404(Users, pk=get_value_or_none('id',get_value_or_none('user',self.initial_data))) # todo: when coarsening layer task - no need of annotator - check which one to set default? parent = None if self.initial_data['parent']: parent = get_object_or_404(Tasks, pk=get_value_or_none('id', self.initial_data['parent'])) active_obj_or_raise_exeption(parent) newTask = Tasks() newTask.created_by = ownerUser newTask.parent_task = parent newTask.status = Constants.TASK_STATUS_JSON['NOT_STARTED'] newTask.type = validated_data['type'] newTask.is_demo = validated_data['is_demo'] newTask.manager_comment = validated_data['manager_comment'] newTask.user_comment = validated_data.get('user_comment','') # Omri Abend (Sep 13) # tasks cannot be created with is_active=True if their parent is not submitted if (newTask.parent_task and newTask.parent_task.status != Constants.TASK_STATUS_JSON['SUBMITTED']): newTask.is_active = False else: newTask.is_active = validated_data['is_active'] newTask.project = project newTask.annotator = annotator if (newTask.type == Constants.TASK_TYPES_JSON['TOKENIZATION']): passage = get_object_or_404(Passages, pk=get_value_or_none('id',get_value_or_none('passage',self.initial_data))) else: passage = self.get_passage_by_parent_task(parent) newTask.passage = passage newTask.created_at = timezone.now() active_obj_or_raise_exeption(project) active_obj_or_raise_exeption(annotator) active_obj_or_raise_exeption(passage) if(newTask.type == Constants.TASK_TYPES_JSON['TOKENIZATION']): newTask.save() self.generate_and_save_tokens(newTask) elif(newTask.type == Constants.TASK_TYPES_JSON['ANNOTATION'] or (newTask.type == Constants.TASK_TYPES_JSON['REVIEW'])): if(self.has_parent_task(newTask) and self.parent_task_layer_is_my_parent_layer(newTask)): # and self.is_parent_task_submitted(newTask)): self.save_task_by_layer_type(newTask) return newTask
def save_layer_categories(self, newLayer, categories): for lc in categories: newLayerCategories = Layers_Categories() newLayerCategories.layer_id = newLayer theCategory = Categories.objects.get(pk=lc['id']) theCategory = get_object_or_404(Categories, pk=get_value_or_none('id', lc)) active_obj_or_raise_exeption(theCategory) if theCategory is not None: newLayerCategories.category_id = theCategory newLayerCategories.was_default = theCategory.is_default newLayerCategories.shortcut_key = get_value_or_none( 'shortcut_key', lc) newLayerCategories.save()
def save_derived_categories(self, newLayer, categories): if newLayer.type == Constants.LAYER_TYPES_JSON['REFINEMENT']: for dlcc in categories: # save the derived_layer_category_category newDerivedLayerCategories = Derived_Layers_Categories_Categories( ) newDerivedLayerCategories.layer_id = newLayer if dlcc['parent'] is not None and dlcc['parent'][ 'id'] is not None: parent_category = get_object_or_404(Categories, id=get_value_or_none( 'id', dlcc['parent'])) active_obj_or_raise_exeption(parent_category) if self.is_category_exsists_in_parent_layer( parent_category, newLayer.parent_layer_id): newDerivedLayerCategories.parent_category_id = parent_category child_category = get_object_or_404(Categories, id=get_value_or_none( 'id', dlcc)) active_obj_or_raise_exeption(child_category) newDerivedLayerCategories.category_id = child_category newDerivedLayerCategories.save() elif newLayer.type == Constants.LAYER_TYPES_JSON['COARSENING']: for dlcc in categories: # for parent_c_id in dlcc['parent_category']: newDerivedLayerCategories = Derived_Layers_Categories_Categories( ) newDerivedLayerCategories.layer_id = newLayer if dlcc['parent'] is not None and dlcc['parent'][ 'id'] is not None: parent_category = get_object_or_404( Categories, id=dlcc['parent']['id']) if self.is_category_exsists_in_parent_layer( parent_category, newLayer.parent_layer_id): newDerivedLayerCategories.parent_category_id = parent_category category = get_object_or_404(Categories, id=dlcc['id']) newDerivedLayerCategories.category_id = category newDerivedLayerCategories.save()
def save_annotation_task(self, instance): print('save_annotation_task - start') # mainly saving an annotations units array self.check_if_parent_task_ok_or_exception(instance) self.reset_current_task(instance) remote_units_array = [] for au in self.initial_data['annotation_units']: annotation_unit = Annotation_Units() annotation_unit.annotation_unit_tree_id = au[ 'annotation_unit_tree_id'] annotation_unit.task_id = instance annotation_unit.type = au['type'] annotation_unit.comment = au['comment'] annotation_unit.is_remote_copy = au['is_remote_copy'] parent_id = None if au['parent_id']: parent_id = get_object_or_404( Annotation_Units, annotation_unit_tree_id=au['parent_id'], task_id=instance.id) annotation_unit.parent_id = parent_id annotation_unit.gui_status = au['gui_status'] if annotation_unit.is_remote_copy == True: annotation_unit.remote_categories = get_value_or_none( 'categories', au) remote_units_array.append(annotation_unit) else: instance.annotation_units_set.add(annotation_unit, bulk=False) self.save_children_tokens( annotation_unit, get_value_or_none('children_tokens', au)) self.save_annotation_categories( annotation_unit, get_value_or_none('categories', au)) for annotation_unit in remote_units_array: remote_unit = self.save_annotation_remote_unit(annotation_unit) self.save_remote_annotation_categories( remote_unit, annotation_unit.remote_categories) print('save_annotation_task - end')
def update(self, instance, validated_data): validated_data.pop('source') # prevent update asset that used in another asset if self.is_used_in_a_task(instance) == False: updated_source = get_object_or_404(Sources, pk=get_value_or_none('id', self.initial_data['source'])) instance.source = validated_data.get('source', updated_source) instance.text = validated_data.get('text', instance.text) instance.type = validated_data.get('type', instance.type) instance.is_active = validated_data.get('is_active', instance.is_active) instance.save() return instance
def create(self, validated_data): ownerUser = self.initial_data['created_by'] validated_data['created_by'] = ownerUser source = get_object_or_404(Sources, pk= get_value_or_none('id',self.initial_data['source']) ) active_obj_or_raise_exeption(source) validated_data['source'] = source texts_array = validated_data['text'].split("<DELIMITER>") for text in texts_array: validated_data['text'] = text newPassage = Passages.objects.create(**validated_data) return newPassage
def save_annotation_task(self, instance): print('save_annotation_task - start') # mainly saving an annotations units array self.check_if_parent_task_ok_or_exception(instance) self.reset_current_task(instance) remote_units_array = [] instance.user_comment = self.initial_data['user_comment'] # validating tokens tokens = self.initial_data['tokens'] if not strictly_increasing([x['start_index'] for x in tokens]): raise TokensInvalid( "tokens should be ordered by their start_index") tokens_id_to_startindex = dict([(x['id'], x['start_index']) for x in tokens]) children_tokens_list_for_validation = [] for au in self.initial_data['annotation_units']: cur_children_tokens = au.get('children_tokens') try: if cur_children_tokens: cur_children_tokens_start_indices = [ tokens_id_to_startindex[x['id']] for x in cur_children_tokens ] else: if au['type'] == 'IMPLICIT' or au['tree_id'] == '0': cur_children_tokens_start_indices = None else: raise TokensInvalid( "Only implicit units may not have a children_tokens field" ) except KeyError: raise TokensInvalid( "children_tokens contains a token which is not in the task's tokens list." ) children_tokens_list_for_validation.append( (au['tree_id'], (au['parent_tree_id'], au['is_remote_copy'], cur_children_tokens_start_indices))) print("children_tokens_list_for_validation: " + str(cur_children_tokens_start_indices)) if not check_children_tokens(children_tokens_list_for_validation): raise TokensInvalid("Inconsistency in children_tokens detected.") all_tree_ids = [] # a list of all tree_ids by their order in the input for au in self.initial_data['annotation_units']: annotation_unit = Annotation_Units() if is_correct_format_tree_id(au['tree_id']): annotation_unit.tree_id = au['tree_id'] all_tree_ids.append(au['tree_id']) else: raise TreeIdInvalid( "tree_id is in an incorrect format; fix unit " + str(annotation_unit.tree_id)) annotation_unit.task_id = instance if au['type'] in [x[0] for x in Constants.ANNOTATION_UNIT_TYPES]: annotation_unit.type = au['type'] else: raise UnallowedValueError( "An annotation unit is given an unallowed type: " + au['type']) annotation_unit.comment = au['comment'] annotation_unit.cluster = au['cluster'] annotation_unit.is_remote_copy = au['is_remote_copy'] parent_id = None if au['parent_tree_id']: if not is_correct_format_tree_id(au['parent_tree_id']): raise TreeIdInvalid( "parent_tree_id is in an incorrect format; fix unit " + str(annotation_unit.tree_id)) if not is_correct_format_tree_id_child(au['parent_tree_id'], au['tree_id']): raise TreeIdInvalid( "parent_tree_id and tree_id do not match in format; fix unit " + str(annotation_unit.tree_id)) parent_id = get_object_or_404(Annotation_Units, tree_id=au['parent_tree_id'], task_id=instance.id) else: if annotation_unit.tree_id != '0': raise TreeIdInvalid( "All annotation units but unit 0 must have a valid, non-null tree_id; fix unit " + str(annotation_unit.tree_id)) annotation_unit.parent_id = parent_id annotation_unit.gui_status = au['gui_status'] if annotation_unit.is_remote_copy: annotation_unit.remote_categories = get_value_or_none( 'categories', au) if au['cloned_from_tree_id']: if not is_correct_format_tree_id( au['cloned_from_tree_id']): raise TreeIdInvalid( "cloned_from_tree_id is in an incorrect format; fix unit " + str(annotation_unit.tree_id)) annotation_unit.cloned_from_tree_id = au[ 'cloned_from_tree_id'] else: raise TreeIdInvalid( "cloned_from_tree_id should be defined for all remote units" ) remote_units_array.append(annotation_unit) else: # not a remote unit if au['cloned_from_tree_id']: raise TreeIdInvalid( "cloned_from_tree_id should not be defined for non-remote units" ) instance.annotation_units_set.add(annotation_unit, bulk=False) self.save_children_tokens( annotation_unit, get_value_or_none('children_tokens', au), tokens_id_to_startindex) self.save_annotation_categories( annotation_unit, get_value_or_none('categories', au)) if not is_tree_ids_uniq_and_consecutive(all_tree_ids): raise TreeIdInvalid( "tree_ids within a unit should be unique and consecutive") for annotation_unit in remote_units_array: remote_unit = self.save_annotation_remote_unit(annotation_unit) self.save_remote_annotation_categories( remote_unit, annotation_unit.remote_categories) print('save_annotation_task - end')