R/copy-pasted-stuff.R

# Creating all of the synth patterns
# scale <- c(4, 7, 10)
# lmh <- c('l','m','h')
# i <- 1
# system.time(
#   for(f in 1:3){
#     for(s in 1:3){
#       for(t in 1:3){
#         print(paste("i =", i, "It should not exceed 27." ))
#         for(j in 1:200){
#           patterns$headshoulders[[i]][[j]] <-SynthesizePattern(head.shoulders, scale[f], lmh[s], lmh[t])
#           patterns$doubletop[[i]][[j]] <-SynthesizePattern(double.top, scale[f], lmh[s], lmh[t])
#           patterns$tripletop[[i]][[j]] <-SynthesizePattern(triple.top, scale[f], lmh[s], lmh[t])
#           patterns$spiketop[[i]][[j]] <-SynthesizePattern(spike.top, scale[f], lmh[s], lmh[t])
#         }
#         i <- i+1
#       }
#     }
#   }
# )

# Creating all of the synth baselines
# double.top.baseline <- CreateCustomTimeSeries( c(0,0,0,0,0), c(1,1,1,1) )
# others.baseline <- CreateCustomTimeSeries( c(0,0,0,0,0,0,0), c(1,1,1,1,1,1) )
# baseline <- list()
# baseline$headshoulders <- list()
# baseline$doubletop <- list()
# baseline$tripletop <- list()
# baseline$spiketop <- list()
# for( i in 1:27){
#   baseline$headshoulders[[i]] <- list()
#   baseline$doubletop[[i]] <- list()
#   baseline$tripletop[[i]] <- list()
#   baseline$spiketop[[i]] <- list()
# }
# scale <- c(4, 7, 10)
# lmh <- c('l','m','h')
# i <- 1
# system.time(
#   for(f in 1:3){
#     for(s in 1:3){
#       for(t in 1:3){
#         print(paste("i =", i, "It should not exceed 27." ))
#         for(j in 1:200){
#           baseline$headshoulders[[i]][[j]] <-SynthesizePattern(others.baseline, scale[f], lmh[s], lmh[t])
#           baseline$doubletop[[i]][[j]] <-SynthesizePattern(double.top.baseline, scale[f], lmh[s], lmh[t])
#           baseline$tripletop[[i]][[j]] <-SynthesizePattern(others.baseline, scale[f], lmh[s], lmh[t])
#           baseline$spiketop[[i]][[j]] <-SynthesizePattern(others.baseline, scale[f], lmh[s], lmh[t])
#         }
#         i <- i+1
#       }
#     }
#   }
# )


# #Query synth patterns with TB
# print("Querying with TB")
# results.tb.noisy <- list()
# results.tb.noisy$headshoulders <- list()
# results.tb.noisy$doubletop <- list()
# results.tb.noisy$tripletop <- list()
# results.tb.noisy$spiketop <- list()
# for( i in 1:27){
#   results.tb.noisy$headshoulders[[i]] <- list()
#   results.tb.noisy$doubletop[[i]] <- list()
#   results.tb.noisy$tripletop[[i]] <- list()
#   results.tb.noisy$spiketop[[i]] <- list()
# }
# results.tb.noisy$time <- system.time(
#   for(i in 1:27){
#     print(paste("Currently at index", i, "out of 27."))
#     for(j in 1:200){
#       results.tb.noisy$headshoulders[[i]][[j]] <- Query(
#         patterns$headshoulders[[i]][[j]],
#         head.shoulders,
#         spearmans.rho.threshold = -1
#       )[[1]]
#       results.tb.noisy$doubletop[[i]][[j]] <- Query(
#         patterns$doubletop[[i]][[j]],
#         double.top,
#         spearmans.rho.threshold = -1
#       )[[1]]
#       results.tb.noisy$tripletop[[i]][[j]] <- Query(
#         patterns$tripletop[[i]][[j]],
#         triple.top,
#         spearmans.rho.threshold = -1
#       )[[1]]
#       results.tb.noisy$spiketop[[i]][[j]] <- Query(
#         patterns$spiketop[[i]][[j]],
#         spike.top,
#         spearmans.rho.threshold = -1
#       )[[1]]
#     }
#   }
# )
#
# print("Querying with DTW")
#
# # Querying synth patterns with dtw
# results.dtw.noisy<-list()
# results.dtw.noisy$headshoulders <- list()
# results.dtw.noisy$doubletop <- list()
# results.dtw.noisy$tripletop <- list()
# results.dtw.noisy$spiketop <- list()
# for( i in 1:27){
#   results.dtw.noisy$headshoulders[[i]] <- list()
#   results.dtw.noisy$doubletop[[i]] <- list()
#   results.dtw.noisy$tripletop[[i]] <- list()
#   results.dtw.noisy$spiketop[[i]] <- list()
# }
# results.dtw.noisy$time <- system.time(
#   for(i in 1:27){
#     print(paste("Currently at index", i, "out of 27."))
#     for(j in 1:200){
#       results.dtw.noisy$headshoulders[[i]][[j]] <- dtw(
#         head.shoulders,
#         patterns$headshoulders[[i]][[j]],
#         step.pattern = symmetric2
#       )$normalizedDistance
#
#       results.dtw.noisy$doubletop[[i]][[j]] <- dtw(
#         double.top,
#         patterns$doubletop[[i]][[j]],
#         step.pattern = symmetric2
#       )$normalizedDistance
#
#       results.dtw.noisy$tripletop[[i]][[j]] <- dtw(
#         triple.top,
#         patterns$tripletop[[i]][[j]],
#         step.pattern = symmetric2
#       )$normalizedDistance
#
#       results.dtw.noisy$spiketop[[i]][[j]] <- dtw(
#         spike.top,
#         patterns$spiketop[[i]][[j]],
#         step.pattern = symmetric2
#       )$normalizedDistance
#     }
#   }
# )
#


