[Discussion]: Handling operations that use/modify RNG

未关闭
#291 2 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
活跃
技术栈
r
领域
backend

调研方向

从 within 和 teal.code::eval_code 代码路径入手,然后跟踪 get_code(names = ...) 和代码解析器如何构造部分代码。阅读 issue 278 及其链接的 RNG 参考资料,以了解 seed 跟踪和可复现性。完成的标准应是贡献者就部分代码执行中的随机操作制定并实现一项可复现的策略。

由索引模型根据 Issue 内容生成。

描述

core discussion
What is your question?

Per discussion with @llrs-roche, here are some topics for the contributors to discuss and research.

Problem

Random/stoichastic operations are not being well handled in within/teal.code::eval_code, in particular when code parse is being used.

In the section below you can observe that q |> get_code(names = "IRIS") is not fully reproducible as the random operations before were not included, nor was the RNG state updated before code execution

Reproducible code

This code highlights the 2 main problems:

  1. set.seed() and other random operations are not kept in partial code (q |> get_code(names = "XXX"))
  2. MTCARS/IRIS random operations are influenced by preceding code
library(teal.code)
q <- qenv() |> 
  within({
    IRIS <- datasets::iris
    MTCARS <- datasets::mtcars
    set.seed(1)
    .random <- runif(1)
    IRIS$new <- runif(nrow(IRIS))
    MTCARS$new <- runif(nrow(MTCARS))
  })

q |> get_code() |> cat()
#> iris <- datasets::iris
#> mtcars <- datasets::mtcars
#> set.seed(1)
#> .random <- runif(1)
#> iris$new <- runif(nrow(iris))
#> mtcars$new <- runif(nrow(mtcars))

q |> get_code(names = "IRIS") |> cat()
#> IRIS <- datasets::iris
#> IRIS$new <- runif(nrow(IRIS))

local({
  eval(str2expression(q |> get_code(names = "IRIS")))
  q$IRIS |> rlang::hash() |> cat("# hash qenv IRIS\n")
  IRIS |> rlang::hash() |> cat("# hash reproduced IRIS\n")
})
#> 9bba33c8ed5cd90cd13316f35f812f55 # hash qenv IRIS
#> c90a89c1814206bc19fdc32d04d1eefa # hash reproduced IRIS

q |> get_code(names = "MTCARS") |> cat()
#> MTCARS <- datasets::mtcars
#> MTCARS$new <- runif(nrow(MTCARS))

local({
  eval(str2expression(q |> get_code(names = "MTCARS")))
  q$mtcars |> rlang::hash() |> cat("# hash qenv MTCARS\n")
  mtcars |> rlang::hash() |> cat("# hash reproduced MTCARS\n")
})
#> 2c0a8a99dc147d5445c3b49d035665b2 # hash qenv MTCARS
#> 6755d143ff87b73a1196c186cec7e86a # hash reproduced MTCARS
Reference material
Possible set of solutions
  • Use teal.code::eval_code() and # @linksto XXXXX to include random operations
    • Status quo
    • Cons: Requires character base code execution and requires manual maintenance
  • Detect random seed at start and inform user
  • Track random seed and incorporate it in code execution
  • When using code parser, re-run partial code to sync modules with reproducible code (while keeping set.seed() expressions)
    • cons: slow down teal app even more and data is not consistent with initial data argument in teal.
  • Always include set.seed() expression and hope it doesn't have a big impact
Code of Conduct
  • I agree to follow this project's Code of Conduct.
Contribution Guidelines
  • I agree to follow this project's Contribution Guidelines.
Security Policy
  • I agree to follow this project's Security Policy.
主要语言
R
星标
12
派生
12
平均合并
4 天 22 小时
30 天内合并 PR
3

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

pharmaverse/teal.code 的其他 Issue

查看 pharmaverse/teal.code 的全部 Issue

相似的 Issue

更多 R Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。