#' An Image Scrambling Function.
#'
#' This function copies the information from tif image files, renames the images with a scramble key, then saves two csv files, one with the scramble key and one with just the scramble numbeers.
#' @param path What is the path to the folder containing the images?
#' @param chunks How many blocks of information should the image names be broken into as separated by "_"? Defaults to 1.
#' @param sep What string so you want to divide the file name with into various chunks
#' @param filetype What filetype are you scrambling
#' @param bin How many bins does your image have
#' @export
#' @keywords image scrambler
#' image_scrambler()
image_scrambler <- function(path, chunks = 1, sep = "_", filetype = "tif", bin = 1){
file_paths <- list.files(path = path)
file_paths <- grep(pattern = paste0("\\.", filetype,"$"), x = file_paths, value = TRUE)
file_number <- length(file_paths)
file_info <- regmatches(file_paths, regexpr(pattern = paste0("([A-z0-9[:punct:] ]+)(?=\\.", filetype, "$)"), file_paths, perl = TRUE)) #this is for grabbing all the info from the file name which will have some sort of identifying information of the image in regards to other images in the folder. It removes the path and the extension from the file name.
scramble_num <- sample(1:file_number, file_number, replace = FALSE) #this creates a set of random numbers from 1 to the number of files so the highest number used for scrambling is the number of files. These will be what the function uses to replace the file names
if(chunks > 1){
info_split <- strsplit(file_info, split = sep)
empty_matrix <- c()
empty_matrix <- matrix(nrow = file_number, ncol = chunks)
name_vec <- c(rep(0, times = chunks))
for(i in 1:chunks){
name_vec[i] <- paste0("Column_", i)
} #For producing generic column names (Column_1 .... Column_i) based on the number of chunks
colnames(empty_matrix) <- name_vec
for(i in 1:chunks){
empty_matrix[,i] <- c(sapply(info_split, "[",i))
}
file_df <- cbind(file_info, empty_matrix, scramble_num, file_paths) ### Combine everything into a matrix
file_df <- as.data.frame(file_df)
} else if(chunks == 1){
file_df <- as.data.frame(cbind(file_info, scramble_num, file_paths))
} else {
print("Error: Chunks parameter can not be negative")
} #This block is for building columns by breaking up the info it pulled from the file number to a number of "chunks" as specified by the chunk arguement
path2 <- sub("(\\s?to\\s?be\\s?scrambled)", "", path, ignore.case = TRUE)
path2 <- paste0(path2, " SCRAMBLED")
dir.create(path = path2)
file.copy(from = file.path(path, file_df[,"file_paths"]) , to = path2, copy.mode = TRUE)
file.rename(from = file.path(path2, file_df[,"file_paths"]) , to = paste0(file.path(path2, file_df[,"scramble_num"]), ".tif")) ###Rename the files in the folder
file_df$scramble_num <- as.numeric(as.character(file_df$scramble_num))
file_df <- file_df[order(file_df$scramble_num), ]
key_file <- file_df
scrambled_file <- subset(file_df, select = c(scramble_num))
if(bin > 1){
scrambled_file <- as.matrix(scrambled_file)
rownum <- nrow(scrambled_file)
rows <- rep(1:rownum, each = bin)
scrambled_file <- scrambled_file[rows, ]
scrambled_file <- as.data.frame(scrambled_file)
colnames(scrambled_file) <- c("scramble_num")
scrambled_file$Bin <- rep(1:bin, rownum)
}
write.csv(key_file, file = file.path(path2, "key_file.csv"), row.names = FALSE)
write.csv(scrambled_file, file.path(path2, "scrambled_file.csv"), row.names = FALSE)
}
#' An Image Unscrambling Function.
#'
#' This function uses the key and scramble csv files generated by the image scrambler and recombines them into a functional csv file for analysis
#' @param path What is the path to the folder containing the csv files?
#' @export
#' @keywords image unscrambler
#' image_unscrambler()
image_unscrambler <- function(path){
files <- list.files(path = path)
stopifnot(sum(grepl(pattern = "key_file.csv", files)) == 1, sum(grepl(pattern = "scrambled_file.csv", files)) == 1)
key <- read.csv(paste0(path, "/key_file.csv"))
scramble <- read.csv(paste0(path, "/scrambled_file.csv"))
complete <- merge(x = key, y = scramble, all.y = TRUE, by = "scramble_num")
write.csv(complete, file = file.path(path, "complete.csv"), row.names = FALSE)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.