Skip to content

Instantly share code, notes, and snippets.

@JohannesNE
Created February 21, 2020 11:58
Show Gist options
  • Save JohannesNE/06ea0e2d9f11dcadc094e81382022d48 to your computer and use it in GitHub Desktop.
Save JohannesNE/06ea0e2d9f11dcadc094e81382022d48 to your computer and use it in GitHub Desktop.
For testing/visualising VSCaptureDrgVent realtime waveforms
library(tidyverse)
library(ggplot2)
# Path to folder with CSVs
path <- '~/path/to/folder'
pressure <- read_csv(paste0(path, 'Airway_pressureDrgWaveExport.csv'),
col_names = c('time', 'unix_ms', 'pressure', 'insp'),
col_types = list(col_datetime(format = '%m/%d/%Y %H:%M:%S'),
col_double(),
col_double(),
col_character()),
na = character())
flow <- read_csv(paste0(path, 'Flow_inspiratory_expiratoryDrgWaveExport.csv'),
col_names = c('time', 'unix_ms', 'flow', 'insp'),
col_types = list(col_datetime(format = '%m/%d/%Y %H:%M:%S'),
col_double(),
col_double(),
col_character()),
na = character())
comb <- bind_cols(pressure, flow) %>%
select(-ends_with('1')) %>%
mutate(insp = cumsum(insp == 'InspStart'),
rel_ms = unix_ms - first(unix_ms), # Relative Unix timestamp
diff_ms = c(0,diff(unix_ms)), # Consecutive difference between unix timestamps
naive_ms = seq(0, by = 20, along.with = insp)) # Naive calculation of relative time
comb_w_vol <- comb %>%
mutate(dVol = c(flow[1], (flow[1:length(flow)-1] + flow[2:length(flow)])/2) * (1000/60e3) * 20) %>% # Trapezoid integration rule
group_by(insp) %>%
mutate(volume = cumsum(dVol)) %>%
filter(insp != 0)
comb_w_vol %>%
group_by(time) %>%
summarise(n())
ggplot(comb, aes(naive_ms, flow)) +
geom_line() +
geom_point(data = filter(comb, diff_ms > 100))
ggplot(comb_w_vol, aes(naive_ms, flow)) +
geom_line() +
facet_wrap(~insp, scales = 'free_x')
ggplot(comb_w_vol, aes(naive_ms, pressure, color = factor(insp))) +
geom_line(show.legend = FALSE) +
scale_color_discrete(limits = sample(1:13))
# Pressure Volume loop
ggplot(comb_w_vol, aes(pressure, volume, color = factor(insp))) +
geom_path()
# Flow Volume Loop
ggplot(comb_w_vol, aes(volume, flow, color = factor(insp))) +
geom_path()
ggplot(comb_w_vol, aes(naive_ms, volume, color = factor(insp))) +
geom_line() +
geom_point(data = filter(comb_w_vol, diff_ms > 100)) +
geom_line(aes(naive_ms, flow))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment