library(tidyverse)
library(imager)
library(recipes)
library(mgcv)
library(caret)
library(modelr)

set.seed(10010011)
train_labels <- read_csv("../input/train.csv")
test_labels <- read_csv("../input/test.csv")

fpaths <- list.files("../input/train_images", full.names = TRUE)
fpath_names <- str_remove_all(list.files("../input/train_images"), "[:punct:]|png")


test_fpaths <- list.files("../input/test_images",  full.names = TRUE)

test_fpath_names <- str_remove_all(list.files("../input/test_images"), "[:punct:]|png")


faster_normalize <- function(v){v/max(v)}

png_to_histogram <- function(img_path){
  cimg <- grayscale(load.image(img_path))
  retina <- cimg > 0.1

  cimg <- faster_normalize(cimg[retina])
  return(
    hist(cimg, plot = FALSE, breaks = seq(from = 0, to = 1, length.out = 100))$density
  )
}

fimgs <- map(.x = fpaths, .f = png_to_histogram) %>%
  set_names(fpath_names) %>%
  bind_cols() %>%
  t %>%
  as.data.frame() %>%
  rownames_to_column() %>%
  rename(id_code = rowname)

train_data <- inner_join(train_labels, fimgs)

case_dist <- ecdf(train_data$diagnosis)

lcontrol <- trainControl(
  method = "repeatedcv",
  repeats = 5,
)

lasso_fit <- train(
  diagnosis ~ ., data = train_data %>%
    select(-id_code) %>% data.frame(),
    method = "glmnet",
  trControl = lcontrol,
  tuneLength = 20
)



test_fimgs <- map(.x = test_fpaths, .f = png_to_histogram) %>%
  set_names(test_fpath_names) %>%
  bind_cols() %>%
  t %>%
  as.data.frame() %>%
  rownames_to_column() %>%
  rename(id_code = rowname) %>%
  inner_join(test_labels) %>%
  add_predictions(lasso_fit) %>%
  as_tibble() %>%
  mutate(diagnosis = floor(quantile(train_data$diagnosis,probs= percent_rank(pred))))

write.csv(x = test_fimgs[,c('id_code', 'diagnosis')], file = "submission.csv", row.names = FALSE)