Skip to content

Commit

Permalink
v0.2.2, minor improvements
Browse files Browse the repository at this point in the history
* Automatically expand `path_out`, so abbreviated paths don't cause Zerene Stacker to error out.
* Added `digits` argument to add leading zeros to file numbers in `expand_zs_dataframe`.
  • Loading branch information
ethanbass committed Mar 18, 2024
1 parent 654f1d6 commit 47135c8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 39 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
Package: zerenebatchR
Type: Package
Title: Utility for Batch Processing in Zerene Stacker
Version: 0.2.1
Version: 0.2.2
Imports: fs,
magrittr,
xml2
xml2,
stringr
Author: Ethan Bass
Maintainer: Ethan Bass <ethanbass@gmail.com>
Description: Writes Zerene Stacker batch files and executes them from the commandline.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ importFrom(fs,file_copy)
importFrom(fs,file_move)
importFrom(fs,path)
importFrom(fs,path_home)
importFrom(stringr,str_pad)
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# zerenebatchR 0.2.2

* Allow multiple columns as inputs to `c_id` in `run_zs_batch`.
* Automatically expand `path_out`, so abbreviated paths don't cause Zerene Stacker to error out.
* Added `digits` argument to add leading zeros to file numbers in `expand_zs_dataframe`.

# zerenebatchR 0.2.1

* Added check for duplicated ids in `expand_zs_dataframe`.
Expand Down
46 changes: 31 additions & 15 deletions R/expand_zs_dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#'
#' This function facilitates stacking photos that are numbered sequentially. It
#' must be supplied with a \code{data.frame} with columns containing the
#' information about the images to be stacked.
#' @importFrom stringr str_pad
#' @param df A data.frame containing the first and last photo, file path, and
#' grouping variable.
#' @param c_path String or numerical index specifying the column where the file
Expand All @@ -15,42 +17,56 @@
#' @param extension String specifying the file extension of the images. The
#' extension must be in the correct case so it matches exactly to the extension
#' of the files to be stacked.
#' @param digits How many digits should be in the number.
#' @return Expanded data.frame
#' @export

