1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
library(reticulate)
library(tidyverse)
library(reshape2)

path_to_model<-"python/Model_Bidirectional_NoShape_3state_Tr10000"
  rm(py)
  #py$MLfolder <- 'D:/Imaging_data/ML_test/'
  cat("Loading Python functions and ML model")
  source_python(file.path(getwd(),"python/MLE_functions.py"))
  py$path_to_model <- path_to_model
  source_python(file.path(getwd(),"python/Load_model.py"))


ML_segment_tracks <- function(tracks,directory=NULL){

  #source_python("python/Load_data.py")

  x <- dlply(tracks,.variables = c("cellID","track"), function(x){

    return(x$X)
  })
  names(x) <- NULL

  py$x <- x


  y <- dlply(tracks,.variables = c("cellID","track"), function(x){
    return(x$Y)
  })
  names(y) <- NULL
  py$y <- y

  indices <- cumsum(daply(tracks,.variables = "cellID", function(x){
    return(length(unique(x$track)))
  }))

  indices <- c(0,indices-1)
  cat("import R data")
  source_python("python/Load_Rdata.py")
  cat("Get ML predictions")

  source_python("python/Get_predictions.py")
  cat("Return data")
  #source_python('python/make_table.py')

  if(!is.null(directory)){
    py_save_object(object = x,filename = file.path(directory,tracks$condition[1],"x.pydata"))
    py_save_object(object = y,filename = file.path(directory,tracks$condition[1],"y.pydata"))
    py_save_object(object = allStates,filename = file.path(directory,tracks$condition[1],"allStates.pydata"))



  }

out <-  py$getTrackPiecesForInfo(x, y, allStates)
x0 <- out[[1]]
y0 <- out[[2]]
x1 <- out[[3]]
y1 <- out[[4]]
x2 <- out[[5]]
y2 <- out[[6]]
trnums0 <- out[[7]]
trnums1 <- out[[8]]
trnums2  <- out[[9]]

  names(x0) <- trnums0
  names(x1) <- trnums1
  names(x2) <- trnums2
  names(y0) <- trnums0
  names(y1) <- trnums1
  names(y2) <- trnums2


  x0 <- melt(x0)
  y0 <- melt(y0)
  x0$y <- y0$value
  x0$state <- 0

  x1 <- melt(x1)
  y1 <- melt(y1)
  x1$y <- y1$value
  x1$state <- 1

  x2 <- melt(x2)
  y2 <- melt(y2)
  x2$y <- y2$value
  x2$state <- 2

  all <- rbind(x0,x1,x2)
  all$L1 <- as.numeric(all$L1)
  all <- plyr::arrange(df = all, L1)

  tracks$state <- all$state

  return(tracks)
}
#plotting
#source_python('python/mss.py')
source_python('python/SMMsplot.py')