## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(pointcoral)
library(dplyr)

## ----import-------------------------------------------------------------------
example_dir <- system.file("extdata", package = "pointcoral")

points_raw <- read_cpce_folder(
  path = example_dir,
  image_root = example_dir,
  recursive = FALSE
)

dplyr::glimpse(points_raw)

## ----match--------------------------------------------------------------------
points_raw <- match_images(points_raw, image_root = example_dir)

## ----bare---------------------------------------------------------------------
validate_points(points_raw)
summarize_images(points_raw)

points_split_raw <- split_ml_points(points_raw, split_by = "image", seed = 1)
ml_points_raw <- make_ml_points(points_split_raw)

dplyr::count(ml_points_raw, label, class_id, sort = TRUE)

## ----crosswalk----------------------------------------------------------------
crosswalk_path <- system.file(
  "extdata", "pointcoral_example_crosswalk.csv",
  package = "pointcoral"
)

crosswalk <- read_label_crosswalk(crosswalk_path)
dplyr::glimpse(crosswalk)

## ----check--------------------------------------------------------------------
check_crosswalk(points_raw, crosswalk)

## ----standardize--------------------------------------------------------------
points_clean <- standardize_labels(
  points_raw,
  crosswalk,
  unknown_action = "warn"
)

points_clean |>
  count(raw_label, full_label, label_class, major_category, class_id, sort = TRUE)

## ----validate-----------------------------------------------------------------
validate_points(points_clean)

## ----summarize----------------------------------------------------------------
summarize_images(points_clean, class_col = "major_category")
summarize_transects(points_clean, class_col = "major_category")
summarize_sites(points_clean, class_col = "major_category")

summarize_images(points_clean, class_col = "clean_label")

## ----ml-----------------------------------------------------------------------
points_split <- split_ml_points(points_clean, split_by = "image", seed = 1)
ml_points <- make_ml_points(points_split, class_col = "ml_class")

out_dir <- tempfile("pointcoral-vignette-")
dir.create(out_dir)

write_ml_points_csv(ml_points, file.path(out_dir, "ml"))

## ----patches, eval = FALSE----------------------------------------------------
# extract_point_patches(
#   points_split,
#   image_root = example_dir,
#   out_dir = file.path(out_dir, "patches"),
#   patch_size = 224,
#   class_col = "ml_class",
#   edge = "skip"
# )

## ----masks, eval = FALSE------------------------------------------------------
# make_sparse_masks(
#   points_split,
#   image_root = example_dir,
#   out_dir = file.path(out_dir, "sparse_masks"),
#   radius = 3
# )

## ----qc, eval = FALSE---------------------------------------------------------
# write_qc_overlays(
#   points_split,
#   image_root = example_dir,
#   out_dir = file.path(out_dir, "qc"),
#   label_col = "ml_class"
# )

## ----wrapper, eval = FALSE----------------------------------------------------
# run_pointcoral(
#   cpce_dir = example_dir,
#   image_root = example_dir,
#   out_dir = file.path(out_dir, "outputs"),
#   make_patches = TRUE,
#   make_masks = TRUE,
#   make_qc = TRUE
# )

