{"cells":[{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"pkg.list <- c(\"svMisc\",\"ParallelLogger\",\"rjson\")\npkg.diff <- setdiff(pkg.list, installed.packages() )\nprint(pkg.diff)\nif( length(pkg.diff) ) install.packages( pkg.diff )","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"library(stringi)\nlibrary(dplyr)\nlibrary(data.table)\nlibrary(svMisc )\nlibrary(parallel)\nlibrary(ParallelLogger)\nlibrary(png)\nlibrary(rjson)\n\noptions(scipen = 999)\noptions(repr.plot.width = 12, repr.plot.height = 12)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dirs <- list(\n    work =  \"../input/indoor-location-navigation\" ,\n    metadata = \"../input/indoor-location-navigation/metadata\",\n    train = \"../input/indoor-location-navigation/train\"\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Functions."},{"metadata":{},"cell_type":"markdown","source":"Function to fetch the datas from input file"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\n\nget_df_from_file <- function(filename, types = NULL ){\n  \n  ParallelLogger::logInfo(\"[start] filename: \", filename)\n  \n  dd <- read.csv2(filename, sep=\"\\t\", header=F, skip=0, stringsAsFactors = F, comment.char = \"#\",\n                  col.names = paste0(\"V\", seq_len(9))\n                  )\n  dd <- dd %>% data.table()\n  \n  #\n  #-- read meta ---\n  #\n  meta_raw <- readLines(filename, n = 10) \n  meta_raw <- meta_raw[ meta_raw %>% stri_detect_regex(\"^#\\\\tSiteID\") ]\n  tmp <- stri_match_all_regex(filename, \"../input/indoor-location-navigation/(test|train)/(\\\\w+/)?(\\\\w\\\\d/)?(\\\\w+).txt\") %>% \n        unlist()\n  \n  meta <- list(\n    type     = tmp[2],\n    file_id  = tmp[5],\n    floor_id = tmp[4] %>% stri_replace_all_fixed(\"/\",\"\"),\n    site_id  = stri_match_first_regex(meta_raw, \"^#\\\\tSiteID\\\\:(\\\\w+)\")[1, 2]\n  )\n  \n  #\n  #--- read data ---\n  #\n  dfl_meta <- list(\n    \"TYPE_WAYPOINT\" = list( ncol=4,  fnames = c('wy_x','wy_y') ),\n    \"TYPE_GYROSCOPE\" = list( ncol=6, suff = '.gyr',      fnames = c('tg_x','tg_y','tg_z','tg_a')  ),\n    \"TYPE_ACCELEROMETER\" = list( ncol=6, suff = '.acc',  fnames = c('acc_x','acc_y','acc_z','acc_a') ),\n    \"TYPE_MAGNETIC_FIELD\" =  list( ncol=6, suff = '.mf', fnames = c('mf_x','mf_y','mf_z','mf_a') ),\n    \"TYPE_ROTATION_VECTOR\" = list( ncol=6, suff = '.rv', fnames = c('rv_x','rv_y','rv_z','rv_a') ) ,\n    \"TYPE_ACCELEROMETER_UNCALIBRATED\" = list( ncol=9, suff = '.accu', fnames = c('accu_x','accu_y','accu_z','accu_x2','accu_y2','accu_z2','accu_a')   ),\n    \"TYPE_GYROSCOPE_UNCALIBRATED\" =  list( ncol=9, suff = '.gyru', fnames = c('gyru_x','gyru_y','gyru_z','gyru_x2','gyru_y2','gyru_z2','gyru_a')  ),\n    \"TYPE_MAGNETIC_FIELD_UNCALIBRATED\" =  list( ncol=9, suff = '.mfu', fnames = c('mfu_x','mfu_y','mfu_z','mfu_x2','mfu_y2','mfu_z2','mfu_a')  ),\n    \"TYPE_WIFI\" =  list( ncol=7, suff = '.wifi', fnames = c('wf_ssid','wf_bssid','wf_rssi','wf_freq','wf_lst'))\n  )  \n  \n  #-- if types not param - take all types\n  if(is.null(types)) types = names(dfl_meta)  \n  \n  #--output\n  out <- list()  \n    \n  #-- init out datas\n  for( type in types ) out[[type]] = NULL\n  \n  #-- fill datas\n  for( type in types ) {\n    \n    if( nrow( dd[ V2 == type ] ))  {\n      \n      tblock = dd[ V2 == type ][, 1:dfl_meta[[type]]$ncol]\n      if(type=='TYPE_WAYPOINT') tblock[,3:4] <- lapply( tblock[,3:4], as.numeric )\n      if(type=='TYPE_WIFI') tblock[,5:7] <- lapply( tblock[,5:7], as.numeric )\n      if(type %in% \n          c('TYPE_ROTATION_VECTOR',\n            'TYPE_MAGNETIC_FIELD',\n            'TYPE_ACCELEROMETER',\n            'TYPE_GYROSCOPE') ) tblock[,3:6] <- lapply( tblock[,3:6], as.numeric )\n      if(type %in% \n         c('TYPE_ACCELEROMETER_UNCALIBRATED',\n           'TYPE_GYROSCOPE_UNCALIBRATED',\n           'TYPE_MAGNETIC_FIELD_UNCALIBRATED') ) tblock[,3:9] <- lapply( tblock[,3:9], as.numeric )\n      \n      \n      tblock <- tblock[, -2] \n      names(tblock) <- c('id', dfl_meta[[type]]$fnames )\n      out[[ type ]] = tblock\n      \n    }#--if\n    \n  }#--for\n    \n  \n  #\n  #-- merging blocks\n  #\n  res <- NULL\n  res <- merge(out$TYPE_GYROSCOPE, out$TYPE_ACCELEROMETER, by = c('id'), suffixes = NULL, all = T, no.dups =T ) %>%\n          merge(y= out$TYPE_MAGNETIC_FIELD, by = c('id'), suffixes = NULL, all = T, no.dups =T ) %>%\n          merge(y= out$TYPE_ROTATION_VECTOR, by = c('id'), suffixes = NULL, all = T, no.dups =T )\n  \n\n  #res  <- cbind(data.table( file_id = meta$file_id, site_id = meta$site_id ), res ) \n  \n  #//-- add column with ts as seconds\n  res$idsec <- (res$id/1000) %>% round()\n  \n  #//-- way points\n  wp   <- out$TYPE_WAYPOINT\n  \n  #//-- wifi block\n  wifi <- out$TYPE_WIFI\n  \n  ParallelLogger::logInfo(\"[stop] filename: \", filename)\n  \n  list(\n    meta = meta,\n    wp   = wp,\n    dd   = res,\n    wifi = wifi\n  )\n}","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Function to print floor image with selected pathes."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"\ndraw_floor_with_path <- function(x.data, site_id, floor_id, dirs){\n  \n  path_floor = paste0( dirs$metadata, \"/\", site_id, \"/\", floor_id )\n  \n  if( !dir.exists(path_floor) ){\n    cat( c(\"error\", path_floor, \"\\n\") )\n    return( FALSE )\n  }\n  \n  #//--floor info\n  floor_info <- fromJSON( file = paste0(path_floor, '/floor_info.json') )\n  \n  #//-- floor image\n  img <- readPNG( paste0(path_floor, \"/floor_image.png\") )\n  \n  floor_image <- list(\n    width  = dim(img)[1],\n    height = dim(img)[2]\n  )\n  \n  floor_image$ratio_x <- floor_image$width/floor_info$map_info$width\n  floor_image$ratio_y <- floor_image$height/floor_info$map_info$height\n  \n  #print( dim(img) )\n  transparent <- img[,,4] == 0\n  img <- as.raster(img[,,1:3])\n  img[transparent] <- NA\n  \n  plot(c(0, floor_image$width), c(0, floor_image$height), type = \"n\", xlab = \"\", ylab = \"\", main = paste0( site_id, \" : \", floor_id ))\n  rasterImage(img, 0, 0, floor_image$width, floor_image$height, interpolate=T)    \n  \n  #//-- select floor pathes form datas\n  ids <- which( sapply(x.data, function(x){ x$meta$floor_id } ) == floor_id )\n  \n  for (i in ids ) {\n    \n    row <- x.data[[i]]\n    #-- draw the path\n    xy <- row$wp[order(id)][, .(x=wy_x*floor_image$ratio_x, y=wy_y*floor_image$ratio_y)]\n    xy$i <- row.names(xy )\n    \n    #-- add labels\n    xy_labels <- xy[, .(lab=list(i) %>% unlist() %>% paste0( collapse = \",\") ), by = .(x,y) ]\n    xy <- merge(xy, xy_labels, on = c('x','y'), all.y = T, sort = F )\n    \n    lines( xy[,1:2], type='b', col=\"red\", pch = 20, cex=0.9 )\n    #text( xy[,1:2], unlist(xy$lab), cex=1.5, pos=4, col=\"red\")\n    \n  }\n  \n}\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Fetch train datas\n\n\n* Get the list of the all train files.\n* Then filter files to the the predefined site only.\n* Get and parse train datas form selected files (using parallel cluster). \n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#\n#//--get train datas\n#\nif(1){\n  site_id <-  \"5a0546857ecc773753327266\" \n  files_train <- list.files(dirs$train, full.names = T, recursive = T)\n    \n  #-- reduce to one site\n  files_train <- files_train[ grep( paste0(\"/\", site_id, \"/\"), files_train) ]\n    \n  logFileName <- 'default.log'\n  addDefaultFileLogger(logFileName)\n  \n  numCores <- detectCores()\n  cl <- makeCluster(numCores)\n\n  clusterEvalQ(cl, {\n    library(stringi)\n    library(dplyr)\n    library(data.table)\n  })\n  \n  print( Sys.time() )\n  x.train <- parLapply(cl, files_train, get_df_from_file, types = NULL)\n  print( Sys.time() )\n  \n  stopCluster(cl)    \n  \n  clearLoggers()\n  \n} \n  ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualize pathes on the floors\n\nGet some views on the floor pathes of the site 5a0546857ecc773753327266 "},{"metadata":{"trusted":true},"cell_type":"code","source":"  \ndraw_floor_with_path(x.train, \"5a0546857ecc773753327266\", \"B1\", dirs = dirs)\ndraw_floor_with_path(x.train, \"5a0546857ecc773753327266\", \"F1\", dirs = dirs)\ndraw_floor_with_path(x.train, \"5a0546857ecc773753327266\", \"F2\", dirs = dirs)\ndraw_floor_with_path(x.train, \"5a0546857ecc773753327266\", \"F3\", dirs = dirs)\ndraw_floor_with_path(x.train, \"5a0546857ecc773753327266\", \"F4\", dirs = dirs)\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"3.6.3"}},"nbformat":4,"nbformat_minor":4}