<jupyter_start><jupyter_text>read data<jupyter_code>cell_type_nlp_emb_file, cell_type_network_file, cl_obo_file = read_ontology_file(dname1, ontology_data_dir)
OnClass_train_obj = OnClassModel(cell_type_nlp_emb_file = cell_type_nlp_emb_file, cell_type_network_file = cell_type_network_file)
feature_file, filter_key, label_key, label_file, gene_file = read_data_file(dname1, scrna_data_dir)
if feature_file.endswith('.pkl'):
    feature1, label1, genes1 = parse_pkl(feature_file, label_file, gene_file, exclude_non_leaf_ontology = True, cell_ontology_file = cell_type_network_file)
elif feature_file.endswith('.h5ad'):
    feature1, genes1, label1, _, _ = read_data(feature_file, cell_ontology_ids = OnClass_train_obj.cell_ontology_ids,
    exclude_non_leaf_ontology = True, tissue_key = 'tissue', filter_key = filter_key, AnnData_label_key=label_key,
    nlp_mapping = False, cl_obo_file = cl_obo_file, cell_ontology_file = cell_type_network_file, co2emb = OnClass_train_obj.co2vec_nlp)

feature_file, filter_key, label_key, label_file, gene_file = read_data_file(dname2, scrna_data_dir)
if feature_file.endswith('.pkl'):
    feature2, label2, genes2 = parse_pkl(feature_file, label_file, gene_file, exclude_non_leaf_ontology = True, cell_ontology_file = cell_type_network_file)
elif feature_file.endswith('.h5ad'):
    feature2, genes2, label2, _, _ = read_data(feature_file, cell_ontology_ids = OnClass_train_obj.cell_ontology_ids,
    exclude_non_leaf_ontology = True, tissue_key = 'tissue', filter_key = filter_key, AnnData_label_key=label_key,
    nlp_mapping = False, cl_obo_file = cl_obo_file, cell_ontology_file = cell_type_network_file, co2emb = OnClass_train_obj.co2vec_nlp)<jupyter_output><empty_output><jupyter_text>Embed cell types based on the Cell Ontology Graph<jupyter_code>print (np.shape(feature1), np.shape(feature2))
feature1 = np.array(feature1, dtype=np.float64)
feature2 = np.array(feature2, dtype=np.float64)
common_genes = np.sort(list(set(genes1) & set(genes2)))
gid1 = find_gene_ind(genes1, common_genes)
gid2 = find_gene_ind(genes2, common_genes)
train_feature = np.array(feature1[:, gid1])
test_feature = np.array(feature2[:, gid2])
train_label = label1
test_label = label2
train_genes = common_genes
test_genes = common_genes
print (np.shape(test_feature), np.shape(train_feature))<jupyter_output>(17614, 31509) (75501, 31509)
(75501, 31509) (17614, 31509)<jupyter_text>Training<jupyter_code>output_dir = '/oak/stanford/groups/rbaltman/swang91/Sheng_repo/result/SingleCell/OnClass/Crossdatasets/'
pred_Y_seen = np.load(output_dir+dname1+'.'+dname2 + 'pred_Y_seen.npy')
pred_Y_all = np.load(output_dir+dname1+'.'+dname2 + 'pred_Y_all.npy')
OnClass_train_obj.EmbedCellTypes(train_label)
train_label = [OnClass_train_obj.co2i[tp] for tp in train_label]
test_label = [OnClass_train_obj.co2i[tp] for tp in test_label]
pred_Y_all_new[:,80]
from sklearn.utils.graph_shortest_path import graph_shortest_path
from utils import get_ontology_name, precision_at_k
from OnClass.OnClass_utils import create_propagate_networks_using_nlp, extend_prediction_2unseen

nct = len(OnClass_train_obj.co2i)
A = np.zeros((nct, nct))
fin = open(OnClass_train_obj.cell_type_network_file)
for line in fin:
    w = line.strip().split('\t')
    A[OnClass_train_obj.co2i[w[0]], OnClass_train_obj.co2i[w[1]]] = 1.
    A[OnClass_train_obj.co2i[w[1]], OnClass_train_obj.co2i[w[0]]] = 1.
fin.close()
A_dis = graph_shortest_path(A,method='FW',directed =False)
    
def create_unseen_candidates(A_dis, co2i, i2co, nseen, use_unseen_distance, test_Y_pred_all):

	min_d = np.min(A_dis[:nseen, nseen:], axis = 0)
	assert(len(min_d) == nct - nseen)
	unseen_cand = np.where(min_d > use_unseen_distance)[0] + nseen
	test_Y_pred_all[:, unseen_cand] = -1000000
	assert(np.shape(test_Y_pred_all)[1] == nct)
	return test_Y_pred_all

co2name, name2co = get_ontology_name(obo_file = cl_obo_file)
network = create_propagate_networks_using_nlp(OnClass_train_obj.co2i, OnClass_train_obj.ontology_dict, 
                                              OnClass_train_obj.ontology_mat, OnClass_train_obj.co2vec_nlp_mat)
