def map_icd_hierarchy(s, version=9): s = str(s) code9 = icd9.search(s) code10 = icd10.find(s) if code9 is None and code10 is None: raise Exception("Invalid ICD code", s) if version == 9: if code9 is not None: return list(reversed([code9.alt_code] + code9.ancestors())) else: raise Exception("Invalid ICD version", s) elif version == 10: if code10 is not None: return [code10.chapter, code10.block, code10.code[:3], code10.code] else: try: # Attempt to convert from version 9 to 10 s_ = convert_icd_9_to_10(code9.alt_code) code = icd10.find(s_) if code is None: # Fall back to version 9 return list(reversed([code9.alt_code] + code9.ancestors())) else: return [code.chapter, code.block, code.code[:3], code.code] except: warnings.warn('Conversion failed: ' + str(s)) return list(reversed([code9.alt_code] + code9.ancestors())) # raise Exception('Conversion error: ' + str(s)) else: raise Exception("Invalid ICD version", s)
def test_find_neoplasm(): dx = icd10.find("C10.1") assert isinstance(dx, icd10.ICD10) assert dx.code == "C101" assert dx.billable assert dx.description == "Malignant neoplasm of anterior surface of epiglottis" assert not dx.categorical assert dx.block == "C00-D48" assert dx.block_description == "Neoplasms" assert str(dx) == "C10.1" assert repr(dx) == "<ICD10: C10.1>"
def test_find_salmonella(): dx = icd10.find("A02.1") assert isinstance(dx, icd10.ICD10) assert dx.code == "A021" assert dx.billable assert dx.description == "Salmonella sepsis" assert not dx.categorical # Categorical codes are by definition not billable assert dx.block == "A00-B99" assert dx.block_description == "Certain infectious and parasitic diseases" assert str(dx) == "A02.1" assert repr(dx) == "<ICD10: A02.1>"
def find_ancestor_for_icdcode(icdcode, icdcode2ancestor): if icdcode in icdcode2ancestor: return icdcode2ancestor[icdcode] = [] ancestor = icdcode[:] while len(ancestor) > 2: ancestor = ancestor[:-1] if ancestor[-1]=='.': ancestor = ancestor[:-1] if icd10.find(ancestor) is not None: icdcode2ancestor[icdcode].append(ancestor) return
def annotate(request, from_save=False): data = request.GET sid = None if 'sid' in data: sid = data['sid'] context = {} if from_save and sid: if DocumentSet.objects.get(id=sid).done: sid = None # Tasks context['tasks'] = MetaTask.objects.all().order_by('name') project = None document = None if 'pid' in data: pid = data['pid'] project = Project.objects.get(id=pid) document = None context['document_sets'] = DocumentSet.objects.filter( project=project).order_by('name') context['pid'] = pid else: # Get first project for this user pid = Project.objects.filter(members=request.user)[0].id project = Project.objects.get(id=pid) document = None context['document_sets'] = DocumentSet.objects.filter( project=project).order_by('name') context['pid'] = pid if 'did' in data: did = int(data['did']) document = Document.objects.get(id=did) elif project is not None: if sid is not None: # Get all ds document = get_doc(project, sid=sid) else: # Get all ds document = get_doc(project) if document is not None: context['documents'] = Document.objects.filter( document_set=document.document_set).order_by('document_id') context['active_doc'] = document else: if sid is not None: ds = DocumentSet.objects.get(id=sid) document = Document.objects.filter( document_set=ds).order_by('document_id') context['documents'] = Document.objects.filter( document_set=document.document_set).order_by('document_id') context['active_doc'] = document else: ds = DocumentSet.objects.filter( project=project).order_by('name')[0] document = Document.objects.filter( document_set=ds).order_by('document_id')[0] context['documents'] = Document.objects.filter( document_set=document.document_set).order_by('document_id') context['active_doc'] = document text = context['active_doc'].text _s = context['active_doc'].start_ind if context['active_doc'].string_orig.lower() in text[_s:].lower(): print("RIGHT VAL") start = text[_s:].lower().index( context['active_doc'].string_orig.lower()) + _s end = start + len(context['active_doc'].string_orig) elif context['active_doc'].string_orig.lower() in text.lower(): print("SECOND") start = text.lower().index(context['active_doc'].string_orig.lower()) end = start + len(context['active_doc'].string_orig) else: start = 0 end = 0 s_start = max(0, start - 1000) s_end = min(len(text), end + 1000) print(s_start) text = text[s_start:start] + "<span class='ann'>" + text[ start:end] + "</span>" + text[end:s_end] text = text.replace("\n", "<br />") context['text'] = text # Info from MedCAT and some dict context['pretty_name'] = None if context['active_doc'].cui.upper() in cdb.cui2pretty_name: context['pretty_name'] = cdb.cui2pretty_name[ context['active_doc'].cui.upper()] try: context['icd'] = icd10.find( context['active_doc'].document_set.name.upper()).description context['ch'] = chapter2name[icd10.find( context['active_doc'].document_set.name.upper()).chapter] context['ch'] = icd10.find(context['active_doc'].document_set.name. upper()).chapter + " - " + context['ch'] except: context['icd'] = "None" context['ch'] = "None" doc = context['active_doc'] try: context['comment'] = Comment.objects.get(document=doc).text except Exception as e: print(e) pass document = doc if document.done: anns = MetaAnnotation.objects.filter(document=document) context['tasks'][0].real_val = False for task in context['tasks']: ann = MetaAnnotation.objects.filter(document=document, meta_task=task) if ann: ann = ann[0] task.cid = ann.meta_task_value.id return render(request, 'annotate.html', context)
def download(modeladmin, request, queryset): if not request.user.is_staff: raise PermissionDenied arr = [] tasks = MetaTask.objects.all() head = [ 'doc_id', 'string_orig', 'cui', 'icd10', 'done', 'db_id', 'icd10-ch', 'comment', 'start', 'end', '_start', '_end', 'text' ] for task in tasks: head.append(task.name) arr.append(head) dss = DocumentSet.objects.filter(project=queryset[0]) for ds in dss: docs = Document.objects.filter(document_set=ds) for doc in docs: try: comment = Comment.objects.get(document=doc).text except: comment = "" _start = doc.start_ind _end = doc.end_ind text = doc.text.lower() if doc.string_orig.lower() in text[_start:]: start = text[_start:].index(doc.string_orig.lower()) + _start end = start + len(doc.string_orig) elif doc.string_orig.lower() in text: start = text.index(doc.string_orig.lower()) end = start + len(doc.string_orig) else: start = -1 end = -1 if start != -1: row = [""] * len(head) row[0] = doc.document_id row[1] = doc.string_orig row[2] = doc.cui row[3] = ds.name row[4] = doc.done row[5] = doc.id row[6] = icd10.find(ds.name.upper()).chapter row[7] = comment row[8] = start row[9] = end row[10] = _start row[11] = _end row[12] = doc.text anns = MetaAnnotation.objects.filter(document=doc) for ann in anns: row[head.index( ann.meta_task.name)] = ann.meta_task_value.name arr.append(row) sio = StringIO() df = pandas.DataFrame(arr[1:], columns=arr[0]) df.to_csv(sio) sio.seek(0) response = HttpResponse(sio, content_type='text/csv') response['Content-Disposition'] = 'attachment; filename=stat-info.csv' return response