Skip to content

Commit 479cedf

Browse files
author
Vitalii Kleshchevnikov
committed
Version: 0.1.11
New: - method == "kmeans" returns C matrix so that data %*% C gives cluster averages - updated website - added method to create pch_fit objects Bug fix: C matrix is now returned correctly as data points * archtypes
1 parent 80788d8 commit 479cedf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+498
-124
lines changed

.DS_Store

0 Bytes
Binary file not shown.

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: ParetoTI
22
Type: Package
33
Title: R toolbox for Archetypal Analysis and Pareto Task Inference on single cell data
4-
Version: 0.1.10
4+
Version: 0.1.11
55
Author: Vitalii Kleshchevnikov
66
Maintainer: Vitalii Kleshchevnikov <[email protected]>
77
Authors@R: c(person("Vitalii", "Kleshchevnikov",, "[email protected]", role = c("aut", "cre")))

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export(map_go_annot)
3737
export(map_gwas_annot)
3838
export(measure_activity)
3939
export(merge_arch_dist)
40+
export(pch_fit)
4041
export(plot.gam_deriv)
4142
export(plot.r_pch_fit)
4243
export(plot_arc)

R/fit_pch.R

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,22 @@ fit_pch = function(data, noc = as.integer(3), I = NULL, U = NULL,
8080
delta = 0, verbose = FALSE, conv_crit = 1e-4,
8181
maxiter = 500, check_installed = T,
8282
order_by = seq(1, nrow(data)),
83-
order_type = c("cosine", "side", "align")[3],
84-
volume_ratio = c("t_ratio", "variance_ratio", "none")[1],
83+
order_type = c("align", "cosine", "side"),
84+
volume_ratio = c("t_ratio", "variance_ratio", "none"),
8585
converge_else_fail = TRUE,
8686
var_in_dims = FALSE, normalise_var = TRUE,
8787
method = c("pcha", "kmeans"), method_options = list()) {
8888

89-
if(check_installed) .py_pcha_installed()
89+
# check arguments
90+
method = match.arg(method)
91+
volume_ratio = match.arg(volume_ratio)
92+
order_type = match.arg(order_type)
93+
94+
95+
if(method == "pcha"){
96+
97+
if(check_installed) .py_pcha_installed()
9098

91-
if(isTRUE(method[1] == "pcha")){
9299
# run PCHA -------------------------------------------------------------------
93100

94101
# coerce to matrix
@@ -112,8 +119,10 @@ fit_pch = function(data, noc = as.integer(3), I = NULL, U = NULL,
112119
})
113120
#---------------------------------------------------------------------------
114121

115-
} else if(isTRUE(method[1] == "kmeans")) {
122+
} else if(method == "kmeans") {
123+
116124
# run k-means --------------------------------------------------------------
125+
117126
# set defaults or replace them with provided options
118127
default = list(iter.max = 10, nstart = 1,
119128
algorithm = c("Hartigan-Wong", "Lloyd", "Forgy",
@@ -122,19 +131,26 @@ fit_pch = function(data, noc = as.integer(3), I = NULL, U = NULL,
122131
options = c(default[default_retain], method_options)
123132

124133
res = kmeans(Matrix::t(data), centers = as.integer(noc),
125-
iter.max = default$iter.max, nstart = default$nstart,
126-
algorithm = default$algorithm, trace = default$trace)
127-
res = list(XC = t(res$centers),
128-
S = Matrix::sparseMatrix(i = res$cluster,
129-
j = seq_len(length(res$cluster)), x = 1),
130-
C = Matrix::sparseMatrix(i = res$cluster,
131-
j = seq_len(length(res$cluster)), x = 0),
134+
iter.max = options$iter.max, nstart = options$nstart,
135+
algorithm = options$algorithm, trace = options$trace)
136+
137+
# Create S as binary cluster membership matrix
138+
S = Matrix::sparseMatrix(i = res$cluster,
139+
j = seq_len(length(res$cluster)), x = 1)
140+
# compute C so that X %*% C gives cluster averages
141+
C = S / matrix(Matrix::rowSums(S), nrow = nrow(S), ncol = ncol(S), byrow = FALSE)
142+
C = Matrix::t(C)
143+
144+
# create pch_fit object to be returned
145+
res = list(XC = t(res$centers), S = S, C = C,
132146
SSE = res$tot.withinss, varexpl = res$betweenss / res$totss)
133147
if(!is.null(rownames(data))) rownames(res$XC) = rownames(data)
134148
colnames(res$XC) = NULL
135149
class(res) = "pch_fit"
150+
136151
#---------------------------------------------------------------------------
137-
} else stop("method should be pcha or kmeans")
152+
153+
} else stop("method should be pcha or kmeans")
138154

139155
if(is.null(res)) return(NULL)
140156

@@ -145,7 +161,7 @@ fit_pch = function(data, noc = as.integer(3), I = NULL, U = NULL,
145161
arch_order = .find_archetype_order(XC2, noc, order_type = order_type)
146162
res$XC = res$XC[, arch_order]
147163
res$S = res$S[arch_order, ]
148-
res$C = res$C[arch_order, ]
164+
res$C = res$C[, arch_order]
149165
} else {
150166
# when only one archetype make sure data is still in the matrix form
151167
res$XC = matrix(res$XC, length(res$XC), 1)
@@ -158,7 +174,7 @@ fit_pch = function(data, noc = as.integer(3), I = NULL, U = NULL,
158174
# when calculating convex hull and volume of the polytope
159175
# adjust number of dimensions to noc
160176
data_dim = seq(1, noc-1)
161-
if(volume_ratio == "t_ratio" & nrow(data) >= length(data_dim) & noc > 2){
177+
if(volume_ratio == "t_ratio" & nrow(data) >= length(data_dim) & noc > 2 & method != "poisson_aa"){
162178
# calculate volume or area of the polytope only when number of archetypes (noc) > number of dimenstions which means
163179
# find volume of the convex hull of the data
164180
hull_vol = fit_convhulln(data[data_dim, ], positions = FALSE)
@@ -201,7 +217,7 @@ fit_pch = function(data, noc = as.integer(3), I = NULL, U = NULL,
201217

202218
} else {
203219

204-
# 3 none
220+
# 3 NA
205221

206222
if(volume_ratio == "t_ratio" & isTRUE(converge_else_fail)) message(paste0("Convex hull and t-ratio not computed for noc: ", noc," and nrow(data) = ", nrow(data),". fit_pch() can calculate volume or area of the polytope only when\nthe number of archetypes (noc) > the number of dimensions (when polytope is convex):\ncheck that noc > nrow(data),\nselect only revelant dimensions or increase noc"))
207223
res$hull_vol = NA
@@ -1095,3 +1111,20 @@ plot_dim_var = function(rand_arch,
10951111
}
10961112
res
10971113
}
1114+
1115+
##' @rdname fit_pch
1116+
##' @name pch_fit
1117+
##' @description \code{pch_fit()} a constructor function for the "pch_fit" class
1118+
##' @export pch_fit
1119+
pch_fit = function(XC, S, C, SSE, varexpl, arc_vol, hull_vol, t_ratio, var_vert,
1120+
var_dim, total_var, summary, call) {
1121+
1122+
# add integrity checks
1123+
1124+
# create object
1125+
structure(list(XC = XC, S = S, C = C, SSE = SSE, varexpl = varexpl,
1126+
arc_vol = arc_vol, hull_vol = hull_vol, t_ratio = t_ratio,
1127+
var_vert = var_vert, var_dim = var_dim, total_var = total_var,
1128+
summary = summary, call = call),
1129+
class = "pch_fit")
1130+
}

docs/LICENSE-text.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/Comparison_to_kmeans.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Loading
Loading
Loading
Loading
Loading

docs/articles/Hepatocyte_example.html

Lines changed: 35 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Loading
Loading
Loading
Loading
Loading
Loading

docs/articles/index.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/authors.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 15 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/align_arc.html

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
4.74 KB
Loading

0 commit comments

Comments
 (0)