responseDynamics.R 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. ##' Function to Calculate Various Parameters of Response Dynamics
  2. ##'
  3. ##' The following parameters are calculated:
  4. ##' Time to 10% of Response Maximum (Minimum),
  5. ##' Time to 90% of Response Maximum (Minimum),
  6. ##' Time to Response Maximum (Minimum),
  7. ##' Time to 90% of Response Maximum (Minimum) after Max (Min),
  8. ##' Time to 10% of Response Maximum (Minimum) after Max (Min),
  9. ##' Risetime to Maximum (Minimum),
  10. ##' Falltime from Maximum (Minimum),
  11. ##' Mean Response Maximum (Minimum).
  12. ##'
  13. ##' @param x Input dataframe with one row per 5 sec trace
  14. ##' @param from Response starting percentage for Risetime calculation,
  15. ##' defaults to 10 percent of response maximum (or minimum, respectively).
  16. ##' @param to Response ending percentage for Risetime calculation,
  17. ##' defaults to 90 percent of response maximum (or minimum, respectively).
  18. ##'
  19. ##' @return New columns with calculated response dynamic parameters
  20. ##'
  21. ##
  22. responseDynamics <- function(x, from=0.1, to=0.9) {
  23. ## sub_sub is the time window where the Max and Min is expected
  24. ## starting 50ms after valve opening)
  25. avo <- 50 ## After Valve Opening
  26. sub_sub <- x[(avo+1) :1000]
  27. ## win is the window size before and after time of Max (or Min) to calculate the mean of the response
  28. win <- 25
  29. ## Max is the time pint of maximum response
  30. Max <- which(x==max(sub_sub))[1] + avo
  31. Max10_1 <- which(x <= max(x * from))
  32. Max10_1 <- Max10_1[which(Max10_1 <= Max)]
  33. Max10_1 <- Max10_1[length(Max10_1)]
  34. Max90_1 <- which(x <= max(x * to))
  35. Max90_1 <- Max90_1[which(Max90_1 <= Max)]
  36. Max90_1 <- Max90_1[length(Max90_1)]
  37. Max10_2 <- which(x <= max(x * from))
  38. Max10_2 <- Max10_2[which(Max10_2 >= Max)]
  39. Max10_2 <- Max10_2[1]
  40. Max90_2 <- which(x <= max(x * to))
  41. Max90_2 <- Max90_2[which(Max90_2 >= Max)]
  42. Max90_2 <- Max90_2[1]
  43. Mean_max <- mean(x[(Max - win + 1) : (Max + win)])
  44. ## Min is the time pint of minimum response
  45. Min <- which(x==min(sub_sub))[1] + avo
  46. Min10_1 <- which(x >= min(x * from))
  47. Min10_1 <- Min10_1[which(Min10_1 <= Min)]
  48. Min10_1 <- Min10_1[length(Min10_1)]
  49. Min90_1 <- which(x >= min(x * to))
  50. Min90_1 <- Min90_1[which(Min90_1 <= Min)]
  51. Min90_1 <- Min90_1[length(Min90_1)]
  52. Min10_2 <- which(x >= min(x * from))
  53. Min10_2 <- Min10_2[which(Min10_2 >= Min)]
  54. Min10_2 <- Min10_2[1]
  55. Min90_2 <- which(x >= min(x * to))
  56. Min90_2 <- Min90_2[which(Min90_2 >= Min)]
  57. Min90_2 <- Min90_2[1]
  58. Mean_min <- mean(x[(Min - win + 1) : (Min + win)])
  59. Timeto10Max <- Max10_1
  60. Timeto10Min <- Min10_1
  61. Max_rise <- Max90_1 - Max10_1
  62. Max_fall <- Max10_2 - Max90_2
  63. Min_rise <- Min90_1 - Min10_1
  64. Min_fall <- Min10_2 - Min90_2
  65. if(length(Max10_1) < 1){
  66. Max10_1 <- NA
  67. Max_rise <- NA
  68. }
  69. if(length(Max90_1) < 1){
  70. Max90_1 <- NA
  71. Max_rise <- NA
  72. }
  73. if(length(Max90_2) < 1){
  74. Max90_2 <- NA
  75. Max_fall <- NA
  76. }
  77. if(length(Max10_2) < 1){
  78. Max10_2 <- NA
  79. Max_fall <- NA
  80. }
  81. if(length(Min10_1) < 1){
  82. Min10_1 <- NA
  83. Min_rise <- NA
  84. }
  85. if(length(Min90_1) < 1){
  86. Min90_1 <- NA
  87. Min_rise <- NA
  88. }
  89. if(length(Min90_2) < 1){
  90. Min90_2 <- NA
  91. Min_fall <- NA
  92. }
  93. if(length(Min10_2) < 1){
  94. Min10_2 <- NA
  95. Min_fall <- NA
  96. }
  97. df <- c(Max10_1,
  98. Max90_1,
  99. Max,
  100. Max90_2,
  101. Max10_2,
  102. Max_rise,
  103. Max_fall,
  104. Min10_1,
  105. Min90_1,
  106. Min,
  107. Min90_2,
  108. Min10_2,
  109. Min_rise,
  110. Min_fall,
  111. Mean_max,
  112. Mean_min)
  113. return(df)
  114. }