11library(dplyr )
22library(stringr )
33
4+ library(parallel )
5+ use_cores <- detectCores() - 2
6+
47# source: https://github.com/andrebert/body-weight-exercises
58path <- " ~/github/body-weight-exercises"
69files <- list.files(path , recursive = TRUE )
@@ -9,8 +12,11 @@ seg_files <- str_subset(files, "txt|INFO$", negate = TRUE)
912info_files <- str_subset(files , " INFO$" )
1013
1114ratings <- lapply(file.path(path , info_files ), function (file ) {
12- readr :: read_delim(file ,
13- delim = " :" , col_names = c(" name" , " value" ), show_col_types = FALSE
15+ readr :: read_delim(
16+ file ,
17+ delim = " :" ,
18+ col_names = c(" name" , " value" ),
19+ show_col_types = FALSE
1420 ) | >
1521 filter(name == " Rating" ) | >
1622 pull(value ) | >
@@ -23,42 +29,83 @@ bodyweight <- seg_files |>
2329 str_split(" /|-" , simplify = TRUE ) | >
2430 as_tibble(.name_repair = " minimal" ) | >
2531 setNames(c(" id" , " ex" , " set" , " loc" , " sens" , " dim" , " rep" )) | >
26- filter(ex == " bi" ) | >
27- mutate(id = as.integer(id ), rep = as.integer(rep ))
28-
29- bw_files <- str_subset(seg_files , " bi" )
30- bi_files <- file.path(path , bw_files )
32+ # rm participants w/o segmented exercises and exercises that were not segmented
33+ filter(! (id %in% c(5 , 9 , 19 )) & ! (ex %in% c(" " , " li" , " ru" ))) | >
34+ mutate(id = as.integer(id ), rep = as.integer(rep ), ex = factor (ex ))
3135
32- bi <- lapply(bi_files , function (file ) {
33- readr :: read_table(file , col_names = FALSE , show_col_types = FALSE ) | >
34- t() | >
36+ get_tfd <- function (files , label ) {
37+ f <- mclapply(
38+ files ,
39+ function (file ) {
40+ readr :: read_table(file , col_names = FALSE , show_col_types = FALSE ) | >
41+ t() | >
42+ as_tibble(.name_repair = " minimal" ) | >
43+ setNames(c(" value" , " arg" )) | >
44+ relocate(arg , value ) | >
45+ distinct(arg , .keep_all = TRUE )
46+ },
47+ mc.cores = use_cores
48+ ) | >
49+ tf :: tfd()
50+ files | >
51+ str_remove(paste0(path , " /" )) | >
52+ str_split(" /|-" , simplify = TRUE ) | >
3553 as_tibble(.name_repair = " minimal" ) | >
36- setNames(c(" value" , " arg" )) | >
37- relocate(arg , value ) | >
38- distinct(arg , .keep_all = TRUE )
39- }) | >
40- tf :: tfd()
54+ setNames(c(" id" , " ex" , " set" , " loc" , " sens" , " dim" , " rep" )) | >
55+ mutate(" {{label}}" : = f , rep = as.numeric(rep )) | >
56+ select(- sens )
57+ }
4158
42- bi_ratings <- vapply(seq_len(nrow(bodyweight )), function (i ) {
43- loc <- str_c(bodyweight [i , 1 : 3 ], collapse = " /" )
44- rep <- bodyweight [i , " rep" , drop = TRUE ]
45- if (is.na(ratings [loc ])) NA else ratings [[loc ]][rep ]
46- }, numeric (1 ))
59+ get_ratings <- function (ex ) {
60+ ex_ratings <- ratings [str_detect(names(ratings ), ex )]
61+ str_split(names(ex_ratings ), " /" , simplify = TRUE ) | >
62+ as_tibble(.name_repair = " minimal" ) | >
63+ setNames(c(" id" , " ex" , " set" )) | >
64+ mutate(rating = ex_ratings ) | >
65+ unnest(cols = c(rating )) | >
66+ group_by(id , ex , set ) | >
67+ mutate(rep = 1 : n())
68+ }
4769
48- bodyweight <- bodyweight | >
49- mutate(
50- loc = factor (loc ,
51- levels = c(" TL" , " TR" , " BL" , " BR" , " CH" ),
52- labels = c(" left arm" , " right arm" , " left leg" , " right leg" , " chest" )
53- ),
54- sens = factor (sens ,
55- levels = c(" acc" , " rot" ), labels = c(" acceleration" , " rotation" )
56- ),
57- dim = as.factor(dim ),
58- set = factor (set , levels = c(" set1" , " set2" , " set3" ), ordered = TRUE ),
59- rating = bi_ratings ,
60- activity = bi
61- ) | >
62- select(- ex )
70+ for (this_ex in levels(bodyweight $ ex )) {
71+ ex_filenames <- str_subset(seg_files , this_ex )
72+ ex_files <- file.path(path , ex_filenames )
73+ ex_acc_files <- str_subset(ex_files , " /acc-" )
74+ ex_rot_files <- str_subset(ex_files , " /rot-" )
75+ acc <- get_tfd(ex_acc_files , acc )
76+ rot <- get_tfd(ex_rot_files , rot )
77+ rating <- get_ratings(this_ex )
78+ ex <- full_join(acc , rot ) | >
79+ right_join(rating ) | >
80+ mutate(
81+ id = factor (id ),
82+ ex = factor (
83+ ex ,
84+ levels = c(" bi" , " cr" , " ha" , " kn" , " lu" , " mo" ),
85+ labels = c(
86+ " Bicycle Crunch" ,
87+ " Crunch" ,
88+ " Jumping Jack" ,
89+ " Squat" ,
90+ " Lunge" ,
91+ " Mountain Climber"
92+ )
93+ ),
94+ loc = factor (
95+ loc ,
96+ levels = c(" TL" , " TR" , " BL" , " BR" , " CH" ),
97+ labels = c(" left arm" , " right arm" , " left leg" , " right leg" , " chest" )
98+ ),
99+ dim = as.factor(dim ),
100+ set = factor (set , levels = c(" set1" , " set2" , " set3" ), ordered = TRUE )
101+ )
102+ assign(this_ex , ex )
103+ cat(this_ex , " done\n " )
104+ }
63105
64- usethis :: use_data(bodyweight , overwrite = TRUE , compress = " xz" )
106+ usethis :: use_data(bi , overwrite = TRUE , compress = " xz" )
107+ usethis :: use_data(cr , overwrite = TRUE , compress = " xz" )
108+ usethis :: use_data(ha , overwrite = TRUE , compress = " xz" )
109+ usethis :: use_data(kn , overwrite = TRUE , compress = " xz" )
110+ usethis :: use_data(lu , overwrite = TRUE , compress = " xz" )
111+ usethis :: use_data(mo , overwrite = TRUE , compress = " xz" )
0 commit comments