pred_Y_all_normalize = extend_prediction_2unseen(pred_Y_seen, network, 
                                                 OnClass_train_obj.nseen, 
                                                 ratio=200, use_normalize = True)
from scipy import stats
ratio = 1#(OnClass_train_obj.nco*1./OnClass_train_obj.nseen)**2
ntest_cell = len(test_label)
test_label = np.array(test_label)

pred_Y_all_normalize = extend_prediction_2unseen(pred_Y_seen, network, 
                                             OnClass_train_obj.nseen, 
                                             ratio=ratio, use_normalize = False)

prec_at_k_1 = precision_at_k(pred_Y_all_normalize, test_label, 1)
prec_at_k_3 = precision_at_k(pred_Y_all_normalize, test_label, 3)
prec_at_k_5 = precision_at_k(pred_Y_all_normalize, test_label, 5)
prec_at_k_10 = precision_at_k(pred_Y_all_normalize, test_label, 10)
#print (np.unique(pred_label))
print (prec_at_k_1, prec_at_k_3, prec_at_k_5, prec_at_k_10)


unseen_confidence = np.max(pred_Y_all_normalize1[:,OnClass_train_obj.nseen:], axis=1) - np.max(pred_Y_all_normalize1[:,:OnClass_train_obj.nseen], axis=1)

unseen_ratio = 0.00001
nexpected_unseen = int(ntest_cell * unseen_ratio) + 1
unseen_ind = np.argpartition(unseen_confidence, -1 * nexpected_unseen)[-1 * nexpected_unseen:]


pred_Y_all_normalize[:,OnClass_train_obj.nseen:] = stats.zscore(pred_Y_all_normalize[:,OnClass_train_obj.nseen:], 
                                                                axis = 0)  

pred_Y_all_normalize[unseen_ind,:OnClass_train_obj.nseen] -= 1000000

prec_at_k_1 = precision_at_k(pred_Y_all_normalize, test_label, 1)
prec_at_k_3 = precision_at_k(pred_Y_all_normalize, test_label, 3)
prec_at_k_5 = precision_at_k(pred_Y_all_normalize, test_label, 5)
prec_at_k_10 = precision_at_k(pred_Y_all_normalize, test_label, 10)
#print (np.unique(pred_label))

print (use_unseen_distance, prec_at_k_1, prec_at_k_3, prec_at_k_5, prec_at_k_10)
sd
sd
unseen_in_test = np.where(np.array(test_label) >= OnClass_train_obj.nseen)[0]
pred_label = np.argmax(pred_Y_all_normalize, axis=1)
depth = []
for i in np.unique(pred_label):
    depth.append(A_dis[OnClass_train_obj.co2i['CL:0000000'], i])
print (Counter(depth))
prec_at_k_3 = precision_at_k(pred_Y_all_normalize[unseen_in_test,:], test_label[unseen_in_test], 3)
prec_at_k_5 = precision_at_k(pred_Y_all_normalize[unseen_in_test,:], test_label[unseen_in_test], 5)
prec_at_k_10 = precision_at_k(pred_Y_all_normalize[unseen_in_test,:], test_label[unseen_in_test], 10)
#print (np.unique(pred_label))

print (use_unseen_distance, prec_at_k_3, prec_at_k_5, prec_at_k_10)
sd

unseen_conf = np.max(pred_Y_all_normalize1[:,OnClass_train_obj.nseen:], axis=1) - np.max(pred_Y_all_normalize1[:,:OnClass_train_obj.nseen], axis=1)

#unseen_ratio = 0.1
expected_unseen = int(ntest_cell * unseen_ratio)
unseen_ind = np.argpartition(unseen_conf, -1 * expected_unseen)[-1 * expected_unseen:]

pred_label = np.argmax(pred_Y_all_normalize1, axis=1)
pred_label[unseen_ind] = np.argmax(pred_Y_all_normalize1[unseen_ind,OnClass_train_obj.nseen:], axis=1)
pred_Y_all_normalize1[unseen_ind,:OnClass_train_obj.nseen] = -1000000


unseen_in_test = np.where(np.array(test_label) >= OnClass_train_obj.nseen)[0]
prec_at_k_3 = precision_at_k(pred_Y_all_normalize1[unseen_in_test,:], test_label[unseen_in_test], 3)
prec_at_k_5 = precision_at_k(pred_Y_all_normalize1[unseen_in_test,:], test_label[unseen_in_test], 5)
prec_at_k_10 = precision_at_k(pred_Y_all_normalize1[unseen_in_test,:], test_label[unseen_in_test], 10)
#print (np.unique(pred_label))

