Using 4D array inside custom update

56 views
Skip to first unread message

baug...@vt.edu

unread,
Apr 16, 2022, 1:41:29 PM4/16/22
to nimble-users
Hello,

I'm having trouble using a 4D array inside a custom update. I've had problems with array sizes before, getting the error "cannot do math with arrays that have more than 2 dimensions", but I've been able to subset them and then use them in those cases. But now I simply want to pull 3 dimensions of a 4D data structure out of "model" and use it in a custom update. I get the "cannot do math" error when 1) trying to pull out the 3 dimensions directly from "model" or 2) pulling out all 4 dimensions and then subsetting inside the custom update.

Here is the problem code inside the custom update:

z <- model$z[t,1:M.both]  #2D
y.full <- model$y.full[t,1:M.both,1:J] #3D
y.event <- model$y.event[t,1:M.both,1:J,1:3] #4D

nimble will pull out z and y.full just fine, but gives the error for y.event, because it is 4D. I'm not sure why the error messages references "more than 2D" because y.full is 3D and it works fine.

Is there any way for me to use y.event inside the custom update?

Thanks,

Ben Augustine

baug...@vt.edu

unread,
Apr 16, 2022, 8:06:40 PM4/16/22
to nimble-users
Well, I came up with a solution, but I'm not sure if it's the best solution.

1. make a list of the nodes in the correct order to do the following steps and supply it to the custom update

2.use values() to extract the nodes you want

  y.event.vec=values(model,y.event.nodes)

3. this will pull the values as a vector. Convert it to an array.

    y.event=array(y.event.vec,dim=c(M.both,J,3))

4. do custom update stuff, the use values() again to feed the updated data back to the model object.

values(model,y.event.nodes) <<- y.event

Perry de Valpine

unread,
Apr 18, 2022, 12:08:18 PM4/18/22
to baug...@vt.edu, nimble-users
Hi Ben,

Thanks for the clear explanation.  First thing is please try updating nimble if you haven't done so recently.  There was a bug fix for 0.12-2 that might fix this case.

If that doesn't work, I'd suggest doing it like this:

y.event <- nimArray(dim = c(M.both, J, 3), init=FALSE)
for(i in 1:3) {
 y.event[1:M.both, 1:J, i] <- model$y.event[t,1:M.both,1:J, i] # "chipping" or subsetting down to 2D from the original 3D should allow the assignment to work.
}

Then do your work with y.event and use a similar for-loop to put values back into the model if needed.

Alternatively you can work with values directly out of the model in 2D subsets (i.e. not even making a copy) if needed.

Here is what I think is confusing about the messages.  nimble can do element-wise math with anything up to 2D, including 2D chips of higher-dimensional objects.  Assignment is included in "doing math".  So the y.full line of your first message worked because you have a 2D subset of a 3D object, so it could do the assignment.  Now, actually *assignment* for higher dimensional objects is the one kind of operation that is, or was supposed to be, supported.  However there was a recent bug where in some situations it wouldn't work, and that was fixed.  I think that fix will cover your case, but from only looking at it briefly I am not sure and could be wrong (please let me know!).  So a solution is to access the higher dimension object using indexing to get down to 2 or fewer dimensions at a time, including for copying.  I hope that helps.

-Perry

--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/e994bdd4-465b-435a-b618-5204822c1b25n%40googlegroups.com.

baug...@vt.edu

unread,
Apr 19, 2022, 10:32:01 AM4/19/22
to nimble-users
Much appreciated Perry. I am using version 0.12-1. I did look at the package news and didn't see anything that was apparently related, but I will update and see if the behavior is different. I'm pretty sure I tried exactly what you suggest here and it gave the error. I'm certain I tried subsetting in this manner. I think I tried exactly this. I've used this subsetting approach in the past to convert a 3D object to a 2D object to then use, but it didn't work for 4D to 3D. I'll report back if this doesn't work in the newest version of nimble.

Ben

baug...@vt.edu

unread,
Apr 25, 2022, 1:05:16 PM4/25/22
to nimble-users
Just replying to say your solution worked even in the older version of nimble. Apparently I wasn't doing exactly what you said!  Thanks, Perry.

Perry de Valpine

unread,
Apr 25, 2022, 2:26:56 PM4/25/22
to baug...@vt.edu, nimble-users
Glad to hear it. Thanks Ben!

Reply all
Reply to author
Forward
0 new messages