Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPG(...)$lcpPerimWeight contains mystery links when input patch raster has NA regions #28

Closed
ecologics opened this issue Oct 23, 2016 · 9 comments
Assignees
Labels

Comments

@ecologics
Copy link
Collaborator

ecologics commented Oct 23, 2016

The following clip from @cxfauvelle input raster can be used to demonstrate this problem.
naErrorExample.zip

res <- raster("naErrorExample.asc")
resMPG <- MPG(res, patch=res==1)
## Red areas are NA
plot(is.na(res), col=c("white", "red"))
## Superimpose the mpgPlot and see the orphaned links
plot(res$mpgPlot, add=TRUE)

As below where red areas are NA and green areas are real patches (==1).

image

@achubaty
Copy link
Owner

This is a byproduct of how the NA values are handled. @samdoctolero can you take a look at this?

@achubaty achubaty added the bug label Oct 24, 2016
@achubaty
Copy link
Owner

@ecologics I spoke to Sam about this and he should have it fixed by next week.

@ecologics
Copy link
Collaborator Author

ecologics commented Nov 16, 2016

Awesome!  I am excited about this work.  Let's try to get a manuscript out early in the new year.

Paul

@achubaty
Copy link
Owner

achubaty commented Nov 18, 2016

It seems the problem is caused by the voronoi being able to spread through NA regions but links aren't permitted, so they stop short at NA boundaries.

To me, the easiest fix (and a good default i.e., conservative behaviour) is to disallow the voronoi spreading and linking through NAs. @ecologics What is the rationale for allowing spread through NA regions?

If the user wants to do something more sophisticated with the NAs they can pre-process the raster to reclassify NAs as another value (perhaps a mean or median of the neighbouring cells).

Thoughts?

@ecologics
Copy link
Collaborator Author

ecologics commented Nov 19, 2016

I agree on all these points.  The old version of grainscape did not handle NAs at all.  Indeed it routinely crashed.  So the user had to deal with them by assigning.  So making them, essentially, undefined areas on the map -- if this is easiest -- makes the most sense.

@achubaty
Copy link
Owner

achubaty commented Dec 1, 2016

Sam pushed a fix for this yesterday (137fe47). I'll take a look later today to do some tests.

@achubaty
Copy link
Owner

achubaty commented Dec 1, 2016

@samdoctolero I get an error running your updated code:

  library(igraph)
  library(raster)

  ## simplest case
  x <- 100
  y <- 100
  m <- matrix(2L, ncol = y, nrow = y)
  m[1:95, 46:55] <- NA_integer_
  m[16:35, 24:35] <- 3L
  m[16:35, 76:90] <- 3L

  r <- raster(m)
  c <- reclassify(r, rcl = cbind(c(1, 2, 3), c(10, 5, 1)))
  p <- (c == 1)

  mpg <- MPG(cost = c, patch = p)

The above fails with:

Error in data.frame(name = uniquePatches, patchId = uniquePatches, patchArea = patchArea,  : 
  arguments imply differing number of rows: 3, 2
Called from: data.frame(name = uniquePatches, patchId = uniquePatches, patchArea = patchArea, 
    patchEdgeArea = patchEdgeArea, coreArea = patchArea - patchEdgeArea)

@ecologics @cxfauvelle please hold off updating until this is resolved

@samdoctolero
Copy link
Collaborator

samdoctolero commented Dec 2, 2016

When ignoring NA values was implemented, MPG.R needed to be updated as well (59241e2).

## Get additional patch information
uniquePatches <- voronoi[] %>% unique() %>% sort()
uniquePatches <- uniquePatches[uniquePatches > 0] ## added this

I understand that the unique sets of patches had to be extracted and sorted at the same time. Since we ignored the NA values, they got included as part of the unique patches. The first element of uniquePatches had to be removed in order to have the right number of patches.

@achubaty feel free to change that line. Basically, I'm just removing the first element of the vector which will always be zero.

@achubaty
Copy link
Owner

achubaty commented Dec 2, 2016

@samdoctolero with the change in 203cff5, this is working. I have tweaked the MPG() code a bit, and built a simple set of unit tests for this issue.

The phantom links are now gone from the test raster posted by @ecologics and @cxfauvelle:

f <- 'naErrorExample.asc'
r <- raster(f)
c <- r
p <- (r == 1)

mpg <- MPG(cost = c, patch = p)

plot(is.na(r), col = c("white", "red")) ## red areas are NA
plot(mpg, add = TRUE)                   ## superimpose the mpg plot

naerrorexample

However, note that not all patches are linked. Is this another bug? See new issue #32.

achubaty added a commit that referenced this issue Mar 2, 2017
@samdoctolero the test passes, which suggests the spatial graph is being correctly built, but the raster is incorrect or being plotted incorrectly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants