{"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":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Introduction","metadata":{"_uuid":"a7b562ca-33cd-47a2-99d9-1ab52cb82141","_cell_guid":"18e5b27a-86db-4080-8bb5-635e37ffdeb6","trusted":true}},{"cell_type":"markdown","source":"This data set has a lot of information in accelerations I want to see if I can agregate the data in a potentially novel way, by converting the 3 axis accelerations into vectors. Then adding those vectors together to get paths of activity over time. I don't expect the vectors to track the movements of the subjects, but I am curious to see what the FOG events look like in this format.","metadata":{"_uuid":"79e96206-8634-4bce-b80b-91041ca4a8c3","_cell_guid":"431a1af0-7a92-48ef-8087-4d30f44c1640","trusted":true}},{"cell_type":"code","source":"library(tidyverse)\nlibrary(plotly)","metadata":{"_uuid":"914ee1ea-80e1-40fb-ad98-0ebbc3ac6636","_cell_guid":"e9fc51ae-6bda-412e-b7cf-3d37e8204ad5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-01T20:57:00.942601Z","iopub.execute_input":"2023-04-01T20:57:00.944754Z","iopub.status.idle":"2023-04-01T20:57:00.964561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Loading a tiny bit of data","metadata":{"_uuid":"0f9e2028-f115-4b2c-8666-619a57fe446b","_cell_guid":"b5c8a634-063a-4fa0-b8d3-178c7882244d","trusted":true}},{"cell_type":"code","source":"x <- read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog/02ea782681.csv')\n\nglimpse(x)","metadata":{"_uuid":"5dfdc07a-0c84-4d30-8e19-33b6805c201d","_cell_guid":"5603cf93-a73e-4e38-ae55-08b31ec3c24d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-01T20:57:06.126946Z","iopub.execute_input":"2023-04-01T20:57:06.128998Z","iopub.status.idle":"2023-04-01T20:57:06.405925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Keep it simple 2D \n\nLet's look at the very first data point, \n\nDesignating Vertical as Z, Medial Latearal as X and Anterior Posterior as Y - we will look at Z later and keep it to just x/y to keep it simlpe and 2D.","metadata":{"_uuid":"4a4dea48-86f9-441c-b146-986a11147140","_cell_guid":"10a3af99-395a-4e26-a3d6-15f10b96d0f6","trusted":true}},{"cell_type":"markdown","source":"#### 5 points plotted \n\nThe trick, to make this look like a journey, is rather than plot each plot individually, to add the plots to each other cummulatively.","metadata":{"_uuid":"4a399c8d-b6d4-48d0-b2e7-65399bcb105e","_cell_guid":"48341b90-7288-4b39-8ff1-e25dc679d07b","trusted":true}},{"cell_type":"code","source":"one_row <- x[1:5,] %>%\n    select(AccV, AccML, AccAP) %>%\n    mutate(x = cumsum(AccML),\n          y = cumsum(AccAP))\n\none_row %>% \nggplot(aes(x = x, y = y)) +\ngeom_point(colour = 'blue') +\ngeom_line(colour = 'green') + \ntheme_minimal() + \ntheme(axis.title.x = element_blank(), \n      axis.title.y = element_blank(),\n      axis.text.x = element_blank(),\n      axis.text.y = element_blank())","metadata":{"_uuid":"9d0e2dba-9f4b-412b-8092-5a362e7272ac","_cell_guid":"3fca0197-e3fe-46e4-8daf-b335b66908d1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-01T20:57:11.117620Z","iopub.execute_input":"2023-04-01T20:57:11.119642Z","iopub.status.idle":"2023-04-01T20:57:11.341856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"A pretty straght line because the accelerometer in the X axis is just 1 (it's probably maxed out and won't/can't return a higher value). \n\nLets see a longer journey 500 records.","metadata":{"_uuid":"abbb03dd-87cf-4c5b-b9f8-facfd5887569","_cell_guid":"799d7870-ae9b-402b-aa8c-0a9309ce15ce","trusted":true}},{"cell_type":"code","source":"one_row <- x[1:500,] %>%\n    select(AccV, AccML, AccAP) %>%\n    mutate(x = cumsum(AccV),\n          y = cumsum(AccML))\n\none_row %>% \nggplot(aes(x = x, y = y)) +\ngeom_point(colour = 'blue') +\ngeom_line(colour = 'green') + \ntheme_minimal() + \ntheme(axis.title.x = element_blank(), \n      axis.title.y = element_blank(),\n      axis.text.x = element_blank(),\n      axis.text.y = element_blank())","metadata":{"_uuid":"0dcd9353-5da6-432c-a750-7c06cac1a39e","_cell_guid":"3ec3a521-d641-4b1f-9753-1cee01fec2fa","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-01T20:57:15.465282Z","iopub.execute_input":"2023-04-01T20:57:15.467278Z","iopub.status.idle":"2023-04-01T20:57:15.709521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Still pretty straight. Lets plot the whole file and shade the FOG events. ","metadata":{"_uuid":"9ae8ded8-80bc-40dd-bfeb-a8334dfbbb04","_cell_guid":"15f43bcf-1c74-42fa-8f31-0a80d0532eb0","trusted":true}},{"cell_type":"code","source":"all_rows <- x %>%\n    select(AccV, AccML, AccAP, Turn) %>%\n    mutate(x = cumsum(AccV),\n          y = cumsum(AccML)) \n\nmax_y <- max(all_rows$y, na.rm = TRUE)\nmin_y <- min(all_rows$y, na.rm = TRUE)\n\nall_rows2<- all_rows %>%\n    mutate(x = cumsum(AccV),\n          y = cumsum(AccML), \n          Turn_FCT = factor(Turn, levels = c(0, 1)), \n          # Gave up trying to get the sec_axis transform function to work in GGplot so just hacked it here\n          Turn_hx = if_else(Turn == 0, 0, \n                            if_else(Turn == 1, max_y, 9*9^9))) # 9*9^9 is just a flag if unepected input goes into the if esle stages \n\nall_rows2 %>% \nggplot(aes(x = x, y = y)) +\ngeom_line(colour = 'green') + \ngeom_col(aes(x = x, y = Turn_hx), \n         fill = 'blue', colour = 'transparent', alpha = .25) +\ntheme_minimal() + \ntheme(axis.title.x = element_blank(), \n      axis.title.y = element_blank(),\n      axis.text.x = element_blank(),\n      axis.text.y = element_blank())","metadata":{"_uuid":"9c9f3584-43b6-4b8e-bf4a-99bf9d9b98dd","_cell_guid":"c61b1d8c-e6d7-4ec7-97f3-427688785d15","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-01T20:57:19.865190Z","iopub.execute_input":"2023-04-01T20:57:19.867278Z","iopub.status.idle":"2023-04-01T20:57:56.996395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"round(length(all_rows$x) / 128 / 60, 0)","metadata":{"_uuid":"6a7d5aac-1b20-4002-8b83-f421b18672fb","_cell_guid":"c54480ec-f22d-4cb0-98e0-258907f15651","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-01T20:58:10.427042Z","iopub.execute_input":"2023-04-01T20:58:10.435718Z","iopub.status.idle":"2023-04-01T20:58:10.468626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fog event Zoom\n\nThe whole file covers about 21 Minutes, lets zoom in on the first event. A time period of about 8 seconds.","metadata":{"_uuid":"d6bf7662-12f2-48ea-8e1a-48ffd3a916c1","_cell_guid":"e9074b95-39b0-4f6c-aaf3-01cd6f733b00","trusted":true}},{"cell_type":"code","source":"all_rows <- x %>%\n    select(AccV, AccML, AccAP, Turn, Time) %>%\n    filter(Time >= 139000, \n           Time <= 140000) %>% \n    mutate(x = cumsum(AccV),\n          y = cumsum(AccML)) \n\nmax_y <- max(all_rows$y, na.rm = TRUE)\nmin_y <- min(all_rows$y, na.rm = TRUE)\n\nall_rows2<- all_rows %>%\n    mutate(x = cumsum(AccV),\n          y = cumsum(AccML), \n          Turn_FCT = factor(Turn, levels = c(0, 1)), \n          # Gave up trying to get the sec_axis transform function to work in GGplot so just hacked it here\n          Turn_hx = if_else(Turn == 0, 0, \n                            if_else(Turn == 1, max_y, 9*9^9))) # 9*9^9 is just a flag if unepected input goes into the if esle stages \n\nall_rows2 %>% \nggplot(aes(x = x, y = y)) +\ngeom_line(colour = 'green') + \ngeom_col(aes(x = x, y = Turn_hx), \n         fill = 'blue', colour = 'transparent', alpha = .25) +\ntheme_minimal() + \ntheme(axis.title.x = element_blank(), \n      axis.title.y = element_blank(),\n      axis.text.x = element_blank(),\n      axis.text.y = element_blank())","metadata":{"_uuid":"4756511d-db42-4fd7-8d89-f5b1682b197b","_cell_guid":"8ad0c40d-1e0a-48c7-88a2-59876f503e4f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-01T20:58:17.328707Z","iopub.execute_input":"2023-04-01T20:58:17.330617Z","iopub.status.idle":"2023-04-01T20:58:17.767259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Not alot to see here. I thought there would be something that jumped out of this data during the events.","metadata":{"_uuid":"79807d97-8f0e-4d43-af88-7571ebce3337","_cell_guid":"0727b906-9759-49a5-97bb-b315ec8a2c4e","trusted":true}},{"cell_type":"markdown","source":"# Now 3D","metadata":{"_uuid":"10ca0a4d-eca8-411d-94b1-ba4b5843f3f9","_cell_guid":"7687c4dd-80bf-4b01-827c-207098304435","trusted":true}},{"cell_type":"code","source":"#disclaimer - this code was written with the assistance of chat-gpt 3 \nall_rows2 <- all_rows2 %>% \n  mutate(z = cumsum(AccAP))\n\nx2 <- all_rows2$x %>% as.vector()\ny2 <- all_rows2$y %>% as.vector()\nz2 <- all_rows2$z %>% as.vector() \n\n# Create a trace for the line graph\ntrace <- list(\n  type = \"scatter3d\",\n  mode = \"lines\",\n  x = x2,\n  y = y2,\n  z = z2,\n  line = list(color = \"red\", width = 2)\n)\n\n# Create a layout for the graph\nlayout <- list(\n  title = \"3D Line Graph\",\n  scene = list(\n    xaxis = list(title = \"X\"),\n    yaxis = list(title = \"Y\"),\n    zaxis = list(title = \"Z\")\n  )\n)\n\n# Create the plot object\nplot_obj <- plot_ly(data = all_rows2, type = \"scatter3d\", mode = \"lines\", x = ~x, y = ~y, z = ~z, line = list(color = \"red\", width = 2))\n\n# Display the plot in the notebook\nplot_obj\n","metadata":{"_uuid":"7f811852-048e-4b38-bb97-1e0e992e2920","_cell_guid":"021df070-e7aa-49bb-b2a2-59dc8b994075","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-01T21:11:42.564936Z","iopub.execute_input":"2023-04-01T21:11:42.574079Z","iopub.status.idle":"2023-04-01T21:11:43.631628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If I had one word to describe that it would be unenlightening. \n\nJust for fun lets do the whole file in 3D","metadata":{}},{"cell_type":"code","source":"all_rows3 <- x %>%\n    select(AccV, AccML, AccAP, Turn, Time) %>%\n    mutate(x = cumsum(AccV),\n          y = cumsum(AccML),\n          z = cumsum(AccAP)) \n\n\nx3 <- all_rows3$x %>% as.vector()\ny3 <- all_rows3$y %>% as.vector()\nz3 <- all_rows3$z %>% as.vector() \n\n# Create a trace for the line graph\ntrace <- list(\n  type = \"scatter3d\",\n  mode = \"lines\",\n  x = x3,\n  y = y3,\n  z = z3,\n  line = list(color = \"red\", width = 2)\n)\n\n# Create a layout for the graph\nlayout <- list(\n  title = \"3D Line Graph\",\n  scene = list(\n    xaxis = list(title = \"X\"),\n    yaxis = list(title = \"Y\"),\n    zaxis = list(title = \"Z\")\n  )\n)\n\n# Create the plot object\nplot_obj <- plot_ly(data = all_rows3, \n                    type = \"scatter3d\", \n                    mode = \"lines\", \n                    x = ~x, y = ~y, z = ~z, \n                    line = list(color = \"red\", width = 2))\n\n# Display the plot in the notebook\nplot_obj\n","metadata":{"execution":{"iopub.status.busy":"2023-04-01T20:58:39.522299Z","iopub.execute_input":"2023-04-01T20:58:39.524266Z","iopub.status.idle":"2023-04-01T20:58:46.377907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Excellent, that is the kind of trace I was hoping to plot, note how because negative values are possible in the accellerometer then the cumsums can go down as well as up. This means the line can go back on its self and do loops. \n\nThis won't match the movement of the subject but could be a good be a good way to explore the data. ","metadata":{}}]}