-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.R
141 lines (113 loc) · 5.19 KB
/
test.R
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
131
132
133
134
135
136
137
138
139
140
141
# devtools::create("~/UNSW/VafaeeLab/psdR")
setwd("~/UNSW/VafaeeLab/psdR")
Sys.setenv(keep.source.pkgs = TRUE)
devtools::document()
devtools::install()
devtools::load_all()
library(SingleCellExperiment)
Sys.setenv(RETICULATE_PYTHON = "../.py_venv/bin/python")
library(reticulate)
source_python('psd.py')
#ran the below line to create mit license
# usethis::use_mit_license()
compare_psd_implementation <- function(data, show_result = FALSE){
# start <- Sys.time()
# psd_r_data <- psdR::psd_fft_apply(data)
# end <- Sys.time()
# print(paste('Using R fft apply, psd execution time :', difftime(end, start, units = 'secs')))
start <- Sys.time()
psd_r_data <- psdR::psd(data)
end <- Sys.time()
print(paste('Using R, psd execution time :', difftime(end, start, units = 'secs')))
if (show_result) {
print('psd output :')
print(psd_r_data)
}
start <- Sys.time()
psd_py_data <- t(psd(t(data)))
end <- Sys.time()
print(paste('Using python via reticulate, psd execution time :', difftime(end, start, units = 'secs')))
if (show_result) {
print('psd output :')
print(psd_py_data)
}
dimnames(psd_py_data) <- dimnames(psd_r_data)
print(all.equal(psd_r_data, psd_py_data))
}
data1 <- matrix(rnorm(10), nrow=2, ncol=5)
compare_psd_implementation(data1)
# [1] "Using R fft apply, psd execution time : 0.00855898857116699"
# [1] "Using R, psd execution time : 0.000279664993286133"
# [1] "Using python via reticulate, psd execution time : 0.00247502326965332"
# [1] TRUE
data2 <- matrix(rnorm(9), nrow=3, ncol=3)
compare_psd_implementation(data2)
# [1] "Using R fft apply, psd execution time : 0.00578093528747559"
# [1] "Using R, psd execution time : 0.000316381454467773"
# [1] "Using python via reticulate, psd execution time : 0.00202488899230957"
# [1] TRUE
data <- matrix(c(-1.2225888, 1.34042501, 0.4259692,-0.23716855,
1.7332330, 0.21851021, -1.0023597, 1.00827522,
-0.6907052, 0.06109564),
nrow=2, ncol=5, byrow = TRUE)
compare_psd_implementation(data)
# [1] "Using R fft apply, psd execution time : 0.0092620849609375"
# [1] "Using R, psd execution time : 0.00032496452331543"
# [1] "Using python via reticulate, psd execution time : 0.00273799896240234"
# [1] TRUE
data <- readRDS(file = 'TabulaMuris_Heart_10X.rds')
x <- counts(data)
x <- x[rowSums(x) != 0, ]
x <- as.matrix(x)
compare_psd_implementation(x)
# [1] "Using R, psd execution time : 210.639530658722"
# [1] "Using R sweep, psd execution time : 212.108340740204"
# [1] "Using python via reticulate, psd execution time : 192.281036138535"
# [1] TRUE
# [1] "Using R fft apply, psd execution time : 205.484230279922"
# [1] "Using R, psd execution time : 219.209401130676"
# [1] "Using R fft apply, psd execution time : 211.930328607559"
# [1] "Using R, psd execution time : 212.845811367035"
# [1] "Using python via reticulate, psd execution time : 191.988132476807"
# [1] TRUE
# [1] "Using R fft apply, psd execution time : 205.381190061569"
# [1] "Using R, psd execution time : 204.640120029449"
# [1] "Using python via reticulate, psd execution time : 191.78946185112"
# [1] TRUE
##########################################
# complexity
data <- readRDS(file = 'TabulaMuris_Heart_10X.rds')
class_colname <- "cell_type1"
x <- as.matrix(SingleCellExperiment::counts(data))
classes <- SingleCellExperiment::colData(data)[class_colname]
classes <- classes[colnames(x),]
start <- Sys.time()
x <- x[rowSums(x) != 0, ]
f1 <- complexity(x, classes)
end <- Sys.time()
print(paste('complexity score = ', f1))
print(paste('complexity execution time :', difftime(end, start, units = 'secs')))
complexity(x)
complexity(x, c())
###############################################
data <- readRDS(file = 'TabulaMuris_Heart_10X.rds')
class_colname <- "cell_type1"
x <- as.matrix(SingleCellExperiment::counts(data))
classes <- SingleCellExperiment::colData(data)[class_colname]
classes <- classes[colnames(x),]
complexity_df_1 <- compare_methods(x, classes,
c('CPM', 'NormCPM', 'TMM', 'Linnorm', 'Seurat'))
complexity_df_2 <- compare_methods(x, classes,
c('Seurat'))
complexity_df_3 <- compare_methods(x, classes,
c('CPM'))
complexity_df_4 <- compare_methods(data = x, classes = classes, plot_file_name = "cpm_tsne.png")
complexity_df_5 <- compare_methods(data = x, classes = NA, plot_file_name = "noclass_cpm_tsne.png")
complexity_df_6 <- compare_methods(data = x, methods = c(), classes = classes, plot_file_name = "none_tsne.png")
complexity_df_7 <- compare_methods(data = x, methods = c(), classes = NA, plot_file_name = "noclass_none_tsne.png")
complexity_df_8 <- compare_methods(data = x, methods = c(), psd = FALSE, classes = classes, plot_file_name = "none_nopsd_tsne.png")
complexity_df_9 <- compare_methods(data = x, methods = c(), psd = FALSE, classes = NA, plot_file_name = "noclass_none_nopsd_tsne.png")
x <- x[, !is.na(classes)]
classes <- classes[!is.na(classes)]
complexity_df_10 <- compare_methods(data = x, methods = c(), psd = FALSE, classes = classes, plot_file_name = "colour_tobesame.png")
complexity_df_11 <- compare_methods(data = x, methods = c(), psd = FALSE, classes = NA, plot_file_name = "nocolour_tobesame.png")