Skip to content

Commit 7087947

Browse files
committed
refactor function a bit more
1 parent 9da558a commit 7087947

1 file changed

Lines changed: 18 additions & 22 deletions

File tree

R/project_waypoints.R

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,7 @@ project_waypoints_coloured <- function(
3434
assert_that(color_trajectory %in% c("nearest", "none"))
3535
assert_that(setequal(cell_positions$cell_id, colnames(wps$geodesic_distances)))
3636

37-
# project wps to dimensionality reduction using kernel and geodesic distances
38-
weights <- wps$geodesic_distances %>% stats::dnorm(sd = trajectory_projection_sd)
39-
assert_that(all(!is.na(weights)))
40-
41-
weights <- weights / rowSums(weights)
42-
positions <- cell_positions %>%
43-
select(.data$cell_id, .data$comp_1, .data$comp_2) %>%
44-
slice(match(colnames(weights), .data$cell_id)) %>%
45-
column_to_rownames("cell_id") %>%
46-
as.matrix()
47-
48-
# make sure weights and positions have the same cell_ids in the same order
49-
assert_that(all.equal(colnames(weights), rownames(positions)))
50-
5137
# calculate positions
52-
matrix_to_tibble <- function(x, rownames_column) {
53-
y <- as_tibble(x)
54-
y[[rownames_column]] <- rownames(x)
55-
y
56-
}
57-
5838
waypoint_positions <-
5939
if (!is.null(edge_positions)) {
6040
comp_names <- colnames(edge_positions) %>% keep(function(x) grepl("comp_", x))
@@ -73,9 +53,25 @@ project_waypoints_coloured <- function(
7353
select(.data$waypoint_id, !!comp_names) %>%
7454
left_join(wps$waypoints, "waypoint_id")
7555
} else {
56+
# project wps to dimensionality reduction using kernel and geodesic distances
57+
weights <- wps$geodesic_distances %>% stats::dnorm(sd = trajectory_projection_sd)
58+
assert_that(all(!is.na(weights)))
59+
60+
weights <- weights / rowSums(weights)
61+
positions <- cell_positions %>%
62+
select(.data$cell_id, .data$comp_1, .data$comp_2) %>%
63+
slice(match(colnames(weights), .data$cell_id)) %>%
64+
column_to_rownames("cell_id") %>%
65+
as.matrix()
66+
67+
# make sure weights and positions have the same cell_ids in the same order
68+
assert_that(all.equal(colnames(weights), rownames(positions)))
69+
7670
(weights %*% positions) %>%
77-
matrix_to_tibble("waypoint_id") %>%
78-
left_join(wps$waypoints, "waypoint_id")
71+
as.data.frame() %>%
72+
rownames_to_column("waypoint_id") %>%
73+
left_join(wps$waypoints, "waypoint_id") %>%
74+
as_tibble()
7975
}
8076

8177

0 commit comments

Comments
 (0)