-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepository-load-epoched-referenced-voltage.R
115 lines (99 loc) · 3.95 KB
/
repository-load-epoched-referenced-voltage.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
#' @author Zhengjia Wang
#' @date Feb 08, 2023
#' @license Apache-2.0
#'
#' @title Load and export RAVE voltage data (with epoch and reference)
#' @param subject string in <project/subject> format (e.g. 'demo/DemoSubject')
#' @param electrodes electrodes to export, or \code{NULL} to export all
#' @param reference_name channel reference name
#' @param epoch_name trial epoch table name
#' @param time_window time window, can be a vector of length 2 or a list of
#' vectors with 2 elements; e.g. \code{c(-1, 2)} (1 second before onset to
#' 2 seconds after onset), or \code{list(c(-1,2), c(3,4))} (-1~2 and 3~4 sec)
#' @param save_path where to save exports to, will be created as directory;
#' existing folders will be backed up
#' @returns Nothing. Please go to `save_path` to check exported voltage data
#' @examples
#'
#' snippet <- raveio::load_snippet("repository-load-epoched-referenced-voltage",
#' local = FALSE)
#' snippet(subject = "demo/DemoSubject", reference_name = "default",
#' epoch_name = "auditory_onset", time_window = c(-1, 2),
#' save_path = "exports")
#'
#' END OF DOC
NULL
# ---- Global variables ----------------------------------------------------
# # Uncomment to change variabels
# subject <- "demo/DemoSubject"
# electrodes <- NULL
# reference_name <- "default"
# epoch_name <- "auditory_onset"
# time_window <- c(-1, 2)
# save_path <- "./exports/demo/DemoSubject/voltage"
# ---- Code part ----------------------------------------------------------
force(subject)
force(time_window)
force(save_path)
`%?<-%` <- dipsaus::`%?<-%`
electrodes %?<-% NULL
subject_instance <- raveio::as_rave_subject(subject_id = subject, strict = FALSE)
reference_name <- subject_instance$reference_names[[1]]
epoch_name <- subject_instance$epoch_names[[1]]
if( !epoch_name %in% subject_instance$epoch_names ) {
stop("Cannot find epoch [", epoch_name, "]. Available epoch names are: ",
paste0(subject_instance$epoch_names))
}
if( !reference_name %in% subject_instance$reference_names ) {
stop("Cannot find reference [", reference_name, "]. Available references are: ",
paste0(subject_instance$reference_names))
}
if(!length(electrodes)) {
electrodes <- subject_instance$electrodes
}
raveio::with_future_parallel({
repository <- raveio::prepare_subject_voltage_with_epoch(
subject = subject, electrodes = electrodes,
reference_name = reference_name, epoch_name = epoch_name,
time_windows = time_window
)
})
# reset parallel
if(file.exists(save_path)) {
raveio::backup_file(save_path, quiet = FALSE, remove = TRUE)
}
raveio::dir_create2(file.path(save_path, "data"))
save_path <- normalizePath(save_path)
# save
write.csv(repository$reference_table, file = file.path(save_path, "reference.csv"))
write.csv(repository$electrode_table, file = file.path(save_path, "electrodes.csv"))
write.csv(repository$epoch$table, file = file.path(save_path, "epoch.csv"))
raveio::with_future_parallel({
dipsaus::lapply_async2(repository$electrode_list, function(e) {
arr <- repository$voltage$data_list[[sprintf("e_%d", e)]]
h5file <- file.path(save_path, "data", sprintf("%d.h5", e))
raveio::save_h5(
arr[drop = FALSE], file = h5file, name = "data",
ctype = "numeric", quiet = TRUE
)
raveio::save_h5(
repository$voltage$dimnames$Time, file = h5file, name = "time_points",
ctype = "numeric", quiet = TRUE
)
raveio::save_h5(
as.integer(repository$voltage$dimnames$Trial), file = h5file, name = "trial",
ctype = "integer", quiet = TRUE
)
}, plan = FALSE, callback = function(e) {
sprintf("Exporting | Electrode channel %d", e)
})
})
raveio::save_yaml(list(
subject = subject_instance$subject_id,
sample_rates = unique(subject_instance$raw_sample_rates),
electrodes = dipsaus::deparse_svec(electrodes),
time_window = time_window,
data = "voltage",
refrenced = TRUE
), file = file.path(save_path, "information.yaml"))
future::plan("sequential")