ArrayGrid and ArrayViewport objects
ArrayGrid-class.Rd
A grid is a partitioning of an array-like object into block-shaped regions called viewports.
The S4Arrays package defines two S4 classes to formally represent grids and viewports: the ArrayGrid and ArrayViewport classes. Note that ArrayGrid and ArrayViewport objects are used extensively in the context of block processing of array-like objects.
There are two variants of ArrayGrid objects:
RegularArrayGrid objects: for grids where all the blocks have the same geometry (except maybe for the edge blocks).
ArbitraryArrayGrid objects: for grids where blocks don't necessarily have the same geometry.
Usage
## Constructor functions:
RegularArrayGrid(refdim, spacings=refdim)
ArbitraryArrayGrid(tickmarks)
downsample(x, ratio=1L)
Arguments
- refdim
An integer vector containing the dimensions of the reference array.
- spacings
An integer vector specifying the grid spacing along each dimension.
- tickmarks
A list of integer vectors, one along each dimension of the reference array, representing the tickmarks along that dimension. Each integer vector must be sorted in ascending order. NAs or negative values are not allowed.
- x
An ArrayGrid object.
- ratio
An integer vector specifying the ratio of the downsampling along each dimension. Can be of length 1, in which case the same ratio is used along all the dimensions.
Details
RegularArrayGrid and ArbitraryArrayGrid are concrete subclasses of ArrayGrid, which itself is a virtual class.
Note that an ArrayGrid or ArrayViewport object doesn't store any array data, only the geometry of the grid or viewport. This makes these objects extremely light-weight, even for grids made of millions of blocks.
Value
For RegularArrayGrid()
, a RegularArrayGrid instance.
For ArbitraryArrayGrid()
, an ArbitraryArrayGrid instance.
For downsample()
, an ArrayGrid object on the same reference
array than x
.
See also
read_block
to read a block of data from an array-like object.blockApply
and family, in the DelayedArray package, for convenient block processing of an array-like object.mapToGrid
for mapping reference array positions to grid positions and vice-versa.
Examples
## ---------------------------------------------------------------------
## A. ArrayGrid OBJECTS
## ---------------------------------------------------------------------
## Create a regularly-spaced grid on top of a 3700 x 100 x 33 array:
grid1 <- RegularArrayGrid(c(3700, 100, 33), c(250, 100, 10))
## Dimensions of the reference array:
refdim(grid1)
#> [1] 3700 100 33
## Number of grid elements along each dimension of the reference array:
dim(grid1)
#> [1] 15 1 4
## Total number of grid elements:
length(grid1)
#> [1] 60
## First element in the grid:
grid1[[1L]] # same as grid1[[1L, 1L, 1L]]
#> 250 x 100 x 10 ArrayViewport object on a 3700 x 100 x 33 array: [1-250, ,1-10]
## Last element in the grid:
grid1[[length(grid1)]] # same as grid1[[15L, 1L, 4L]]
#> 200 x 100 x 3 ArrayViewport object on a 3700 x 100 x 33 array: [3501-3700, ,31-33]
## Dimensions of the grid elements:
dims(grid1) # one row per grid element
#> [,1] [,2] [,3]
#> [1,] 250 100 10
#> [2,] 250 100 10
#> [3,] 250 100 10
#> [4,] 250 100 10
#> [5,] 250 100 10
#> [6,] 250 100 10
#> [7,] 250 100 10
#> [8,] 250 100 10
#> [9,] 250 100 10
#> [10,] 250 100 10
#> [11,] 250 100 10
#> [12,] 250 100 10
#> [13,] 250 100 10
#> [14,] 250 100 10
#> [15,] 200 100 10
#> [16,] 250 100 10
#> [17,] 250 100 10
#> [18,] 250 100 10
#> [19,] 250 100 10
#> [20,] 250 100 10
#> [21,] 250 100 10
#> [22,] 250 100 10
#> [23,] 250 100 10
#> [24,] 250 100 10
#> [25,] 250 100 10
#> [26,] 250 100 10
#> [27,] 250 100 10
#> [28,] 250 100 10
#> [29,] 250 100 10
#> [30,] 200 100 10
#> [31,] 250 100 10
#> [32,] 250 100 10
#> [33,] 250 100 10
#> [34,] 250 100 10
#> [35,] 250 100 10
#> [36,] 250 100 10
#> [37,] 250 100 10
#> [38,] 250 100 10
#> [39,] 250 100 10
#> [40,] 250 100 10
#> [41,] 250 100 10
#> [42,] 250 100 10
#> [43,] 250 100 10
#> [44,] 250 100 10
#> [45,] 200 100 10
#> [46,] 250 100 3
#> [47,] 250 100 3
#> [48,] 250 100 3
#> [49,] 250 100 3
#> [50,] 250 100 3
#> [51,] 250 100 3
#> [52,] 250 100 3
#> [53,] 250 100 3
#> [54,] 250 100 3
#> [55,] 250 100 3
#> [56,] 250 100 3
#> [57,] 250 100 3
#> [58,] 250 100 3
#> [59,] 250 100 3
#> [60,] 200 100 3
## Lengths of the grid elements:
lengths(grid1) # same as rowProds(dims(grid1))
#> [1] 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000
#> [11] 250000 250000 250000 250000 200000 250000 250000 250000 250000 250000
#> [21] 250000 250000 250000 250000 250000 250000 250000 250000 250000 200000
#> [31] 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000
#> [41] 250000 250000 250000 250000 200000 75000 75000 75000 75000 75000
#> [51] 75000 75000 75000 75000 75000 75000 75000 75000 75000 60000
stopifnot(sum(lengths(grid1)) == prod(refdim(grid1)))
maxlength(grid1) # does not need to compute lengths(grid1)) first
#> [1] 250000
# so is more efficient than max(lengths(grid1))
stopifnot(maxlength(grid1) == max(lengths(grid1)))
## Create an arbitrary-spaced grid on top of a 15 x 9 matrix:
grid2 <- ArbitraryArrayGrid(list(c(2L, 7:10, 13L, 15L), c(5:6, 6L, 9L)))
refdim(grid2)
#> [1] 15 9
dim(grid2)
#> [1] 7 4
length(grid2)
#> [1] 28
grid2[[1L]] # same as grid2[[1L, 1L]]
#> 2 x 5 ArrayViewport object on a 15 x 9 array: [1-2,1-5]
grid2[[length(grid2)]] # same as grid2[[15L, 9L]]
#> 2 x 3 ArrayViewport object on a 15 x 9 array: [14-15,7-9]
dims(grid2)
#> [,1] [,2]
#> [1,] 2 5
#> [2,] 5 5
#> [3,] 1 5
#> [4,] 1 5
#> [5,] 1 5
#> [6,] 3 5
#> [7,] 2 5
#> [8,] 2 1
#> [9,] 5 1
#> [10,] 1 1
#> [11,] 1 1
#> [12,] 1 1
#> [13,] 3 1
#> [14,] 2 1
#> [15,] 2 0
#> [16,] 5 0
#> [17,] 1 0
#> [18,] 1 0
#> [19,] 1 0
#> [20,] 3 0
#> [21,] 2 0
#> [22,] 2 3
#> [23,] 5 3
#> [24,] 1 3
#> [25,] 1 3
#> [26,] 1 3
#> [27,] 3 3
#> [28,] 2 3
lengths(grid2)
#> [1] 10 25 5 5 5 15 10 2 5 1 1 1 3 2 0 0 0 0 0 0 0 6 15 3 3
#> [26] 3 9 6
array(lengths(grid2), dim(grid2)) # display the grid element lengths in
#> [,1] [,2] [,3] [,4]
#> [1,] 10 2 0 6
#> [2,] 25 5 0 15
#> [3,] 5 1 0 3
#> [4,] 5 1 0 3
#> [5,] 5 1 0 3
#> [6,] 15 3 0 9
#> [7,] 10 2 0 6
# an array of same shape as grid2
stopifnot(sum(lengths(grid2)) == prod(refdim(grid2)))
maxlength(grid2) # does not need to compute lengths(grid2)) first
#> [1] 25
# so is more efficient than max(lengths(grid2))
stopifnot(maxlength(grid2) == max(lengths(grid2)))
## Max (i.e. highest) resolution grid:
Hgrid <- RegularArrayGrid(6:4, c(1, 1, 1))
Hgrid
#> 6 x 5 x 4 RegularArrayGrid object on a 6 x 5 x 4 array:
#> , , 1
#>
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] [1,1,1] [1,2,1] [1,3,1] [1,4,1] [1,5,1]
#> [2,] [2,1,1] [2,2,1] [2,3,1] [2,4,1] [2,5,1]
#> [3,] [3,1,1] [3,2,1] [3,3,1] [3,4,1] [3,5,1]
#> [4,] [4,1,1] [4,2,1] [4,3,1] [4,4,1] [4,5,1]
#> [5,] [5,1,1] [5,2,1] [5,3,1] [5,4,1] [5,5,1]
#> [6,] [6,1,1] [6,2,1] [6,3,1] [6,4,1] [6,5,1]
#>
#> , , 2
#>
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] [1,1,2] [1,2,2] [1,3,2] [1,4,2] [1,5,2]
#> [2,] [2,1,2] [2,2,2] [2,3,2] [2,4,2] [2,5,2]
#> [3,] [3,1,2] [3,2,2] [3,3,2] [3,4,2] [3,5,2]
#> [4,] [4,1,2] [4,2,2] [4,3,2] [4,4,2] [4,5,2]
#> [5,] [5,1,2] [5,2,2] [5,3,2] [5,4,2] [5,5,2]
#> [6,] [6,1,2] [6,2,2] [6,3,2] [6,4,2] [6,5,2]
#>
#> , , 3
#>
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] [1,1,3] [1,2,3] [1,3,3] [1,4,3] [1,5,3]
#> [2,] [2,1,3] [2,2,3] [2,3,3] [2,4,3] [2,5,3]
#> [3,] [3,1,3] [3,2,3] [3,3,3] [3,4,3] [3,5,3]
#> [4,] [4,1,3] [4,2,3] [4,3,3] [4,4,3] [4,5,3]
#> [5,] [5,1,3] [5,2,3] [5,3,3] [5,4,3] [5,5,3]
#> [6,] [6,1,3] [6,2,3] [6,3,3] [6,4,3] [6,5,3]
#>
#> , , 4
#>
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] [1,1,4] [1,2,4] [1,3,4] [1,4,4] [1,5,4]
#> [2,] [2,1,4] [2,2,4] [2,3,4] [2,4,4] [2,5,4]
#> [3,] [3,1,4] [3,2,4] [3,3,4] [3,4,4] [3,5,4]
#> [4,] [4,1,4] [4,2,4] [4,3,4] [4,4,4] [4,5,4]
#> [5,] [5,1,4] [5,2,4] [5,3,4] [5,4,4] [5,5,4]
#> [6,] [6,1,4] [6,2,4] [6,3,4] [6,4,4] [6,5,4]
#>
dim(Hgrid) # same as refdim(Hgrid)
#> [1] 6 5 4
stopifnot(identical(dim(Hgrid), refdim(Hgrid)))
stopifnot(all(lengths(Hgrid) == 1))
## Min (i.e. lowest) resolution grid:
Lgrid <- RegularArrayGrid(6:4, 6:4)
Lgrid
#> 1 x 1 x 1 RegularArrayGrid object on a 6 x 5 x 4 array:
#> , , 1
#>
#> [,1]
#> [1,] [ , , ]
#>
stopifnot(all(dim(Lgrid) == 1))
stopifnot(identical(dim(Lgrid[[1L]]), refdim(Lgrid)))
stopifnot(identical(dims(Lgrid), matrix(refdim(Lgrid), nrow=1)))
## ---------------------------------------------------------------------
## B. ArrayViewport OBJECTS
## ---------------------------------------------------------------------
## Grid elements are ArrayViewport objects:
grid1[[1L]]
#> 250 x 100 x 10 ArrayViewport object on a 3700 x 100 x 33 array: [1-250, ,1-10]
stopifnot(is(grid1[[1L]], "ArrayViewport"))
grid1[[2L]]
#> 250 x 100 x 10 ArrayViewport object on a 3700 x 100 x 33 array: [251-500, ,1-10]
grid1[[2L, 1L, 1L]]
#> 250 x 100 x 10 ArrayViewport object on a 3700 x 100 x 33 array: [251-500, ,1-10]
grid1[[15L, 1L, 4L]]
#> 200 x 100 x 3 ArrayViewport object on a 3700 x 100 x 33 array: [3501-3700, ,31-33]
## Construction of a standalong ArrayViewport object:
m0 <- matrix(1:30, ncol=5)
block_dim <- c(4, 3)
viewport1 <- ArrayViewport(dim(m0), IRanges(c(3, 2), width=block_dim))
viewport1
#> 4 x 3 ArrayViewport object on a 6 x 5 array: [3-6,2-4]
dim(viewport1) # 'block_dim'
#> [1] 4 3
length(viewport1) # number of array elements in the viewport
#> [1] 12
ranges(viewport1)
#> IRanges object with 2 ranges and 0 metadata columns:
#> start end width
#> <integer> <integer> <integer>
#> [1] 3 6 4
#> [2] 2 4 3
## ---------------------------------------------------------------------
## C. GRIDS CAN BE TRANSPOSED
## ---------------------------------------------------------------------
tgrid2 <- t(grid2)
dim(tgrid2)
#> [1] 4 7
refdim(tgrid2)
#> [1] 9 15
## Use aperm() if the grid has more than 2 dimensions:
tgrid1 <- aperm(grid1)
dim(tgrid1)
#> [1] 4 1 15
refdim(tgrid1)
#> [1] 33 100 3700
aperm(grid1, c(3, 1, 2))
#> 4 x 15 x 1 RegularArrayGrid object on a 33 x 3700 x 100 array:
#> , , 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] [1-10,1-250, ] [1-10,251-500, ] [1-10,501-750, ] [1-10,751-1000, ]
#> [2,] [11-20,1-250, ] [11-20,251-500, ] [11-20,501-750, ] [11-20,751-1000, ]
#> [3,] [21-30,1-250, ] [21-30,251-500, ] [21-30,501-750, ] [21-30,751-1000, ]
#> [4,] [31-33,1-250, ] [31-33,251-500, ] [31-33,501-750, ] [31-33,751-1000, ]
#> [,5] [,6] [,7]
#> [1,] [1-10,1001-1250, ] [1-10,1251-1500, ] [1-10,1501-1750, ]
#> [2,] [11-20,1001-1250, ] [11-20,1251-1500, ] [11-20,1501-1750, ]
#> [3,] [21-30,1001-1250, ] [21-30,1251-1500, ] [21-30,1501-1750, ]
#> [4,] [31-33,1001-1250, ] [31-33,1251-1500, ] [31-33,1501-1750, ]
#> [,8] [,9] [,10]
#> [1,] [1-10,1751-2000, ] [1-10,2001-2250, ] [1-10,2251-2500, ]
#> [2,] [11-20,1751-2000, ] [11-20,2001-2250, ] [11-20,2251-2500, ]
#> [3,] [21-30,1751-2000, ] [21-30,2001-2250, ] [21-30,2251-2500, ]
#> [4,] [31-33,1751-2000, ] [31-33,2001-2250, ] [31-33,2251-2500, ]
#> [,11] [,12] [,13]
#> [1,] [1-10,2501-2750, ] [1-10,2751-3000, ] [1-10,3001-3250, ]
#> [2,] [11-20,2501-2750, ] [11-20,2751-3000, ] [11-20,3001-3250, ]
#> [3,] [21-30,2501-2750, ] [21-30,2751-3000, ] [21-30,3001-3250, ]
#> [4,] [31-33,2501-2750, ] [31-33,2751-3000, ] [31-33,3001-3250, ]
#> [,14] [,15]
#> [1,] [1-10,3251-3500, ] [1-10,3501-3700, ]
#> [2,] [11-20,3251-3500, ] [11-20,3501-3700, ]
#> [3,] [21-30,3251-3500, ] [21-30,3501-3700, ]
#> [4,] [31-33,3251-3500, ] [31-33,3501-3700, ]
#>
aperm(grid1, c(1, 3, 2))
#> 15 x 4 x 1 RegularArrayGrid object on a 3700 x 33 x 100 array:
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] [1-250,1-10, ] [1-250,11-20, ] [1-250,21-30, ]
#> [2,] [251-500,1-10, ] [251-500,11-20, ] [251-500,21-30, ]
#> [3,] [501-750,1-10, ] [501-750,11-20, ] [501-750,21-30, ]
#> [4,] [751-1000,1-10, ] [751-1000,11-20, ] [751-1000,21-30, ]
#> [5,] [1001-1250,1-10, ] [1001-1250,11-20, ] [1001-1250,21-30, ]
#> [6,] [1251-1500,1-10, ] [1251-1500,11-20, ] [1251-1500,21-30, ]
#> [7,] [1501-1750,1-10, ] [1501-1750,11-20, ] [1501-1750,21-30, ]
#> [8,] [1751-2000,1-10, ] [1751-2000,11-20, ] [1751-2000,21-30, ]
#> [9,] [2001-2250,1-10, ] [2001-2250,11-20, ] [2001-2250,21-30, ]
#> [10,] [2251-2500,1-10, ] [2251-2500,11-20, ] [2251-2500,21-30, ]
#> [11,] [2501-2750,1-10, ] [2501-2750,11-20, ] [2501-2750,21-30, ]
#> [12,] [2751-3000,1-10, ] [2751-3000,11-20, ] [2751-3000,21-30, ]
#> [13,] [3001-3250,1-10, ] [3001-3250,11-20, ] [3001-3250,21-30, ]
#> [14,] [3251-3500,1-10, ] [3251-3500,11-20, ] [3251-3500,21-30, ]
#> [15,] [3501-3700,1-10, ] [3501-3700,11-20, ] [3501-3700,21-30, ]
#> [,4]
#> [1,] [1-250,31-33, ]
#> [2,] [251-500,31-33, ]
#> [3,] [501-750,31-33, ]
#> [4,] [751-1000,31-33, ]
#> [5,] [1001-1250,31-33, ]
#> [6,] [1251-1500,31-33, ]
#> [7,] [1501-1750,31-33, ]
#> [8,] [1751-2000,31-33, ]
#> [9,] [2001-2250,31-33, ]
#> [10,] [2251-2500,31-33, ]
#> [11,] [2501-2750,31-33, ]
#> [12,] [2751-3000,31-33, ]
#> [13,] [3001-3250,31-33, ]
#> [14,] [3251-3500,31-33, ]
#> [15,] [3501-3700,31-33, ]
#>
aperm(grid1, c(3, 1)) # some dimensions can be dropped
#> 4 x 15 RegularArrayGrid object on a 33 x 3700 array:
#> [,1] [,2] [,3] [,4]
#> [1,] [1-10,1-250] [1-10,251-500] [1-10,501-750] [1-10,751-1000]
#> [2,] [11-20,1-250] [11-20,251-500] [11-20,501-750] [11-20,751-1000]
#> [3,] [21-30,1-250] [21-30,251-500] [21-30,501-750] [21-30,751-1000]
#> [4,] [31-33,1-250] [31-33,251-500] [31-33,501-750] [31-33,751-1000]
#> [,5] [,6] [,7] [,8]
#> [1,] [1-10,1001-1250] [1-10,1251-1500] [1-10,1501-1750] [1-10,1751-2000]
#> [2,] [11-20,1001-1250] [11-20,1251-1500] [11-20,1501-1750] [11-20,1751-2000]
#> [3,] [21-30,1001-1250] [21-30,1251-1500] [21-30,1501-1750] [21-30,1751-2000]
#> [4,] [31-33,1001-1250] [31-33,1251-1500] [31-33,1501-1750] [31-33,1751-2000]
#> [,9] [,10] [,11] [,12]
#> [1,] [1-10,2001-2250] [1-10,2251-2500] [1-10,2501-2750] [1-10,2751-3000]
#> [2,] [11-20,2001-2250] [11-20,2251-2500] [11-20,2501-2750] [11-20,2751-3000]
#> [3,] [21-30,2001-2250] [21-30,2251-2500] [21-30,2501-2750] [21-30,2751-3000]
#> [4,] [31-33,2001-2250] [31-33,2251-2500] [31-33,2501-2750] [31-33,2751-3000]
#> [,13] [,14] [,15]
#> [1,] [1-10,3001-3250] [1-10,3251-3500] [1-10,3501-3700]
#> [2,] [11-20,3001-3250] [11-20,3251-3500] [11-20,3501-3700]
#> [3,] [21-30,3001-3250] [21-30,3251-3500] [21-30,3501-3700]
#> [4,] [31-33,3001-3250] [31-33,3251-3500] [31-33,3501-3700]
aperm(grid1, c(3, 2, 3)) # and some can be repeated
#> 4 x 1 x 4 RegularArrayGrid object on a 33 x 100 x 33 array:
#> , , 1
#>
#> [,1]
#> [1,] [1-10, ,1-10]
#> [2,] [11-20, ,1-10]
#> [3,] [21-30, ,1-10]
#> [4,] [31-33, ,1-10]
#>
#> , , 2
#>
#> [,1]
#> [1,] [1-10, ,11-20]
#> [2,] [11-20, ,11-20]
#> [3,] [21-30, ,11-20]
#> [4,] [31-33, ,11-20]
#>
#> , , 3
#>
#> [,1]
#> [1,] [1-10, ,21-30]
#> [2,] [11-20, ,21-30]
#> [3,] [21-30, ,21-30]
#> [4,] [31-33, ,21-30]
#>
#> , , 4
#>
#> [,1]
#> [1,] [1-10, ,31-33]
#> [2,] [11-20, ,31-33]
#> [3,] [21-30, ,31-33]
#> [4,] [31-33, ,31-33]
#>
## ---------------------------------------------------------------------
## D. DOWNSAMPLING AN ArrayGrid OBJECT
## ---------------------------------------------------------------------
## The elements (ArrayViewport) of an ArrayGrid object can be replaced
## with bigger elements obtained by merging adjacent elements. How many
## adjacent elements to merge along each dimension is specified via the
## 'ratio' vector (one integer per dimension). We call this operation
## "downsampling. It can be seen as reducing the "resolution" of a grid
## by the specified ratio (if we think of the grid elements as pixels).
downsample(grid2, 2)
#> 4 x 2 ArbitraryArrayGrid object on a 15 x 9 array:
#> [,1] [,2]
#> [1,] [1-7,1-6] [1-7,7-9]
#> [2,] [8-9,1-6] [8-9,7-9]
#> [3,] [10-13,1-6] [10-13,7-9]
#> [4,] [14-15,1-6] [14-15,7-9]
downsample(grid2, 3)
#> 3 x 2 ArbitraryArrayGrid object on a 15 x 9 array:
#> [,1] [,2]
#> [1,] [1-8,1-6] [1-8,7-9]
#> [2,] [9-13,1-6] [9-13,7-9]
#> [3,] [14-15,1-6] [14-15,7-9]
downsample(grid2, 4)
#> 2 x 1 ArbitraryArrayGrid object on a 15 x 9 array:
#> [,1]
#> [1,] [1-9, ]
#> [2,] [10-15, ]
## Downsampling preserves the dimensions of the reference array:
stopifnot(identical(refdim(downsample(grid2, 2)), refdim(grid2)))
stopifnot(identical(refdim(downsample(grid2, 3)), refdim(grid2)))
stopifnot(identical(refdim(downsample(grid2, 4)), refdim(grid2)))
## A big enough ratio will eventually produce the coarsest possible grid
## i.e. a grid with a single grid element covering the entire reference
## array:
grid3 <- downsample(grid2, 7)
length(grid3)
#> [1] 1
grid3[[1L]]
#> 15 x 9 ArrayViewport object on a 15 x 9 array: [ , ]
stopifnot(identical(dim(grid3[[1L]]), refdim(grid3)))
## Downsampling by a ratio of 1 is a no-op:
stopifnot(identical(downsample(grid2, 1), grid2))
## Using one ratio per dimension:
downsample(grid2, c(2, 1))
#> 4 x 4 ArbitraryArrayGrid object on a 15 x 9 array:
#> [,1] [,2] [,3] [,4]
#> [1,] [1-7,1-5] [1-7,6] [1-7,7-6] [1-7,7-9]
#> [2,] [8-9,1-5] [8-9,6] [8-9,7-6] [8-9,7-9]
#> [3,] [10-13,1-5] [10-13,6] [10-13,7-6] [10-13,7-9]
#> [4,] [14-15,1-5] [14-15,6] [14-15,7-6] [14-15,7-9]
## Downsample a max resolution grid:
refdim <- c(45, 16, 20)
grid4 <- RegularArrayGrid(refdim, c(1, 1, 1))
ratio <- c(6, 1, 3)
stopifnot(identical(
downsample(grid4, ratio),
RegularArrayGrid(refdim, ratio)
))