Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
add tuple_def_docs to phenotype API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
charhart committed Dec 14, 2021
1 parent c05bee7 commit f5f02b3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
15 changes: 9 additions & 6 deletions nlp/apis/phenotype_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def phenotype():
else:
background = True
p_cfg = PhenotypeModel.from_dict(request.get_json())
return json.dumps(post_phenotype(p_cfg, background=background), indent=4)
tuple_def_docs = get_tuple_def(p_cfg)
return json.dumps(post_phenotype(p_cfg, background=background, tuple_def_docs=tuple_def_docs), indent=4)
except Exception as ex:
log(ex)
return 'Failed to load and insert phenotype. ' + str(ex), 400
Expand All @@ -101,12 +102,13 @@ def nlpql():
"""POST to run NLPQL phenotype"""
if request.method == 'POST' and request.data:
raw_nlpql = request.data.decode("utf-8")
modified_nlpql, tuple_def_docs = tuple_processor.modify_nlpql(raw_nlpql)
if tuple_def_docs is None:
# tuple syntax error
return 'Tuple syntax error'
# modified_nlpql, tuple_def_docs = tuple_processor.modify_nlpql(raw_nlpql)
# if tuple_def_docs is None:
# # tuple syntax error
# return 'Tuple syntax error'

nlpql_results = run_nlpql_parser(modified_nlpql)
nlpql_results = run_nlpql_parser(raw_nlpql)
t
if nlpql_results['has_errors'] or nlpql_results['has_warnings']:
return json.dumps(nlpql_results)
else:
Expand All @@ -116,6 +118,7 @@ def nlpql():
else:
background = True
p_cfg = nlpql_results['phenotype']
tuple_def_docs = get_tuple_def(p_cfg)
phenotype_info = post_phenotype(p_cfg, raw_nlpql, background=background, tuple_def_docs=tuple_def_docs)
return json.dumps(phenotype_info, indent=4)

Expand Down
6 changes: 4 additions & 2 deletions nlp/data_access/phenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, name: str, action: str, data_entities: list, final: bool = Fa
raw_text=raw_text, normalized_expr=normalized_expr)



class PhenotypeEntity(dict):

def __init__(self, name: str, declaration: str, alias: str = '', version: str = '',
Expand All @@ -62,7 +63,8 @@ def __init__(self, name: str, declaration: str, alias: str = '', version: str =
description: str = '', concept: str = '', final: bool = False,
raw_text: str = '', job_results: list = None,
tuple_: bool = False, tuple_object: dict = None,
tuple_predicate: PhenotypeOperations = None
tuple_predicate: PhenotypeOperations = None,
tuple_raw_text: str = ''
):

if named_arguments is None:
Expand Down Expand Up @@ -96,7 +98,7 @@ def __init__(self, name: str, declaration: str, alias: str = '', version: str =
description=description, concept=concept, final=final,
raw_text=raw_text, job_results=job_results_init,
tuple_=tuple_, tuple_object=tuple_object_init,
tuple_predicate=tuple_predicate)
tuple_predicate=tuple_predicate, tuple_raw_text=tuple_raw_text)


class PhenotypeModel(BaseModel):
Expand Down
53 changes: 48 additions & 5 deletions nlp/nlpql/nlpql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import traceback
from data_access import PhenotypeModel, PhenotypeDefine, PhenotypeEntity, PhenotypeOperations
from claritynlp_logging import log, ERROR, DEBUG
import json

if __name__ is not None and "." in __name__:
from .nlpql_parserParser import *
Expand All @@ -11,13 +12,16 @@
from nlpql_lexer import *


def get_obj_context(obj):
def get_obj_context(obj, to_string=False):
value = dict()
for c in obj.getChildren():
if type(c) == nlpql_parserParser.PairContext:
pair = get_pair_context(c)
c_name = pair["name"]
c_value = pair["value"]
if to_string:
c_value = str(pair["value"])
else:
c_value = pair["value"]
value[c_name] = c_value
return value

Expand Down Expand Up @@ -377,14 +381,32 @@ def handle_data_entity(context, phenotype: PhenotypeModel, define_name, final):
def handle_tuple(context, phenotype: PhenotypeModel, define_name, final):
log('tuple')

obj = get_obj_context(context.getChild(0).getChild(1))
obj = get_obj_context(context.getChild(0).getChild(1), to_string=True)
num_children = len(context.children)
op_raw_text = ''
if num_children == 2:
operation = parse_operation(context.getChild(1), define_name, final)
if operation:
if not phenotype.operations:
phenotype.operations = list()
phenotype.operations.append(operation)
op_raw_text = operation.get('raw_text')

else:
operation = None

pe = PhenotypeEntity(define_name, 'define', final=final, tuple_=True, tuple_object=obj, tuple_predicate=operation)
raw_text = '''
Tuple {}
{}
'''

if not operation:
where = ''
else:
where = 'where {}'.format(op_raw_text)
tuple_str = json.dumps(obj, indent=4)
pe = PhenotypeEntity(define_name, 'define', final=final, tuple_=True, tuple_object=obj, tuple_predicate=operation,
raw_text=raw_text.format(tuple_str, where), tuple_raw_text='Tuple {}'.format(tuple_str))

if not phenotype.tuples:
phenotype.tuples = list()
Expand Down Expand Up @@ -533,7 +555,7 @@ def get_predicate_boolean(expression: nlpql_parserParser.PredicateBooleanContext
return op


def parse_operation(context, define_name, final):
def parse_operation(context, define_name, final) -> PhenotypeOperations:
expression_context = context.getChild(1)
first = expression_context.getChild(0)

Expand Down Expand Up @@ -632,6 +654,27 @@ def run_nlpql_parser(nlpql_txt: str):
return res


def get_tuple_def(phenotype: PhenotypeModel):
tuple_def_docs = list()
if phenotype.tuples:
'''
{ "_id" : ObjectId("61a97048fa06a4f85363a6fb"),
"job_id" : 87,
"nlpql_feature" : "tuple_definition",
"define_text" : "PatientTemp_Step1",
"tuple_string" : "Tuple { \"question_concept\": \"201342454\", \"answer_concept\": \"2313-4\", \"answer_value\": Temperature.value }" }
'''
for t in phenotype.tuples:
tuple_def_doc = {
'job_id': None,
'nlpql_feature': "tuple_definition",
'define_text': '{}_Step1'.format(t.get('name')),
'tuple_string': t.get('tuple_raw_text')
}
tuple_def_docs.append(tuple_def_doc)
return tuple_def_docs


if __name__ == '__main__':
with open('samples/sample3.nlpql') as f:
nlpql_txt = f.read()
Expand Down

0 comments on commit f5f02b3

Please sign in to comment.