Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This program will convert Profusion XML files into a single, combined CSV file that lists staging and scored events
- # Prerequisites: XML package
- # Upon running for first time, uncomment the "install.packages("XML")" line
- #
- # How to use: Put XML files into 'xml_path' folder below and run program.
- #install.packages("XML")
- library(XML)
- # Set project path
- project_path <- "C:/NSRR/chat"
- # Set path for XML files
- xml_path <- paste(project_path, "xml", sep="/")
- # Set path for CSV exports of XML files
- csv_path <- paste(project_path, "csv", sep="/")
- # Set path for merged output CSV file
- output_path <- paste(project_path, "output", sep="/")
- # Grab list of XML files
- xmlFiles <- list.files(path = xml_path, pattern = "*.xml")
- n <- length(xmlFiles)
- for(i in 1:n) {
- pxml <- xmlParse(paste(xml_path, xmlFiles[i], sep="/"))
- xmldata <- xmlToDataFrame(getNodeSet(pxml,"//SleepStage"))
- write.csv(xmldata, file = file.path(project_path, "sleepstages.csv"), row.names = TRUE)
- xml_in <- read.csv(file.path(project_path, "sleepstages.csv"), header = TRUE, sep = ",", )
- xml_in$epoch <- xml_in$X
- xml_in$SleepStage <- xml_in$text
- xml_in$X <- xml_in$text <- NULL
- xml_in$EpochStartTime <- ((xml_in$epoch * 30) - 30)
- xml_event <- xmlToDataFrame(getNodeSet(pxml, "//ScoredEvent"))
- xml_event$Start <- as.numeric(xml_event$Start)
- xml_event$Duration <- as.numeric(xml_event$Duration)
- xml_event$epoch <- ceiling(xml_event$Start/30)
- xml_merged <- merge(xml_in, xml_event, by = "epoch", all.x = TRUE)
- xml_merged$pptid <- paste(xmlFiles[i])
- xml_merged <- xml_merged[c("pptid", "epoch", "EpochStartTime", "SleepStage", "Name", "Start", "Duration", "Input", "LowestSpO2", "Desaturation")]
- filename <- as.character(paste(substr(xmlFiles[[i]], 1, 20), ".csv", sep=""))
- write.csv(xml_merged, file = file.path(csv_path, filename), row.names = FALSE)
- }
- # Remove temporary 'sleepstages.csv' file
- file.remove(file.path(project_path, "sleepstages.csv"))
- # Merge all the CSVs of interest
- csvs_to_merge <- list.files(path = csv_path, pattern = "*.csv")
- all_events <- do.call("rbind", lapply(paste(csv_path,"/",csvs_to_merge,sep=""), read.csv, header = TRUE))
- write.csv(all_events, file = file.path(output_path, "all_events.csv"), row.names = FALSE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement