Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit ec7900f

Browse files
authored
Merge pull request #116 from CogStack/add-concept-search-fix
CU-862j6fpd2: Fix edge case of adding new concept without typeID
2 parents 12784ce + 36f1591 commit ec7900f

5 files changed

Lines changed: 43 additions & 18 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.2.28 on 2023-02-03 17:15
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('api', '0067_delete_concept'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='projectannotateentities',
15+
name='restrict_concept_lookup',
16+
field=models.BooleanField(default=False, help_text='Users can only search for concept terms from the list configured for the project, i.e. either from the cuis or cuis_file lists. Checking this when bothcuis and cuis_file are empty does nothing. If "add new entities" is available & added, and cuis or cuis_fileis non-empty the new CUI will be added.'),
17+
),
18+
]

webapp/api/api/models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,12 @@ class ProjectAnnotateEntities(Project):
248248
add_new_entities = models.BooleanField(default=False,
249249
help_text='Allow the creation of new terms to be added to the CDB')
250250
restrict_concept_lookup = models.BooleanField(default=False,
251-
help_text='Users can only search for concept terms from the list '
252-
'configured for the project, i.e. either from the cuis '
253-
'or cuis_file lists.')
251+
help_text='Users can only search for concept terms from the '
252+
'list configured for the project, i.e. either from '
253+
'the cuis or cuis_file lists. Checking this when both'
254+
'cuis and cuis_file are empty does nothing. If "add new '
255+
'entities" is available & added, and cuis or cuis_file'
256+
'is non-empty the new CUI will be added.')
254257
terminate_available = models.BooleanField(default=True,
255258
help_text='Enable the option to terminate concepts.')
256259
irrelevant_available = models.BooleanField(default=False,

webapp/api/api/solr_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def search_collection(cdbs: List[int], raw_query: str):
7979
parsed_doc = {
8080
'cui': str(d['cui'][0]),
8181
'pretty_name': d['pretty_name'][0],
82-
'type_ids': d['type_ids'],
82+
'type_ids': d.get('type_ids', []),
8383
'synonyms': d['synonyms']
8484
}
8585
if d.get('icd10'):

webapp/api/api/utils.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import json
2-
import os
32
import logging
3+
import os
44
from typing import Union, Dict, List, Type
55

66
import pkg_resources
7+
from django.contrib.auth.models import User
78
from django.db.models.signals import post_save
89
from django.dispatch import receiver
9-
10-
from .models import Entity, AnnotatedEntity, ICDCode, OPCSCode, ProjectAnnotateEntities, ProjectCuiCounter, \
11-
ConceptDB
12-
13-
from medcat.cdb import CDB
14-
from medcat.vocab import Vocab
1510
from medcat.cat import CAT
11+
from medcat.cdb import CDB
1612
from medcat.utils.filters import check_filters
1713
from medcat.utils.helpers import tkns_from_doc
14+
from medcat.vocab import Vocab
1815

19-
from .solr_utils import ensure_concept_searchable
16+
from .models import Entity, AnnotatedEntity, ICDCode, OPCSCode, ProjectAnnotateEntities, ProjectCuiCounter, \
17+
ConceptDB
2018

2119
log = logging.getLogger('trainer')
2220

@@ -148,7 +146,8 @@ def _remove_overlap(project, document, start, end):
148146
ann.delete()
149147

150148

151-
def create_annotation(source_val, selection_occurrence_index, cui, user, project, document, cat, icd_code=None,
149+
def create_annotation(source_val: str, selection_occurrence_index: int, cui: str, user: User,
150+
project: ProjectAnnotateEntities, document, cat: CAT, icd_code=None,
152151
opcs_code=None):
153152
text = document.text
154153
id = None
@@ -198,9 +197,6 @@ def create_annotation(source_val, selection_occurrence_index, cui, user, project
198197
ann_ent.save()
199198
id = ann_ent.id
200199

201-
# Add concept detail to SOLR search service
202-
ensure_concept_searchable(cui, cat.cdb, project.concept_db)
203-
204200
# upload icd / opcs codes if available
205201
# also expects icd / opcs addl info dicts to include:
206202
# {code: <the code>: name: <human readable desc>}

webapp/api/api/views.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
from rest_framework import viewsets
1010
from rest_framework.decorators import api_view
1111
from rest_framework.response import Response
12-
from rest_framework.viewsets import ViewSet
12+
1313

1414
from .admin import download_projects_with_text, download_projects_without_text, \
1515
import_concepts_from_cdb, upload_projects_export
1616
from .permissions import *
1717
from .serializers import *
18-
from .solr_utils import collections_available, search_collection
18+
from .solr_utils import collections_available, search_collection, ensure_concept_searchable
1919
from .utils import get_cached_medcat, \
2020
clear_cached_medcat
2121
from .utils import get_medcat, add_annotations, remove_annotations, train_medcat, create_annotation
@@ -356,6 +356,14 @@ def add_concept(request):
356356
document=document,
357357
cat=cat)
358358

359+
# ensure new concept detail is available in SOLR search service
360+
ensure_concept_searchable(cui, cat.cdb, project.concept_db)
361+
362+
# add to project cuis if required.
363+
if (project.cuis or project.cuis_file) and project.restrict_concept_lookup:
364+
project.cuis = ','.join(project.cuis.split(',') + [cui])
365+
project.save()
366+
359367
return Response({'message': 'Concept and Annotation added successfully', 'id': id})
360368

361369

0 commit comments

Comments
 (0)