Skip to content

Commit 2aaa227

Browse files
committed
add parquet transformations
Former-commit-id: 39abc83 Former-commit-id: 03510b2e9dbdc61f2a69a015053e437dacee4ef3
1 parent 8794541 commit 2aaa227

11 files changed

Lines changed: 220 additions & 19 deletions

File tree

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export(ct_args)
99
export(ct_axes)
1010
export(ct_data)
1111
export(ct_name)
12+
export(ct_rotate)
13+
export(ct_scale)
14+
export(ct_translate)
1215
export(ct_type)
1316
export(data)
1417
export(feature_key)
@@ -66,6 +69,8 @@ importFrom(SummarizedExperiment,assay)
6669
importFrom(SummarizedExperiment,colData)
6770
importFrom(anndataR,read_zarr)
6871
importFrom(arrow,open_dataset)
72+
importFrom(dplyr,mutate)
73+
importFrom(dplyr,select)
6974
importFrom(grDevices,col2rgb)
7075
importFrom(grDevices,colorRampPalette)
7176
importFrom(grDevices,rainbow)

R/Zattrs.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ NULL
3131
#' @rdname Zattrs
3232
#' @export
3333
ct_data <- new_generic("ct_data", "x")
34+
method(ct_data, sdArrayFrame) <- \(x) ct_data(zattrs(x))
3435
method(ct_data, Zattrs) <- \(x) {
3536
ms <- x@data$multiscales[[1]]
3637
if (!is.null(ms)) x <- ms
@@ -40,20 +41,23 @@ method(ct_data, Zattrs) <- \(x) {
4041
#' @rdname Zattrs
4142
#' @export
4243
ct_type <- new_generic("ct_type", "x")
44+
method(ct_type, sdArrayFrame) <- \(x) ct_type(zattrs(x))
4345
method(ct_type, Zattrs) <- \(x) {
4446
vapply(ct_data(x), \(.) .$type, character(1))
4547
}
4648

4749
#' @rdname Zattrs
4850
#' @export
4951
ct_name <- new_generic("ct_name", "x")
52+
method(ct_name, sdArrayFrame) <- \(x) ct_name(zattrs(x))
5053
method(ct_name, Zattrs) <- \(x) {
5154
vapply(ct_data(x), \(.) .$output$name, character(1))
5255
}
5356

5457
#' @rdname Zattrs
5558
#' @export
5659
ct_axes <- new_generic("ct_axes", "x")
60+
method(ct_axes, sdArrayFrame) <- \(x) ct_axes(zattrs(x))
5761
method(ct_axes, Zattrs) <- \(x) {
5862
ax <- ct_data(x)[[1]]$input$axes
5963
`names<-`(
@@ -66,6 +70,7 @@ method(ct_axes, Zattrs) <- \(x) {
6670
#' @importFrom S7 class_any
6771
#' @export
6872
ct_args <- new_generic("ct_args", c("x", "i"))
73+
method(ct_args, list(sdArrayFrame, class_any)) <- \(x, i) ct_args(zattrs(x), i)
6974
method(ct_args, list(Zattrs, class_any)) <- \(x, i) {
7075
if (missing(i)) {
7176
i <- ct_name(x)[1]
@@ -91,3 +96,8 @@ method(ct_args, list(Zattrs, class_any)) <- \(x, i) {
9196
# unlist(t)
9297
# }
9398
}
99+
100+
#' @rdname Zattrs
101+
#' @importFrom S7 class_any
102+
#' @export
103+
ct_args <- new_generic("ct_args", c("x", "i"))

R/class.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#' For \code{Shape/PointFrame}s, an \code{arrow} Table or derivative.
1515
#' @param zattrs \code{Zattrs} objects; used to represent .zattrs.
1616
#' @param metadata (option) list of free-form extra data.
17+
#' @param x \code{SpatialData} object.
1718
#' @param ... ignored.
1819
#'
1920
#' @return \code{SpatialData}
@@ -254,6 +255,8 @@ method(instance_key, PointFrame) <- \(x)
254255
#' @export
255256
ShapeFrame <- new_class("ShapeFrame", parent=sdFrame)
256257

258+
sdArrayFrame <- new_union(sdArray, sdFrame)
259+
257260
#' @rdname SpatialData
258261
#' @export
259262
zattrs <- new_generic("zattrs", "x")

R/plot-point.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ sd_plot_point <- \(x, i, c, a, ...) {
2828
dot <- list(...)
2929
if (missing(c)) c <- NULL
3030
if (missing(i)) i <- 1
31-
if (is.numeric(i)) i <- names(x)$points[i]
31+
if (is.numeric(i)) i <- names(x@points)[i]
3232
p <- x@points[[i]]
3333
df <- as.data.frame(p@data)
3434
aes <- aes(.data[["x"]], .data[["y"]])

R/plot-shape.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@
2929
#' @export
3030
sd_plot_shape <- \(x, i=1, c=TRUE, ...) {
3131
s <- x@shapes[[i]]
32-
df <- read_sf_dataset(s@data)
32+
df <- if (is(s@data, "sf")) s@data else read_sf_dataset(s@data)
3333
aes <- aes()
3434
thm <- list()
3535
dot <- list(...)
3636
if (isTRUE(c)) {
3737
df$.id <- factor(seq(length(s)))
38-
aes$colour <- aes(.data$.id)[[1]]
38+
aes$colour <- aes(.data[[".id"]])[[1]]
3939
thm <- c(thm, list(guides(col="none")))
4040
} else if (.str_is_col(c)) {
4141
ex <- grepv("col", names(dot))
4242
dot <- dot[setdiff(names(dot), ex)]
4343
dot$colour <- c
4444
}
45-
if (!is.null(df$radius))
46-
aes$size <- aes(.data$radius)[[1]]
45+
if (!is.null(df[["radius"]]))
46+
aes$size <- aes(.data[["radius"]])[[1]]
4747
dot$inherit.aes <- FALSE
4848
arg <- c(list(data=df, mapping=aes), dot)
4949
list(do.call(geom_sf, arg), thm)

R/read.R

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
#' @return \code{\link{SpatialData}} object, or one of its elements.
1717
#'
1818
#' @examples
19-
#' # package demo data
2019
#' pa <- file.path("extdata", "blobs.zarr")
2120
#' pa <- system.file(pa, package="SpatialData")
21+
#'
22+
#' # read complete store
2223
#' (sd <- readSpatialData(pa))
2324
#'
25+
#' # read single element
26+
#' readImage(file.path(pa, "images", "blobs_image"))
27+
#'
2428
#' # view data available on OSN,
2529
#' # then retrieve & ingest one
2630
#' Sys.setenv(AWS_REGION="us-east-1")
@@ -67,24 +71,18 @@ NULL
6771
md <- read_zarr_attributes(x)
6872
ps <- .get_multiscales_datasets_path(md)
6973
as <- lapply(ps, \(.) ZarrArray(file.path(x, .)))
70-
list(array=as, md=md)
74+
list(data=as, zattrs=Zattrs(md))
7175
}
7276

7377
#' @rdname readSpatialData
7478
#' @importFrom Rarr ZarrArray
7579
#' @export
76-
readImage <- \(x) {
77-
za <- .read_za(x)
78-
ImageArray(data=za$array, zattrs=Zattrs(za$md))
79-
}
80+
readImage <- \(x) do.call(ImageArray, .read_za(x))
8081

8182
#' @rdname readSpatialData
8283
#' @importFrom Rarr ZarrArray
8384
#' @export
84-
readLabel <- \(x) {
85-
za <- .read_za(x)
86-
LabelArray(data=za$array, zattrs=Zattrs(za$md))
87-
}
85+
readLabel <- \(x) do.call(LabelArray, .read_za(x))
8886

8987
#' @rdname readSpatialData
9088
#' @importFrom arrow open_dataset
@@ -142,7 +140,6 @@ readTable <- \(x) {
142140
readSpatialData <- \(x,
143141
images=TRUE, labels=TRUE, points=TRUE, shapes=TRUE, tables=TRUE)
144142
{
145-
#if (!anndataR) tables <- FALSE # will do manually below
146143
args <- as.list(environment())[.LAYERS]
147144
skip <- vapply(args, isFALSE, logical(1))
148145
sd <- lapply(.LAYERS[!skip], \(i) {
@@ -158,7 +155,6 @@ readSpatialData <- \(x,
158155
}
159156
f <- get(paste0("read", toupper(substr(i, 1, 1)), substr(i, 2, nchar(i)-1)))
160157
lapply(j, \(.) do.call(f, list(.)))
161-
})
162-
#if (!anndataR) sd$tables <- .readTables_basilisk(x)
158+
})
163159
do.call(SpatialData, sd)
164160
}

R/trans.R

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#' @name coord-trans
2+
#' @title Coordinate transformations
3+
#'
4+
#' @aliases ct_translate ct_scale ct_rotate
5+
#'
6+
#' @param x \code{SpatialData} element.
7+
#' @param t transformation data.
8+
#' @param ... ignored.
9+
#'
10+
#' @return Object of same type as input.
11+
#'
12+
#' @examples
13+
#' pa <- file.path("extdata", "blobs.zarr")
14+
#' pa <- system.file(pa, package="SpatialData")
15+
#' sd <- readSpatialData(pa)
16+
#' gg <- sd_plot()
17+
#'
18+
#' # point
19+
#'
20+
#' x <- sd@points[[1]]
21+
#' sd@points$a <- ct_translate(x, c(-10, 10))
22+
#' sd@points$b <- ct_scale(x, c(0.5, 1.2))
23+
#' sd@points$c <- ct_rotate(x, 5)
24+
#'
25+
#' gg +
26+
#' sd_plot_point(sd, 1, col=1) +
27+
#' sd_plot_point(sd, "a", col=2) +
28+
#' sd_plot_point(sd, "b", col=3) +
29+
#' sd_plot_point(sd, "c", col=4)
30+
#'
31+
#' # shape
32+
#'
33+
#' x <- sd@shapes[[2]]
34+
#' sd@shapes$a <- ct_translate(x, c(-10, 10))
35+
#' sd@shapes$b <- ct_scale(x, c(0.5, 1.2))
36+
#' sd@shapes$c <- ct_rotate(x, 5)
37+
#'
38+
#' gg +
39+
#' sd_plot_shape(sd, 2, col=1) +
40+
#' sd_plot_shape(sd, "a", col=2) +
41+
#' sd_plot_shape(sd, "b", col=3) +
42+
#' sd_plot_shape(sd, "c", col=4)
43+
NULL
44+
45+
# translate ----
46+
47+
#' @rdname coord-trans
48+
#' @export
49+
ct_translate <- new_generic("ct_translate", "x")
50+
51+
method(ct_translate, PointFrame) <- \(x, t) {
52+
y <- NULL # R CMD check
53+
x@data <- x@data |>
54+
mutate(x=x+t[1]) |>
55+
mutate(y=y+t[2])
56+
return(x)
57+
}
58+
59+
#' @importFrom sfarrow read_sf_dataset
60+
method(ct_translate, ShapeFrame) <- \(x, t) {
61+
x@data <- read_sf_dataset(x@data)
62+
x@data$geometry <- x@data$geometry+t
63+
return(x)
64+
}
65+
66+
# scale ----
67+
68+
#' @rdname coord-trans
69+
#' @export
70+
ct_scale <- new_generic("ct_scale", "x")
71+
72+
#' @importFrom dplyr mutate
73+
method(ct_scale, PointFrame) <- \(x, t) {
74+
y <- NULL # R CMD check
75+
x@data <- x@data |>
76+
mutate(x=x*t[1]) |>
77+
mutate(y=y*t[2])
78+
return(x)
79+
}
80+
81+
# NOTE: this shifts the origin if it's not (0,0);
82+
# could fix, but unclear what's expected.
83+
#' @importFrom sfarrow read_sf_dataset
84+
method(ct_scale, ShapeFrame) <- \(x, t) {
85+
x@data <- read_sf_dataset(x@data)
86+
x@data$geometry <- x@data$geometry*t(t)
87+
return(x)
88+
}
89+
90+
# rotate ----
91+
92+
#' @rdname coord-trans
93+
#' @export
94+
ct_rotate <- new_generic("ct_rotate", "x")
95+
96+
# rotation matrix to rotate points
97+
# counter-clockwise through an angle 't'
98+
.R <- \(t) matrix(c(cos(t), -sin(t), sin(t), cos(t)), 2, 2)
99+
100+
#' @importFrom dplyr mutate select
101+
method(ct_rotate, PointFrame) <- \(x, t) {
102+
y <- .y <- .x <- NULL # R CMD check
103+
R <- .R(t*pi/180)
104+
x@data <- x@data |>
105+
mutate(.x=x*R[1,1], .y=y*R[1,2]) |>
106+
mutate(x=.x+.y) |>
107+
mutate(.x=x*R[2,1], .y=y*R[2,2]) |>
108+
mutate(y=.x+.y) |>
109+
select(-.x, -.y)
110+
return(x)
111+
}
112+
113+
#' @importFrom sfarrow read_sf_dataset
114+
method(ct_rotate, ShapeFrame) <- \(x, t) {
115+
R <- .R(t*pi/180)
116+
x@data <- read_sf_dataset(x@data)
117+
x@data$geometry <- x@data$geometry * R
118+
return(x)
119+
}

man/SpatialData.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/Zattrs.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/coord-trans.Rd

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)