# redmont data helper functions # # ds 2019-07-05 # ds 2021-03-01, tidied up. require(tidyverse) require(jsonlite) #' get_s_name #' #' get the name from the dataframe get_s_name <- function(d) { # theDate <- d$MedmontStudioExportFile$M600ThresholdExam$EntryDate[1] n <- tolower( paste0( d$MedmontStudioExportFile$Patient$LastName, "_", d$MedmontStudioExportFile$Patient$FirstName ) ) } #' convert medmont JSON to data frame #' #' returns #' list(result, the_points, d) #' read_medmont <- function(fname) { # get data j <- jsonlite::read_json(fname) d <- fromJSON(fname) # unpack what’s in JSON structure ----------------------------------------- exam <- d$MedmontStudioExportFile[4] test <- exam$M600ThresholdExam$ThresholdTest$Test # e.g. # test$BlindSpot # Now look for the experiment data ---------------------------------------- # test points are inside here n_points <- parse_integer(test$TestPoints$elements) the_points <- test$TestPoints$ThresholdTestPoint # exporting to CSV (tidy) style format -------------------------------------- # this is how two write out a subset to CSV file... # make sure to take last set of measurements (if it's a list) # also length less than 5 (which is the right order of mag for ATTEMPTS, rather than points) # if there is only 1 test, a previous version of the check didn't seem to do the right thing/ if (is_list(the_points) && length(the_points) < 5) { the_points <- the_points[[length(the_points)]] } result <- the_points %>% mutate( ecc = parse_double(Position$Ring), pa = parse_double(Position$Meridian), pa_rad = pi* pa / 180, Value = parse_double(Value) ) } # plotting function ------------------------------------------------------- plot_medmont <- function(the_points) { # NB-- the coord_polar step requires setting the starting point for 0 radians / 0 degrees... # to be offset from 12 o'clock the_points %>% mutate( xcoord = ecc * cos(pa_rad), ycoord = ecc * sin(pa_rad) ) %>% filter(State != "TS_UNTESTED") %>% ggplot(aes(x = xcoord, y = ycoord, size = Value, colour=State)) + geom_point(alpha = 0.9) + coord_fixed() + scale_colour_manual(values = c("red", "black", "gray")) + # "TS_SEEN" "TS_UNTESTED" "TS_NOT_SEEN" # coord_polar is kind of broken... # https://community.rstudio.com/t/making-coord-polar-behave-like-standard-polar-coordinate-plotting/3762/6 # coord_polar(theta = "x", start = -pi/4, direction = -1) + theme_minimal() } # convert - combined function --------------------------------------------- #' read, make filename, write #' convert_medmont <- function(fname) { # read the data file data_list <- read_medmont(fname) # list(result, the_points, d) # filename from within? new_fname <- file.path(dirname(fname), paste0(get_s_name(data_list[[3]]), "-visualField", ".csv")) write_csv(data_list[[1]], new_fname) print(paste("wrote file:", new_fname)) }