forked from dipterix/rave-gists
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-generate-subcortical-surfaces.R
More file actions
99 lines (84 loc) · 2.62 KB
/
image-generate-subcortical-surfaces.R
File metadata and controls
99 lines (84 loc) · 2.62 KB
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
#' @author Zhengjia Wang
#' @date Jan 08, 2025
#' @license Apache-2.0
#'
#' @title Generate subcortical surfaces from atlas segmentation files
#'
#' @param subject_code RAVE Subject code
#' @param project_name RAVE project name; default is `"YAEL"`
#' @param roi_path path to the atlas file
#' @param roi_keys a list of ROI keys; see 'Examples'
#' @param preview whether to preview the results; default is `TRUE`
#'
#' @examples
#'
#' snippet <- raveio::load_snippet("image-generate-subcortical-surfaces")
#' snippet(subject_code = "DemoSubject",
#' project_name = "YAEL",
#' roi_path = "~/rave_data/raw_dir/DemoSubject/rave-imaging/fs/mri/aparc+aseg.mgz",
#'
#' roi_keys = list(
#' # name = c(left key, right key)
#' "hippocampus" = c(17, 53)
#' ),
#'
#' preview = TRUE)
#'
#'
#' END OF DOC
NULL
# ---- Variable Section --------------------------------------------------------
# subject_code <- "DemoSubject"
# project_name <- "YAEL"
#
# roi_path <- "~/rave_data/raw_dir/DemoSubject/rave-imaging/fs/mri/aparc+aseg.mgz"
#
# roi_keys <- list(
# "hippocampus" = c(17, 53)
# )
#
# preview <- TRUE
# ---- Code Section ------------------------------------------------------------
# initialize variables
`%?<-%` <- dipsaus::`%?<-%`
project_name %?<-% "YAEL"
preview %?<-% TRUE
# ---- start!
subject <- raveio::RAVESubject$new(project_name = project_name, subject_code = subject_code, strict = FALSE)
brain <- raveio::rave_brain(subject)
if(is.null(brain)) {
stop("RAVE subject does not have brain objects.")
}
volume <- threeBrain::read_volume(roi_path)
subcortical_path <- raveio::dir_create2(file.path(brain$base_path, "surf", "subcortical"))
# generate subcortical ROI surfaces
lapply(names(roi_keys), function(name) {
keys <- roi_keys[[name]]
if(!length(keys)) { return() }
if(length(keys) == 1) {
keys <- c(keys, keys)
}
left_key <- as.numeric(keys[[1]])
right_key <- as.numeric(keys[[2]])
left_fname <- sprintf("lh.%s-%.0f", name, left_key)
right_fname <- sprintf("rh.%s-%.0f", name, right_key)
threeBrain::volume_to_surf(
volume = volume,
threshold_lb = left_key - 0.5,
threshold_ub = left_key + 0.5,
save_to = file.path(subcortical_path, left_fname)
)
threeBrain::volume_to_surf(
volume = volume,
threshold_lb = right_key - 0.5,
threshold_ub = right_key + 0.5,
save_to = file.path(subcortical_path, right_fname)
)
return()
})
if(preview) {
brain <- raveio::rave_brain(subject, surfaces = names(roi_keys))
print(brain$plot())
}
message("Done. Please check folder\n ", subcortical_path)
invisible(subcortical_path)