## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  echo    = TRUE,
  message = FALSE,
  warning = FALSE,
  fig.width  = 7,
  fig.height = 4.5
)
library(grayleafspotr)

# Gate the pipeline chunks with a presence check. If an external basilisk
# cache is configured, it must exist before the live examples can run. Live
# Python examples are not run on Bioconductor build machines.
is_bioc_build <- identical(Sys.getenv("IS_BIOC_BUILD_MACHINE"), "true")
basilisk_dir <- Sys.getenv("BASILISK_EXTERNAL_DIR", unset = "")
py_ok <- !is_bioc_build &&
  (!nzchar(basilisk_dir) || dir.exists(basilisk_dir)) &&
  tryCatch(
    grayleafspot_python_available(engine_model = "localunet"),
    error = function(e) FALSE
  )

## ----load-example-run---------------------------------------------------------
example_run <- example_grayleafspot_results()

## ----inspect-feature-table----------------------------------------------------
example_run$results[, c("filename", "day", "area_mm2", "diameter_mm",
                         "circularity", "crack_coverage_pct", "qc_status")]

## ----locate-bundled-images----------------------------------------------------
image_dir <- system.file("extdata", "testdata", "06FEB", package = "grayleafspotr")
list.files(image_dir)

## ----plot-colony-expansion----------------------------------------------------
plot_colony_expansion(example_run)

## ----plot-growth-roughness----------------------------------------------------
plot_growth_roughness(example_run)

## ----plot-stress-remodeling---------------------------------------------------
plot_stress_remodeling(example_run)

## ----plot-texture-organization------------------------------------------------
plot_texture_organization(example_run)

## ----plot-shape-vs-stress-----------------------------------------------------
plot_shape_vs_stress(example_run)

## ----plot-feature-heatmap-----------------------------------------------------
plot_feature_heatmap(example_run)

## ----plot-radial-profile------------------------------------------------------
plot_radial_profile(example_run)

## ----growth-data-tidy---------------------------------------------------------
growth_data <- as_grayleafspot_growth_data(example_run)
growth_data

## ----custom-plot--------------------------------------------------------------
plot_colony_expansion(example_run) +
  ggplot2::labs(
    title    = "Colony expansion: 06-FEB experiment",
    subtitle = "WT strain, PCBM substrate"
  ) +
  ggplot2::theme_classic()

## ----run-simple-entry-point, eval=py_ok---------------------------------------
# img_dir <- system.file("extdata", "testdata", "06FEB", package = "grayleafspotr")
# out_dir <- file.path(tempdir(), "grayleafspotr-workflow-demo")
# 
# res <- grayleafspot_run(
#   input_dir  = img_dir,
#   output_dir = out_dir,
#   run_name   = "isolate_A"
# )
# 
# res$results   # per-image feature table (data frame)
# res$run       # run manifest

## ----run-full-featured, eval=py_ok--------------------------------------------
# run <- grayleafspot_analyze(
#   input_dir  = img_dir,
#   output_dir = out_dir,
#   run_name   = "isolate_A_analyze",
#   verbose    = FALSE
# )
# 
# plot_colony_expansion(run)

## ----visualize-overlay, eval=py_ok--------------------------------------------
# image_file <- list.files(img_dir, full.names = TRUE)[1]
# plot_grayleafspot_overlay(run, image_path = image_file)

## ----reload-saved-results, eval=py_ok-----------------------------------------
# saved_dir <- run$run$output_dir
# reloaded <- read_grayleafspot_results(saved_dir)
# plot_colony_expansion(reloaded)

## ----session-info-------------------------------------------------------------
sessionInfo()

