Skip to content

Commit

Permalink
dict wrapped as collections::dict()
Browse files Browse the repository at this point in the history
  • Loading branch information
24sharkS committed Aug 9, 2020
1 parent 3c0634b commit fbdc872
Show file tree
Hide file tree
Showing 7 changed files with 697 additions and 326 deletions.
71 changes: 38 additions & 33 deletions Rtests/test_code_generator_libcpp.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,70 +112,75 @@ test_that("Testing LibCppTest Processes",{

# process 12
out <- t$process12(1,2.0)
expect_identical(out,list("1"=2.0))
expect_identical(out$get(1L),2)
#expect_identical(out,list("1"=2.0))

# process 13
out <- t$process13(EEE$new()$A,2)
print(out)
p13 <- list(2)
names(p13) <- EEE$new()$A
print(p13)
print(unlist(out) == unlist(p13))
expect_equivalent(out,p13)
out$as_list()
expect_equivalent(unlist(out$keys()),EEE$new()$A)
expect_equivalent(unlist(out$values()),2L)

# process 14
out <- t$process14(EEE$new()$A,3)
expect_equivalent(out,list("3" = EEE$new()$A))
expect_true(out$has(3L))
expect_equivalent(out$get(3L),EEE$new()$A)

# process 15
out <- t$process15(12)
k <- as.integer(names(out));v <- unname(out)[[1]]
expect_equivalent(k,12L)
expect_true(out$has(12L))
v <- out$get(12L)
expect_equivalent(v$gett(),12L)

# process 16
expect_equivalent(t$process16(list("42"=2.0,"12"=1.0)),2.0)
expect_equivalent(t$process16(collections::dict(c(2.0,1.0),c(42L,12L))),2.0)

# process 17
p17 <- list(2.0,1.0)
names(p17) <- c(EEE$new()$A,EEE$new()$B)
p17 <- collections::dict(c(2.0,1.0),c(EEE$new()$A,EEE$new()$B))
expect_equivalent(t$process17(p17),2.0)

# process 18
expect_equivalent(t$process18(list("23"=t,"12"=t2)),t$gett())
p18 <- collections::dict(c(t,t2),c(23L,12L))
expect_equivalent(t$process18(p18),t$gett())

# process 19
dd <- list()
dd <- collections::dict()
t$process19(dd)
expect_equal(length(dd),1)
expect_equal(as.integer(names(dd)),23)
expect_equivalent(dd[[1]]$gett(),12L)
expect_equal(dd$size(),1)
expect_equal(dd$keys(),list(23L))
expect_equivalent(dd$values()[[1]]$gett(),12L)

# process 20
dd <- list()
dd <- collections::dict()
t$process20(dd)
expect_equal(as.integer(names(dd)),23)
expect_equivalent(dd[[1]],42.0)
expect_equal(dd$keys(),list(23L))
expect_equivalent(dd$get(23L),42.0)

# process 21
d1 <- list()
t$process21(d1,list("42"=11))
expect_equivalent(d1[["1"]],11L)
d1 <- collections::dict()
t$process21(d1,collections::dict(11L,42L))
expect_true(d1$has(1L))
expect_equivalent(d1$get(1L),11L)

# process 211
d1 <- list()
t$process211(d1,list("42"=list(11,6)))
expect_equivalent(d1[["1"]],11L)
d1 <- collections::dict()
list("42"=list(11,6))
t$process211(d1,collections::dict(list(list(11L,6L)),list("42")))
expect_equivalent(d1$get(1L),11L)

# process 212
d2 <- list()
t$process212(d2,list("42"=list(list(11,6),list(2),list(8))))
expect_equivalent(d1[["1"]],11L)
d2 <- collections::dict()
v <- list(list(list(11,6),list(2),list(8)))
k <- list("42")
t$process212(d2,collections::dict(v,k))
expect_equivalent(d2$get(1L),11L)

# process 214
d3 <- list()
t$process214(d3,list("42"=list(list(11,6),list(2,8))))
expect_equivalent(d1[["1"]],11L)
d3 <- collections::dict()
v <- list(list(list(11,6),list(2,8)))
k <- list("42")
t$process214(d3,collections::dict(v,k))
expect_equivalent(d3$get(1L),11L)

d1 <- list(42L)
d2 <- list()
Expand Down
147 changes: 71 additions & 76 deletions Rtests/test_files/py_libcpp_test.R

Large diffs are not rendered by default.

190 changes: 190 additions & 0 deletions Rtests/test_files/pyopenms_AAIndex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
library(reticulate)
library(R6)
library(purrr)
listDepth <- plotrix::listDepth
check.numeric <- varhandle::check.numeric
Pymod <- reticulate::import("pyopenms")
reticulate::py_run_string("import gc")
copy <- reticulate::import("copy")
py_builtin <- reticulate::import_builtins(convert = F)

# R6 class object conversion to python class object.
`r_to_py.R6` <- function(i,...){
tryCatch({
i$.__enclos_env__$private$py_obj
}, error = function(e) { "conversion not supported for this class"}
)
}

# python function to convert a python dict having byte type key to R named list with names as string.
py_run_string(paste("def transform_dict(d):"," return dict(zip([k.decode('utf-8') for k in d.keys()], d.values()))",sep = "\n"))

# Returns the name of wrapper R6 class
class_to_wrap <- function(py_ob){
strsplit(class(py_ob)[1],"\\.")[[1]][2]
}

# R implementation of _AAIndex
AAIndex <- R6Class(classname = "AAIndex",cloneable = FALSE,

private = list(py_obj = NA),


public = list(

# C++ signature: double aliphatic(char aa)
aliphatic = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$aliphatic(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double acidic(char aa)
acidic = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$acidic(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double basic(char aa)
basic = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$basic(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double polar(char aa)
polar = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$polar(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double getKHAG800101(char aa)
getKHAG800101 = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$getKHAG800101(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double getVASM830103(char aa)
getVASM830103 = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$getVASM830103(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double getNADH010106(char aa)
getNADH010106 = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$getNADH010106(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double getNADH010107(char aa)
getNADH010107 = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$getNADH010107(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double getWILM950102(char aa)
getWILM950102 = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$getWILM950102(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double getROBB760107(char aa)
getROBB760107 = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$getROBB760107(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double getOOBM850104(char aa)
getOOBM850104 = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$getOOBM850104(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double getFAUJ880111(char aa)
getFAUJ880111 = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$getFAUJ880111(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double getFINA770101(char aa)
getFINA770101 = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$getFINA770101(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
},

# C++ signature: double getARGP820102(char aa)
getARGP820102 = function(aa){

if(!(is_scalar_character(aa))){ stop("arg aa wrong type") }
py_run_string("aa = bytes(aa)")
py_ans = private$py_obj$getARGP820102(py$aa)
py_run_string("del aa")
r_ans = py_ans
return(r_ans)
}

)
)
Loading

0 comments on commit fbdc872

Please sign in to comment.