library(tidyverse)
library(tidymodels)
library(patchwork)
library(gettyR)
dir <- "C:/Users/DHill/Desktop/robert_cell_sorting/sessions"
session <- "U-2020-07-30-s2"

dir <- "C:/Users/DHill/Desktop/test_uly_data"
session <- "test_session"


spike_data <- gettyR::open_spike_data(dir, session) 
trace_data <- gettyR::open_cell_trace(dir)
cells <- unique(unlist(spike_data$sorted_spikes) %>%
           .[grepl("^cell", .)])

create_spike_dirs(dir = dir, session, cells)
bits <- c("free_reward", "fixation_cross", "win_lose", "fractal_display")
situations <- c(4, rep(list(1:3), 3))

walk2(bits, situations,
      function(b, s) {
        p <- plot_getty_responses(spike_data, trial_situations = s, trial_bits = b)
        path <- paste0(file.path(dir, session, "figures/unsorted_responses", b), ".png")
        ggsave(plot = p, filename = path, device = "png")
      })
cells <- unique(trace_data$cell)
walk(cells, gettyR::plot_and_save_cluster_data, session_folder = session, dir_folder = dir, x_length = 500)
#gettyR::plot_and_save_cluster_data(NULL, session_folder = session, dir_folder = dir)
bits <- c("free_reward", "fixation_cross", "win_lose", "fractal_display")
situations <- c(4, rep(list(1:3), 3))
cells <- unique(unlist(spike_data$sorted_spikes) %>%
           .[grepl("^cell", .)])

arguments_df <- data.frame(trial_bits = bits) %>%
  dplyr::mutate(trial_situations = situations) %>%
  slice(rep(row_number(), length(cells))) %>%
  dplyr::mutate(specific_cell = rep(cells, each = length(bits)))

safe_plot <- purrr::safely(gettyR::plot_getty_responses)
pwalk(arguments_df, function(trial_situations, trial_bits, specific_cell) {
  p <- safe_plot(spike_data, specific_cell = specific_cell, trial_situations = trial_situations, trial_bits = trial_bits)
  if(is.null(p$error)) {
    path <- paste0(file.path(dir, session, "figures", specific_cell, trial_bits), ".png")
    ggsave(plot = p$result, filename = path, device = "png")
  }
})


RobWHickman/gettyR documentation built on Sept. 16, 2020, 10:54 p.m.