def run_demo_only(self,
                      demo_target,
                      demo_evidence_static=list(),
                      demo_evidence_dynamic=list(),
                      demo_evidence_val=0):
        #-- Set Up Reasoner
        reasoner = Reasoner(self.bkb, None)
        reasoner.set_src_metadata(self.patient_data_file)
        reasoner.cpp_reasoning = False

        #-- Initialize Result dict
        result = {'X': demo_evidence_val, 'Compute_time': -1}

        #-- Setup demographic evidence tuple
        if len(demo_evidence_dynamic) > 0:
            demo_evidence_dynamic_run = [
                tuple(list(demo_evidence_dynamic) + [demo_evidence_val])
            ]
        demo_evidence_run = demo_evidence_static + demo_evidence_dynamic_run

        #-- Make query and analyze
        query = Query(meta_evidence=demo_evidence_run,
                      meta_targets=[demo_target],
                      type='updating')
        query = reasoner.analyze_query(copy.deepcopy(query), save_dir=None)
        result['Compute_time'] = query.compute_time

        #-- Collect Updates and put into results
        for update, prob in query.result.updates.items():
            comp_idx, state_idx = update
            comp_name = query.bkb.getComponentName(comp_idx)
            state_name = query.bkb.getComponentINodeName(comp_idx, state_idx)

            result[('Updates', comp_name, state_name)] = prob
        return result
