Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. # This program will convert Profusion XML files into a single, combined CSV file that lists staging and scored events
  2. # Prerequisites: XML package
  3. # Upon running for first time, uncomment the "install.packages("XML")" line
  4. #
  5. # How to use: Put XML files into 'xml_path' folder below and run program.
  6.  
  7. #install.packages("XML")
  8. library(XML)
  9.  
  10. # Set project path
  11. project_path <- "C:/NSRR/chat"
  12.  
  13. # Set path for XML files
  14. xml_path <- paste(project_path, "xml", sep="/")
  15.  
  16. # Set path for CSV exports of XML files
  17. csv_path <- paste(project_path, "csv", sep="/")
  18.  
  19. # Set path for merged output CSV file
  20. output_path <- paste(project_path, "output", sep="/")
  21.  
  22. # Grab list of XML files
  23. xmlFiles <- list.files(path = xml_path, pattern = "*.xml")
  24.  
  25. n <- length(xmlFiles)
  26.  
  27. for(i in 1:n) {
  28. pxml <- xmlParse(paste(xml_path, xmlFiles[i], sep="/"))
  29. xmldata <- xmlToDataFrame(getNodeSet(pxml,"//SleepStage"))
  30. write.csv(xmldata, file = file.path(project_path, "sleepstages.csv"), row.names = TRUE)
  31. xml_in <- read.csv(file.path(project_path, "sleepstages.csv"), header = TRUE, sep = ",", )
  32. xml_in$epoch <- xml_in$X
  33. xml_in$SleepStage <- xml_in$text
  34. xml_in$X <- xml_in$text <- NULL
  35. xml_in$EpochStartTime <- ((xml_in$epoch * 30) - 30)
  36. xml_event <- xmlToDataFrame(getNodeSet(pxml, "//ScoredEvent"))
  37. xml_event$Start <- as.numeric(xml_event$Start)
  38. xml_event$Duration <- as.numeric(xml_event$Duration)
  39. xml_event$epoch <- ceiling(xml_event$Start/30)
  40. xml_merged <- merge(xml_in, xml_event, by = "epoch", all.x = TRUE)
  41. xml_merged$pptid <- paste(xmlFiles[i])
  42. xml_merged <- xml_merged[c("pptid", "epoch", "EpochStartTime", "SleepStage", "Name", "Start", "Duration", "Input", "LowestSpO2", "Desaturation")]
  43. filename <- as.character(paste(substr(xmlFiles[[i]], 1, 20), ".csv", sep=""))
  44. write.csv(xml_merged, file = file.path(csv_path, filename), row.names = FALSE)
  45. }
  46.  
  47. # Remove temporary 'sleepstages.csv' file
  48. file.remove(file.path(project_path, "sleepstages.csv"))
  49.  
  50. # Merge all the CSVs of interest
  51. csvs_to_merge <- list.files(path = csv_path, pattern = "*.csv")
  52. all_events <- do.call("rbind", lapply(paste(csv_path,"/",csvs_to_merge,sep=""), read.csv, header = TRUE))
  53. 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