Skip to content

Commit

Permalink
interim commit for meta anno pred integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
tomolopolis committed Aug 18, 2024
1 parent 3938c4a commit 0fb9ae8
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 20 deletions.
41 changes: 32 additions & 9 deletions webapp/api/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from medcat.vocab import Vocab

from .models import Entity, AnnotatedEntity, ProjectAnnotateEntities, \
ConceptDB, MetaAnnotation
ConceptDB, MetaAnnotation, MetaTask

log = logging.getLogger('trainer')

Expand All @@ -41,6 +41,19 @@ def add_annotations(spacy_doc, user, project, document, existing_annotations, ca
tkns_in = []
ents = []
existing_annos_intervals = [(ann.start_ind, ann.end_ind) for ann in existing_annotations]
# all MetaTasks and associated values
# that can be produced are expected to have available models
try:
metatask2obj = {task_name: MetaTask.objects.get(name=task_name)
for task_name in spacy_doc._.ents[0]._.meta_anns.keys()}
metataskvals2obj = {task_name: {v.name: v for v in MetaTask.objects.get(name=task_name).values.all()}
for task_name in spacy_doc._.ents[0]._.meta_anns.keys()}
except AttributeError:
# ignore meta_anns that are not present - i.e. non model pack preds,
# or model pack preds with no meta_anns
metatask2obj = {}
metataskvals2obj = {}
pass

def check_ents(ent):
return any((ea[0] < ent.start_char < ea[1]) or
Expand All @@ -56,10 +69,6 @@ def check_ents(ent):
for tkn in ent:
tkns_in.append(tkn)
ents.append(ent)
# check the ent._.meta_anns if it exists
# meta_anno_objs = MetaAnnotation.objects
# for meta_ann in ent._.meta_anns:
# MetaAnnotation.objects.filter(meta_ann)

for ent in ents:
label = ent._.cui
Expand All @@ -72,10 +81,11 @@ def check_ents(ent):
else:
entity = Entity.objects.get(label=label)

if AnnotatedEntity.objects.filter(project=project,
document=document,
start_ind=ent.start_char,
end_ind=ent.end_char).count() == 0:
ann_ent = AnnotatedEntity.objects.filter(project=project,
document=document,
start_ind=ent.start_char,
end_ind=ent.end_char).first()
if ann_ent is None:
# If this entity doesn't exist already
ann_ent = AnnotatedEntity()
ann_ent.user = user
Expand All @@ -94,6 +104,19 @@ def check_ents(ent):

ann_ent.save()

# check the ent._.meta_anns if it exists
for meta_ann_task, pred in ent._.meta_anns.items():
# raise exception here if got here with no metatask2obj or vals present
meta_anno_obj = MetaAnnotation.objects.filter(annotated_entity=ann_ent,
meta_task=metatask2obj[meta_ann_task]).first()
if meta_anno_obj is None or not meta_anno_obj.validated:
meta_anno_obj = MetaAnnotation()
meta_anno_obj.meta_task = metatask2obj[meta_ann_task]
meta_anno_obj.annotated_entity = ann_ent
meta_anno_obj.meta_task_value = metataskvals2obj[meta_ann_task][pred['value']]
meta_anno_obj.acc = pred['confidence']
meta_anno_obj.save()


def get_create_cdb_infos(cdb, concept, cui, cui_info_prop, code_prop, desc_prop, model_clazz):
codes = [c[code_prop] for c in cdb.cui2info.get(cui, {}).get(cui_info_prop, []) if code_prop in c]
Expand Down
18 changes: 15 additions & 3 deletions webapp/frontend/src/components/usecases/MetaAnnotationTask.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<div class="task-name">{{task.name}}</div>
<div class="task-description">{{task.description}}</div>
<div class="task-values-container">
<button class="btn btn-outline-primary task-value" :class="{'selected': task.value === option.id}"
<button class="btn btn-outline-primary task-value"
:class="optionStyle(option)"
v-for="option of task.options" :key="option.id"
@click="selectTaskValue(option)">{{option.name}}</button>
</div>
Expand All @@ -19,6 +20,12 @@ export default {
methods: {
selectTaskValue (option) {
this.$emit('select:metaAnno', this.task, option)
},
optionStyle (option) {
return {
'selected': this.task.value === option.id && this.task.validated,
'unvalidated': this.task.value === option.id && !this.task.validated
}
}
}
}
Expand All @@ -45,8 +52,13 @@ export default {
.selected {
color: #fff;
background-color: #005EB8;
border-color: #005EB8;
background-color: $primary-alt;
border-color: $primary-alt;
}
.unvalidated {
background: lightgrey;
border: 1px solid lightgrey;
}
.task-values-container {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default {
},
mixins: [MetaAnnotationService],
props: {
modelPackSet: Boolean,
taskIDs: Array,
selectedEnt: {
type: Object,
Expand Down Expand Up @@ -64,13 +65,15 @@ export default {
const that = this
this.fetchMetaTasks(this.taskIDs, () => {
if (that.selectedEnt) {
that.fetchMetaAnnotations(that.selectedEnt)
that.fetchMetaAnnotations(that.selectedEnt, !this.modelPackSet)
}
})
},
watch: {
'taskIDs': 'fetchMetaTasks',
'selectedEnt': 'fetchMetaAnnotations'
'selectedEnt': function() {
this.fetchMetaAnnotations(this.selectedEnt, !this.modelPackSet)
}
}
}
</script>
Expand Down
7 changes: 3 additions & 4 deletions webapp/frontend/src/mixins/MetaAnnotationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ export default {
}
return this.$http.post(`/api/meta-annotations/`, payload)
},
fetchMetaAnnotations (selectedEnt, callback, useDefault) {
if (useDefault === undefined) {
useDefault = true
}
fetchMetaAnnotations (selectedEnt, useDefault, callback) {
if (this.tasks.length > 0 && selectedEnt !== null) {
for (let t of this.tasks) {
t.value = null
Expand All @@ -61,6 +58,8 @@ export default {
let r = savedTask[0]
task.value = r.meta_task_value
task.annotation_id = r.id
task.validated = r.validated
task.acc = r.acc
taskValues.push(task)
} else if (useDefault && task.default) {
// no annotation exists, should be validated and set as default value
Expand Down
3 changes: 2 additions & 1 deletion webapp/frontend/src/styles/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

$theme-colors: (
"primary": #0072CE, //NHS Bright Blue
//"primary": #005EB8, //NHS Blue
"danger": #DA291C, //NHS Emergency Services Red
"warning": #768692, // NHS Mid Grey
"info": #E8EDEE, // NHS Pale Grey
"success": #009639, // NHS Green
"light-success": #32ab60 // slightly lighter than NHS Green
);

$primary-alt: #005EB8; //NHS Blue

$navbar-bg: #0072CE; // NHS Bright Blue

// colour palette - for backgrounds, borders, text-color etc.
Expand Down
3 changes: 2 additions & 1 deletion webapp/frontend/src/views/TrainAnnotations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
</b-tabs>
</transition>
<transition name="slide-left">
<meta-annotation-task-container v-if="metaAnnotate" :taskIDs="hasMetaTasks"
<meta-annotation-task-container v-if="metaAnnotate" :model-pack-set="!!project.model_pack"
:taskIDs="hasMetaTasks"
:selectedEnt="currentEnt">
</meta-annotation-task-container>
</transition>
Expand Down

0 comments on commit 0fb9ae8

Please sign in to comment.