## ----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.
basilisk_dir <- Sys.getenv("BASILISK_EXTERNAL_DIR", unset = "")
py_ok <- (!nzchar(basilisk_dir) || dir.exists(basilisk_dir)) &&
  tryCatch(
    grayleafspot_python_available(engine_model = "localunet"),
    error = function(e) FALSE
  )

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

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

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

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

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

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

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

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

## ----convert-tidy-data--------------------------------------------------------
df <- as_grayleafspot_growth_data(run)
df

## ----run-own-images, eval=py_ok-----------------------------------------------
img_dir <- system.file("extdata", "testdata", "06FEB", package = "grayleafspotr")
out_dir <- file.path(tempdir(), "grayleafspotr-run-demo")

res <- grayleafspot_run(input_dir = img_dir, output_dir = out_dir, run_name = "demo")

# Per-image feature table
res$results

# Run manifest (paths, timestamp)
res$run

## ----analyze-own-images, eval=py_ok-------------------------------------------
run_own <- grayleafspot_analyze(
  input_dir = img_dir, output_dir = out_dir,
  run_name = "demo-analyze", verbose = FALSE
)
plot_colony_expansion(run_own)

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

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

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

