Commit 9bf349f4 authored by jlopez's avatar jlopez
Browse files

Update

parent a34f7551
.Rproj.user
.Rhistory
.RData
.Ruserdata
# ===================================================================================
distance3D <- function(a, b) {
return(sqrt((b[1] - a[1])^2 + (b[2] - a[2])^2 + (b[3] - a[3])^2))
}
importDataMosquito <- function(path) {
# ===================================================================================
distance4D2 <- function(a, b, f) {
t <- (b[4] - a[4])^2
d <- (b[1] - a[1])^2 + (b[2] - a[2])^2 + (b[3] - a[3])^2 + t
score <-d
return(score)
}
distance4D <- function(a, b, f) {
t <- (b[4] - a[4])^2
d <- (b[1] - a[1])^2 + (b[2] - a[2])^2 + (b[3] - a[3])^2
score <- sqrt(d + (t*0.5))
if(t == 1) {
t <- 1
}
if((d/t) > 1) {
score <- score + 800
}
return(score)
}
# ===================================================================================
distanceTime <- function(a, b) {
return(sqrt((b - a)^2))
}
# ===================================================================================
distanceApp <- function(m1, m2, start) {
if(m1$id == m2$id) {
return(9999)
} else {
if(m1$min < start && m2$min < start) {
return(9999)
}
a <- c(m1$minX, m1$minY, m1$minZ, m1$min)
b <- c(m2$minX, m2$minY, m2$minZ, m2$min)
timeD <- distance4D(a,b, 2)
if(timeD <= 1) {
xyzD <- distance3D(a,b)
return(xyzD)
} else {
return(9999)
}
#return(distance4D2(a,b))
}
}
# ===================================================================================
distanceDis <- function(m1, m2, start) {
if(m1$id == m2$id) {
return(9999)
} else {
if(m1$max < start && m2$max < start) {
return(9999)
}
a <- c(m1$maxX, m1$maxY, m1$maxZ, m1$max)
b <- c(m2$maxX, m2$maxY, m2$maxZ, m2$max)
timeD <- distance4D(a,b, 2)
if(timeD <= 1) {
xyzD <- distance3D(a,b)
return(xyzD)
} else {
return(9999)
}
}
}
# ===================================================================================
distanceM2 <- function(m1, m2, minSize) {
if(m1$max <= m2$min) {
a <- c(m1$maxX, m1$maxY, m1$maxZ, m1$max)
b <- c(m2$minX, m2$minY, m2$minZ, m2$min)
xyzD <- distance4D(a,b, 2)
return(xyzD)
} else {
return(999)
}
}
# ===================================================================================
importOldDataMosquito <- function(path) {
data <- NULL
ext <- file_ext(path)
if(ext == "csv") {
data <- read.csv2(path, dec = ".")
} else if(ext == "txt") {
data <- read.table(path,header=T)
}
data <- data %>% filter(!is.na(XSplined)) %>% filter(!is.na(YSplined)) %>% filter(!is.na(ZSplined))
id <- unique(data$Oldlabel)
size <- length(id)
tabMinMax <- data.frame(id = id, cluster = rep(0, size), min = rep(0, size), max = rep(0, size), minX = rep(0, size), minY = rep(0, size), minZ = rep(0, size), maxX = rep(0, size), maxY = rep(0, size), maxZ = rep(0, size))
for(index in 1:size) {
currentM <- data %>% filter(Oldlabel == tabMinMax[index,]$id)
#currentM <- currentM %>% filter(!is.na(X)) %>% filter(!is.na(Y)) %>% filter(!is.na(Z))
tabMinMax[index,]$cluster <- currentM[1,]$Newlabel
tabMinMax[index,]$minX <- currentM[1,]$XSplined
tabMinMax[index,]$minY <- currentM[1,]$YSplined
tabMinMax[index,]$minZ <- currentM[1,]$ZSplined
tabMinMax[index,]$maxX <- currentM[nrow(currentM),]$XSplined
tabMinMax[index,]$maxY <- currentM[nrow(currentM),]$YSplined
tabMinMax[index,]$maxZ <- currentM[nrow(currentM),]$ZSplined
tabMinMax[index,]$min = min(currentM$time, na.rm = TRUE)
tabMinMax[index,]$max = max(currentM$time, na.rm = TRUE)
}
minM <- min(tabMinMax$min)
maxM <- max(tabMinMax$max)
mosquitoT1 <- tabMinMax %>% select(id, cluster, min) %>% rename(time = min)
mosquitoT2 <- tabMinMax %>% select(id, cluster, max) %>% rename(time = max)
mosquitoT <- rbind(mosquitoT1, mosquitoT2) %>% arrange(cluster)
tabMinMax$time <- tabMinMax$max - tabMinMax$min
RAWOLDDATA <<- tabMinMax
return(mosquitoT)
}
importDataMosquito <- function(path) {
data <- NULL
ext <- file_ext(path)
......@@ -15,10 +176,19 @@ importDataMosquito <- function(path) {
data <- read.table(path,header=TRUE)
}
data <- data %>% filter(!is.na(X)) %>% filter(!is.na(Y)) %>% filter(!is.na(Z))
data <- data %>% filter(!is.na(XSplined)) %>% filter(!is.na(YSplined)) %>% filter(!is.na(ZSplined))
data <- data %>% filter (YSplined >= -0.70) %>% filter (YSplined <= 0.70) %>% filter (XSplined <= 200) %>% filter (ZSplined <= 180)
RAWPOINT <<- data
#For debug
#data$object <- data$Newlabel
id <- unique(data$object)
size <- length(id)
tabMinMax <- data.frame(id = id, min = rep(0, size), max = rep(0, size), minX = rep(0, size), minY = rep(0, size), minZ = rep(0, size), maxX = rep(0, size), maxY = rep(0, size), maxZ = rep(0, size))
......@@ -29,13 +199,13 @@ importDataMosquito <- function(path) {
#currentM <- currentM %>% filter(!is.na(X)) %>% filter(!is.na(Y)) %>% filter(!is.na(Z))
tabMinMax[index,]$minX <- currentM[1,]$X
tabMinMax[index,]$minY <- currentM[1,]$Y
tabMinMax[index,]$minZ <- currentM[1,]$Z
tabMinMax[index,]$minX <- currentM[1,]$XSplined
tabMinMax[index,]$minY <- currentM[1,]$YSplined
tabMinMax[index,]$minZ <- currentM[1,]$ZSplined
tabMinMax[index,]$maxX <- currentM[nrow(currentM),]$X
tabMinMax[index,]$maxY <- currentM[nrow(currentM),]$Y
tabMinMax[index,]$maxZ <- currentM[nrow(currentM),]$Z
tabMinMax[index,]$maxX <- currentM[nrow(currentM),]$XSplined
tabMinMax[index,]$maxY <- currentM[nrow(currentM),]$YSplined
tabMinMax[index,]$maxZ <- currentM[nrow(currentM),]$ZSplined
tabMinMax[index,]$min = min(currentM$time, na.rm = TRUE)
tabMinMax[index,]$max = max(currentM$time, na.rm = TRUE)
......@@ -48,5 +218,9 @@ importDataMosquito <- function(path) {
mosquitoT2 <- tabMinMax %>% select(id, max) %>% rename(time = max)
mosquitoT <- rbind(mosquitoT1, mosquitoT2) %>% arrange(id)
tabMinMax$time <- tabMinMax$max - tabMinMax$min
RAWDATA <<- tabMinMax
return(mosquitoT)
}
\ No newline at end of file
MenuGauche = sidebarMenu(id = "sidebarmenu",
menuItem("Home", tabName = "Home", icon = icon("home", lib="font-awesome")),
menuItem("Manual", tabName = "Manual", icon = icon("home", lib="font-awesome")),
tags$br(), tags$br(),
sliderInput("sizeMosquito", "Minimum size range mosquito (secondes) :", min = 0, max= 10, value = 2, step = 0.1),
sliderInput("timeMosquito", "Minimum detection mosquito (secondes) :", min = 0, max= 15, value = 2, step = 0.1),
sliderInput("maxRangeTimeMosquito", "Maximum size group mosquito (secondes) :", min = 0, max = 100, value = 18, step = 1),
sliderInput("maxDistanceMosquito", "Maximum distance next mosquito (meter) :", min = 0, max=1, value = 0.33, step = 0.01),
#tags$br(),
#numericInput("numberMosquito", "Number mosquito :", min = 0, value = 0),
tags$br(),
selectInput("selectMosquito", "Show born mosquito :", c(), multiple = TRUE),
checkboxInput("showCouplingPossibility", "Show coupling possibility", FALSE),
numericInput("startF", "Start female release (second)", value = 23),
tags$br(),
numericInput("startCoupling", "Start coupling (second)", value = 0),
numericInput("endCoupling", "End coupling (second)", value = 0),
actionButton("Clustering", "Clustering", style="background-color: #2980b9; border-color: #2980b9"),
tags$br(), tags$br(),
selectInput("pathMosquito", "Construct path mosquito :", c(), multiple = TRUE),
downloadButton("exportCluster", "Export", style="background-color: #27ae60; border-color: #27ae60" ),
actionButton("filterMosquito", "Filter", style = "primary"),
tags$br(), tags$br(), tags$br(),
......
......@@ -6,15 +6,18 @@ library(dplyr)
library(devtools)
library(randomcoloR)
library(ggplot2)
library(plotly)
library(xfun)
library(stats)
source("./R/helper_functions.R", local = T)
source("./R/menugauche.R", local = T)
source("./pages/pages_def_home.R", local = T)
source("./pages/pages_def_old.R", local = T)
options(encoding = 'UTF-8')
#style <- tags$style(HTML(readLines("www/added_styles.css")) )
style <- tags$style(HTML(readLines("www/added_styles.css")) )
UI <- dashboardPage(
skin = "blue",
dashboardHeader(title = "RMosquito"),
......@@ -24,10 +27,11 @@ UI <- dashboardPage(
shinyjs::useShinyjs(),
#extendShinyjs(text = jscode),
tags$head(tags$link(rel = "stylesheet", type = "text/css", href = "bootstrap.min.readable.css")) ,
#tags$head(style),
tags$head(style),
tags$head(tags$script(src = "message-handler.js")),
tabItems(
tabItem(tabName = "Home", tabHome)
tabItem(tabName = "Home", tabHome),
tabItem(tabName = "Manual", tabOld)
)
)
)
......@@ -35,7 +39,14 @@ UI <- dashboardPage(
server <- function( input, output, session) {
source("./server/opt_home.R", local=TRUE)
timeMaxVisible <<- 0
timeMinVisible <<- 0
clusterNotPrint <<- c()
source("./server/opt_home.R", local=T)
source("./server/opt_old.R", local=TRUE)
}
shinyApp(ui = UI, server = server)
\ No newline at end of file
......@@ -7,10 +7,20 @@ tabHome = fluidPage(align="left",
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
plotOutput("plotRawData", height = "1600px")
includeHTML("./www/spinner0.html"),
plotlyOutput("plotRawData",height = "800px"),
br(), br(),
includeHTML("./www/spinner.html"),
#actionButton("hideCluster", "Show/Hide elements", style="background-color: #27ae60; border-color: #27ae60"),
#actionButton("showAllCluster", "Show all elements", style="background-color: #2980b9; border-color: #2980b9"),
plotlyOutput("plotClustering",height = "800px")
)
)
\ No newline at end of file
tabOld = fluidPage(align="left",
"Old",
column(width = 12,
fileInput("dataOldMosquito", "Choose CSV File : ",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
plotlyOutput("plotOldRawData",height = "800px")
)
)
\ No newline at end of file
output$plotRawData = renderPlot({
req(input$dataMosquito)
# ===================================================================================
output$exportCluster <- downloadHandler(
filename = function() {
paste("result.csv", sep = "")
},
content = function(file) {
clusters <- as.numeric(names(h_clusters))
path <- input$dataMosquito$datapath
result <- RAWPOINT %>% filter(object %in% clusters)
result$oldobject <- result$object
for(c in c(1:max(h_clusters))) {
clusters <- as.numeric(names(h_clusters[h_clusters == c]))
for(id in clusters) {
#change the id
result <- result %>% mutate(object = replace(object, object == id, c))
}
}
result <- result %>% arrange(object)
result <- result %>% select(object, oldobject, time, XSplined, YSplined, ZSplined, VXSplined, VYSplined, VZSplined)
mosquitoT <- importDataMosquito(path)
write.csv(result, file, row.names = FALSE)
}
)
dataM <<- mosquitoT
# ===================================================================================
eventButtonCluster <- eventReactive(input$Clustering, {
color_mosquito <<- distinctColorPalette(nrow(mosquitoT)/2)
color_mosquito[1] <<- "#000000"
session$sendCustomMessage(type = 'start_gear', message = "start")
uuid = unique(dataM$id)
tabMinMax <- RAWDATA %>% filter(time >= input$sizeMosquito)
size2 <- nrow(tabMinMax)
individu <- matrix(rep(999, size2*size2), nrow=size2, ncol = size2)
rownames(individu) <- tabMinMax$id
colnames(individu) <- tabMinMax$id
for(i in 1:(size2-1)) {
for(j in (i+1):size2) {
m1 <- tabMinMax[i,]
m2 <- tabMinMax[j,]
dist <- round(distanceM2(m1, m2, isolate(input$timeMosquito)), digits = 4)
individu[i,j] = dist
individu[j,i] = dist
}
}
updateSelectInput(session, "selectMosquito",
label = "Select mosquito",
choices = uuid,
selected = uuid[1]
)
JIMMY <<- individu
updateSelectInput(session, "pathMosquito",
label = "Construct path mosquito",
choices = uuid
)
d <- as.dist(individu)
h <<- hclust(d)
kMosquito <<- size2 - length(which((h$height < 999)==TRUE))
h_clusters <<- cutree(h, k=kMosquito)
vertical_id <- c(uuid[1])
#DELETE SOUND
p1 <- ggplot()
mosquitoT <- dataM
if(length(uuid) > 1) {
for(index in c(1:length(uuid))) {
mosquito <- filter(mosquitoT, id == uuid[index])
p1 <- p1 + geom_line(aes(y = id, x = time), size=1.5, colour = color_mosquito[index],
data = mosquito)
if(uuid[index] %in% vertical_id) {
p1 <- p1 +geom_vline(aes(xintercept = time), size=0.5, mosquito, linetype = "dashed", colour = color_mosquito[index])
all_LL <- list()
indexALL <- 1
for(f in c(1:kMosquito)) {
mosquitoCluster <- as.numeric(names(h_clusters[h_clusters==f]))
MM <- data.frame(id = numeric(), time = numeric())
for(mos in mosquitoCluster) {
mosquito <- filter(mosquitoT, id == mos)
MM <- rbind(MM, mosquito)
}
mosquitoCluster <- unique((MM %>% arrange(time))$id)
indexE <- 1
indexA <- 1
maxlongCluster <- c()
currentL <- c()
all_List <- list()
lastM <- NULL
for(mos in mosquitoCluster) {
mosquito <- RAWDATA %>% filter(id==mos)
if(indexE == 1) {
currentL <- c(currentL, mos)
} else {
dist <- abs(lastM$max - mosquito$min)
if(dist > isolate(input$timeMosquito)) {
all_List[[indexA]] <- currentL
currentL <- c(mos)
indexA <- indexA + 1
} else {
a <- c(lastM$maxX, lastM$maxY, lastM$maxZ)
b <- c(mosquito$minX, mosquito$minY, mosquito$minZ)
distXYZ <- distance3D(a, b)
if(distXYZ > isolate(input$maxDistanceMosquito)) {
all_List[[indexA]] <- currentL
currentL <- c(mos)
indexA <- indexA + 1
} else {
currentL <- c(currentL, mos)
}
}
}
lastM <- mosquito
indexE <- indexE + 1
}
all_List[[indexA]] <- currentL
if(length(all_List) > 1) {
indexSelect <- 1
lastMaxdistance <- 0
for(mos in c(1:length(all_List))) {
LL <- all_List[[mos]]
totaltime <- 0
for(mt in LL) {
mosquito <- mosquito <- RAWDATA %>% filter(id==mt)
totaltime <- totaltime + (mosquito$max - mosquito$min)
}
if(totaltime >= isolate(input$maxRangeTimeMosquito)) {
all_LL[[indexALL]] <- LL
indexALL <- indexALL + 1
}
}
} else {
all_LL[[indexALL]] <- all_List[[1]]
indexALL <- indexALL + 1
}
}
p1 <- p1 + scale_y_continuous(breaks=uuid)
cont <- TRUE
p1
numberL <- 1
Nlist <- list()
while(cont) {
if(length(all_LL)>=1) {
maxC <- 0
indexLL <- 1
LLC <- NULL
for(idd in c(1:length(all_LL))) {
LL <- all_LL[[idd]]
mosquito <- RAWDATA %>% filter(id==LL[1])