-
Notifications
You must be signed in to change notification settings - Fork 0
/
femg-02_detect-artifacts.R
executable file
·99 lines (75 loc) · 2.95 KB
/
femg-02_detect-artifacts.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
library(dplyr)
library(purrr)
library(readr)
library(stringr)
library(tidyr)
library(RcppRoll)
library(ggplot2)
#### functions ####
detect_artifacts <- function(df, prefixes, win.sec, flag.threshold) {
roll_range <- function(x, ...) roll_max(x, ...) - roll_min(x, ...)
sample.duration <- diff(df$stimTime.sec[1:2])
nSamples <- win.sec/sample.duration
for (muscleName in prefixes) {
rawVar <- names(df) %>% str_subset(muscleName)
name.z <- paste0(muscleName,'.z')
name.zrange <- paste0(name.z,'range')
name.flagged <- paste0(muscleName, '.flagged')
name.rawfixed <- paste0(muscleName, '.fixed')
name.zfixed <- paste0(muscleName, '.zfixed')
df <- df %>%
mutate(!!name.z := scale(.[[rawVar]])[,1]) %>%
mutate(!!name.zrange := roll_range(.[[name.z]], n = nSamples, fill = NA)) %>%
mutate(!!name.flagged := abs(.[[name.zrange]]) > flag.threshold) %>%
mutate(!!name.flagged := replace_na(.[[name.flagged]], FALSE)) %>%
mutate(!!name.rawfixed := if_else(.[[name.flagged]],
as.numeric(NA),
.[[rawVar]])) %>%
mutate(!!name.zfixed := if_else(.[[name.flagged]],
as.numeric(NA),
.[[name.z]]))
}
return(df)
}
####
#### main ####
output.file.name <- 'data/processed/femg-02_artifacts-labelled'
femg.files <- list.files('data/processed/femg-01_aligned', full.names = TRUE)
prefixes <- c('t.zyg', 't.cor', 'r.zyg', 'r.cor')
##### artifact detection #####
femg.z <- femg.files %>%
map_dfr(read_csv, col_types = 'ddiicddddccccccicic') %>%
# cut anything after the last stimulus (where trilalNum is 0)
filter(trialNo >= 1) %>%
group_by(session) %>%
do(detect_artifacts(., prefixes, win.sec = 0.05, flag.threshold = 3))
##### trial durations #####
# to check where to trim excess data
trial.durations <- femg.z %>%
group_by(session,cued,trialNo) %>%
summarise(stim.duration = max(stimTime.sec, na.rm = TRUE),
nonstim.duration = min(stimTime.sec, na.rm = TRUE)) %>%
ungroup()
trial.durations %>%
ggplot(aes(x=stim.duration)) +facet_grid(cued ~.) + geom_histogram(binwidth = 1)
trial.durations %>%
filter(stim.duration < 1) %>%
arrange(stim.duration)
trial.durations %>%
ggplot(aes(x=-nonstim.duration)) +facet_grid(cued ~.) + geom_histogram(binwidth = 1)
trial.durations %>%
filter(nonstim.duration > -10) %>%
arrange(nonstim.duration)
# count trials
# per session
trial.durations %>% group_by(session) %>% tally()
# per cue
trial.durations %>% group_by(cued) %>% tally()
# total
trial.durations %>% tally()
##### trim pre-stim data and save ####
femg.data <- femg.z %>%
filter(stimTime.sec >= -2)
# save as RData format to save on memory issues when loading the data in analysis step
save(femg.data, file = paste0(output.file.name, '.RData') )
write_csv(femg.data, paste0(output.file.name, '.csv'))