I was trying to write tests that would break this to get the expected error for each of the "stops" in the function. I used a secondary weights raster where I randomly changed some cells to NA but couldn't reproduce this stop:
stop(crayon::red('Some weights for polygon', check_weights$poly_id[i], 'are NA. Should sum to 1 or all be NA.'))
I always got an output that was 1 or NA, even though only some of the cells within certain polygons were NA.
I did some investigating and it looks like there is a part of the function where we check to get all polygons ids that have at least one NA value for secondary weights and then we set all cells within that polygon id to NA. See:
Line 184 # List any polygons with NA values in 1 or more grid cells
na_polys <- data.frame(w_merged) %>%
dplyr::filter(is.na(weight)) %>%
dplyr::select(poly_id) %>%
dplyr::distinct() # Unique polygon ids where at least one cell is NA
# Update the weight to equal w_area for all grid cells in na_polys
w_merged <- w_merged %>%
dplyr::mutate(weight = ifelse(poly_id %in% c(na_polys$poly_id, zero_polys$poly_id), NA, weight)) %>%
data.table::as.data.table()
Despite the comment over the last section, this is actually just setting every grid cell within the polygon to NA. And then when you calculate weights for those polygons you get NA, because now every cell within that polygon is NA.
Is this what we are intending to do? If secondary weights are missing for even one cell within a polygon we want to return NA for the summarized output? If it is then the last check is pointless because it isn't possible to have summarized values that are not 1 or NA, so we can remove that. Just wanted to confirm since I'm not sure when we made that change.
I was trying to write tests that would break this to get the expected error for each of the "stops" in the function. I used a secondary weights raster where I randomly changed some cells to NA but couldn't reproduce this stop:
I always got an output that was 1 or NA, even though only some of the cells within certain polygons were NA.
I did some investigating and it looks like there is a part of the function where we check to get all polygons ids that have at least one NA value for secondary weights and then we set all cells within that polygon id to NA. See:
Despite the comment over the last section, this is actually just setting every grid cell within the polygon to NA. And then when you calculate weights for those polygons you get NA, because now every cell within that polygon is NA.
Is this what we are intending to do? If secondary weights are missing for even one cell within a polygon we want to return NA for the summarized output? If it is then the last check is pointless because it isn't possible to have summarized values that are not 1 or NA, so we can remove that. Just wanted to confirm since I'm not sure when we made that change.