expand_zs_dataframe <- function(df, c_path, c_id, c_start, c_end,
extension = "JPG"){
extension = "JPG", digits = 4){
# check missing lines
rm <- which(is.na(df[,c_start]) | is.na(df[,c_end]))
df <- as.data.frame(df)
df[,c_start] <- as.numeric(df[,c_start])
df[,c_end] <- as.numeric(df[,c_end])
rm <- which(is.na(df[, c_start]) | is.na(df[, c_end]))
if (length(rm) > 0){
df <- df[-rm,]
}
if (length(c_id) > 1){
df$id <- apply(df[, c_id], MARGIN = 1, function(x) paste(x, collapse = "-"))
} else{
df$id <- df[, c_id]
}
# check for duplicated IDs
duplicated_ids <- duplicated(df[,c_id])
duplicated_ids <- duplicated(df$id)
if (any(duplicated_ids)){
stop(paste0("Some identifiers appear to be duplicated. Please double check IDs and try again.
Duplicated IDs: ", sQuote(paste0(df[which(duplicated_ids), c_id])), "."))
Duplicated IDs: ", paste(sQuote(df[which(duplicated_ids), "id"]), collapse = ", "), "."))
}
# check for photos out of order
wrong_order <- which(!(df[,c_start] < df[,c_end]))
wrong_order <- which(!(df[, c_start] < df[, c_end]))
if (length(wrong_order) > 0){
stop(paste0("Images appear to be out of order. Double check line ",
sQuote(paste0(wrong_order,collapse=", ")), "."))
stop(paste0("Images appear to be out of order. Double check IDs: ",
paste(sQuote(df[wrong_order,"id"]), collapse = ", "), "."))
}
extension <- gsub("^\\." ,"", extension)
pp <- lapply(1:nrow(df), function(i){
data.frame(id = paste(df[i, c_id], collapse="_"),
path = paste0(df[i,c_path],
seq(df[i, c_start], df[i, c_end], by = 1),
data.frame(id = paste(df[i, "id"], collapse = "_"),
path = paste0(df[i, c_path],
stringr::str_pad(seq(df[i, c_start], df[i, c_end], by = 1),
width = digits, side = "left", pad = "0"),
".", extension))
})
pp <- do.call(rbind, pp)
# check for missing files
missing_files <- which(!file.exists(pp$path))
if (length(missing_files > 0)){
warning(paste0("Some image files could not be found: ",
(paste0(sQuote(missing_files), collapse=", ")), "."))
pp <- pp[-missing_files,]
missing_files <- pp[which(!file.exists(pp$path)), "path"]
if (length(missing_files) > 0){
if (length(missing_files) == nrow(df)){
stop("Files could not be found. Please check path(s) and try again.")
} else{
warning(paste0("Some image files could not be found: ",
(paste0(sQuote(missing_files), collapse=", ")), "."))
pp <- pp[-missing_files,]
}
}
pp
}
30 changes: 14 additions & 16 deletions R/run_zs_batch.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ globalVariables(c("."))
#' @author Ethan Bass
#' @export

run_zs_batch <- function(files, c_path = 1, c_split = 2,
run_zs_batch <- function(files, c_split = 1, c_path = 2,
path_out, stacker = c("pmax", "dmap"),
temp, path_template = NULL,
path_xml = NULL, stack = TRUE){
Expand All @@ -51,7 +51,7 @@ run_zs_batch <- function(files, c_path = 1, c_split = 2,
if (stack){
stop("If `stack == TRUE`, a `data.frame` should be provided to the files argument.")
}
exists <- dir_exists(files)
exists <- fs::dir_exists(files)
if (!any(exists)){
stop("The provided directories do not exist. Please check paths and try again.")
}
Expand Down Expand Up @@ -85,8 +85,8 @@ run_zs_batch <- function(files, c_path = 1, c_split = 2,
system <- .Platform$OS.type

launch_cmd_path <- switch(system,
"unix" = path_home("Library/Preferences/ZereneStacker/zerenstk.launchcmd"),
"windows" = path_home("AppData\\ZereneStacker\\zerenstk.launchcmd"),
"unix" = fs::path_home("Library/Preferences/ZereneStacker/zerenstk.launchcmd"),
"windows" = fs::path_home("AppData\\ZereneStacker\\zerenstk.launchcmd"),
"linux" = "~/.ZereneStacker/zerenstk.launchcmd"
)

Expand Down Expand Up @@ -118,13 +118,11 @@ run_zs_batch <- function(files, c_path = 1, c_split = 2,
l <- x %>% xml_children() %>% .[[2]] %>% xml_find_all("//Sources") %>% xml_children %>% length
sources <- x %>% xml_children() %>% .[[2]] %>% xml_find_all("//Sources")
xml_attr(sources, "length") <- as.character(l)
# x %>% xml_children() %>% .[[2]] %>% xml_find_all("//Sources")

# set path out

x %>% xml_children() %>% .[[2]] %>% xml_find_all("//OutputImagesDesignatedFolder") %>%
xml_replace(paste0('OutputImagesDesignatedFolder value="', path_out, '"'))
# x %>% xml_children %>% .[[4]] %>% xml_add_child(.value=gsub("path_out", path_out, parser))
xml_replace(paste0('OutputImagesDesignatedFolder value="', fs::path_expand(path_out), '"'))

# set stacking algorithm

Expand All @@ -138,7 +136,8 @@ run_zs_batch <- function(files, c_path = 1, c_split = 2,
# write batch file

if (is.null(path_xml)){
path_xml <- paste0(path_out, "batchfile_", strftime(Sys.time(),format = "%Y-%m-%d_%H-%M-%S"), ".xml")
path_xml <- paste0(path_out, "batchfile_", strftime(Sys.time(),
format = "%Y-%m-%d_%H-%M-%S"), ".xml")
}

write_xml(x, file = path_xml)
Expand All @@ -148,12 +147,11 @@ run_zs_batch <- function(files, c_path = 1, c_split = 2,
system(paste0(launch_cmd,
" -noSplashScreen -runMinimized -exitOnBatchScriptCompletion -batchScript ",
path_xml))
# , " -sourcePath=", path

# delete temp folders

if (temp){
dir_delete(stacks)
fs::dir_delete(stacks)
}
}

Expand All @@ -174,22 +172,22 @@ run_zs_batch <- function(files, c_path = 1, c_split = 2,
stack_files <- function(df, c_path, c_split, temp = TRUE){

if (length(c_split) > 1){
df$id <-apply(df[,c_split], MARGIN = 1, function(x) paste(x, collapse="-"))
df$id <-apply(df[, c_split], MARGIN = 1, function(x) paste(x, collapse = "-"))
} else{
df$id <- df[,c_split]
df$id <- df[, c_split]
}

df <- split(as.data.frame(df), df[, "id"])
file_action <- switch(as.character(temp),
"TRUE" = file_copy,
"FALSE" = file_move)
"TRUE" = fs::file_copy,
"FALSE" = fs::file_move)
paths <- sapply(df, function(x){
if (any(fs::file_exists(x[, c_path]))){
path <- x[1, c_path]
dirn <- dirname(path)
dirn <- ifelse(temp, fs::path(dirn,"temp"), dirn)
dirn <- ifelse(temp, fs::path(dirn, "temp"), dirn)
dir_path <- fs::path(dirn, x[, "id"][1])
dir_create(dir_path)
fs::dir_create(dir_path)
sapply(x[,c_path], function(file){
try(file_action(file, dir_path))
})
Expand Down
13 changes: 12 additions & 1 deletion man/expand_zs_dataframe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/run_zs_batch.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 47135c8

Please sign in to comment.