print (use_normalize, unseen_ratio, use_unseen_distance, prec_at_k_3, prec_at_k_5, prec_at_k_10)
'''
act = Counter(pred_label)
#for ct in act:
#    nct = act[ct]
#    print (ct, co2name[OnClass_train_obj.i2co[ct]], nct)

unseen_acc = 0.
seen_acc = 0.
for i in range(ntest_cell):
    if pred_label[i] == test_label[i]:
        if pred_label[i] < OnClass_train_obj.nco:
            seen_acc += 1
        else:
            unseen_acc += 1
print (use_normalize, unseen_ratio, unseen_acc / ntest_cell, seen_acc / ntest_cell) 
'''

unseen_ratio = 0.00001
nexpected_unseen = int(ntest_cell * unseen_ratio) + 1
unseen_ind = np.argpartition(unseen_confidence, -1 * nexpected_unseen)[-1 * nexpected_unseen:]<jupyter_output><empty_output><jupyter_text>Classify test cells<jupyter_code>unseen_ind

unseen_ratio = 0.00001
nexpected_unseen = int(ntest_cell * unseen_ratio) + 1
unseen_ind = np.argpartition(unseen_confidence, -1 * nexpected_unseen)[-1 * nexpected_unseen:]
seen_ind = np.argpartition(unseen_confidence, -1 * nexpected_unseen)[:-1 * nexpected_unseen]

pred_Y_all_normalize[:,OnClass_train_obj.nseen:] = stats.zscore(pred_Y_all_normalize[:,OnClass_train_obj.nseen:], 
                                                                axis = 0)  

pred_Y_all_normalize[unseen_ind,:OnClass_train_obj.nseen] -= 1000000
pred_Y_all_normalize[seen_ind,:OnClass_train_obj.nseen] -= 1000000
prec_at_k_1 = precision_at_k(pred_Y_all_normalize, test_label, 1)
prec_at_k_3 = precision_at_k(pred_Y_all_normalize, test_label, 3)
prec_at_k_5 = precision_at_k(pred_Y_all_normalize, test_label, 5)
prec_at_k_10 = precision_at_k(pred_Y_all_normalize, test_label, 10)
#print (np.unique(pred_label))

print (use_unseen_distance, prec_at_k_1, prec_at_k_3, prec_at_k_5, prec_at_k_10)


pred_Y_all_normalize = stats.zscore(pred_Y_all_normalize, axis = 0)  

prec_at_k_1 = precision_at_k(pred_Y_all_normalize, test_label, 1)
prec_at_k_3 = precision_at_k(pred_Y_all_normalize, test_label, 3)
prec_at_k_5 = precision_at_k(pred_Y_all_normalize, test_label, 5)
prec_at_k_10 = precision_at_k(pred_Y_all_normalize, test_label, 10)
#print (np.unique(pred_label))

print (prec_at_k_1, prec_at_k_3, prec_at_k_5, prec_at_k_10)

unseen_confidence = np.max(pred_Y_all_normalize1[:,OnClass_train_obj.nseen:], axis=1) - np.max(pred_Y_all_normalize1[:,:OnClass_train_obj.nseen], axis=1)

unseen_ratio = 0.00001
nexpected_unseen = int(ntest_cell * unseen_ratio)
unseen_ind = np.argpartition(unseen_confidence, -1 * nexpected_unseen)[-1 * nexpected_unseen:]

pred_Y_all_normalize[unseen_ind,:OnClass_train_obj.nseen] -= 1000000
pred_label = np.argmax(pred_Y_all_normalize1, axis=1)

prec_at_k_1 = precision_at_k(pred_Y_all_normalize, test_label, 1)
prec_at_k_3 = precision_at_k(pred_Y_all_normalize, test_label, 3)
prec_at_k_5 = precision_at_k(pred_Y_all_normalize, test_label, 5)
prec_at_k_10 = precision_at_k(pred_Y_all_normalize, test_label, 10)
#print (np.unique(pred_label))

print (prec_at_k_1, prec_at_k_3, prec_at_k_5, prec_at_k_10)
pred_Y_all[:, OnClass_train_obj.nseen:] -= 1000000
pred_label = np.argmax(pred_Y_all, axis=1)
unseen_acc = 0.
seen_acc = 0.
for i in range(ntest_cell):
    if pred_label[i] == test_label[i]:
        if pred_label[i] < OnClass_train_obj.nseen:
            seen_acc += 1
        else:
            unseen_acc += 1
print (unseen_acc / ntest_cell, seen_acc / ntest_cell)

prec_at_k_1 = precision_at_k(pred_Y_all, test_label, 3)
print (prec_at_k_1)
pred_label
test_label<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10523954781984568, 'fraction_numerical': 0.032388300735689934, 'mean_word_length': 6.214886731391585, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '264961', 'license': 'MIT', 'n_tokens_mistral': 4709, 'n_tokens_neox': 4479, 'n_words': 621, 'path': '.ipynb_checkpoints/run_OnClass_example-checkpoint.ipynb', 'repo_name': 'williamhowardsnyder/OnClass', 'size': '11146'}