Example #2
0
class CrossValidator:
    def __init__(self, bkb, test_patient_hashes, patient_data_file):
        self.bkb = bkb
        self.queryCopy = None
        self.test_patient_hashes = test_patient_hashes
        self.patient_data_file = patient_data_file
        self.first = True

    def run_demo_suite(self, target, patientIDs, patientsDynamicEvidence):
        #-- Set Up Reasoner
        self.reasoner = Reasoner(self.bkb, None)
        self.reasoner.set_src_metadata(self.patient_data_file)
        self.reasoner.cpp_reasoning = False
        #queryResults = list()
        #probs = list()
        #summaries = list()
        for idx, patID in tqdm.tqdm(enumerate(patientIDs)):
            queryResults = list()
            probs = list()
            summaries = list()
            evs = list()
            probOneState = ""
            probOne = 1
            probTwoState = ""
            probTwo = 1
            badGenes = list()
            for patientDynamicEvidence in tqdm.tqdm(
                    patientsDynamicEvidence[idx]):
                prob, summary = self.run_demo_only(target, patID,
                                                   patientDynamicEvidence)
                evs.append(patientDynamicEvidence)
                queryResults.append(patID)
                probs.append(prob)
                summaries.append(summary)
            for i in range(0, len(queryResults)):
                probOneState = probs[i][0][1]
                probTwoState = probs[i][1][1]
                one = float(probs[i][0][2])
                two = float(probs[i][1][2])
                print(target, evs[i])
                print(probOneState, one, probTwoState, two,
                      "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
                print(summaries[i])
                if one == -1 and two == -1:
                    print("")
                elif one == -1 and two != -1:
                    two = 1.0
                    one = 0.0
                elif one != -1 and two == -1:
                    one = 1.0
                    two = 0
                else:
                    sum = one + two
                    one /= sum
                    two /= sum
                if one != 0 and two != 0:
                    probOne *= one
                    probTwo *= two
                    sum = probOne + probTwo
                    probOne /= sum
                    probTwo /= sum
                else:
                    badGenes.append(evs[i])
            print(patID, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            print(probOneState, probTwoState)
            print(probOne, probTwo)
            for badGene in badGenes:
                print(badGene, end='', sep=', ')
                #print(summaries[i])

    def run_demo_only(self, target, patID, evidence):
        #print(evidence)
        #print([target])
        #-- Make query and analyze
        query = Query(evidence=evidence,
                      targets=[],
                      meta_evidence=[],
                      meta_targets=[target],
                      type='updating')
        probs = list()
        summary = None
        if self.first:
            query = self.reasoner.analyze_query(copy.deepcopy(query),
                                                save_dir=None)
            self.processed_bkb = copy.deepcopy(query.bkb)
            self.first = False
            summary = query.result.summary()
            #query.result.summary()
            for update, prob in query.result.updates.items():
                comp_idx, state_idx = update
                comp_name = query.bkb.getComponentName(comp_idx)
                state_name = query.bkb.getComponentINodeName(
                    comp_idx, state_idx)
                probs.append((comp_name, state_name, prob))
        else:
            query = self.reasoner.analyze_query(
                copy.deepcopy(query), preprocessed_bkb=self.processed_bkb)
            summary = query.result.summary()
            #query.result.summary()
            for update, prob in query.result.updates.items():
                comp_idx, state_idx = update
                comp_name = query.bkb.getComponentName(comp_idx)
                state_name = query.bkb.getComponentINodeName(
                    comp_idx, state_idx)
                probs.append((comp_name, state_name, prob))

        return (probs[0], probs[1]), summary
Example #3
0
class CrossValidator:
    def __init__(self, bkb, test_patient_hashes, patient_data_file,
                 patient_dict):
        self.bkb = bkb
        self.queryCopy = None
        self.test_patient_hashes = test_patient_hashes
        self.patient_data_file = patient_data_file
        self.patient_dict = patient_dict
        self.drug = None

        self.target_strategy = 'chain'
        self.interpolation = 'independence'

    def run_demo_suite(self, target, patientIDs, patientsMutationEvidence):
        #-- Set Up Reasoner
        self.reasoner = Reasoner(self.bkb, None)
        self.reasoner.set_src_metadata(self.patient_data_file)
        self.reasoner.cpp_reasoning = False
        holdoutResults = list()

        for idx, patID in tqdm.tqdm(enumerate(patientIDs)):
            probs = list()
            for mutationDrugEvidence in patientsMutationEvidence[idx]:
                prob = self.run_demo_only(target, patID, mutationDrugEvidence)
                probs.append(prob)
                print(patID)
                print(probs)
            probOne = 1.0
            probTwo = 1.0
            for prob in probs:
                sumProbs = prob[0][2] + prob[1][2]
                probOne *= (prob[0][2] / sumProbs)
                probTwo *= (prob[1][2] / sumProbs)
                sumProbs = probOne + probTwo
                probOne /= sumProbs
                probTwo /= sumProbs
            prob = list()
            prob.append((probs[0][0], probs[0][1], probOne))
            prob.append((probs[1][0], probs[1][1], probTwo))
            holdoutResults.append((prob[0], prob[1]))

        self.getResults(target, holdoutResults, patientIDs)

    def run_demo_only(self, target, patID, evidence):
        #-- Make query and analyze
        query = Query(evidence=evidence[1],
                      targets=[],
                      meta_evidence=evidence[0],
                      meta_targets=[target],
                      type='updating')
        probs = list()
        if True:  #self.drug != evidence[0][0][2]:
            query = self.reasoner.analyze_query(
                copy.deepcopy(query),
                save_dir=None,
                target_strategy=self.target_strategy,
                interpolation=self.interpolation)
            query.result.summary()
            query.getReport()
            for comp_name, state_dict in query.independ_result.items():
                for state_name, prob in state_dict.items():
                    probs.append((comp_name, state_name, prob))
        else:
            query = self.reasoner.analyze_query(
                copy.deepcopy(query),
                preprocessed_bkb=self.processed_bkb,
                target_strategy=self.target_strategy,
                interpolation=self.interpolation)
            query.result.summary()
            query.getReport()
            for comp_name, state_dict in query.independ_result.items():
                for state_name, prob in state_dict.items():
                    probs.append((comp_name, state_name, prob))

    def getResults(self, target, holdoutResults, patientIDs):
        assert len(holdoutResults) == len(
            self.test_patient_hashes), "results mismatch with test patients"
        print("P(" + target[0] + " " + target[1] + " " + str(target[2]) +
              " | geneVariants)")
        numCorrect = 0
        numWrong = 0
        for idx, tph in enumerate(self.test_patient_hashes):
            targetVal = int(self.patient_dict[int(tph)][target[0]])
            patID = self.patient_dict[int(tph)]['Patient_ID']
            assert patID == patientIDs[idx], "holdout patient mismatch"
            op = _process_operator(target[1])

            indexTrue = None
            indexFalse = None
            if holdoutResults[idx][0][1] == 'True':
                indexTrue = 0
                indexFalse = 1
            else:
                indexTrue = 1
                indexFalse = 0

            probOne = holdoutResults[idx][indexFalse][2]  #false
            probTwo = holdoutResults[idx][indexTrue][2]  #true
            sum = probOne + probTwo
            probOne /= sum
            probTwo /= sum
            if op(targetVal, target[2]):
                print(patID)
                print(holdoutResults[idx][indexFalse],
                      holdoutResults[idx][indexTrue])
                print(probOne, probTwo)
                if holdoutResults[idx][indexTrue][2] > holdoutResults[idx][
                        indexFalse][2]:
                    print("\ttrue -", targetVal, "CORRECT")
                    numCorrect += 1
                else:
                    print("\ttrue -", targetVal, "WRONG")
                    numWrong += 1
            else:
                print(patID)
                print(holdoutResults[idx][indexFalse],
                      holdoutResults[idx][indexTrue])
                print(probOne, probTwo)
                if holdoutResults[idx][indexFalse][2] > holdoutResults[idx][
                        indexTrue][2]:
                    print("\tfalse -", targetVal, "CORRECT")
                    numCorrect += 1
                else:
                    print("\tfalse -", targetVal, "WRONG")
                    numWrong += 1
        print("Number correct:", numCorrect)
        print("Number wrong:", numWrong)
reasoner = Reasoner(fused_bkb=fused_bkb,
                    gene_var_direct=gene_var_direct_file,
                    max_new_ev=2)

#-- Set the patient data file
reasoner.set_src_metadata(patient_data_file)
#reasoner.collapsed_bkb.makeGraph()

#-- If you want to see what genetic or demographic evidence is avaliable, uncomment the line below
#print(reasoner.metadata_ranges)

#-- Make a query (evidence is for genetic info, and meta_ is for demographic info)
query0 = Query(name='sample_independ',
               evidence={
                   "var_5'FLANK=": "True",
                   "var_3'UTR=": "True"
               },
               targets=None,
               meta_evidence=[('Age_of_Diagnosis', '>=', 20000)],
               meta_targets=[('Survival_Time', '>=', 943)])

#-- Run the query.
query = reasoner.analyze_query(query0,
                               check_mutex=False,
                               target_strategy='explicit',
                               interpolation='independence')

#-- Return the report
query.getReport()
query.save(os.getcwd())
Example #5
0
#fused_bkb.makeGraph(layout='neato')
print(counts)

for gene in GENES:
    print('=' * 20 + str(' No Connection:'))
    #-- Instantiate Reasoner
    fused_bkb_ = copy.deepcopy(fused_bkb)
    reasoner = Reasoner(fused_bkb_, None)
    reasoner.metadata = patient_data_hash

    #-- Make query
    query1 = Query(evidence={gene_: 'True'
                             for gene_ in [GENES[0], GENES[2]]},
                   targets=['_Source_[{}]_'.format(gene) for gene in GENES],
                   type='updating')
    query1 = reasoner.analyze_query(query1)

    query1.getReport()
    query1.bkb._name = 'No Connection'
    #query1.bkb.makeGraph(layout='neato')

    #-- Structure Learn
    fused_bkb_1 = copy.deepcopy(fused_bkb_)
    gene0Comp_idx = fused_bkb_1.getComponentIndex('Gene0')
    gene2Comp_idx = fused_bkb_1.getComponentIndex('Gene2')
    gene0State_idx = fused_bkb_1.getComponentINodeIndex(gene0Comp_idx, 'True')
    gene2State_idx = fused_bkb_1.getComponentINodeIndex(gene2Comp_idx, 'True')

    fused_bkb_1.addSNode(
        BKB_S_node(init_component_index=gene0Comp_idx,
                   init_state_index=gene0State_idx,
Example #6
0
                 [str(hash(patient)) for patient in PATIENTS],
                 working_dir=os.getcwd())

#fused_bkb.makeGraph(layout='neato')
#col_bkb = collapse_sources(fused_bkb)
#col_bkb.makeGraph(layout='neato')

patient_data_hash = dict()
for patient, dict_ in patient_data.items():
    patient_data_hash[hash(patient)] = dict_

print(patient_data)

#-- Denote Demographic evidence.
demo_ev = [('Age', '>=', 50), ('Gender', '==', 'Male')]

demo_tar = [('Survival', '>=', 2)]

reasoner = Reasoner(fused_bkb, patient_data_hash)

query0 = Query(evidence=dict(),
               targets=list(),
               meta_evidence=demo_ev,
               meta_targets=demo_tar,
               type='updating')

query0 = reasoner.analyze_query(query0, target_strategy='topological')
query0.getReport()
print(checkMutex(query0.bkb))
query0.bkb.makeGraph()
Example #7
0
import os
import sys

from pybkb import bayesianKnowledgeBase as BKB

#-- Change to your local data folder
NCATS_DIR = '/home/cyakaboski/src/python/pojects/bkb-pathway-provider'

sys.path.append(os.path.join(NCATS_DIR, 'core'))

from query import Query
from reasoner import Reasoner

if __name__ == '__main__':
    bkb = BKB()
    bkb.load('/home/public/data/ncats/fusedPatientBKBSmall/fusion.bkb')

    reasoner = Reasoner(bkb)

    query0 = Query(evidence={
        '_sourceFusion_14_02_2020_16:35:04__mut_CACNA1H=':
        '[0]_-7750956075882572850'
    },
                   targets=['mut_CACNA1H=', 'mu-STD>=CACNA1H<=mu+STD='],
                   type='updating')

    result = reasoner.analyze_query(query0)
#fused_bkb.makeGraph(layout='neato')
#col_bkb = collapse_sources(fused_bkb)
#col_bkb.makeGraph(layout='neato')

patient_data_hash = dict()
for patient, dict_ in patient_data.items():
    patient_data_hash[hash(patient)] = dict_

print(patient_data)

#-- Denote Demographic evidence.
demo_ev = [('Age', '>=', 50), ('Gender', '==', 'Male')]

demo_tar = [('Survival', '>=', 2)]

reasoner = Reasoner(fused_bkb, patient_data=patient_data_hash)
random_genes = {'mut_{}'.format(gene): 'True' for gene in GENES[:5]}
query0 = Query(evidence=random_genes,
               targets=list(),
               meta_evidence=demo_ev,
               meta_targets=demo_tar,
               type='updating')

query0 = reasoner.analyze_query(query0,
                                target_strategy='topological',
                                interpolation='independence')
query0.getReport()
#print(checkMutex(query0.bkb))
#query0.bkb.makeGraph()
    def run_patients(self,
                     demo_target,
                     gene_evidence=False,
                     demo_evidence=None,
                     demo_value=0):
        reasoner = Reasoner(self.bkb, None)
        reasoner.set_src_metadata(self.patient_data_file)
        reasoner.cpp_reasoning = False
        #-- Comment this out for real thing
        #self.test_patient_hashes = [list(reasoner.metadata.keys())[0]]

        target_actual = '{} {} {} Actual'.format(demo_target[0],
                                                 demo_target[1],
                                                 demo_target[2])
        results = {
            'X':
            [demo_value for _ in range(len(self.test_patient_hashes[:10]))],
            'Patient': list(),
            'Compute_time': list(),
            'Length_evid': list(),
            target_actual: list()
        }

        if gene_evidence is False and demo_evidence is None:
            raise ValueError('Gene and demo evidence can not be None.')
        if demo_evidence is not None:
            title = 'Evidence = {} {} X'.format(demo_evidence[0],
                                                demo_evidence[1])
            demo_evidence_run = [tuple(list(demo_evidence) + [demo_value])]
            for patient_hash in tqdm.tqdm(self.test_patient_hashes[:10]):
                results['Patient'].append(patient_hash)
                #-- Calculate Truth
                prop_targ, op_targ, val_targ = demo_target
                op_ = _process_operator(op_targ)
                results[target_actual].append(
                    op_(reasoner.metadata[patient_hash][prop_targ], val_targ))
                if gene_evidence:
                    gene_evidence_patient = {
                        gene: 'True'
                        for gene in patient_data[patient_hash]['Patient_Genes']
                    }
                    title += ' w/ GeneEvidence'
                else:
                    title += ' w/o GeneEvidence'
                    gene_evidence_patient = dict()

                #-- Make query
                query = Query(evidence=gene_evidence_patient,
                              meta_evidence=demo_evidence_run,
                              meta_targets=[demo_target],
                              type='updating')
                query = reasoner.analyze_query(copy.deepcopy(query),
                                               save_dir=None)
                results['Length_evid'].append(len(query.evidence))

                results['Compute_time'].append(query.compute_time)
                for update, prob in query.result.updates.items():
                    comp_idx, state_idx = update
                    comp_name = query.bkb.getComponentName(comp_idx)
                    state_name = query.bkb.getComponentINodeName(
                        comp_idx, state_idx)
                    try:
                        results[('Updates', comp_name,
                                 state_name)].append(prob)
                    except:
                        results[('Updates', comp_name, state_name)] = [prob]

        else:
            title = 'No Demographic Evidence w/ Gene Evidence'
            for i, patient_hash in enumerate(self.test_patient_hashes[:10]):
                results['Patient'].append(patient_hash)
                #-- Calculate Truth
                prop_targ, op_targ, val_targ = demo_target
                op_ = _process_operator(op_targ)
                #-- Calculate Truth
                results[target_actual].append(
                    op_(reasoner.metadata[patient_hash][prop_targ], val_targ))
                #-- Take intersection between all bkb genes and patient genes
                print('Intersecting genes')
                genes_patient = set([
                    'mut_{}='.format(gene) for gene in
                    reasoner.metadata[patient_hash]['Patient_Genes']
                ])
                bkb_comps = set(self.bkb.getAllComponentNames())
                gene_intersect = genes_patient.intersection(bkb_comps)
                gene_evidence_patient = {
                    gene: 'True'
                    for gene in gene_intersect
                }
                #print(set(gene_evidence_patient.keys()) - set(self.bkb.getAllComponentNames()))
                #-- Make query
                #print(gene_evidence_patient)
                results['Length_evid'].append(len(gene_evidence_patient))
                query = Query(evidence=gene_evidence_patient,
                              meta_targets=[demo_target],
                              type='updating',
                              name='Pat50-10_600_{}'.format(i))
                print('Analyzing Query.')
                query = reasoner.analyze_query(copy.deepcopy(query),
                                               save_dir=None)
                query.getReport()
                results['Compute_time'].append(query.compute_time)
                for update, prob in query.result.updates.items():
                    comp_idx, state_idx = update
                    comp_name = query.bkb.getComponentName(comp_idx)
                    state_name = query.bkb.getComponentINodeName(
                        comp_idx, state_idx)
                    try:
                        results[('Updates', comp_name,
                                 state_name)].append(prob)
                    except:
                        results[('Updates', comp_name, state_name)] = [prob]
                del query
                print('Complete Patient {}.'.format(i))
        return self.results_to_dataframe(results), title
class Driver:
    def __init__(self, config_file):
        #-- Read in configuration from config file.
        self.config = dict()
        with open(config_file, 'r') as csv_file:
            reader = csv.reader(csv_file)
            for row in reader:
                self.config[row[0]] = row[1]
        self.fused_bkb_path = self.config['fused_bkb_path']
        self.ncats_dir = self.config['ncats_dir']
        self.src_metadata_path = self.config['src_metadata_path']

        self.fused_bkb = BKB()
        self.fused_bkb.load(self.fused_bkb_path)
        self.reasoner = Reasoner(self.fused_bkb, None)
        self.reasoner.set_src_metadata(self.src_metadata_path)

    def run_query(self, query):
        result_query = self.reasoner.analyze_query(query)
        return result_query

    def run(self, i=0):
        if i == 0:
            print('Welcome to the BKB Pathways Driver.')
            input('Press any key to begin reasoning...')

        print("Let's build a query!")
        meta_evidence = self.chooseDemoEvidence()
        evidence = self.chooseStandardEvidence()
        meta_targets = self.chooseDemoTargets()
        targets = self.chooseStandardTargets()

        while True:
            print('Choose reasoning Type:\n1)\tRevision\n2)\tUpdating')
            reason_type = int(input('Your Choice: '))
            if reason_type == 1:
                reason_type = 'revision'
                break
            elif reason_type == 2:
                reason_type = 'updating'
                break
            else:
                print('Unrecognized reasoning type.')

        query = Query(evidence=evidence,
                      targets=targets,
                      meta_evidence=meta_evidence,
                      meta_targets=meta_targets,
                      type=reason_type)
        #print(meta_evidence)
        query = self.reasoner.analyze_query(query)
        query.getReport()
        again = input('Do you wish to reason again? ([y],n): ') or 'y'
        if again == 'y':
            self.run(i=i + 1)
        else:
            return

    def chooseDemoEvidence(self):
        meta_evidence = list()
        while True:
            print('Choose Demographic Evidence:')
            for j, label in enumerate(self.reasoner.metadata_labels):
                print('{})\t{}'.format(j, label))
            print('{})\tNo More Demographic Evidence'.format(j + 1))
            rv = int(input('Your Choice: '))
            if rv == j + 1:
                return meta_evidence
            else:
                rvName = self.reasoner.metadata_labels[rv]
                while True:
                    op = input('Choose a operation ([==], >=, <=): ') or '=='
                    if op == '==' or op == '>=' or op == '<=':
                        print('Choose a value for demographic evidence.')
                        range_ = self.reasoner.metadata_ranges[rvName]
                        if type(range_) == set:
                            for item in range_:
                                print('\t{}'.format(item))
                        else:
                            print('\tmin={}\n\tmax={}'.format(
                                range_[0], range_[1]))
                        state = input('Your choice: ')
                        try:
                            state = float(state)
                        except:
                            pass
                        break
                    else:
                        print('Unrecognized operation!')
                while True:
                    print('This is your demographic choice:')
                    print('\t{} {} {}'.format(rvName, op, state))
                    correct = input('Is it correct? ([y], n): ') or 'y'
                    if correct == 'y':
                        meta_evidence.append((rvName, op, state))
                        break
                    elif correct == 'n':
                        break
                    else:
                        print('Unrecognized selection.')

    def chooseDemoTargets(self):
        meta_targets = list()
        while True:
            print('Choose Demographic Targets:')
            for j, label in enumerate(self.reasoner.metadata_labels):
                print('{})\t{}'.format(j, label))
            print('{})\tNo More Demographic Targets'.format(j + 1))
            rv = int(input('Your Choice: '))
            if rv == j + 1:
                return meta_targets
            else:
                rvName = self.reasoner.metadata_labels[rv]
                while True:
                    op = input('Choose a operation ([==], >=, <=): ') or '=='
                    if op == '==' or op == '>=' or op == '<=':
                        print('Choose a value for demographic target.')
                        range_ = self.reasoner.metadata_ranges[rvName]
                        if type(range_) == set:
                            for item in range_:
                                print('\t{}'.format(item))
                        else:
                            print('\tmin={}\n\tmax={}'.format(
                                range_[0], range_[1]))
                        state = input('Your choice: ')
                        try:
                            state = float(state)
                        except:
                            pass
                        break
                    else:
                        print('Unrecognized operation!')
                while True:
                    print('This is your demographic choice:')
                    print('\t{} {} {}'.format(rvName, op, state))
                    correct = input('Is it correct? ([y], n): ') or 'y'
                    if correct == 'y':
                        meta_targets.append((rvName, op, state))
                        break
                    elif correct == 'n':
                        break
                    else:
                        print('Unrecognized selection.')

    def chooseStandardEvidence(self):
        evidence = dict()
        while True:
            print('Choose Evidence:')
            time.sleep(1)
            for comp_idx in self.fused_bkb.getAllComponentIndices():
                comp_name = self.fused_bkb.getComponentName(comp_idx)
                if not 'Source' in comp_name and not 'source' in comp_name:
                    print('{})\t{}'.format(comp_idx, comp_name))
            print('{})\t{}'.format(comp_idx + 1, 'Done selecting evidence.'))
            compIdx = int(input('Your Choice: '))
            if compIdx == comp_idx + 1:
                return evidence
            print('Choose your state:')
            for state_idx in self.fused_bkb.getAllComponentINodeIndices(
                    compIdx):
                state_name = self.fused_bkb.getComponentINodeName(
                    compIdx, state_idx)
                print('{})\t{}'.format(state_idx, state_name))
            stateIdx = int(input('Your Choice: '))
            compName = self.fused_bkb.getComponentName(compIdx)
            stateName = self.fused_bkb.getComponentINodeName(compIdx, stateIdx)
            while True:
                print('This is your evidence choice:\n\t{} = {}'.format(
                    compName, stateName))
                correct = input('Is that correct? ([y],n): ') or 'y'
                if correct == 'y':
                    evidence.update({compName: stateName})
                    break
                elif correct == 'n':
                    break
                else:
                    print('Unrecognized selection.')

    def chooseStandardTargets(self):
        targets = list()
        while True:
            print('Choose Targets:')
            time.sleep(1)
            for comp_idx in self.fused_bkb.getAllComponentIndices():
                comp_name = self.fused_bkb.getComponentName(comp_idx)
                if not 'Source' in comp_name and not 'source' in comp_name:
                    print('{})\t{}'.format(comp_idx, comp_name))
            print('{})\t{}'.format(comp_idx + 1, 'Done selecting targets.'))
            compIdx = int(input('Your Choice: '))
            if compIdx == comp_idx + 1:
                return targets
            compName = self.fused_bkb.getComponentName(compIdx)
            while True:
                print('This is your target choice:\n\t{}'.format(compName))
                correct = input('Is that correct? ([y],n): ') or 'y'
                if correct == 'y':
                    targets.append(compName)
                    break
                elif correct == 'n':
                    break
                else:
                    print('Unrecognized selection.')

    def collectVariables(self):
        #-- Demographics exposed to the user.
        availableDemographics = set([
            'Gender', 'Patient ID', 'Survival_Time', 'Age_of_Diagnosis',
            'Drug_Name(s)'
        ])
        #-- Get all inodes
        all_inode_names = self.fused_bkb.getINodeNames()
        #-- Filter out sources
        inode_names = [
            inode for inode in all_inode_names if 'Source' not in inode[0]
        ]
        #-- Collect avaliable demographics
        demographics = {
            demo_name: list(demo_range)
            for demo_name, demo_range in self.reasoner.metadata_ranges.items()
            if demo_name in availableDemographics
        }
        return {'genetic_info': inode_names, 'demographic_info': demographics}
    #        rand_rv = random.choice(bkb.components)
    #    #-- Get a random state
    #    rand_state = random.choice(rand_rv.states)
    #    evidence[rand_rv.name] = rand_state.name

    #print('Got Evidence')

    ##-- Select Random Targets
    #num_targets = 2
    #targets = list()
    #for _ in range(num_targets):
    #    rand_target = random.choice(bkb.components)
    #    while rand_target.name in targets or rand_target.name[:3] != 'mut':
    #        rand_target = random.choice(bkb.components)
    #    targets.append(rand_target.name)

    #print('Got Targets')

    reasoner = Reasoner(bkb, None)
    reasoner.set_src_metadata(os.path.join(NCATS_DIR,'core','src_dict.pik'))

    query0 = Query(evidence=dict(),
                   #targets=targets,
                   type='revision',
                   meta_evidence=[('Age_of_Diagnosis', '>=', 15000),
                                  ('Gender', '==', 'FEMALE')]
                  )

    query0 = reasoner.analyze_query(query0)
    query0.getReport()
print(patient_data)

#-- Denote Demographic evidence.
demo_ev = [('Age', '>=', 50), ('Gender', '==', 'Male')]

demo_tar = [('Survival', '>=', 2)]

reasoner1 = Reasoner(fused_bkb, None)
reasoner1.metadata = patient_data_hash
reasoner1.cpp_reasoning = True

reasoner2 = Reasoner(fused_bkb_wStruct, None)
reasoner2.metadata = patient_data_hash
reasoner2.cpp_reasoning = True

query0 = Query(
    evidence={'Gene{}_mutated'.format(i): 'True'
              for i in range(NUM_GENES)},
    targets=list(),
    meta_evidence=demo_ev,
    meta_targets=demo_tar,
    type='updating')

query01 = reasoner1.analyze_query(copy.deepcopy(query0))
query02 = reasoner2.analyze_query(copy.deepcopy(query0))
query01.getReport()
query02.getReport()
#query01.bkb.makeGraph()
query02.bkb.makeGraph()
fused_bkb = BKB()

#-- Load in the fused bkb from our datafiles
fused_bkb.load('/home/public/data/ncats/BabelBKBs/collapsedAll/fusion.bkb')

#-- Here are the associated patient data files
patient_data_file = '/home/public/data/ncats/BabelBKBs/collapsedAll/patient_data.pk'
withheld_patients_file = '/home/public/data/ncats/BabelBKBS/collapsedAll/withheldPatients.csv'

#-- Instaniate reasoner
reasoner = Reasoner(fused_bkb=fused_bkb)

#-- Set the patient data file 
reasoner.set_src_metadata(patient_data_file)
#reasoner.collapsed_bkb.makeGraph()

#-- If you want to see what genetic or demographic evidence is avaliable, uncomment the line below
#print(reasoner.metadata_ranges)

#-- Make a query (evidence is for genetic info, and meta_ is for demographic info)
query0 = Query(evidence={'_mut_TMEM245': 'True'},
               targets=list(),
               meta_evidence=[('Age_of_Diagnosis', '>=',20000)],
               meta_targets=[('Survival_Time', '>=', 300)])

#-- Run the query.
query = reasoner.analyze_query(query0, check_mutex=False, target_strategy='chain', interpolation='correlation')

#-- Return the report
query.getReport()
Example #14
0
class CrossValidator:
    def __init__(self, bkb, test_patient_hashes, patient_data_file):
        self.bkb = bkb
        self.queryCopy = None
        self.test_patient_hashes = test_patient_hashes
        self.patient_data_file = patient_data_file
        self.first = True

    def run_demo_suite(self, target, patientIDs, patientsDynamicEvidence):
        #queryResults = list()
        #probs = list()
        #summaries = list()
        for idx, patID in enumerate(patientIDs):
            queryResults = list()
            probs = list()
            for patientDynamicEvidence in tqdm.tqdm(patientsDynamicEvidence[idx]):
                print(patientDynamicEvidence)
                prob = self.run_demo_only(target, patID, patientDynamicEvidence)
                queryResults.append(patID)
                probs.append(prob)
                #print(prob)
                #input()
            print(probs)
            #for i in range(0, len(queryResults)):
            #    print(probs[i])
            #    print(summaries[i])

    def run_demo_only(self, target, patID, evidence):
        #-- Set Up Reasoner
        self.reasoner = Reasoner(self.bkb, None)
        self.reasoner.set_src_metadata(self.patient_data_file)
        self.reasoner.cpp_reasoning = False

        #print(evidence)
        #print([target])
        #-- Make query and analyze
        query = Query(evidence=evidence,
                      targets=[],
                      meta_evidence=[],
                      meta_targets=[target],
                      type='updating')
        probs = list()
        if self.first:
            query = self.reasoner.analyze_query(copy.deepcopy(query), save_dir=None)
            self.processed_bkb = copy.deepcopy(query.bkb)
            self.first = False
            #query.result.summary()
            for update, prob in query.result.updates.items():
                comp_idx, state_idx = update
                comp_name = query.bkb.getComponentName(comp_idx)
                state_name = query.bkb.getComponentINodeName(comp_idx, state_idx)
                probs.append((comp_name, state_name, prob))
        else:
            query = self.reasoner.analyze_query(copy.deepcopy(query), preprocessed_bkb=self.processed_bkb)
            #query.result.summary()
            for update, prob in query.result.updates.items():
                comp_idx, state_idx = update
                comp_name = query.bkb.getComponentName(comp_idx)
                state_name = query.bkb.getComponentINodeName(comp_idx, state_idx)
                probs.append((comp_name, state_name, prob))

        return probs