status == "healthy" && mammogram == "+ve" ~ "incorrect"), status = fct_reorder(status,"healthy")) d <- tibble::tibble(age = age, status = c(rep("cancer", n*p_cancer), rep("healthy", n*(1-p_cancer))), mammogram = c(rep("+ve", n_c_p ), rep("-ve", n_c_n ), rep("+ve", n_h_p ) , rep("-ve", n_h_n )) ) d <- d %>% mutate(diagnosis = case_when(status == "cancer" && mammogram == "+ve" ~ "correct", status == "cancer" && mammogram == "-ve" ~ "incorrect", status == "healthy" && mammogram == "-ve" ~ "correct", status == "healthy" && mammogram == "+ve" ~ "incorrect"), status = fct_reorder(status,"healthy")) g1 <- ggplot(d, aes(area = age, fill = status)) + geom_treemap(layout="fixed") + scale_fill_brewer() g2 <- ggplot(d, aes(area = age, fill = mammogram)) + geom_treemap(layout="fixed") + scale_fill_brewer() g1 g2 g1 <- ggplot(d, aes(area = age, fill = status)) + geom_treemap() + scale_fill_brewer() g2 <- ggplot(d, aes(area = age, fill = mammogram)) + geom_treemap() + scale_fill_brewer() g1 g2 d <- d %>% mutate(diagnosis = case_when(status == "cancer" && mammogram == "+ve" ~ "correct", status == "cancer" && mammogram == "-ve" ~ "incorrect", status == "healthy" && mammogram == "-ve" ~ "correct", status == "healthy" && mammogram == "+ve" ~ "incorrect"), status = fct_reorder(status,"healthy", "cancer"), diagnosis = fct_reorder(status,"-ve", "+ve")) d <- d %>% mutate(diagnosis = case_when(status == "cancer" && mammogram == "+ve" ~ "correct", status == "cancer" && mammogram == "-ve" ~ "incorrect", status == "healthy" && mammogram == "-ve" ~ "correct", status == "healthy" && mammogram == "+ve" ~ "incorrect"), status = fct_reorder(status,"healthy", "cancer"), diagnosis = fct_reorder(diagnosis,"-ve", "+ve")) d <- tibble::tibble(age = age, status = c(rep("cancer", n*p_cancer), rep("healthy", n*(1-p_cancer))), mammogram = c(rep("+ve", n_c_p ), rep("-ve", n_c_n ), rep("+ve", n_h_p ) , rep("-ve", n_h_n )) ) d <- d %>% mutate(diagnosis = case_when(status == "cancer" && mammogram == "+ve" ~ "correct", status == "cancer" && mammogram == "-ve" ~ "incorrect", status == "healthy" && mammogram == "-ve" ~ "correct", status == "healthy" && mammogram == "+ve" ~ "incorrect"), status = fct_reorder(status,"healthy", "cancer"), mammogram = fct_reorder(mammpgram,"-ve", "+ve")) # library(tidyverse) library(ggbeeswarm) # library(cowplot) library(treemapify) n <- 1000 age <- base::sample(35:45, n, replace=TRUE) p_cancer <- 0.01 p_test_positive <- 0.8 p_test_false_positive <- 0.1 n_cancer <- n*p_cancer n_c_p <- n_cancer*p_test_positive n_c_n <- n_cancer - n_c_p n_h_p <- (n-n_cancer)*p_test_false_positive n_h_n <- (n-n_cancer) - n_h_p d <- tibble::tibble(age = age, status = c(rep("cancer", n*p_cancer), rep("healthy", n*(1-p_cancer))), mammogram = c(rep("+ve", n_c_p ), rep("-ve", n_c_n ), rep("+ve", n_h_p ) , rep("-ve", n_h_n )) ) d <- d %>% mutate(diagnosis = case_when(status == "cancer" && mammogram == "+ve" ~ "correct", status == "cancer" && mammogram == "-ve" ~ "incorrect", status == "healthy" && mammogram == "-ve" ~ "correct", status == "healthy" && mammogram == "+ve" ~ "incorrect"), status = fct_reorder(status,"healthy", "cancer"), mammogram = fct_reorder(mammpgram,"-ve", "+ve")) ?fct_relevel d <- d %>% mutate(diagnosis = case_when(status == "cancer" && mammogram == "+ve" ~ "correct", status == "cancer" && mammogram == "-ve" ~ "incorrect", status == "healthy" && mammogram == "-ve" ~ "correct", status == "healthy" && mammogram == "+ve" ~ "incorrect"), status = fct_relevel(status,"healthy", "cancer"), mammogram = fct_relevel(mammpgram,"-ve", "+ve")) d <- d %>% mutate(diagnosis = case_when(status == "cancer" && mammogram == "+ve" ~ "correct", status == "cancer" && mammogram == "-ve" ~ "incorrect", status == "healthy" && mammogram == "-ve" ~ "correct", status == "healthy" && mammogram == "+ve" ~ "incorrect"), status = fct_relevel(status,"healthy", "cancer"), mammogram = fct_relevel(mammogram,"-ve", "+ve")) g1 <- ggplot(d, aes(area = age, fill = status)) + geom_treemap() + scale_fill_brewer() g2 <- ggplot(d, aes(area = age, fill = mammogram)) + geom_treemap() + scale_fill_brewer() g1 g2 g3 <- ggplot(d, aes(area = age, fill = diagnosis)) + geom_treemap() + scale_fill_brewer(palette = "Set2") g3 d <- d %>% mutate(diagnosis = case_when(status == "cancer" & mammogram == "+ve" ~ "correct", status == "cancer" & mammogram == "-ve" ~ "incorrect", status == "healthy" & mammogram == "-ve" ~ "correct", status == "healthy" & mammogram == "+ve" ~ "incorrect"), status = fct_relevel(status,"healthy", "cancer"), mammogram = fct_relevel(mammogram,"-ve", "+ve")) g3 <- ggplot(d, aes(area = age, fill = diagnosis)) + geom_treemap() + scale_fill_brewer(palette = "Set2") g3 g3 <- ggplot(d, aes(area = age, group, status, fill = diagnosis)) + geom_treemap() + scale_fill_brewer(palette = "Set2") g3 g3 <- ggplot(d, aes(area = age, group= status, fill = diagnosis)) + geom_treemap() + scale_fill_brewer(palette = "Set2") g3 g3 <- ggplot(d, aes(area = age, group= status, fill = diagnosis)) + geom_treemap(layout="fixed") + scale_fill_brewer(palette = "Set2") g3 g3 <- ggplot(d, aes(area = age, color= status, fill = diagnosis)) + geom_treemap(layout="fixed") + scale_fill_brewer(palette = "Set2") g3 g3 <- ggplot(d, aes(area = age, width= status, fill = diagnosis)) + geom_treemap(layout="fixed") + scale_fill_brewer(palette = "Set2") g3 ?geom_treemap g3 <- ggplot(d, aes(area = age, alpha= status, fill = diagnosis)) + geom_treemap(layout="fixed") + scale_fill_brewer(palette = "Set2") g3 g1 g2 g3 ?ggsave() ggsave("plot1.pdf", g1) ggsave("plot2.pdf", g2) ggsave("plot3.pdf", g3) source('~/OneDrive - The University of Nottingham/teaching/2019-tutorials/2019-11-17-bayes/bayes-bee-plot.R', echo=TRUE) ggsave("plot1.png", g1) ggsave("plot2.png", g2) ggsave("plot3.png", g3) # colorPalette <- "Set3" g1 <- ggplot(d, aes(area = age, fill = status)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) g2 <- ggplot(d, aes(area = age, fill = mammogram)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) g3 <- ggplot(d, aes(area = age, alpha=status, fill = diagnosis)) + geom_treemap(layout="fixed") + scale_fill_brewer(palette = "Set2") + scale_alpha_discrete(range = c(0.3, 1.o)) g3 <- ggplot(d, aes(area = age, alpha=status, fill = diagnosis)) + geom_treemap(layout="fixed") + scale_fill_brewer(palette = "Set2") + scale_alpha_discrete(range = c(0.3, 1.0)) ggsave("plot1.png", g1) ggsave("plot2.png", g2) ggsave("plot3.png", g3) ?scale_fill_brewer # colorPalette <- "Accent" g1 <- ggplot(d, aes(area = age, fill = status)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) g2 <- ggplot(d, aes(area = age, fill = mammogram)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) g3 <- ggplot(d, aes(area = age, alpha=status, fill = diagnosis)) + geom_treemap(layout="fixed") + scale_fill_brewer(palette = "Set2") + scale_alpha_discrete(range = c(0.3, 1.0)) ggsave("plot1.png", g1) ggsave("plot2.png", g2) # colorPalette <- "Dark2" g1 <- ggplot(d, aes(area = age, fill = status)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) g2 <- ggplot(d, aes(area = age, fill = mammogram)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) ggsave("plot1.png", g1) ggsave("plot2.png", g2) # colorPalette <- "Pastel1" g1 <- ggplot(d, aes(area = age, fill = status)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) g2 <- ggplot(d, aes(area = age, fill = mammogram)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) ggsave("plot1.png", g1) ggsave("plot2.png", g2) # colorPalette <- "Set3" g1 <- ggplot(d, aes(area = age, fill = status)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) g2 <- ggplot(d, aes(area = age, fill = mammogram)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) ggsave("plot1.png", g1) ggsave("plot2.png", g2) # colorPalette <- "Set1" g1 <- ggplot(d, aes(area = age, fill = status)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) g2 <- ggplot(d, aes(area = age, fill = mammogram)) + geom_treemap() + scale_fill_brewer(palette = colorPalette) ggsave("plot1.png", g1) ggsave("plot2.png", g2) g1 <- ggplot(d, aes(area = age, fill = status)) + geom_treemap() + scale_fill_brewer() ggsave("plot1.png", g1) g2 <- ggplot(d, aes(area = age, fill = mammogram)) + geom_treemap() + scale_fill_brewer() ggsave("plot1.png", g1) ggsave("plot2.png", g2) g3 <- ggplot(d, aes(area = age, alpha=status, fill = diagnosis)) + geom_treemap(layout="fixed") + scale_fill_brewer(palette = "Set2") + scale_alpha_discrete(range = c(0.6, 1.0)) ggsave("plot3.png", g3) install.packages("reticulate") library(reticulate) use_python("/usr/local/bin/python3") reticulate::repl_python() knitr::opts_chunk$set(echo = TRUE) library(reticulate) use_python("/usr/local/bin/python3") summary(cars) library(tidyverse) ?qplot qplot(speed, distance, cars) glimpse(cars) qplot(speed, distance, data=cars) qplot(speed, dist, data=cars) cars with(cars, lm(dist~speed)) install.packages("matlabr") install.packages("reticulate") library("reticulate") Sys.which("python") library(reticulate) use_python("/usr/local/bin/python3") repl_python() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() install.packages("tidyverse") install.packages("tidyverse") plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() plumber::plumb(file='r/speech-to-text/api/define_api.R')$run() setwd() setwd('~') devtools::install_github('rstudio/bookdown') install.packages("gapminder") source('~/OneDrive - The University of Nottingham/teaching/_ug/2020-3rd-year-projects/example.R', echo=TRUE) 1+1 "asf" 124*23 meaningOfLife <- 42 meaningOfLife * 2 require(tidyverse) require(jsonlite) source("./perimetry_helpers.R") setwd("~/The University of Nottingham/Anthony's PhD Team - strkvis_pub_archive/strkvis-mri/code") require(tidyverse) require(jsonlite) source("./perimetry_helpers.R") # example file data_fname <- "../perimetry/sub-14196-perimetry-data.json" read_medmont(data_fname) %>% plot_medmont() 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), ycoord = ecc * sin(pa) ) %>% filter(State != "TS_UNTESTED") %>% ggplot(aes(x = ecc, y = pa, size = Value, symbol=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() } read_medmont(data_fname) %>% plot_medmont() the_points %>% mutate( xcoord = ecc * cos(pa), ycoord = ecc * sin(pa) ) %>% filter(State != "TS_UNTESTED") %>% ggplot(aes(x = ecc, y = pa, size = Value, symbol=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() 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), ycoord = ecc * sin(pa) ) %>% filter(State != "TS_UNTESTED") %>% ggplot(aes(x = ecc, y = pa, size = Value, symbol=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() } read_medmont(data_fname) %>% plot_medmont() 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), ycoord = ecc * sin(pa) ) %>% filter(State != "TS_UNTESTED") %>% ggplot(aes(x = pa, y = ecc, size = Value, symbol=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() } read_medmont(data_fname) %>% plot_medmont() coord_polar >coord_polar ? coord_polar 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), ycoord = ecc * sin(pa) ) %>% filter(State != "TS_UNTESTED") %>% ggplot(aes(x = pa, y = ecc, size = Value, symbol=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() } read_medmont(data_fname) %>% plot_medmont() plot_medmont(data) data <- read_medmont(data_fname) plot_medmont(data) the_points %>% mutate( pa_rad = pi * pa / 180.0, xcoord = ecc * cos(pa_rad), ycoord = ecc * sin(pa_rad) ) %>% filter(State != "TS_UNTESTED") %>% ggplot(aes(x = pa_rad, y = ecc, size = Value, symbol=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() data <- read_medmont(data_fname) plot_medmont(data) require(tidyverse) require(jsonlite) source("./perimetry_helpers.R") # example file data_fname <- "../perimetry/sub-14196-perimetry-data.json" data <- read_medmont(data_fname) plot_medmont(data) require(tidyverse) require(jsonlite) source("./perimetry_helpers.R") # example file data_fname <- "../perimetry/sub-14196-perimetry-data.json" data <- read_medmont(data_fname) #' 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) ) } data <- read_medmont(data_fname) the_points %>% mutate( xcoord = ecc * cos(pa_rad), ycoord = ecc * sin(pa_rad) ) %>% filter(State != "TS_UNTESTED") %>% ggplot(aes(x = pa_rad, y = ecc, size = Value, symbol=State)) + geom_point(alpha = 0.9) + 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() ?coord_polar source("./perimetry_helpers.R") # example file data_fname <- "../perimetry/sub-14196-perimetry-data.json" data <- read_medmont(data_fname) plot_medmont(data) 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, symbol=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() } source("./perimetry_helpers.R") # example file data_fname <- "../perimetry/sub-14196-perimetry-data.json" data <- read_medmont(data_fname) plot_medmont(data) 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() } plot_medmont(data) # example file data_fname <- "../perimetry/sub-14326-perimetry-data.json" data <- read_medmont(data_fname) plot_medmont(data)