-
Notifications
You must be signed in to change notification settings - Fork 0
/
femg-04_clean-and-select-baseline.R
114 lines (83 loc) · 3.32 KB
/
femg-04_clean-and-select-baseline.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
library(dplyr)
library(readr)
library(stringr)
#### functions ####
summarise_flagged_trials <- function(df, prefixes) {
sort_unique <- function(x) {
y <- unique(x)
y[order(y)] }
output <- list()
output[['flaggedOnAnyMuscle']] <- c()
for (muscleName in prefixes) {
name.flagged <- paste0(muscleName, '.flagged')
allFlaggedTrials <- df %>%
filter( .[[name.flagged]] ) %>%
distinct(expt.trialNo) %>%
pull(expt.trialNo)
nTrials <- n_distinct(df$expt.trialNo)
nFlaggedTrials <- length(allFlaggedTrials)
output[[muscleName]] <- list('allFlaggedTrials' = allFlaggedTrials,
'nTrials' = nTrials,
'nFlaggedTrials' = nFlaggedTrials,
'pcFlaggedTrials' = 100*nFlaggedTrials/nTrials)
output[['flaggedOnAnyMuscle']] <- output[['flaggedOnAnyMuscle']] %>%
c(allFlaggedTrials)
}
output[['flaggedOnAnyMuscle']] <- output[['flaggedOnAnyMuscle']] %>% sort_unique()
return(output)
}
#### main ####
output.folder <- 'data/processed/femg-04_cleaned-time-selected'
if (!dir.exists(output.folder)) dir.create(output.folder)
# trim to just baseline and stimulus
# chosen based on time plot
baseline.duration.sec <- 0.2
baseline.offset.sec <- 1.0
femg.binned <- read_csv('data/processed/femg-03_binned-100ms-clean.csv') %>%
mutate(phase = replace(phase, # values to replace
time >= -(baseline.duration.sec + baseline.offset.sec) &
time < -baseline.offset.sec, # conditions
'baseline')) %>%
filter(phase == 'stimulus' | phase == 'baseline')
#### export data for classifier (python) ####
# #femg channels
prefixes <- c('t.zyg', 't.cor', 'r.zyg', 'r.cor')
window.subsets <- list(c(0,4))
femg.out <- list()
for (slice in seq_along(window.subsets)) {
subwindow <- window.subsets[[slice]]
femg.subset <- femg.binned %>%
mutate(expt.trialNo = paste(session,trialNo) %>% as.factor %>% as.numeric) %>%
filter( (phase == 'baseline') | (time >= subwindow[1] & time < subwindow[2]) )
femg.out[[slice]] <- list()
for (muscleName in prefixes) {
femg.out[[slice]][[muscleName]] <- list()
# names of variables depending on the muscle
name_clean = paste0(str_remove(muscleName,'\\.'),'_clean')
name_norm = paste0(str_remove(muscleName,'\\.'),'_norm')
femg.out[[slice]][[muscleName]] <- femg.subset %>%
select(time,
trial = trialNo,
session,
phase,
cue = cued,
!!name_clean := !!paste0(muscleName,'.fixed'),
!!name_norm := !!paste0(muscleName,'.zfixed')) %>%
na.omit()
nByCue <- femg.out[[slice]][[muscleName]] %>%
group_by(cue, session, trial) %>%
tally() %>% tally() %>% summarise(n = sum(n))
N <- nByCue %>% summarise(n = sum(n))
print(
paste('slice:', subwindow[1], '-', subwindow[2],'s',
'muscle:', muscleName, 'N =', N)
)
print(nByCue)
write_csv(femg.out[[slice]][[muscleName]],
paste0(output.folder,'/fEMG_clean_',
str_remove(muscleName,'\\.'),
'_',subwindow[1],
'to',subwindow[2],'sec_100ms-binned.csv')
)
}
}