Skip to content

Commit 9da558a

Browse files
committed
BUG FIX project_waypoints_coloured(): Fix wrong results when projecting waypoint segments (#54 bis).
1 parent 2011979 commit 9da558a

4 files changed

Lines changed: 32 additions & 34 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ importFrom(purrr,map_dbl)
103103
importFrom(purrr,map_df)
104104
importFrom(purrr,map_int)
105105
importFrom(purrr,pmap)
106+
importFrom(purrr,pmap_df)
106107
importFrom(purrr,set_names)
107108
importFrom(stats,approx)
108109
importFrom(stats,as.dendrogram)

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# dynplot 1.1.1
22

3-
* BUG FIX `project_waypoints()`: Fix refactoring issue "Must supply a symbol or a string as argument" (#54).
3+
* BUG FIX `project_waypoints_coloured()`: Fix refactoring issue "Must supply a symbol or a string as argument" (#54).
4+
5+
* BUG FIX `project_waypoints_coloured()`: Fix wrong results when projecting waypoint segments (#54 bis).
46

57
# dynplot 1.1.0
68

R/package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#' @importFrom dynutils is_sparse list_as_tibble %all_in% calculate_distance scale_minmax
1818
#' @import dynwrap
1919
#' @importFrom dyndimred dimred_mds dimred_landmark_mds list_dimred_methods dimred_umap
20-
#' @importFrom purrr %>% map map_df map_chr keep pmap map2 set_names map_int map_dbl list_modify discard
20+
#' @importFrom purrr %>% map map_df map_chr keep pmap map2 set_names map_int map_dbl list_modify discard pmap_df
2121
#' @importFrom purrr map2_df map2_dbl map2_df
2222
#' @importFrom assertthat assert_that
2323
#' @importFrom tidygraph as_tbl_graph tbl_graph

R/project_waypoints.R

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ project_waypoints_coloured <- function(
2424
trajectory_projection_sd = sum(trajectory$milestone_network$length) * 0.05,
2525
color_trajectory = "none"
2626
) {
27-
waypoints$waypoint_network <- waypoints$waypoint_network %>%
27+
wps <- waypoints
28+
wps$waypoint_network <- wps$waypoint_network %>%
2829
rename(
2930
milestone_id_from = .data$from_milestone_id,
3031
milestone_id_to = .data$to_milestone_id
3132
)
3233

3334
assert_that(color_trajectory %in% c("nearest", "none"))
34-
assert_that(setequal(cell_positions$cell_id, colnames(waypoints$geodesic_distances)))
35+
assert_that(setequal(cell_positions$cell_id, colnames(wps$geodesic_distances)))
3536

36-
# project waypoints to dimensionality reduction using kernel and geodesic distances
37-
weights <- waypoints$geodesic_distances %>% stats::dnorm(sd = trajectory_projection_sd)
37+
# project wps to dimensionality reduction using kernel and geodesic distances
38+
weights <- wps$geodesic_distances %>% stats::dnorm(sd = trajectory_projection_sd)
3839
assert_that(all(!is.na(weights)))
3940

4041
weights <- weights / rowSums(weights)
@@ -54,34 +55,28 @@ project_waypoints_coloured <- function(
5455
y
5556
}
5657

57-
if (!is.null(edge_positions)) {
58-
approx_funs <-
59-
edge_positions %>%
60-
gather("comp_name", "comp_value", starts_with("comp_")) %>%
61-
group_by(.data$from, .data$to, .data$comp_name) %>%
62-
summarise(
63-
approx_fun = {
64-
pct <- .data$percentage
65-
cv <- .data$comp_value
66-
list(function(x) stats::approx(pct, cv, x)$y)
67-
},
68-
.groups = "drop"
69-
)
58+
waypoint_positions <-
59+
if (!is.null(edge_positions)) {
60+
comp_names <- colnames(edge_positions) %>% keep(function(x) grepl("comp_", x))
7061

71-
waypoint_positions <-
72-
waypoints$progressions %>%
73-
left_join(approx_funs, by = c("from", "to")) %>%
74-
mutate(
75-
comp_value = map2_dbl(.data$approx_fun, .data$percentage, function(f, pct) f(pct))
76-
) %>%
77-
spread(.data$comp_name, .data$comp_value) %>%
78-
select(.data$waypoint_id, starts_with("comp_")) %>%
79-
left_join(waypoints$waypoints, "waypoint_id")
80-
} else {
81-
waypoint_positions <- (weights %*% positions) %>%
82-
matrix_to_tibble("waypoint_id") %>%
83-
left_join(waypoints$waypoints, "waypoint_id")
84-
}
62+
wps$progressions %>%
63+
select(.data$from, .data$to) %>%
64+
unique() %>%
65+
pmap_df(function(from, to) {
66+
wp_progr <- wps$progressions %>% filter(.data$from == !!from, .data$to == !!to)
67+
edge_pos <- edge_positions %>% filter(.data$from == !!from, .data$to == !!to)
68+
for (cn in comp_names) {
69+
wp_progr[[cn]] <- approx(edge_pos$percentage, edge_pos[[cn]], wp_progr$percentage)$y
70+
}
71+
wp_progr
72+
}) %>%
73+
select(.data$waypoint_id, !!comp_names) %>%
74+
left_join(wps$waypoints, "waypoint_id")
75+
} else {
76+
(weights %*% positions) %>%
77+
matrix_to_tibble("waypoint_id") %>%
78+
left_join(wps$waypoints, "waypoint_id")
79+
}
8580

8681

8782
# add color of closest cell
@@ -99,7 +94,7 @@ project_waypoints_coloured <- function(
9994

10095
segments <- left_join(
10196
waypoint_positions,
102-
waypoints$progressions,
97+
wps$progressions,
10398
by = "waypoint_id"
10499
) %>%
105100
mutate(group = factor(paste0(.data$from, "---", .data$to))) %>%

0 commit comments

Comments
 (0)