From 59f1eb7a03f2417641863bd256405a8f5a8901e9 Mon Sep 17 00:00:00 2001 From: Ando Keisuke Date: Fri, 15 Apr 2022 23:52:12 +0900 Subject: [PATCH] =?UTF-8?q?[add]=20rcg=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?(=E6=96=B0=E5=BD=A2=E5=BC=8F)=E3=81=AE=E3=83=91=E3=83=BC?= =?UTF-8?q?=E3=82=B5=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/make_graphics.R | 17 -------- R/read_rcg.R | 108 ++++++++-------------------------------------- R/read_rcl.R | 6 +++ README.Rmd | 12 +++++- README.md | 26 +++++++++++ 5 files changed, 60 insertions(+), 109 deletions(-) delete mode 100644 R/make_graphics.R diff --git a/R/make_graphics.R b/R/make_graphics.R deleted file mode 100644 index 36d7aee..0000000 --- a/R/make_graphics.R +++ /dev/null @@ -1,17 +0,0 @@ -make_graph <- function(p_data,teamname,num){ - p <- ggplot(data = p_data %>% filter(team==teamname,unum==num), aes(x = as.numeric(x), y = as.numeric(y))) + - ggplot2::coord_fixed(xlim=c(-55,55),ylim =c(-35,35))+ - geom_hline(yintercept = -34.1, linetype = "solid")+ - geom_hline(yintercept = 34.1, linetype = "solid")+ - geom_hline(yintercept = 0, linetype = "solid")+ - geom_vline(xintercept = 52.6, linetype = "solid")+ - geom_vline(xintercept = -52.6, linetype = "solid")+ - geom_vline(xintercept = 0, linetype = "solid")+ - geom_point(size=0.1) + - geom_density_2d_filled(alpha = 0.7) - return(p) -} - -#ggplot2::geom_path(size=1.2,aes(color = as.numeric(step))) + -# + -# ggplot2::geom_density2d(size = 0.25, colour = "black") \ No newline at end of file diff --git a/R/read_rcg.R b/R/read_rcg.R index cc08da8..c268619 100644 --- a/R/read_rcg.R +++ b/R/read_rcg.R @@ -1,94 +1,20 @@ -# remove(list = ls());gc() -# path = "~/socceR/R/test.rcg" -tag <- c("x","y","vx","vy") - -read_rcg_ball <- function(path){ - ball_log <- path |> - readr::read_lines() |> +#' Read RCG file +#' +#' @param path RCG file path +#' @return Parsed tibble of RCG file in \code{path} +#' @examples +#' read_rcg("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcg") +read_rcg <- function(path) { + rcg <- path |> + readr::read_file() |> + jsonlite::parse_json(simplifyVector = TRUE, flatten = TRUE) |> tibble::as_tibble() |> - dplyr::mutate( - step = value |> stringr::str_extract("\\(show ([0-9]*)*") - |> stringr::str_remove("\\(show "), - ball = value |> stringr::str_extract("\\(\\(b\\)( [0-9\\-\\.]*)*\\)") - |> stringr::str_remove("\\(\\(b\\) ") - |> stringr::str_remove("\\)"), - x = ball |> stringr::str_extract("[0-9\\-\\.]+"), - y = ball |> stringr::str_remove(x) - |> stringr::str_extract("[0-9\\-\\.]+"), - vx = ball |> stringr::str_remove(x) - |> stringr::str_remove(y) - |> stringr::str_extract("[0-9\\-\\.]+"), - vy = ball |> stringr::str_remove(x) - |> stringr::str_remove(y) - |> stringr::str_remove(vx) - |> stringr::str_extract("[0-9\\-\\.]+"), - )|> - dplyr::select( - step, - # ball, - x, - y, - vx, - vy, - ) %>% tidyr::drop_na() - return(ball_log) -} - -read_rcg_player <- function(path){ - player_log <- path |> - readr::read_lines() |> - tibble::as_tibble() |> - dplyr::mutate( - step = value |> stringr::str_extract("\\(show ([0-9]*)*") - |> stringr::str_remove("\\(show "), - players = value |> stringr::str_extract_all("\\(\\([lr] [0-9]*\\) [0-9]* [0-9x]* ([0-9\\-\\.]* )*\\(v h [0-9\\-\\.]*\\) \\(s( [0-9\\-\\.]*)*\\)( \\(f [rl] [0-9\\-\\.]*\\))* \\(c( [0-9\\-\\.]*)*\\)\\)") - ) |> + dplyr::filter(type == "show") |> + dplyr::select(time, players, ball.x, ball.y, ball.vx, ball.vy) |> tidyr::unnest(players) |> - dplyr::mutate( - player = players |> stringr::str_extract("\\([rl] [0-9]+\\)"), - team = player |> stringr::str_extract("[rl]"), - unum = player |> stringr::str_extract("[0-9]+"), - params = players |> stringr::str_remove("\\([rl] [0-9]+\\)"), - - type = params|> stringr::str_extract("[0-9\\-\\.x]+"), - l1 = params |> stringr::str_remove(type), - - state = l1 |> stringr::str_extract("[0-9\\-\\.x]+"), - l2 = l1 |> stringr::str_remove(state), - - x = l2 |> stringr::str_extract("[0-9\\-\\.]+"), - l3 = l2 |> stringr::str_remove(x), - - y = l3 |> stringr::str_extract("[0-9\\-\\.]+"), - l4 = l3 |> stringr::str_remove(y), - - vx = l4 |> stringr::str_extract("[0-9\\-\\.]+"), - l5 = l4 |> stringr::str_remove(vx), - - vy = l5 |> stringr::str_extract("[0-9\\-\\.]+"), - l6 = l5 |> stringr::str_remove(vy), - - body = l6 |> stringr::str_extract("[0-9\\-\\.]+"), - l7 = l6 |> stringr::str_remove(body), - - neck = l7 |> stringr::str_extract("[0-9\\-\\.]+"), - l8 = l7 |> stringr::str_remove(neck), - ) |> - dplyr::select( - step, - # players, - # player, - team, - unum, - # params, - # type, - # state, - x, - y, - vx, - vy, - body, - neck, - ) %>% tidyr::drop_na() - return(player_log) + dplyr::select(time:capacity, ball.x:ball.vy) |> + dplyr::rename(step = time) |> + dplyr::rename_with(stringr::str_replace, pattern = "\\.", replacement = "_") + + return(rcg) } diff --git a/R/read_rcl.R b/R/read_rcl.R index cbe8902..156d4ba 100644 --- a/R/read_rcl.R +++ b/R/read_rcl.R @@ -1,3 +1,9 @@ +#' Read RCL file +#' +#' @param path RCL file path +#' @return Parsed tibble of RCL file in \code{path} +#' @examples +#' read_rcg("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcl") read_rcl <- function(path) { rcl <- path |> readr::read_lines() |> diff --git a/README.Rmd b/README.Rmd index 9438ea2..bf2453e 100644 --- a/README.Rmd +++ b/README.Rmd @@ -13,4 +13,14 @@ RoboCup Soccer 2Dの試合分析のためのツール群 library(tidyverse) source("R/read_rcl.R") read_rcl("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcl") -``` \ No newline at end of file +``` + +### `read_rcg` + +指定されたrcgファイルを解析し、tibbleにして返します。 + +```{r, message=FALSE, collapse = TRUE} +library(tidyverse) +source("R/read_rcg.R") +read_rcg("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcg") +``` diff --git a/README.md b/README.md index 7726b98..755903a 100644 --- a/README.md +++ b/README.md @@ -26,3 +26,29 @@ read_rcl("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcl") ## 10 0 HELIOS_base 1 turn "0,115\tRecv HELIOS_base_1: (t~ ## # ... with 867,088 more rows ``` + +### `read_rcg` + +指定されたrcgファイルを解析し、tibbleにして返します。 + +``` r +library(tidyverse) +source("R/read_rcg.R") +read_rcg("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcg") +## # A tibble: 143,418 x 21 +## step side unum type state x y vx vy body neck vq +## +## 1 1 l 1 0 9 -49 0 0 0 54.7 0 h +## 2 1 l 2 17 1 -25 -5 0 0 -118. 0 h +## 3 1 l 3 8 1 -25 5 0 0 108. 0 h +## 4 1 l 4 13 1 -25 -10 0 0 -117. 0 h +## 5 1 l 5 6 1 -25 10 0 0 -174. 0 h +## 6 1 l 6 11 1 -25 0 0 0 114. 0 h +## 7 1 l 7 10 1 -15 -5 0 0 162. 0 h +## 8 1 l 8 5 1 -15 5 0 0 -170. 0 h +## 9 1 l 9 4 1 -15 -10 0 0 175. 0 h +## 10 1 l 10 16 1 -15 10 0 0 -122. 0 h +## # ... with 143,408 more rows, and 9 more variables: vw , stamina , +## # effort , recovery , capacity , ball_x , ball_y , +## # ball_vx , ball_vy +``` -- GitLab