@@ -532,24 +532,26 @@ sits_sampling_design <- function(cube,
532532# ' with a number of samples per class and allocates a set of
533533# ' locations for each class.
534534# '
535- # ' (b) When the parameter "n_alloc_class " is available , the method selects
536- # ' a set of locations based on a classified cube based on the number of
537- # ' samples per class in this parameter. The "n_alloc_class" parameter
535+ # ' (b) When the parameter "sampling_design " is not provided , the method selects
536+ # ' a set of locations based on a classified cube based on the parameter
537+ # ' "samples_per_class". This parameter
538538# ' should either be a named vector (names are the labels of the cube)
539- # ' or a single value. In the latter case, this value is used to retrieve
539+ # ' or a single value. In the latter case, the same value is used to retrieve
540540# ' the samples for all classes.
541541# '
542542# ' @param cube Classified cube
543543# ' @param sampling_design Result of sits_sampling_design
544544# ' @param alloc Allocation method chosen
545+ # ' @param samples_per_class Number of samples per class
546+ # ' (in case sampling_design is NULL)
545547# ' @param overhead Additional percentage to account
546548# ' for border points
547549# ' @param multicores Number of cores that will be used to
548550# ' sample the images in parallel.
549551# ' @param memsize Memory available for sampling.
550552# ' @param shp_file Name of shapefile to be saved (optional)
551553# ' @param progress Show progress bar? Default is TRUE.
552- # ' @return samples Point sf object with required samples
554+ # ' @return samples Point sf object with required samples and label
553555# '
554556# ' @examples
555557# ' if (sits_run_examples()) {
@@ -571,24 +573,31 @@ sits_sampling_design <- function(cube,
571573# ' probs_cube,
572574# ' output_dir = tempdir()
573575# ' )
576+ # ' # Option 1 - select samples based on sampling design
574577# ' # estimated UA for classes
575578# ' expected_ua <- c(
576579# ' Cerrado = 0.95, Forest = 0.95,
577580# ' Pasture = 0.95, Soy_Corn = 0.95
578581# ' )
579582# ' # design sampling
580583# ' sampling_design <- sits_sampling_design(label_cube, expected_ua)
581- # ' # select samples
584+ # ' # select samples using the sampling design
582585# ' samples <- sits_stratified_sampling(
583586# ' label_cube,
584- # ' sampling_design, "alloc_prop"
587+ # ' sampling_design = sampling_design,
588+ # ' alloc = "alloc_prop"
589+ # ' )
590+ # ' # Option 2 - Select samples based on a fixed number of samples per class
591+ # ' samples <- sits_stratified_sampling(
592+ # ' label_cube,
593+ # ' samples_per_class = 100
585594# ' )
586595# ' }
587596# ' @export
588597sits_stratified_sampling <- function (cube ,
589598 sampling_design = NULL ,
590599 alloc = " alloc_prop" ,
591- n_alloc_class = 100 ,
600+ samples_per_class = 100 ,
592601 overhead = 1.2 ,
593602 multicores = 2L ,
594603 memsize = 2L ,
@@ -605,12 +614,21 @@ sits_stratified_sampling <- function(cube,
605614 # get the labels
606615 labels <- .cube_labels(cube )
607616 n_labels <- length(labels )
608- # check number of labels
609- .check_smoothness(n_alloc_class , n_labels )
610- # Prepare smoothness parameter
611- if (length(n_alloc_class ) == 1L ) {
612- n_alloc_class <- rep(n_alloc_class , n_labels )
617+ # check progress
618+ progress <- .message_progress(progress )
619+ # check samples_per_class parameter is either 1
620+ # or is a named vector with the cube labels
621+ .check_samples_per_class(samples_per_class , labels )
622+ # Prepare samples_per_class parameter
623+ if (length(samples_per_class ) == 1L ) {
624+ samples_per_class <- rep(samples_per_class , n_labels )
625+ names(samples_per_class ) <- labels
626+ } else {
627+ .check_that(all(names(samples_per_class ) %in% labels ),
628+ msg = .conf(" messages" ,
629+ " sits_stratified_sampling_wrong_labels" ))
613630 }
631+ # if a sampling_design parameter exists, use it
614632 if (.has(sampling_design )) {
615633 .check_that(nrow(sampling_design ) < = n_labels )
616634 # check names of labels
@@ -619,15 +637,14 @@ sits_stratified_sampling <- function(cube,
619637 .check_that(alloc %in% colnames(sampling_design ),
620638 msg = .conf(" messages" , " sits_stratified_sampling_alloc" )
621639 )
640+
622641 # check samples by class
623- n_alloc_class <- unlist(sampling_design [, alloc ])
642+ samples_per_class <- .samples_by_design(sampling_design ,
643+ labels ,
644+ alloc ,
645+ overhead )
624646 }
625- .check_int_parameter(n_alloc_class ,
626- is_named = TRUE ,
627- msg = .conf(" messages" , " sits_stratified_sampling_samples" )
628- )
629- # check progress
630- progress <- .message_progress(progress )
647+
631648 # The following functions define optimal parameters for parallel processing
632649 # Get block size
633650 block <- .raster_file_blocksize(.raster_open_rast(.tile_path(cube )))
@@ -656,14 +673,11 @@ sits_stratified_sampling <- function(cube,
656673 if (.parallel_start(workers = multicores )) {
657674 on.exit(.parallel_stop(), add = TRUE )
658675 }
659- samples_class <- .samples_by_design(sampling_design ,
660- labels ,
661- alloc ,
662- overhead )
676+
663677 # call function to allocate sample per strata
664678 samples <- .samples_alloc_strata(
665679 cube = cube ,
666- samples_class = samples_class ,
680+ samples_per_class = samples_per_class ,
667681 alloc = alloc ,
668682 block = block ,
669683 progress = progress
0 commit comments