1
- # ' @title Calculate spectral metrics
2
- # ' @description Calculates CV, SV, and CHV from pixel values dataframe with columns for each wavelength + site_name + aoi_id
3
- # ' @param pixel_values_df the data frame containing pixel values, obtained from extract_pixel_values function
4
- # ' @param wavelengths a list of wavelengths which correspond to column names from pixel_values_df
5
- # ' @param rarefaction either TRUE or FALSE to apply rarefaction step. substantially increases processing time
6
- # ' @param min_points if rarefaction = T, the minimum number of pixels for any aoi - to standardise uneven number of pixels across different sites
7
- # ' @param n if rarefaction = T, number of subset permutations
8
- # ' @return a dataframe containing spectral metrics for each aois within each site/raster
1
+ # ' @title Calculate Spectral Metrics
2
+ # ' @description Calculates CV, SV, and CHV from a pixel values dataframe with columns for each wavelength, `site_name`, and `aoi_id`.
3
+ # ' This help file applies to the functions `calculate_cv`, `calculate_sv`, `calculate_chv`, `calculate_chv_nopca`, and `calculate_spectral_metrics`.
4
+ # ' @param pixel_values_df A data frame containing pixel values, typically obtained from the `extract_pixel_values` function.
5
+ # ' @param wavelengths A list of wavelengths that correspond to column names in `pixel_values_df`.
6
+ # ' @param rarefaction Logical; if TRUE, applies a rarefaction step that increases processing time.
7
+ # ' @param min_points Integer; minimum number of pixels per `aoi` to standardize uneven pixel numbers across sites (used if `rarefaction = TRUE`).
8
+ # ' @param n Integer; number of subset permutations if `rarefaction = TRUE`.
9
+ # ' @return A dataframe containing spectral metrics for each `aoi` within each site/raster.
10
+ # ' @aliases calculate_cv calculate_sv calculate_chv calculate_chv_nopca calculate_spectral_metrics
9
11
# ' @export
10
-
11
- # rarefraction cv function
12
+ # ' @import data.table
13
+ # ' @examples
14
+ # ' set.seed(123)
15
+ # ' df <- data.frame(
16
+ # ' site_name = rep(c("site_one", "site_two", "site_three", "site_four"), each = 5000),
17
+ # ' aoi_id = 1,
18
+ # ' blue = runif(20000, min = 0, max = 1),
19
+ # ' green = runif(20000, min = 0, max = 1),
20
+ # ' red = runif(20000, min = 0, max = 1),
21
+ # ' red_edge = runif(20000, min = 0, max = 1),
22
+ # ' nir = runif(20000, min = 0, max = 1))
23
+ # ' pixelvalues <- calculate_cv(df,
24
+ # ' wavelengths = c('blue','green','red','red_edge','nir'),
25
+ # ' rarefaction = TRUE, min_points = 5000, n = 999)
12
26
calculate_cv <- function (pixel_values_df ,
13
27
wavelengths ,
14
28
rarefaction = FALSE ,
15
29
min_points = NULL ,
16
30
n = NULL ) {
17
31
18
32
# convert to a data.table for efficiency
19
- setDT(pixel_values_df )
33
+ data.table :: setDT(pixel_values_df )
20
34
21
35
if (rarefaction ) {
22
36
# initialize a list to store CV values for each replication
@@ -56,7 +70,9 @@ calculate_cv <- function(pixel_values_df,
56
70
57
71
58
72
59
- # sv function
73
+ # sv function\
74
+ # ' @import data.table
75
+ # ' @export
60
76
calculate_sv <- function (pixel_values_df , wavelengths ) {
61
77
# convert pixel_values_df to data.table for better performance
62
78
setDT(pixel_values_df )
@@ -83,6 +99,8 @@ calculate_sv <- function(pixel_values_df, wavelengths) {
83
99
84
100
85
101
# chv function
102
+ # ' @import data.table
103
+ # ' @export
86
104
calculate_chv <- function (df , dim ) {
87
105
CHV_df <- df %> %
88
106
select(1 : dim )
@@ -96,6 +114,8 @@ calculate_chv <- function(df, dim) {
96
114
}
97
115
98
116
# function to calculate chv for each aoi
117
+ # ' @import data.table
118
+ # ' @export
99
119
calculate_chv_nopca <- function (df ,
100
120
wavelengths ,
101
121
rarefaction = FALSE ,
@@ -141,7 +161,8 @@ calculate_chv_nopca <- function(df,
141
161
}
142
162
143
163
144
- # # FUNCTION FOR CALCULATING ALL METRICS
164
+ # ' @import data.table
165
+ # ' @export
145
166
calculate_spectral_metrics <- function (pixel_values_df ,
146
167
masked = TRUE ,
147
168
wavelengths ,
0 commit comments