Skip to content

Commit

Permalink
Add marshalling for 'h2o' [#5]
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Sep 30, 2023
1 parent 4b953eb commit 70b6cd8
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
Package: marshal
Version: 0.0.0-9033
Title: Framework to Marshal Objects to be Used in Another R Process
Depends:
R (>= 3.2.0)
Suggests:
bundle,
caret,
data.table,
DBI,
RSQLite,
digest,
h2o,
inline,
keras,
tensorflow,
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
S3method(marshal,"magick-image")
S3method(marshal,CFunc)
S3method(marshal,DBIConnection)
S3method(marshal,H2OAutoML)
S3method(marshal,H2OBinomialModel)
S3method(marshal,H2OMultinomialModel)
S3method(marshal,H2ORegressionModel)
S3method(marshal,RasterLayer)
S3method(marshal,SOCK0node)
S3method(marshal,SOCKcluster)
Expand Down Expand Up @@ -30,6 +34,10 @@ S3method(marshal,xml_nodeset)
S3method(marshallable,"magick-image")
S3method(marshallable,CFunc)
S3method(marshallable,DBIConnection)
S3method(marshallable,H2OAutoML)
S3method(marshallable,H2OBinomialModel)
S3method(marshallable,H2OMultinomialModel)
S3method(marshallable,H2ORegressionModel)
S3method(marshallable,RasterLayer)
S3method(marshallable,SOCK0node)
S3method(marshallable,SOCKcluster)
Expand Down
118 changes: 118 additions & 0 deletions R/marshal.h2o.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#' Marshalling of 'h2o' objects
#'
#' @param x
#' An "h2o" object.
#'
#' @param \dots Not used.
#'
#' @return
#' A `marshalled` object as described in [marshal()].
#'
#' @details
#' [h2o::h2o.save_mojo()] and [h2o::h2o.saveModel()] are used to produce
#' a marshalled version of the original object.
#' [h2o::h2o.import_mojo()] and [h2o::h2o.loadModel()] are used to
#' reconstruct a version of the original object from the marshalled object.
#'
#' @rdname marshal.h2o
#' @aliases marshal.H2OAutoML
#' @export
marshal.H2OAutoML <- function(x, ...) {
marshal_h2o(x, ...)
}

#' @rdname marshal.h2o
#' @aliases marshal.H2OMultinomialModel
#' @export
marshal.H2OMultinomialModel <- function(x, ...) {
marshal_h2o(x, ...)
}

#' @rdname marshal.h2o
#' @aliases marshal.H2OBinomialModel
#' @export
marshal.H2OBinomialModel <- function(x, ...) {
marshal_h2o(x, ...)
}

#' @rdname marshal.h2o
#' @aliases marshal.H2ORegressionModel
#' @export
marshal.H2ORegressionModel <- function(x, ...) {
marshal_h2o(x, ...)
}


marshal_h2o <- function(x, ...) {
td <- tempdir()

have_mojo <- x@have_mojo
if (have_mojo) {
tf <- h2o::h2o.save_mojo(x, path = td)
} else {
tf <- h2o::h2o.saveModel(x, path = td)
}
on.exit(file.remove(tf))
raw <- readBin(tf, what = raw(), n = file.size(tf), endian = "little")

res <- list(
marshalled = raw,
have_mojo = have_mojo
)
class(res) <- marshal_class(x)

## IMPORTANT: We don't want any of the input arguments
## to be part of the unmarshal() environment
rm(list = c("x", names(list(...))))

res[["unmarshal"]] <- unmarshal_h2o
assert_no_references(res)
res
}


unmarshal_h2o <- function(x, ...) {
object <- x[["marshalled"]]

tf <- tempdir()
on.exit(file.remove(tf))
writeBin(object, con = tf, endian = "little")

if (x[["have_mojo"]]) {
res <- h2o::h2o.import_mojo(tf)
} else {
res <- h2o::h2o.loadModel(tf)
}

stopifnot(all.equal(class(res), marshal_unclass(x), check.attributes = FALSE))
res
}


#' @rdname marshal.h2o
#' @aliases marshallable.H2OAutoML
#' @export
marshallable.H2OAutoML <- function(...) {
TRUE
}

#' @rdname marshal.h2o
#' @aliases marshallable.H2OMultinomialModel
#' @export
marshallable.H2OMultinomialModel <- function(...) {
TRUE
}

#' @rdname marshal.h2o
#' @aliases marshallable.H2OBinomialModel
#' @export
marshallable.H2OBinomialModel <- function(...) {
TRUE
}

#' @rdname marshal.h2o
#' @aliases marshallable.H2ORegressionModel
#' @export
marshallable.H2ORegressionModel <- function(...) {
TRUE
}
46 changes: 46 additions & 0 deletions man/marshal.h2o.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 70b6cd8

Please sign in to comment.