Contour check for Triadica sebifera

The goals of this notebook are:
1. To check the original contour data of Triadica sebifera.
2. To check contours reconstructed from oriented true EFD normalization for all samples.

Output figures of this notebook are not included in the manuscript, but they are saved in the results/ directory.

Setup


Attaching package: 'Momocs'
The following object is masked from 'package:stats':

    filter
source("R/normalization.R")
source("R/compare_contours.R")
# color palette for colorblind-friendly visualization
palette("Okabe-Ito")
# set the data directory from environment variable (.Renviron)
data_dir <- Sys.getenv("PROJECT_DATA_DIR")

Load contour data

Load the contour data from the CSV files in the data/contour_triadica_sebifera/ directory. There are 30 samples.

# file paths of contour CSV files
file_paths_xy <- list.files(
  file.path(data_dir, "data/contour_triadica_sebifera"),
  full.names = TRUE,
  pattern = "\\.csv$"
)
# extract sample IDs from file names
id_name <- file_path_sans_ext(basename(file_paths_xy))

# load contour data into a list of matrices
xy_list <- lapply(file_paths_xy, function(fp) {
  #cat("File:", fp, "\n")
  df <- read.csv(fp)
  df <- df[, c("x", "y")]
  df <- as.matrix(df)
  storage.mode(df) <- "double"
  # if the contour is not closed, add the first point to the end to close it
  if (!all(df[1, ] == df[nrow(df), ])) {
    df <- rbind(df, df[1, ])
  }
  return(df)
})
# assign names to the list of contours
names(xy_list) <- id_name

Load oriented true normalized EFD coefficients

Load the oriented true normalized EFD coefficients from the CSV files in the data/coefficients_efd_normalized_triadica_sebifera/ directory. These data are generated with LeafContourEFD.

# file paths of EFD coefficient CSV files
file_paths <- list.files(
  file.path(data_dir, "data/coefficients_efd_normalized_triadica_sebifera"),
  full.names = TRUE,
  pattern = "\\.csv$"
)

# load EFD coefficients into a list of data frames
ef_list <- lapply(file_paths, function(fp) {
  #cat("File:", fp, "\n")
  df <- read.csv(fp)
  return(df)
})

Visualize the original contours and the contours reconstructed from oriented true EFD normalization

Overlay the original contour and the reconstructed contour from Oriented True EFD normalization.

layout(matrix(seq_along(xy_list), nrow = 5, byrow = TRUE))
for (i in seq_along(xy_list)) {
  compare_contour_oriented_true_EFD_normalization(
    original = xy_list[[i]],
    ef_normalized = ef_list[[i]],
    label = i,
    nb.h = 5,
    mar = c(0, 0, 0, 0),
    lwd = 3,
    cex.text = 5
  )
}

Save the plot as SVG file
save_dir <- "results"
dir.create(save_dir, showWarnings = FALSE)
save_file_name <- "supplementary_compare_contours_oriented_true_efd_normalized_triadica_sebifera.svg"
svg(file.path(save_dir, save_file_name), bg = "transparent")
layout(matrix(seq_along(xy_list), nrow = 5, byrow = TRUE))
for (i in seq_along(xy_list)) {
  compare_contour_oriented_true_EFD_normalization(
    original = xy_list[[i]],
    ef_normalized = ef_list[[i]],
    label = i,
    nb.h = 5,
    mar = c(0, 0, 0, 0),
    lwd = 3,
    cex.text = 5
  )
}
dev.off()