{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Notebook Goals\n\nThis data is pretty messy. This notebook will show you how to clean up the data for one path using R."},{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"library(tidyverse) \nlibrary(stringr)\nlibrary(magrittr)\nlibrary(sf)\nlibrary(grid)\n\nfig <- function(width, heigth){\n     options(repr.plot.width = width, repr.plot.height = heigth)\n}\n\noptions(scipen = 999)\noptions(warn=-1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We define a function that first reads in the data, then split the data into a list which contains separate dataframes for each type, because the columns mean different things depending on the type. Finally, we rename the columns and return all dataframes in a list."},{"metadata":{"trusted":true},"cell_type":"code","source":"clean_data <- function(place, floor, path){\n    raw <- tibble(raw = read_lines(paste0(\"../input/indoor-location-navigation/train/\",\n      place, \"/\", floor, \"/\", path, \".txt\")))\n\n    metadata_rows <- str_detect(raw$raw, \"^#\")\n\n    metadata <- raw[metadata_rows, ]\n    data <- raw[!metadata_rows, ]\n    \n    all_meta <- raw[metadata_rows,] %>% \n      mutate(raw = str_replace(raw, \" U\", \"_U\")) %>%\n      separate(raw, into = paste0(\"V\", 1:11), sep = \"\\\\s\") %>%\n      select(-V1)\n\n    devices <- all_meta[str_detect(all_meta$V2, \"type\"), ] %>%\n      janitor::remove_empty(which = \"cols\") %>%\n      select(V4, V6, V3, everything()) %>%\n      mutate(across(V2:V9, parse_number)) %>%\n      set_colnames(c(\"device\", \"vendor\", \"name\", \"type\", \n        \"version\", \"resolution\", \"power\", \"maximumRange\"))\n  \n    meta_info <- all_meta[!str_detect(all_meta$V2, \"type\"), ] %>% \n      as.list() %>%\n      Reduce(c, .) %>%\n      as_tibble() %>%\n      separate(value, into = c(\"key\", \"value\"), sep = \":\") %>%\n      drop_na() %>%\n      add_row(key = \"path\", value = path) %>%\n      add_row(key = \"floor\", value = floor) %>%\n      add_row(key = \"place\", value = place)\n\n    all <- data %>% separate(raw, into = paste0(\"V\", 1:11), sep = \"\\\\s\") %>%\n      split(.$V2) %>%\n      map(~janitor::remove_empty(., which = \"cols\"))\n    \n    waypoint <- all$TYPE_WAYPOINT %>%\n      set_colnames(c(\"time\", \"type\", \"x\", \"y\")) %>%\n      mutate(across(-type, as.numeric))\n    \n    accelerometer <- all$TYPE_ACCELEROMETER %>%\n      set_colnames(c(\"time\", \"type\", \"x\", \"y\", \"z\", \"accuracy\")) %>%\n      mutate(across(-type, as.numeric))\n\n    gyroscope <- all$TYPE_GYROSCOPE %>%\n      set_colnames(c(\"time\", \"type\", \"x\", \"y\", \"z\", \"accuracy\")) %>%\n      mutate(across(-type, as.numeric))\n\n    magnetic_field <- all$TYPE_MAGNETIC_FIELD %>%\n      set_colnames(c(\"time\", \"type\", \"x\", \"y\", \"z\", \"accuracy\")) %>%\n      mutate(across(-type, as.numeric))\n\n    rotation_vector <- all$TYPE_ROTATION_VECTOR %>%\n      set_colnames(c(\"time\", \"type\", \"x\", \"y\", \"z\", \"accuracy\")) %>%\n      mutate(across(-type, as.numeric))\n\n    acc_uncalibrated <- all$TYPE_ACCELEROMETER_UNCALIBRATED %>%\n      set_colnames(c(\"time\", \"type\", \"x\", \"y\", \"z\", \"x2\", \"y2\", \"z2\", \"accuracy\")) %>%\n      mutate(across(-type, as.numeric))\n\n    gyro_uncalibrated <- all$TYPE_GYROSCOPE_UNCALIBRATED %>%\n      set_colnames(c(\"time\", \"type\", \"x\", \"y\", \"z\", \"x2\", \"y2\", \"z2\", \"accuracy\")) %>%\n      mutate(across(-type, as.numeric))\n\n    mag_uncalibrated <- all$TYPE_MAGNETIC_FIELD_UNCALIBRATED %>%\n      set_colnames(c(\"time\", \"type\", \"x\", \"y\", \"z\", \"x2\", \"y2\", \"z2\", \"accuracy\")) %>%\n      mutate(across(-type, as.numeric)) \n\n    wifi <- all$TYPE_WIFI %>%\n      set_colnames(c(\"time\", \"type\", \"ssid\", \"bssid\", \"RSSI\", \"frequency\", \"lasttime\")) %>%\n      mutate(across(-(type:bssid), as.numeric)) \n\n    beacon <- all$TYPE_BEACON %>% \n      set_colnames(c(\"time\", \"type\", \"UUID\", \"MajorID\",\"MinorID\", \"TxPower\",\n                     \"RSSI\", \"Distance\", \"MAC Address\", \"pad_time\")) %>%\n      mutate(across(TxPower:Distance, as.numeric)) %>%\n      mutate(across(time, as.numeric)) %>%\n      mutate(across(pad_time, as.numeric)) \n    \n    return(list(\n      \"meta\" = meta_info,\n      \"devices\" = devices,\n      \"way\" = waypoint,\n      \"acc\" = accelerometer, \n      \"gyro\" = gyroscope, \n      \"mag\" = magnetic_field, \n      \"rot\" = rotation_vector, \n      \"acc_unc\" = acc_uncalibrated, \n      \"gyro_unc\" = gyro_uncalibrated, \n      \"mag_unc\" = mag_uncalibrated, \n      \"wifi\" = wifi, \n      \"beacon\" = beacon)\n    )\n}","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Call the function, providing a place, floor, and path ID that match the folder names. "},{"metadata":{"trusted":true},"cell_type":"code","source":"place <- \"5a0546857ecc773753327266\"\nfloor <- \"F2\"\npath <- \"5dccf516c04f060006e6e3c9\"\n\nsample <- clean_data(place, floor, path)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can plot these waypoints on the map provided in GeoJSON format in the metadata folder using R's simple features (sf) package:"},{"metadata":{"trusted":true},"cell_type":"code","source":"visualize_path <- function(points_df, place, floor){\n\n    map <- st_read(paste0(\"../input/indoor-location-navigation/metadata/\",\n                          place, \"/\", floor, \"/geojson_map.json\")) \n\n    path_for_map <- points_df\n\n    max_scale <- st_as_sf(map) %>%\n      filter(!is.na(max_scale))\n\n    map_sf <- st_intersection(map, max_scale) %>% st_as_sf()\n\n    ms_width <- st_bbox(max_scale)$xmax - st_bbox(max_scale)$xmin\n    ms_height <- st_bbox(max_scale)$ymax - st_bbox(max_scale)$ymin\n\n    map_info <- rjson::fromJSON(\n        file = paste0(\"../input/indoor-location-navigation/metadata/\",\n                          place, \"/\", floor, \"/floor_info.json\")\n    )\n\n    path_for_map$x <- path_for_map$x * (ms_width/map_info$map_info$width) + st_bbox(max_scale)$xmin\n    path_for_map$y <- path_for_map$y * (ms_height/map_info$map_info$height) + st_bbox(max_scale)$ymin\n\n    path <- st_sfc(st_linestring(as.matrix(path_for_map[,3:4]))) %>%\n      st_set_crs(st_crs(map))\n\n    points <- st_sfc(st_multipoint(as.matrix(path_for_map[,3:4]))) %>%\n      st_set_crs(st_crs(map))\n    \n    ggplot() + geom_sf(data = map_sf) +\n      geom_sf(data = path, color = \"red\") + \n      geom_sf(data = points, color = \"red\")\n\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig(12,12)\n\nvisualize_path(sample$way, place, floor)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Graphing Acceleration"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"fig(12, 4)\n\nsample$acc %>%\n  ggplot(aes(x = time, y = x)) +\n  geom_vline(xintercept = sample$way$time, linetype = \"longdash\") +\n  geom_line() +\n  theme_minimal()\n\nsample$acc %>%\n  ggplot(aes(x = time, y = y)) +\n  geom_vline(xintercept = sample$way$time, linetype = \"longdash\") +\n  geom_line() +\n  theme_minimal()\n\nsample$acc %>%\n  ggplot(aes(x = time, y = z)) +\n  geom_vline(xintercept = sample$way$time, linetype = \"longdash\") +\n  geom_line() +\n  theme_minimal()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Graphing Gyro"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"sample$gyro %>%\n  ggplot(aes(x = time, y = x)) +\n  geom_vline(xintercept = sample$way$time, linetype = \"longdash\") +\n  geom_line() +\n  theme_minimal()\n\nsample$gyro %>%\n  ggplot(aes(x = time, y = y)) +\n  geom_vline(xintercept = sample$way$time, linetype = \"longdash\") +\n  geom_line() +\n  theme_minimal()\n\nsample$gyro %>%\n  ggplot(aes(x = time, y = z)) +\n  geom_vline(xintercept = sample$way$time, linetype = \"longdash\") +\n  geom_line() +\n  theme_minimal()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Graphing Rotation"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"sample$rot %>%\n  ggplot(aes(x = time, y = x)) +\n  geom_vline(xintercept = sample$way$time, linetype = \"longdash\") +\n  geom_line() +\n  theme_minimal()\n\nsample$rot %>%\n  ggplot(aes(x = time, y = y)) +\n  geom_vline(xintercept = sample$way$time, linetype = \"longdash\") +\n  geom_line() +\n  theme_minimal()\n\nsample$rot %>%\n  ggplot(aes(x = time, y = z)) +\n  geom_vline(xintercept = sample$way$time, linetype = \"longdash\") +\n  geom_line() +\n  theme_minimal()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"![](../input/indoor-location-navigation/metadata/5a0546857ecc773753327266/B1/floor_image.png)"}],"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}