|
a |
|
b/python-scripts/runSingleAE.py |
|
|
1 |
from keras.layers import Input, Dense |
|
|
2 |
from keras.models import Model |
|
|
3 |
import numpy as np |
|
|
4 |
import pandas as pd |
|
|
5 |
import matplotlib.pyplot as plt |
|
|
6 |
from sklearn.cluster import KMeans |
|
|
7 |
from sklearn.cluster import k_means |
|
|
8 |
from sklearn.metrics import silhouette_score, davies_bouldin_score |
|
|
9 |
from sklearn.preprocessing import normalize |
|
|
10 |
import time |
|
|
11 |
from sklearn import metrics |
|
|
12 |
from myUtils import * |
|
|
13 |
from AEclass import AE |
|
|
14 |
import os |
|
|
15 |
|
|
|
16 |
if __name__ == '__main__': |
|
|
17 |
datapath = 'data/single-cell/' |
|
|
18 |
resultpath = 'result/single-cell/' |
|
|
19 |
# groundtruth = np.loadtxt('{}/c.txt'.format(datapath)) |
|
|
20 |
# groundtruth = list(np.int_(groundtruth)) |
|
|
21 |
|
|
|
22 |
omics = np.loadtxt('{}/omics.txt'.format(datapath)) |
|
|
23 |
omics = np.transpose(omics) |
|
|
24 |
omics1=omics[0:206] |
|
|
25 |
omics2=omics[206:412] |
|
|
26 |
omics1 = normalize(omics1, axis=0, norm='max') |
|
|
27 |
omics2 = normalize(omics2, axis=0, norm='max') |
|
|
28 |
omics = np.concatenate((omics1, omics2), axis=1) |
|
|
29 |
|
|
|
30 |
# omics1 = np.loadtxt('{}/ata.txt'.format(datapath)) |
|
|
31 |
# omics1 = np.transpose(omics1) |
|
|
32 |
# omics1 = normalize(omics1, axis=0, norm='max') |
|
|
33 |
# |
|
|
34 |
# omics2 = np.loadtxt('{}/rna.txt'.format(datapath)) |
|
|
35 |
# omics2 = np.transpose(omics2) |
|
|
36 |
# omics2 = normalize(omics2, axis=0, norm='max') |
|
|
37 |
# |
|
|
38 |
# omics = np.concatenate((omics1, omics2), axis=1) |
|
|
39 |
|
|
|
40 |
print(omics1.shape) |
|
|
41 |
print(omics2.shape) |
|
|
42 |
|
|
|
43 |
# data = omics |
|
|
44 |
# input_dim = data.shape[1] |
|
|
45 |
# encoding1_dim = 4096 |
|
|
46 |
# encoding2_dim = 1024 |
|
|
47 |
# middle_dim = 2 |
|
|
48 |
# dims = [encoding1_dim, encoding2_dim, middle_dim] |
|
|
49 |
# ae = AE(data, dims) |
|
|
50 |
# ae.autoencoder.summary() |
|
|
51 |
# ae.train() |
|
|
52 |
# encoded_factors = ae.predict(data) |
|
|
53 |
# if not os.path.exists("{}/AE_FCTAE_EM.txt".format(resultpath)): |
|
|
54 |
# os.mknod("{}/AE_FCTAE_EM.txt".format(resultpath)) |
|
|
55 |
# np.savetxt("{}/AE_FCTAE_EM.txt".format(resultpath), encoded_factors) |
|
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
# |
|
|
61 |
# # if not os.path.exists("AE_FCTAE_Kmeans.txt"): |
|
|
62 |
# # os.mknod("AE_FCTAE_Kmeans.txt") |
|
|
63 |
# # fo = open("AE_FCTAE_Kmeans.txt", "a") |
|
|
64 |
# clf = KMeans(n_clusters=typenum) |
|
|
65 |
# t0 = time.time() |
|
|
66 |
# clf.fit(encoded_factors) # 模型训练 |
|
|
67 |
# km_batch = time.time() - t0 # 使用kmeans训练数据消耗的时间 |
|
|
68 |
# |
|
|
69 |
# print(datatype, typenum) |
|
|
70 |
# print("K-Means算法模型训练消耗时间:%.4fs" % km_batch) |
|
|
71 |
# |
|
|
72 |
# # 效果评估 |
|
|
73 |
# score_funcs = [ |
|
|
74 |
# metrics.adjusted_rand_score, # ARI(调整兰德指数) |
|
|
75 |
# metrics.v_measure_score, # 均一性与完整性的加权平均 |
|
|
76 |
# metrics.adjusted_mutual_info_score, # AMI(调整互信息) |
|
|
77 |
# metrics.mutual_info_score, # 互信息 |
|
|
78 |
# ] |
|
|
79 |
# centers = clf.cluster_centers_ |
|
|
80 |
# # print("centers:") |
|
|
81 |
# # print(centers) |
|
|
82 |
# print("zlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzly") |
|
|
83 |
# labels = clf.labels_ |
|
|
84 |
# print("labels:") |
|
|
85 |
# print(labels) |
|
|
86 |
# labels = list(np.int_(labels)) |
|
|
87 |
# if not os.path.exists("{}/AE_FCTAE_CL.txt".format(resultpath)): |
|
|
88 |
# os.mknod("{}/AE_FCTAE_CL.txt".format(resultpath)) |
|
|
89 |
# np.savetxt("{}/AE_FCTAE_CL.txt".format(resultpath), labels, fmt='%d') |
|
|
90 |
# print("zlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzly") |
|
|
91 |
# # 2. 迭代对每个评估函数进行评估操作 |
|
|
92 |
# for score_func in score_funcs: |
|
|
93 |
# t0 = time.time() |
|
|
94 |
# km_scores = score_func(groundtruth, labels) |
|
|
95 |
# print("K-Means算法:%s评估函数计算结果值:%.5f;计算消耗时间:%0.3fs" % (score_func.__name__, km_scores, time.time() - t0)) |
|
|
96 |
# t0 = time.time() |
|
|
97 |
# jaccard_score = jaccard_coefficient(groundtruth, labels) |
|
|
98 |
# print("K-Means算法:%s评估函数计算结果值:%.5f;计算消耗时间:%0.3fs" % ( |
|
|
99 |
# jaccard_coefficient.__name__, jaccard_score, time.time() - t0)) |
|
|
100 |
# silhouetteScore = silhouette_score(encoded_factors, labels, metric='euclidean') |
|
|
101 |
# davies_bouldinScore = davies_bouldin_score(encoded_factors, labels) |
|
|
102 |
# print("silhouetteScore:", silhouetteScore) |
|
|
103 |
# print("davies_bouldinScore:", davies_bouldinScore) |
|
|
104 |
# print("zlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzlyzly") |
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|