def _score(patient_feature):
    conn = sqlite3.connect('concept_data/omop_v5')
    c = conn.cursor()

    # print (json.dumps(patient_feature))
    results = []
    for name in os.listdir("ucb_spaceship/model"):
         if name != ".DS_Store":
             model_path = "ucb_spaceship/model/{}/".format(name)
             adapter = SpaceshipAdapter(model_path)
             top_features = adapter.top_features(patient_feature)
             score = adapter.score(patient_feature)
             statistics = adapter.model_statistics()
             # print ('--top_features and stat--')
             # print (top_features)
             # print (json.dumps(statistics))
             parsed_top_features = []
             for top_feature in top_features:
                 splited = top_feature.split(":", 4);
                 if splited[0] in ('diagnostic', 'procedure'):
                     concept_code = splited[1]
                     concept_code_value = splited[2]
                     if len(concept_code_value) > 3:
                         concept_code_value = concept_code_value[:3]+'.'+concept_code_value[3:]
                     # print ('{}'.format(concept_code))
                     # print ('{}'.format(concept_code_value))
                     if concept_code == 'icd-9-cm':
                         c.execute('SELECT concept_name FROM concept WHERE vocabulary_id="ICD9CM" and concept_code="{}"'.format(concept_code_value))
                         feature_name = c.fetchone()[0]
                         # print (feature_name)
                     elif concept_code == 'cpt':
                         c.execute('SELECT concept_name FROM concept WHERE vocabulary_id="CPT4" and concept_code="{}"'.format(concept_code_value))
                         feature_name = c.fetchone()[0]
                         # print (feature_name)
                     else:
                         feature_name = ''
                 else:
                     feature_name = splited[1]

                 parsed_top_features.append({'type':splited[0], 'name':feature_name})

             # print(parsed_top_features)
             result = {u'model':name, u'score':u'{:.0f}'.format(score*100),u'statistics':statistics,u'top_features':parsed_top_features}
             results.append(result)
    #         statistics = model_statistics(name,model_path)
    #         result = {u'model':name, u'score':u'{:.0f}'.format(score(config, model_path, patient_feature)*100),u'statistics':statistics,u'top_features':top_features}
    #         results.append(result)
    #
    conn.close()
    results = sorted(results, key=sort_results, reverse=True)

    return results
Пример #2
0
__author__ = 'suhang'

from ucb_spaceship.spaceship.export.adapter import SpaceshipAdapter
import json

model_path = 'model/ZONISAMIDE/'

adapter = SpaceshipAdapter(model_path)
print (adapter._top_features)

patient_str = '{ "attribute": [ {"name": "DOB","schema": "date_attr","value": "1976-01-01"},{"name":"gender","schema": "cat_attr","value": "F"}],"events": {"diagnostic": {"items": [{ "eventName": "icd-9-cm:7231","timestamp": "2011-07-12T00:00:00-04:00"},{"eventName": "icd-9-cm:78079","timestamp": "2011-07-12T00:00:00-04:00"},{"eventName": "icd-9-cm:3544","timestamp": "2011-07-19T00:00:00-04:00"}],"schema": "bin_event"},"medication": {"items": [{"eventName": "VENLAFAXINE_HCL","stopTime": "2013-06-27T00:00:00-04:00","timestamp": "2013-05-29T00:00:00-04:00","value": 30},{"eventName": "TOPIRAMATE","stopTime": "2013-10-31T00:00:00-04:00","timestamp": "2013-07-29T00:00:00-04:00","value": 30}],"schema": "num_event"},"procedure": {"items": [{"eventName": "cpt:99000","timestamp": "2011-01-10T00:00:00-05:00"},{"eventName": "cpt:81000","timestamp": "2011-01-10T00:00:00-05:00"}],"schema": "bin_event"}},"id": "313"}'

patient = json.loads(patient_str)

import time

begin = time.time()
print (adapter.score(patient))
print (adapter.top_features(patient))
stop = time.time()
print (stop - begin)