Git Product home page Git Product logo

Comments (9)

enazario11 avatar enazario11 commented on September 26, 2024 2

Hi Ian,

I went back and used the last few lines of your code to calculate my 2-D gradient, and now it works perfectly! I needed to keep the simulated tracks off land and within the domain of my study area, so I had to develop a custom gradient file. Thank you so much for the help and information!

Emily

from animotum.

enazario11 avatar enazario11 commented on September 26, 2024

Hi again,

A bit more information is provided here. At first, this error was likely caused by my SSM locations lying outside the extent of my gradient file. I cropped the SSM locations according to the extent of my gradient document to ensure they were limited to my study area. I also changed the CRS of my gradient file to match the CRS of the predicted SSM locations. Lastly, I am using aniMotum v1.2.

See a quarto document here where I have outlined each of my steps leading up to the error.

Thank you,
Emily

from animotum.

ingomiller avatar ingomiller commented on September 26, 2024

Hi,

I do get the same error using the package provided grad file.
The weird thing is that everything worked perfectly fine about 2-3 weeks ago. I just wanted to re-run everything now with some additional locations for one of my PTT's. And now this error occurs and I can't figure out what the issue is. Running sim_fit() without the grad = argument works, but it would be favourable avoid land before using sim_filter().

I'm working on MacOS Ventura; RStudio 'Ocean Storm', R 4.3.2
I tried using the newest Github version (remotes::install_github("ianjonsen/aniMotum")) and also the staged version (remotes::install_github("ianjonsen/aniMotum@staging", dependencies = TRUE)), both producing the same error.

Any help/advise would be much appreciated.

Cheers,
Ingo

from animotum.

ianjonsen avatar ianjonsen commented on September 26, 2024

@enazario11 I've had a quick glance at your quarto doc and it looks like you're not calculating the gradients correctly. Below is the code I used to create the gradient rasters provided with aniMotum. Note that as of v 1.2 the raster object has to be a SpatRaster (terra pkg), not a RasterLayer, etc (raster pkg).

Also, you should not transform the spatial coordinate system of your gradient rasters as this will mess up the gradients! Apply any required spatial projection before calculating the gradients as below.

## use World Mercator projection
proj <- "+proj=merc +units=km +datum=WGS84"

## create world land raster
wm <- rnaturalearth::ne_countries(scale = 10, returnclass = "sf")

y <- terra::rast(crs = sf::st_crs(wm)$wkt,
          vals = 1,
          resolution = c(0.5, 0.5),
          xmin = -180,
          xmax = 180,
          ymin = -86,
          ymax = 84)

x <- terra::rasterize(wm, y, fun = "min")
x <- terra::project(x, proj)

## generate gradient rasters
# set land to NA, water to 1 - if goal is to keep tracks off land, otherwise do the reverse
x[is.na(x)] <- -1
x[x == 1] <- NA
x[x == -1] <- 1

## calculate gradient rasters
dist <- terra::distance(x)
x1 <- terra::terrain(dist, v = "slope", unit = "radians")
y1 <- terra::terrain(dist, v = "aspect", unit = "radians")
grad.x <- -1 * x1 * cos(0.5 * pi - y1)
grad.y <- -1 * x1 * sin(0.5 * pi - y1)
grad <- c(grad.x, grad.y)

Cheers, Ian

from animotum.

ianjonsen avatar ianjonsen commented on September 26, 2024

@ingomiller you'll have to provide a reproducible example of your error & include what version of the terra pkg you have installed. The following using aniMotum 1.2 and terra 1.7-55 works fine for me:

fit <- fit_ssm(ellie, vmax = 4, model = "crw", time.step = 24)

load(system.file("extdata/grad.rda", package = "aniMotum"))
grad <- terra::unwrap(grad)

sfit <- sim_fit(fit, "p", reps = 5, grad = grad, beta = c(-250,-250))

## confirm track simulation works as expected
plot(sift)

Rplot

from animotum.

enazario11 avatar enazario11 commented on September 26, 2024

Thank you so much for the help, @ianjonsen ! I went back and transformed the CRS of the distance gradient inputs before calculating and checked to make sure the final object is now a SpatRaster. Below is the information for my distance gradient calculation:

dist_grad <- distance(ROMS_large_rast, grad_spatVect, unit = "km", rasterize = TRUE)

#ROMS_large_rast details
> ROMS_large_rast
class       : SpatRaster 
dimensions  : 139, 126, 1  (nrow, ncol, nlyr)
resolution  : 31.74849, 31.74849  (x, y)
extent      : -16252.65, -12252.34, 2722.523, 7135.563  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=km +no_defs

#grad_spatVect details
> grad_spatVect
 class       : SpatVector 
 geometry    : polygons 
 dimensions  : 1, 0  (geometries, attributes)
 extent      : -14916.81, -12550.36, 3482.189, 6075.085  (xmin, xmax, ymin, ymax)
 coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=km +no_defs 

I then duplicated the dist_grad output and added it as an additional layer so the resulting spatRaster has two duplicate layers:

class       : SpatRaster 
dimensions  : 139, 126, 2  (nrow, ncol, nlyr)
resolution  : 31.74849, 31.74849  (x, y)
extent      : -16252.65, -12252.34, 2722.523, 7135.563  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=km +no_defs 
source(s)   : memory
names       :   layer,   layer 
min values  :       0,       0 
max values  : 3098490, 3098490 

and tried supplying that for the grad argument. The same error appeared:

Error in if (mu[2] < y_rng[1]) { : missing value where TRUE/FALSE needed

Any help or thoughts are really appreciated!

Thank you,
Emily

from animotum.

ianjonsen avatar ianjonsen commented on September 26, 2024

you're getting that error because your dist_grad object isn't a 2-D gradient, it's just 2 copies of distance (or squared distance) from coastline over water. First of all, you need to calculate distance from coast over land but there is more... If you look at the script in my previous comment you'll see how the gradients for the x and y directions are calculated (last 6 lines of the script). You'll see that calculating the distance from coastline is only the first step. Your SpatRaster won't work in the sim_fit function because it doesn't contain the required gradient information.

As you are just trying to constrain your simulated tracks to stay off land, you should be able to use the built-in gradient object:

load(system.file("extdata/grad.rda", package = "aniMotum"))
grad <- terra::unwrap(grad)

terra::plot(grad)

The gradients look like this, which is very different from yours!

Rplot01

from animotum.

ingomiller avatar ingomiller commented on September 26, 2024

@ingomiller you'll have to provide a reproducible example of your error & include what version of the terra pkg you have installed. The following using aniMotum 1.2 and terra 1.7-55 works fine for me:

Hi Ian,

I've updated the terra package, as well as TMB and Matrix to assure that everything is up to date. The error still persists though.
I include a minimum working example.

I found that cutting down the number of reps to 10 (instead of 100) will work without error. When using the package track "Ellie", 100 reps work fine.

And using the same data and workflow, the simulations worked fine and as expected with even 200 reps.

Thanks!
Ingo
MWE.zip

from animotum.

ianjonsen avatar ianjonsen commented on September 26, 2024

Thanks for the ex code. There was an issue with the sim_fit code when tracks cross +/- 180 longitude. Some portion of these locations, after wrapping, can end up slightly beyond the bounds of the gradient raster and this causes the error you reported. I've pushed a fix to the master branch so this should no longer occur. Your code now runs without error:

sim2 <- aniMotum::sim_fit(fit.crw, what = "predicted", reps = 100, grad = grad)
plot(sim2)

Rplot01

from animotum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.