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_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=', ')
def run(self, args, atomspace): print 'Received request inference ' + str(args) bio = Bio(atomspace) #bio.atomspace = bio.a = atomspace r = Reasoner(atomspace) r.do_one_steps(*args)
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
def run_demo_suite(self, target, patientIDs, patientsMutationVariantEvidence): #-- 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)): prob = self.run_demo_only(target, patID, patientsMutationVariantEvidence[idx]) holdoutResults.append((prob[0],prob[1])) self.getResults(target, holdoutResults, patientIDs)
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 __init__(self): self.gateway = JavaGateway() self.italian_lexicon = self.gateway.jvm.simplenlg.lexicon.italian.ITXMLLexicon( ) self.italian_factory = self.gateway.jvm.simplenlg.framework.NLGFactory( self.italian_lexicon) self.realiser = self.gateway.jvm.simplenlg.realiser.Realiser() self.word_translator = WordTanslator(language_from=Languages.ENGLISH, language_to=Languages.ITALIAN) self.features = self.gateway.jvm.simplenlg.features self.Feature = self.gateway.jvm.simplenlg.features.Feature self.Tense = self.gateway.jvm.simplenlg.features.Tense self.Gender = self.gateway.jvm.simplenlg.features.Gender self.reasoner = Reasoner() self.parser = load_parser('simple-sem.fcfg', trace=0)
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
#-- Initalize a BKB fused_bkb = BKB() #-- Load in the fused bkb from our datafiles fused_bkb.load( '/home/public/data/ncats/AxleBKBS/AxleDemoBKB3-14-20/fusion.bkb') #-- Here are the associated patient data files patient_data_file = '/home/public/data/ncats/AxleBKBS/AxleDemoBKB3-14-20/patient_data.pk' withheld_patients_file = '/home/public/data/ncats/AxleBKBS/AxleDemoBKB3-14-20/withheldPatients.csv' gene_var_direct_file = '/home/public/data/ncats/AxleBKBS/AxleDemoBKB3-14-20/geneFreqAnal.csv' #-- Instaniate reasoner 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" },
[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()
bkfs = patient_bkfs + pathway_bkfs source_names = [str(hash(patient)) for patient in PATIENTS] + PATHWAYS #-- Fuse patients together. fused_bkb = fuse(bkfs, [1 for _ in range(len(source_names))], source_names, working_dir=os.getcwd()) #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_)
#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()
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)
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)
def _get_input(self, scene): self.N, self.target, self.possible_situation = self._preprocess(scene) self.length = 5 self.reasoner = Reasoner(self.N) self.PO = self._get_po()
'Survival': random.randrange(SURIVAL_YRS[0], SURIVAL_YRS[1]) } for patient in PATIENTS } 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)] 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')
# 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()
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
os.path.join("mappings", args.country + "-" + args.flavor + ".json"), args.globalmap, args.instance ] #print files files = map(path.isfile, files) #print files if all(files): from reasoner import Reasoner a = Reasoner(os.path.join("countries", args.country, "flow.json"), local_map=os.path.join("countries", args.country, "local.json"), flavor_map=os.path.join("flavours", args.flavor, "local.json"), global_map=args.globalmap, mapping=os.path.join( "mappings", args.country + "-" + args.flavor + ".json"), detail=args.detail, output=args.output, language=os.path.join("countries", args.country, "i18n", args.language + ".json")) a.parse_input(args.instance) a.info() if args.query is None: a.run() else: a.query(args.query) print a.get_result() if args.mode == "url":
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}
class EnglishToItalianTranslator: """ English to italian translator, convert english sentence to FOL logical formula, then transform the logical formula in a simpleNLG sentence plan and generate the italian sentence """ def __init__(self): self.gateway = JavaGateway() self.italian_lexicon = self.gateway.jvm.simplenlg.lexicon.italian.ITXMLLexicon( ) self.italian_factory = self.gateway.jvm.simplenlg.framework.NLGFactory( self.italian_lexicon) self.realiser = self.gateway.jvm.simplenlg.realiser.Realiser() self.word_translator = WordTanslator(language_from=Languages.ENGLISH, language_to=Languages.ITALIAN) self.features = self.gateway.jvm.simplenlg.features self.Feature = self.gateway.jvm.simplenlg.features.Feature self.Tense = self.gateway.jvm.simplenlg.features.Tense self.Gender = self.gateway.jvm.simplenlg.features.Gender self.reasoner = Reasoner() self.parser = load_parser('simple-sem.fcfg', trace=0) def translate_sentence(self, sentence): """ Translate an english sentence to an italian sentence :param sentence: the input english sentence :return: the output italian sentence """ formula = self.translate_sentence_to_formula(sentence) plan = self.__formula_to_sentece_plan(formula) return self.realiser.realiseSentence(plan) def translate_sentence_to_formula(self, sentence): """ Translate an English sentence to a logical FOL formula :param sentence: the input sentence :return: the FOL formula """ read_expr = Expression.fromstring tree = list(self.parser.parse(sentence.split()))[0] #print(tree) formula = read_expr(str(tree.label()['SEM'])).simplify() return str(formula) def __formula_to_sentece_plan(self, formula): """ Transform a valid FOL formula in a simpleNLG sentence plan :param formula: :return: """ formula = self.reasoner.make_inference(formula) clause_t = "clause\((.+?)\)" s = re.search(clause_t, formula) if s: clause_t = s.group(1).split(',') if len(clause_t) == 3: return self.__get_ternary_clause(clause_t[0], clause_t[1], clause_t[2], formula) if len(clause_t) == 2: return self.__get_binary_clause(clause_t[0], clause_t[1], formula) else: return self.__get_exist_clause(formula) def __get_binary_clause(self, s, v, formula): """ :param s: the subject variable name :param v: the verb :param formula: the logical formula :return: sentence plan """ sbj = self.word_translator.translate_word(s) subject = self.__get_object_reference(sbj, formula) verb = self.__get_verb(v, formula) clause = self.italian_factory.createClause(subject, verb) return clause def __get_ternary_clause(self, s, v, o, formula): """ :param s: the subject variable name :param v: the verb :param o: the object variable name :param formula: the logical formula :return: sentence plan """ if s is None: subject = None else: sbj = self.word_translator.translate_word(s) subject = self.__get_object_reference(sbj, formula) verb = self.__get_verb(v, formula) obj = self.__get_object_reference(o, formula) clause = self.italian_factory.createClause(subject, verb, obj) return clause def __get_exist_clause(self, formula): """ If verb if not present, "there is" is assumed :param formula: the logical formula :return: the sentence plan or None if exist is not present """ clause = None match_exist = "exists (.+?)\." exist = re.match(match_exist, formula) if exist: clause = self.__get_ternary_clause(None, "c'è", exist.group(1), formula) return clause def __get_verb(self, v, formula): """ :param v: the verb :param formula: the logical formula :return: the simpleNLG verb object """ v_t = self.word_translator.translate_word(v) verb = self.italian_factory.createVerbPhrase(v_t) verb = self.__set_verb_tense(verb, v, formula) verb = self.__set_verb_complement(verb, v, formula) return verb def __get_object_reference(self, variable, formula): """ :param variable: the object variable name :param formula: the logical formula :return: the simpleNLG object """ object_ref = "objectRef\({},(.+?)\)".format(variable) s = re.search(object_ref, formula) if s: object_props = s.group(1).split(',') o = self.word_translator.translate_word(object_props[0]) obj = self.italian_factory.createNounPhrase(o) obj = self.__set_object_specifier(obj, variable, formula) obj = self.__manage_lexical_exception(obj) obj = self.__set_possessive_pron(obj, variable, formula) obj = self.__set_object_adj(obj, variable, formula) obj = self.__set_object_complement(obj, variable, formula) if object_props[1] == "pl": obj.setPlural(True) return obj else: return self.italian_factory.createNounPhrase( self.word_translator.translate_word(variable)) def __set_verb_tense(self, verb, v, formula): """ Set the tense of the verb :param verb: the verb object :param v: the verb constant name :param formula: the logical formula :return: the simpleNLG verb object """ verbe_tense = "verbTense\({},(.+?)\)".format(v) s = re.search(verbe_tense, formula) if s: tense = s.group(1) if tense == "progPres": verb.setFeature(self.Feature.PROGRESSIVE, True) verb.setFeature(self.Feature.PERFECT, False) verb.setFeature(self.Feature.TENSE, self.Tense.PRESENT) return verb @staticmethod def __set_object_specifier(obj, variable, formula): """ Set the object specifier :param obj: the simpleNLG object :param variable: the variable name :param formula: the logical formula :return: the simpleNLG object """ only_one = "onlyOne\({}\)".format(variable) if re.search(only_one, formula): obj.setSpecifier("il") else: obj.setSpecifier("un") return obj def __set_object_adj(self, obj, variable, formula): """ Set adjective of the object :param obj: the simpleNLG object :param variable: the variable name :param formula: the logical formula :return: simpleNLG object """ adj_match = "adj\((.+?),{}\)".format(variable) s = re.search(adj_match, formula) if s: adj = self.italian_factory.createAdjectivePhrase( self.word_translator.translate_word(s.group(1))) obj.addPreModifier(adj) return obj def __set_possessive_pron(self, obj, variable, formula): """ Add possessive pronoun :param obj: the simpleNLG object :param variable: the variable name :param formula: the logical formula :return: simpleNLG object """ pron_match = "pronPoss\((.+?),{}\)".format(variable) s = re.search(pron_match, formula) if s: pron = self.italian_factory.createAdjectivePhrase( self.word_translator.translate_word(s.group(1))) obj.addModifier(pron) return obj def __set_verb_complement(self, verb, v, formula): """ :param verb: the simpleNLG verb object :param v: the variable name :param formula: the logical formila :return: the simpleNLG object """ compl_match = "verbCompl\({},(.+?)\)".format(v) s = re.search(compl_match, formula) if s: compl_variable = s.group(1) compl_int_match = "compl\({},(.+?)\)".format(compl_variable) s_int = re.search(compl_int_match, formula) if s_int: compl = s_int.group(1).split(",") if len(compl) == 1: verb.addComplement( self.word_translator.translate_word(compl[0])) return verb def __manage_lexical_exception(self, obj): """ Manage some expection in lexicon, Ex: fixing gender for missing word :param obj: :return: """ if "opportunità" in obj.getNoun().toString(): obj.setFeature(self.features.LexicalFeature.GENDER, self.Gender.FEMININE) return obj def __set_object_complement(self, obj, variable, formula): """ Add complement to an object :param obj: the simpleNLG object :param variable: the variable name :param formula: the logica formula :return: simpleNLG object (with complement if present) """ compl_match = "propP\((.+?),{},(.+?)\)".format(variable) compl = re.search(compl_match, formula) if compl and compl.lastindex == 2: prop = compl.group(1) variable_compl = compl.group(2) obj_compl = self.__get_object_reference(variable_compl, formula) obj_prop_compl = self.italian_factory.createPrepositionPhrase( self.word_translator.translate_word(prop), obj_compl) obj.addComplement(obj_prop_compl) return obj
import os import sys import pickle sys.path.append('/home/cyakaboski/src/python/projects/bkb-pathway-provider/core') from reasoner import Reasoner from query import Query from pybkb.core.common.bayesianKnowledgeBase import bayesianKnowledgeBase as BKB #-- Load fused bkb fused_bkb = BKB() fused_bkb.load('10PatientFusion.bkb') #-- Instantiate Reasoner reasoner = Reasoner(fused_bkb, None) reasoner.set_src_metadata('patient_data.pk') #print(reasoner.metadata) #-- Make query query1 = Query(evidence=dict(), targets=list(), meta_evidence=[('Age_of_Diagnosis','>=', 20000)], meta_targets=[('Survival_Time', '>=', 500)], type='updating') query1 = reasoner.analyze_query(query1) query1.getReport() query1.bkb.makeGraph()
if args.mode == "url": f = tempfile.NamedTemporaryFile("w", delete=False) data = cacher.get(args.instance) #print data.encoding #print data.text #if data.status_code != 200: # raise Exception("Wrong Status ==> Network Error.") f.write(data) f.close() args.instance = f.name files = [ os.path.join("countries", args.country,"flow.json"), os.path.join("flavours",args.flavor, "local.json"), os.path.join("countries", args.country, "local.json"), os.path.join("mappings", args.country+"-"+args.flavor+".json"), args.globalmap, args.instance] #print files files = map(path.isfile, files) #print files if all(files): from reasoner import Reasoner a = Reasoner(os.path.join("countries",args.country,"flow.json"), local_map = os.path.join("countries", args.country, "local.json"), flavor_map = os.path.join("flavours",args.flavor, "local.json"), global_map = args.globalmap, mapping=os.path.join("mappings", args.country+"-"+args.flavor+".json"),detail=args.detail, output=args.output, language=os.path.join("countries", args.country, "i18n", args.language+".json")) a.parse_input(args.instance) a.info() if args.query is None: a.run() else: a.query(args.query) print a.get_result() if args.mode == "url": os.remove(args.instance) sys.exit()
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
from flask_socketio import SocketIO from uploader import Ontology import roslibpy from reasoner import Reasoner app = Flask(__name__) # Initialize SocketIO socketio = SocketIO(app) KnowledgeBase = Ontology() # Connect with ROS client = roslibpy.Ros(host='localhost', port=9090) client.run() reasoner = Reasoner(client) camera_scan = reasoner.camera_scan robot_move = reasoner.robot_move gripper_grasp = reasoner.gripper_grasp @app.route('/') def index(): return render_template('tasks.html') @socketio.on('refresh_data') def load_data(): data = KnowledgeBase.get_instances() if data: for i in data:
from reasoner import Reasoner from query import Query #-- Initalize a BKB 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.
def __init__(self, cfg): super(GenericModel, self).__init__() self.cfg = cfg self.embedder = Embedder(cfg['embedder']) self.reasoner = Reasoner(cfg['reasoner'])
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