Sfoglia il codice sorgente

Added patient environment (for OOP)

Deben Oldert 9 anni fa
parent
commit
0fddecf598
4 ha cambiato i file con 74 aggiunte e 11 eliminazioni
  1. 37 0
      code/functions.R
  2. 4 4
      code/head.R
  3. 18 0
      code/patient.R
  4. 15 7
      code/state.R

+ 37 - 0
code/functions.R

@@ -15,3 +15,40 @@ specify_decimal <- function(x, k) format(round(x, k), nsmall=k)
 #
 # Returns => bool, true contains all values/ false not
 consistent <- function(a) suppressWarnings(all(a == min(a):max(a)))
+
+# group. Makes groups of consistent integer vectors
+#   a => vector containing integers
+#   n => integer defining minimum group size
+#
+# Returns => list, containing consistent integer vectors
+group <- function(a, n=1){
+  if(consistent(a)) return (list(A=a))
+  else{
+    l_ <- list()
+    cur_ <- 1
+    for(i in a){
+      if(is.null(l_$A)){ # Create first group
+        l_$A <- c(i)
+        next
+      }
+
+      if(max(l_[[cur_]])+1 == i) l_[[cur_]] <- c(l_[[cur_]], i) # Add to group
+      else{ # Create new group
+        cur_ <- cur_ + 1
+        l_[[LETTERS[cur_]]] <- c(i)
+      }
+    }
+    cur_ <- 1
+    while(cur_ <= length(l_)){
+      if(length(l_[[cur_]]) < n){
+        l_[[cur_]] <- NULL
+        cur_ <- 0
+      }
+      cur_ <- cur_ + 1
+    }
+    #for(i in 1:length(l_))
+    names(l_) <- LETTERS[1:length(l_)]
+    remove(cur_)
+    return(l_)
+  }
+}

+ 4 - 4
code/head.R

@@ -16,16 +16,16 @@ for(i in 1:nrow(DATA)){
 differ_ <- 0.5
 
 # Algorithm to calculate walk phase
-f <- function(x) (sin((x - 5) / 2.7) / 30) + WALKBASE
+f <- function(x) (sin((x - 5) / 2.7) / 30) + patient$WALKBASE
 
 # Draw Optimum walk
-lines(WALKING, f(WALKING), col = "orange", type = "l")
+lines(patient$WALKING, f(patient$WALKING), col = "orange", type = "l")
 
 # Check if each point is close to optimum track
-WALK <- data.frame(index=WALKING)
+WALK <- data.frame(index=patient$WALKING)
 WALK$probability <- 0
 
-for(i in WALKING){
+for(i in patient$WALKING){
    prob_ <- 1 - abs((abs(DATA[i,]$Head.y - f(i))) / differ_)
    WALK[WALK$index==i,]$probability <- if(prob_ > 1) 1 else if(prob_ < 0) 0 else prob_
 }

+ 18 - 0
code/patient.R

@@ -0,0 +1,18 @@
+# PATIENT CLASS
+
+patient <- new.env()
+
+  patient$WALKING <- NULL
+
+  patient$SITTING <- NULL
+
+  patient$UP <- NULL
+
+  patient$DOWN <- NULL
+
+  patient$WALKBASE <- NULL
+
+  patient$SITBASE <- NULL
+
+
+  patient$height <- function() abs(patient$SITBASE) + patient$WALKBASE

+ 15 - 7
code/state.R

@@ -65,20 +65,28 @@ DATA <- DATA[complete.cases(DATA),]
 rownames(DATA) <- 1:nrow(DATA)
 DATA$index = as.integer(rownames(DATA))
 
-WALKING <- strtoi(rownames(DATA[DATA$state==1,]))
+# USE PATIENT ENVIRONMENT
+source("code/patient.R")
 
-if(!consistent(WALKING)){
+patient$WALKING <- strtoi(rownames(DATA[DATA$state==1,]))
+patient$SITTING <- group(strtoi(rownames(DATA[DATA$state==4,])), 10)
+patient$UP <- strtoi(rownames(DATA[DATA$state==2,]))
+patient$DOWN <- strtoi(rownames(DATA[DATA$state==3,]))
+
+patient$SITBASE <- mean(c(DATA[as.integer(unlist(patient$SITTING)),]$FootLeft.y, DATA[as.integer(unlist(patient$SITTING)),]$FootRight.y))
+
+if(!consistent(patient$WALKING)){
   stop("Patient not consistently walking, (Maybe he/she fell). Anyway, we can't analyse this data", call. = FALSE)
 }
 
 # CALCULATE STRAIGHT WALKING PATH
-yPrediction <-lm(Head.y ~ I(index^2)+index, data=DATA[min(WALKING):max(WALKING),])
-yPredicted <- as.vector(predict(yPrediction, data.frame(index=WALKING)))
+yPrediction <-lm(Head.y ~ I(index^2)+index, data=DATA[min(patient$WALKING):max(patient$WALKING),])
+yPredicted <- as.vector(predict(yPrediction, data.frame(index=patient$WALKING)))
 
-WALKBASE <- mean(c(yPredicted[1], tail(yPredicted, n=1)))
+patient$WALKBASE <- mean(c(yPredicted[1], tail(yPredicted, n=1)))
 
-for(i in WALKING[1]:(length(WALKING) + WALKING[1] - 1)){
-  DATA[i,]$Head.y <- DATA[i,]$Head.y - (yPredicted[i-WALKING[1]+1] - WALKBASE)
+for(i in patient$WALKING[1]:(length(patient$WALKING) + patient$WALKING[1] - 1)){
+  DATA[i,]$Head.y <- DATA[i,]$Head.y - (yPredicted[i-patient$WALKING[1]+1] - patient$WALKBASE)
 }
 
 plot(POINTS$probability, type = "l")