PredictiveEcology / reproducible

A set of tools for R that enhance reproducibility beyond package management
https://reproducible.predictiveecology.org/
41 stars 14 forks source link

Cache false positive in LandR::prepRawBiomassMap #396

Open ianmseddy opened 3 months ago

ianmseddy commented 3 months ago

It's unclear to me what is the source of this error, and I can't reliably reproduce it. But I have done so in two different R sessions. Essentially, the LandR::prepRawBiomassMap function is retrieving the incorrect SpatRaster under conditions where the studyArea and rasterToMatch have the same CRS but different extent. I do not think it is related to the if statements, as I tried removing them to no avail. Below is a reprex (note you must run the function at least three times)

packageVersion("LandR") #1.1.2
packageVersion("reproducible") #2.1.0 
Require::Require("PredictiveEcology/reproducible@development (>= 2.1.1)")

targetCRS <- paste("+proj=lcc +lat_1=49 +lat_2=77 +lat_0=0 +lon_0=-95 +x_0=0 +y_0=0",
                     "+datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0")
sa1 <- terra::vect(cbind(-1209980, 7586865), crs = targetCRS)
sa2 <- terra::vect(cbind(-1219980, 7386865), crs = targetCRS)
sa1 <- LandR::randomStudyArea(center = sa1, size = 10000 * 250 * 30000, seed = 1002)
sa2 <- LandR::randomStudyArea(center = sa2, size = 10000 * 250 * 30000, seed = 1003)

rtm1 <- terra::rast(extent = terra::ext(sa1), crs = terra::crs(sa1), vals = 1) 
rtm2 <-   terra::rast(extent = terra::ext(sa2), crs = terra::crs(sa2), vals = 1) 
rtm1 <- terra::mask(rtm1, sa1)
rtm2 <- terra::mask(rtm2, sa2)

## don't project to SA if RTMs not present
test <- function(sa, rtm) {
  needRTML <- FALSE
  needRTM <- FALSE
  biomassURL <- paste0("https://ftp.maps.canada.ca/pub/nrcan_rncan/Forests_Foret/",
                       "canada-forests-attributes_attributs-forests-canada/",
                       "2011-attributes_attributs-2011/NFI_MODIS250m_2011_kNN_Structure_",
                       "Biomass_TotalLiveAboveGround_v1.tif")

  rawBiomassMap <- LandR::prepRawBiomassMap(
    url = biomassURL,
    studyAreaName = "foo",
    cacheTags = "thisIsTheSame",
    to = if (!needRTML) rtm  else sa,
    projectTo = if (!needRTML) rtm else if (!needRTM) sa else NA,
    destinationPath = tempdir())

  return(rawBiomassMap)
}

testOutput1 <- test(sa1, rtm1)
testOutput2 <- test(sa2, rtm2)
testOutput1 <- test(sa1, rtm1) #rerun it 
testOutput2 <- test(sa2, rtm2)
terra::compareGeom(testOutput1, testOutput2) #this should never be TRUE
terra::compareGeom(rtm1, rtm2)
ianmseddy commented 3 months ago

Notice the filenames are the same.

> testOutput1 <- test(sa1, rtm1) #rerun it 
No cachePath supplied and getOption('reproducible.cachePath') is inside a temporary directory;
  this will not persist across R sessions.
Object to retrieve (fn: prepInputs, 7cd6911c99db678e.tif) ... 
Loaded! Cached result from previous prepInputs call
> testOutput2 <- test(sa2, rtm2)
No cachePath supplied and getOption('reproducible.cachePath') is inside a temporary directory;
  this will not persist across R sessions.
Object to retrieve (fn: prepInputs, 3b735d25a8d34c2d.tif) ... 
Loaded! Cached result from previous prepInputs call
> terra::sources(testOutput2)
[1] "C:/Users/ieddy/AppData/Local/Temp/Rtmp0EIuZb/rawBiomassMap_foo.tif"
> terra::sources(testOutput1)
[1] "C:/Users/ieddy/AppData/Local/Temp/Rtmp0EIuZb/rawBiomassMap_foo.tif"