-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscDeepCluster.py
130 lines (102 loc) · 4.86 KB
/
scDeepCluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from time import time
import math, os
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.nn import Parameter
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
from scDCC import scDCC
import numpy as np
import collections
from sklearn import metrics
import h5py
import scanpy.api as sc
from preprocess import read_dataset, normalize
from utils import cluster_acc, generate_random_pair
if __name__ == "__main__":
# setting the hyper parameters
import argparse
parser = argparse.ArgumentParser(description='train',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--n_clusters', default=8, type=int)
parser.add_argument('--label_cells', default=0.2, type=float)
parser.add_argument('--label_cells_files', default='label_selected_cells_1.txt')
parser.add_argument('--n_pairwise', default=0, type=int)
parser.add_argument('--n_pairwise_error', default=0, type=float)
parser.add_argument('--batch_size', default=256, type=int)
parser.add_argument('--data_file', default='../data/10X_PBMC_select_2100.h5')
parser.add_argument('--maxiter', default=2000, type=int)
parser.add_argument('--pretrain_epochs', default=300, type=int)
parser.add_argument('--gamma', default=1., type=float,
help='coefficient of clustering loss')
parser.add_argument('--update_interval', default=1, type=int)
parser.add_argument('--tol', default=0.001, type=float)
parser.add_argument('--ae_weights', default=None)
parser.add_argument('--save_dir', default='results/scDCC_p0_1/')
parser.add_argument('--ae_weight_file', default='AE_weights_p0_1.pth.tar')
args = parser.parse_args()
data_mat = h5py.File(args.data_file)
x = np.array(data_mat['X'])
y = np.array(data_mat['Y'])
data_mat.close()
if not os.path.exists(args.label_cells_files):
indx = np.arange(len(y))
np.random.shuffle(indx)
label_cell_indx = indx[0:int(np.ceil(args.label_cells*len(y)))]
else:
label_cell_indx = np.loadtxt(args.label_cells_files, dtype=np.int)
eva_indx = np.delete(np.arange(len(y)), label_cell_indx)
x = x[eva_indx,:]
y = y[eva_indx]
# preprocessing scRNA-seq read counts matrix
adata = sc.AnnData(x)
adata.obs['Group'] = y
adata = read_dataset(adata,
transpose=False,
test_split=False,
copy=True)
adata = normalize(adata,
size_factors=True,
normalize_input=True,
logtrans_input=True)
input_size = adata.n_vars
print(args)
print(adata.X.shape)
print(y.shape)
x_sd = adata.X.std(0)
x_sd_median = np.median(x_sd)
print("median of gene sd: %.5f" % x_sd_median)
ml_ind1, ml_ind2, cl_ind1, cl_ind2 = np.array([]), np.array([]), np.array([]), np.array([])
sd = 2.5
model = scDCC(input_dim=adata.n_vars, z_dim=32, n_clusters=args.n_clusters,
encodeLayer=[256, 64], decodeLayer=[64, 256], sigma=sd, gamma=args.gamma).cuda()
print(str(model))
t0 = time()
if args.ae_weights is None:
model.pretrain_autoencoder(x=adata.X, raw_counts=adata.raw.X, size_factor=adata.obs.size_factors,
batch_size=args.batch_size, epochs=args.pretrain_epochs, ae_weights=args.ae_weight_file)
else:
if os.path.isfile(args.ae_weights):
print("==> loading checkpoint '{}'".format(args.ae_weights))
checkpoint = torch.load(args.ae_weights)
model.load_state_dict(checkpoint['ae_state_dict'])
else:
print("==> no checkpoint found at '{}'".format(args.ae_weights))
raise ValueError
print('Pretraining time: %d seconds.' % int(time() - t0))
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
y_pred, _, _, _, _ = model.fit(X=adata.X, X_raw=adata.raw.X, sf=adata.obs.size_factors, y=y, batch_size=args.batch_size, num_epochs=args.maxiter,
ml_ind1=ml_ind1, ml_ind2=ml_ind2, cl_ind1=cl_ind1, cl_ind2=cl_ind2,
update_interval=args.update_interval, tol=args.tol, save_dir=args.save_dir)
print('Total time: %d seconds.' % int(time() - t0))
eval_cell_y_pred = np.delete(y_pred, label_cell_indx)
eval_cell_y = np.delete(y, label_cell_indx)
acc = np.round(cluster_acc(eval_cell_y, eval_cell_y_pred), 5)
nmi = np.round(metrics.normalized_mutual_info_score(eval_cell_y, eval_cell_y_pred), 5)
ari = np.round(metrics.adjusted_rand_score(eval_cell_y, eval_cell_y_pred), 5)
print('Evaluating cells: ACC= %.4f, NMI= %.4f, ARI= %.4f' % (acc, nmi, ari))
if not os.path.exists(args.label_cells_files):
np.savetxt(args.label_cells_files, label_cell_indx, fmt="%i")