#
# # Seperating results into vectors
# print("Seperating into vectors")
# metric<-list()
# for( i in 1:27 ){
#   for(j in 1:200){
#     if(i%%3==1){
#       metric$headshoulders$dtw$low <- c(metric$headshoulders$dtw$low, results.dtw.noisy$headshoulders[[i]][[j]])
#       metric$doubletop$dtw$low <- c(metric$doubletop$dtw$low, results.dtw.noisy$doubletop[[i]][[j]])
#       metric$tripletop$dtw$low <- c(metric$tripletop$dtw$low, results.dtw.noisy$tripletop[[i]][[j]])
#       metric$spiketop$dtw$low <- c(metric$spiketop$dtw$low, results.dtw.noisy$spiketop[[i]][[j]])
#
#       metric$headshoulders$tb$low <- c(metric$headshoulders$tb$low, results.tb.noisy$headshoulders[[i]][[j]])
#       metric$doubletop$tb$low <- c(metric$doubletop$tb$low, results.tb.noisy$doubletop[[i]][[j]])
#       metric$tripletop$tb$low <- c(metric$tripletop$tb$low, results.tb.noisy$tripletop[[i]][[j]])
#       metric$spiketop$tb$low <- c(metric$spiketop$tb$low, results.tb.noisy$spiketop[[i]][[j]])
#     }
#     if(i%%3==2){
#       metric$headshoulders$dtw$medium <- c(metric$headshoulders$dtw$medium, results.dtw.noisy$headshoulders[[i]][[j]])
#       metric$doubletop$dtw$medium <- c(metric$doubletop$dtw$medium, results.dtw.noisy$doubletop[[i]][[j]])
#       metric$tripletop$dtw$medium <- c(metric$tripletop$dtw$medium, results.dtw.noisy$tripletop[[i]][[j]])
#       metric$spiketop$dtw$medium <- c(metric$spiketop$dtw$medium, results.dtw.noisy$spiketop[[i]][[j]])
#
#       metric$headshoulders$tb$medium <- c(metric$headshoulders$tb$medium, results.tb.noisy$headshoulders[[i]][[j]])
#       metric$doubletop$tb$medium <- c(metric$doubletop$tb$medium, results.tb.noisy$doubletop[[i]][[j]])
#       metric$tripletop$tb$medium <- c(metric$tripletop$tb$medium, results.tb.noisy$tripletop[[i]][[j]])
#       metric$spiketop$tb$medium <- c(metric$spiketop$tb$medium, results.tb.noisy$spiketop[[i]][[j]])
#     }
#     if(i%%3==0){
#       metric$headshoulders$dtw$high <- c(metric$headshoulders$dtw$high, results.dtw.noisy$headshoulders[[i]][[j]])
#       metric$doubletop$dtw$high <- c(metric$doubletop$dtw$high, results.dtw.noisy$doubletop[[i]][[j]])
#       metric$tripletop$dtw$high <- c(metric$tripletop$dtw$high, results.dtw.noisy$tripletop[[i]][[j]])
#       metric$spiketop$dtw$high <- c(metric$spiketop$dtw$high, results.dtw.noisy$spiketop[[i]][[j]])
#
#       metric$headshoulders$tb$high <- c(metric$headshoulders$tb$high, results.tb.noisy$headshoulders[[i]][[j]])
#       metric$doubletop$tb$high <- c(metric$doubletop$tb$high, results.tb.noisy$doubletop[[i]][[j]])
#       metric$tripletop$tb$high <- c(metric$tripletop$tb$high, results.tb.noisy$tripletop[[i]][[j]])
#       metric$spiketop$tb$high <- c(metric$spiketop$tb$high, results.tb.noisy$spiketop[[i]][[j]])
#     }
#   }
# }

