def plot_brave_network(model, heuristics, file_suffix='', graph_label=''): response_logic.brave_solving(model, heuristics=heuristics) brave_net = pd.DataFrame(model['response']['brave edge array'], index=readouts, columns=readouts) fix, ax = plt.subplots(figsize=[6, 6]) sns.heatmap(brave_net, square=True) plt.tight_layout() plt.savefig(os.path.join(plot_folder, '{0}_brave.pdf'.format(plot_prefix))) plt.show() if draw_graphs == True: draw_graph(brave_net.values, readouts_shortnames, os.path.join( plot_folder, 'graphs/{0}_brave_net_{1}'.format( plot_prefix, file_suffix)), perturbations=perturbations, pert_to_players=pert_to_players_shortnames, label='brave_net {}'.format(graph_label)) return
def score_brave_model(model, heuristics, edges_known, rank_randomization_repeats, brave_array=None): '''Computes brave model. To compute AUCs score, ties are broken randomly. This is repeated and the averages are returned. If brave_array is given it is not re-computed.''' if brave_array is None: response_logic.brave_solving(model, heuristics=heuristics) brave_array = model['response']['brave edge array'] pr_aucs = [] roc_aucs = [] for i in range(rank_randomization_repeats): randomly_ranked_prediction = response_logic.convenience.break_score_ties( brave_array, np.random.rand(len(readouts), len(readouts))) score_dict = response_logic.convenience.score_binary_classifier( gold_edges_dict, randomly_ranked_prediction, edges_known) pr_aucs.append(score_dict['pre_rec_auc']) roc_aucs.append(score_dict['roc_auc']) return {'ROC auc': np.mean(roc_aucs), 'PR auc': np.mean(pr_aucs)}
(1, 2): 1, (0, 2): 0 }, 'Sen_known': Sen_known, }, 'response': { 'nonzero': response_pattern, 'confidence': confidence_pattern, } } #%%infer networks #no heuristics response_logic.conform_response_pattern(model, scheme='general') response_logic.brave_solving(model) brave = model['response']['brave edge array'] #heuristics: parsimonious response_logic.conform_response_pattern(model, scheme='general', heuristics={'parsimonious': True}) response_logic.brave_solving(model, heuristics={'parsimonious': True}) brave_parsimonious = model['response']['brave edge array'] #iterate solutions all_parsimonious_nets = response_logic.iterate_conforming_networks( model, 'all', heuristics={'parsimonious': True}) #local link number heuristics: response_logic.conform_response_pattern(
def run_DREAM_inference(DREAM_N, data_set_number, confidence_cut=0): data_folder = os.path.join( analysis_folder, './DREAM3 in silico challenge/Size{0}/DREAM3 data'.format(DREAM_N)) timeseries = pd.read_csv(os.path.join( data_folder, 'InSilicoSize{0}-{1}-trajectories.tsv'.format(DREAM_N, data_set_number)), sep='\t') ko = pd.read_csv(os.path.join( data_folder, 'InSilicoSize{0}-{1}-null-mutants.tsv'.format(DREAM_N, data_set_number)), sep='\t', index_col=0).T ko.index.name = 'readout_gene' wts = timeseries[timeseries.Time == 200].drop(columns='Time') wts.append(ko.wt) ko = ko.drop(columns='wt') response_pattern, confidence_pattern, Rexpt = create_response_pattern( ko, wts, confidence_cut=confidence_cut) Sen_known = np.eye(DREAM_N) np.fill_diagonal(Sen_known, np.nan) model = { 'N': DREAM_N, 'P': DREAM_N, 'Q': DREAM_N, 'input': { 'node_names': ko.index.values, #[str(i) for i in nodes], 'pert_names': ko.index.values, #[str(i) for i in targets], 'edges_known': {}, 'Sen_known': Sen_known, }, 'response': { 'nonzero': response_pattern, 'confidence': confidence_pattern, } } response_logic.conform_response_pattern(model) response_logic.brave_solving(model) ordered_zero_candidates = response_logic.convenience.order_zero_candidates_by_nonzero_groups( model['response']['brave edge array'], response_pattern, confidence_pattern) response_logic.sparsify_brave_structure(model, ordered_zero_candidates) sparse_scores = model['response']['brave edge array sparsified'] sparse_scores = response_logic.convenience.break_score_ties( sparse_scores, Rexpt) #write down results file curr_result_dir = os.path.join(result_folder, '{0}/conf_thresh_{1}/').format( DREAM_N, str(confidence_cut).replace('.', '_')) if not os.path.exists(curr_result_dir): os.makedirs(curr_result_dir) results_filename = os.path.join( curr_result_dir, 'conf_thresh_{2}_net_InSilico_Size{0}_{1}.txt'.format( DREAM_N, data_set_number, str(confidence_cut).replace('.', '_'))) write_result_files(sparse_scores, results_filename, model) return
def score_pathway(pathway, rate_missing_data, rate_misclassification): model = generate_test_data(pathway, rate_missing_data, rate_misclassification) response_logic.conform_response_pattern(model, scheme='star-model') response_logic.brave_solving(model) # brave_net=pd.DataFrame(model['response']['brave edge array'], # index=model['input']['node_names'],columns=model['input']['node_names']) #sparsify# #order ambiguous links by first taking those belonging to zero then nan then nonzero datapoints #then order them according to their confidence (zeros) or inverse confidence (nonzeros) (not possible for nan) ambiguous_links_zero_mask = np.logical_and( model['response']['brave edge array'] == 0.5, model['response']['nonzero'] == 0) ambiguous_links_nan_mask = np.logical_and( model['response']['brave edge array'] == 0.5, np.isnan(model['response']['nonzero'])) ambiguous_links_nonzero_mask = np.logical_and( model['response']['brave edge array'] == 0.5, model['response']['nonzero'] == 1) ordered_zero_candidates = np.concatenate( (np.transpose(np.where(ambiguous_links_zero_mask))[np.argsort( model['response']['confidence'][ambiguous_links_zero_mask])][::-1], np.transpose(np.where(ambiguous_links_nan_mask)), np.transpose(np.where(ambiguous_links_nonzero_mask))[np.argsort( model['response']['confidence'][ambiguous_links_nonzero_mask])])) response_logic.sparsify_brave_structure(model, ordered_zero_candidates) #compute sensitivity, specitivity, precision Jac_known = response_logic.convenience.edges_known2Jac_known( model['input']['edges_known'], model['N']) np.fill_diagonal(Jac_known, 0) relevant_selection = np.isnan(Jac_known) relevant_prediction = model['response']['brave edge array sparsified'][ relevant_selection] relevant_gold = pathway['gold_Jac'][relevant_selection] positives_inds = relevant_prediction == 1 nbr_true_pos = np.sum(relevant_gold[positives_inds] == 1) nbr_false_pos = np.sum(relevant_gold[positives_inds] == 0) negative_inds = relevant_prediction == 0 nbr_true_neg = np.sum(relevant_gold[negative_inds] == 0) nbr_false_neg = np.sum(relevant_gold[negative_inds] == 1) sensitivity = nbr_true_pos / (nbr_true_pos + nbr_false_neg) specificity = nbr_true_neg / (nbr_true_neg + nbr_false_neg) precision = nbr_true_pos / (nbr_true_pos + nbr_false_pos) #store results classification_result = { 'N': model['N'], 'KEGG_id': pathway['KEGG_id'], 'effective_node_ratio': pathway['effective_node_ratio'], 'sensitivity': sensitivity, 'specificity': specificity, 'precision': precision, # 'rate_perturbed_nodes':rate_perturbed_nodes, # 'actual_rate_perturbed_nodes':model['extra_info']['actual_rate_perturbed_nodes'], 'rate_missing_data': rate_missing_data, 'actual_rate_missing_data': model['extra_info']['actual_rate_missing_data'], 'rate_misclassification': rate_misclassification, 'actual_rate_misclassification': model['extra_info']['actual_rate_misclassification'], } return classification_result