File: SpatialImage-class.Rd

package info (click to toggle)
r-bioc-spatialexperiment 1.16.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,648 kB
  • sloc: makefile: 2
file content (170 lines) | stat: -rw-r--r-- 6,264 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/SpatialImage.R
\name{SpatialImage-class}
\alias{SpatialImage-class}
\alias{SpatialImage}
\alias{VirtualSpatialImage-class}
\alias{LoadedSpatialImage-class}
\alias{StoredSpatialImage-class}
\alias{RemoteSpatialImage-class}
\alias{dim,VirtualSpatialImage-method}
\alias{dim,StoredSpatialImage-method}
\alias{imgRaster}
\alias{imgRaster,LoadedSpatialImage-method}
\alias{imgRaster,StoredSpatialImage-method}
\alias{imgRaster,RemoteSpatialImage-method}
\alias{imgRaster<-}
\alias{imgRaster<-,LoadedSpatialImage-method}
\alias{imgSource}
\alias{imgSource,LoadedSpatialImage-method}
\alias{imgSource,StoredSpatialImage-method}
\alias{imgSource,RemoteSpatialImage-method}
\alias{imgSource<-}
\alias{imgSource<-,StoredSpatialImage,character-method}
\alias{imgSource<-,RemoteSpatialImage,character-method}
\alias{coerce,VirtualSpatialImage,LoadedSpatialImage-method}
\alias{coerce,RemoteSpatialImage,StoredSpatialImage-method}
\alias{rotateImg}
\alias{rotateImg,VirtualSpatialImage-method}
\alias{rotateImg,LoadedSpatialImage-method}
\alias{mirrorImg}
\alias{mirrorImg,VirtualSpatialImage-method}
\alias{mirrorImg,LoadedSpatialImage-method}
\title{The \code{SpatialImage} class}
\description{
The \code{SpatialImage} class hierarchy provides representations of images
from a variety of sources. It is used by the \linkS4class{SpatialExperiment}
class to manage the loading of images across multiple studies.
}
\section{Constructor}{

\code{SpatialImage(x, is.url)} will return a \code{SpatialImage} object.
The class of the object depends on the type of \code{x}:
\itemize{
\item If \code{x} is a raster object, a \code{LoadedSpatialImage} is 
  returned. This represents an image that is fully realized into memory, 
  where the raster representation is stored inside the output object.
\item If \code{x} is a string and \code{is.url=TRUE} or it starts with 
  \code{"http://"}, \code{"http://"} or \code{"ftp://"}, 
  a \code{RemoteSpatialImage} is returned. This represents an image 
  that is remotely hosted and retrieved only on request.
\item If \code{x} is a string and \code{is.url=TRUE} or it does not 
  start with a URL-like prefix, a \code{StoredSpatialImage} is returned.
  This represents an image that is stored in a local file 
  and is loaded into memory only on request.
}
}

\section{Getting the raster image}{

For a \code{SpatialImage} object \code{x}, \code{imgRaster(x, ...)} 
will return a raster object (see \code{?\link{as.raster}}).
This is effectively a matrix of RGB colors for each pixel in the image.

For a \code{StoredSpatialImage} object \code{x}, additional arguments 
in \code{...} are passed to \code{\link{image_read}}.
This controls how the image is read into memory.

For a \code{RemoteSpatialImage} object \code{x}, the image file is first
downloaded before the raster is returned. Here, \code{...} may contain an
extra \code{cache} argument, which should be a \code{BiocFileCache} object 
(from the \pkg{BiocFileCache} package) specifying the file cache location. 
The default location is determined by 
\code{options("SpatialExperiment.remote.cache.path")},
otherwise it defaults to a subdirectory in the R temporary directory.
Any further named arguments in \code{...} are passed to \code{image_read}.

\code{as.raster(x, ...)} is the same as \code{imgRaster(x, ...)}.
}

\section{In-memory caching}{

For \code{StoredSpatialImage} and \code{RemoteSpatialImage} objects, 
loading the image with \code{imgRaster} will automatically 
store the loaded raster object in an in-memory cache.
Any subsequent \code{imgRaster} call will retrieve the raster 
from the cache, avoiding costly retrieval from the file system.

The cache policy is to evict the least recently used images when 
a new image would be added that exceeds the maximum cache size.
If the new image by itself exceeds the maximum cache size, all images are 
evicted from the cache to trigger garbage collection and free up memory.

By default, the maximum size of the cache is 4 GB. This can be 
modified by setting \code{options("SpatialExperiment.cache.size")} 
to some number of bytes, e.g., \code{2^32}.
}

\section{Transformations}{

Two basic image transformations are currently 
supported for any \code{SpatialImage} \code{x}, namely,
\code{rotateImg(x, degrees)} for clockwise (\code{degrees > 0}) and 
counter-clockwise (\code{degrees < 0}) rotation, and 
\code{mirrorImg(x, axis)} for horizontal (\code{axis = "h"}) and 
vertical (\code{axis = "v"}) mirroring.

Note that, both \code{rotateImg()} and \code{mirrorImg()} operate
on the \code{raster} matrix of the input \code{SpatialImage}. 
Thus, any \code{SpatialImage} will automatically be coerced 
into a \code{LoadedSpatialImage} upon rotation/mirroring.
}

\section{Other methods}{

\code{dim(x)} will return an integer vector of length 2, 
containing the width and height of the image in pixels.
Note that this calls \code{imgRaster} under the hood and thus 
may interact with the file and memory caches as described above.

For any \code{SpatialImage x}, \code{as(x, "LoadedSpatialImage")} will
create a \code{LoadedSpatialImage} containing an in-memory raster object.

For a \code{RemoteSpatialImage x}, \code{as(x, "StoredSpatialImage")} will
create a \code{StoredSpatialImage} pointing to the file cache location.
}

\examples{
path <- system.file(
  "extdata", "10xVisium", "section1", "outs", "spatial", 
  "tissue_lowres_image.png", package="SpatialExperiment")

spi <- SpatialImage(path)
plot(imgRaster(spi))

# the following operations all use the cache 
# so there is no need to reload the image
nrow(spi)
ncol(spi)
plot(as.raster(spi)) 

# coercing to an explicitly in-memory raster
spi <- as(spi, "LoadedSpatialImage")
plot(as.raster(spi))

###################
# transformations #
###################

# (counter-)clockwise rotation
spi1 <- rotateImg(spi, degrees = +90)
spi2 <- rotateImg(spi, degrees = -90)

par(mfrow = c(1, 3))
plot(as.raster(spi))
plot(as.raster(spi1))
plot(as.raster(spi2))

# horizontal/vertical mirroring
spi1 <- mirrorImg(spi, axis = "h")
spi2 <- mirrorImg(spi, axis = "v")

par(mfrow = c(1, 3))
plot(as.raster(spi))
plot(as.raster(spi1))
plot(as.raster(spi2))

}
\author{
Aaron Lun
}