#Plotting

# #dtw

# plot( density(metric[[1]][[1]][[1]]), main="DTW, All Patterns, Low Noise" )
# lines( density(metric[[2]][[1]][[1]]) )
# lines( density(metric[[3]][[1]][[1]]) )
# lines( density(metric[[4]][[1]][[1]]) )
#
# plot( density(metric[[1]][[1]][[2]]), main="DTW, All Patterns, Medium Noise"  )
# lines( density(metric[[2]][[1]][[2]]) )
# lines( density(metric[[3]][[1]][[2]]) )
# lines( density(metric[[4]][[1]][[2]]) )
#
# plot( density(metric[[1]][[1]][[3]]), main="DTW, All Patterns, High Noise"  )
# lines( density(metric[[2]][[1]][[3]]) )
# lines( density(metric[[3]][[1]][[3]]) )
# lines( density(metric[[4]][[1]][[3]]) )

# #tb
#
# plot( density(metric[[1]][[2]][[1]]), main="TB, All Patterns, Low Noise"  )
# lines( density(metric[[2]][[2]][[1]]) )
# lines( density(metric[[3]][[2]][[1]]) )
# lines( density(metric[[4]][[2]][[1]]) )
#
# plot( density(metric[[1]][[2]][[2]]), main="TB, All Patterns, Medium Noise" )
# lines( density(metric[[2]][[2]][[2]]) )
# lines( density(metric[[3]][[2]][[2]]) )
# lines( density(metric[[4]][[2]][[2]]) )
#
# plot( density(metric[[1]][[2]][[3]]), main="TB, All Patterns, High Noise" )
# lines( density(metric[[2]][[2]][[3]]) )
# lines( density(metric[[3]][[2]][[3]]) )
# lines( density(metric[[4]][[2]][[3]]) )

#ecg rule

# wider.second.peak <- function(window, pips){
#   baseline <- min(pips)
#   ceiling <- max(pips)
#   return( (pips[[4]] - baseline > (2/3)*(ceiling-baseline) ) & (pips[[1]] - baseline < (1/3)*(ceiling-baseline) ) )
# }


#The number of Queries that didnt match
# wrong <- vector("numeric")
# for( i in 1:27 ){
#   x<-vector("numeric")
#   for( j in 1:200 ){
#     x[j]<-results.tb$headshoulders[[i]][[j]]
#   }
#   wrong[[i]] <- length(x[x!=1])
# }

# write.csv(as.matrix(ts), file="tutorial_data/part2_ts.csv")
#
# ts.fromcsv <- read.csv("tutorial_data/part2_ts.csv")
# ts.xts <- xts(ts.fromcsv[[2]], order.by = as.POSIXct(ts.fromcsv[[1]]))
#
#
# ts.with.low.var <- CreateCustomTimeSeries(
#   c(
#     as.vector(runif(50, 4,7)),
#     as.vector(pattern),
#     as.vector(runif(50, 4,7))
#   ),
#   seq(from=10, to=10, length.out=106)
# )
#
# results <- Query(
#   ts.with.low.var,
#   pattern,
#   window.length = 40,
#   return.matched.patterns = T
# )

# ts.headandshoulders <- createRandTimeSeries(
#   50,
#   0,
#   2,
#   c(0,4,2,6,2,4,0),
#   interval = 5
#   )


# for(i in 1:1000){
# ts.headandshoulders <- createRandTimeSeries(
#   50,
#   0,
#   2,
#   c(0,4,2,6,2,4,0),
#   interval = 5
# )
#   variances <- vector("numeric")
#   for(i in length(pattern.headandshoulders):length(ts.headandshoulders)){
#     variances <- c(variances,
#                    var(ts.headandshoulders[(i-length(pattern.headandshoulders)):i]))
#   }
# }

# for( i in 1:length(results)){
#   for( j in 1:length(results[[i]])){
#     plot(
#       results[[i]][[j]],
#       main = c("ts", i, "match", j)
#     )
#   }
# }

# for(i in 1:10){
#   tslist[[i]] <- createRandTimeSeries(
#       100,
#       0,
#       1,
#       c(0,4,2,4+i,2,4,0),
#       interval = 5
#     )




# rule <- function(window, pips){
#   baseline <- mean(pips[[1]], pips[[7]])
#   first.peak <- pips[[2]] - baseline
#   second.peak <- pips[[4]] - baseline
#   return( second.peak > 2*first.peak )
# }
# results <- list()
# for( i in 1:length(tslist)){
#   results[[i]]<-Query(
#     tslist[[i]],
#     pattern,
#     window.length = 45,
#     ruleset = rule,
#     return.matched.patterns = T
#   )
# }
# plotMatches(results)
joshmarsh/TSTestDataUtil documentation built on May 19, 2019, 8:54 p.m.