python - match.call() 有什么作用?
问题描述
我试图手动将一些 R 代码翻译成 Python 并遇到了这个片段:
"drm" <- function(
formula, curveid, pmodels, weights, data = NULL, subset, fct,
type = c("continuous", "binomial", "Poisson", "quantal", "event"), bcVal = NULL, bcAdd = 0,
start, na.action = na.omit, robust = "mean", logDose = NULL,
control = drmc(), lowerl = NULL, upperl = NULL, separate = FALSE,
pshifts = NULL)
{
## ... elided ...
## Storing call details
callDetail <- match.call()
## Handling the 'formula', 'curveid' and 'data' arguments
anName <- deparse(substitute(curveid)) # storing name for later use
if (length(anName) > 1) {anName <- anName[1]} # to circumvent the behaviour of 'substitute' in do.call("multdrc", ...)
if (nchar(anName) < 1) {anName <- "1"} # in case only one curve is analysed
mf <- match.call(expand.dots = FALSE)
nmf <- names(mf)
mnmf <- match(c("formula", "curveid", "data", "subset", "na.action", "weights"), nmf, 0)
mf[[1]] <- as.name("model.frame")
mf <- eval(mf[c(1,mnmf)], parent.frame()) #, globalenv())
mt <- attr(mf, "terms")
dose <- model.matrix(mt, mf)[,-c(1)] # with no intercept
resp <- model.response(mf, "numeric")
origDose <- dose
origResp <- resp # in case of transformation of the response
lenData <- length(resp)
numObs <- length(resp)
xDim <- ncol(as.matrix(dose))
varNames <- names(mf)[c(2, 1)]
varNames0 <- names(mf)
# only used once, but mf is overwritten later on
## Retrieving weights
wVec <- model.weights(mf)
if (is.null(wVec))
{
wVec <- rep(1, numObs)
}
## Finding indices for missing values
missingIndices <- attr(mf, "na.action")
if (is.null(missingIndices)) {removeMI <- function(x){x}} else {removeMI <- function(x){x[-missingIndices,]}}
## Handling "curveid" argument
assayNo <- model.extract(mf, "curveid")
if (is.null(assayNo)) # in case not supplied
{
assayNo <- rep(1, numObs)
}
uniqueNames <- unique(assayNo)
colOrder <- order(uniqueNames)
uniqueNames <- as.character(uniqueNames)
# ...
}
这是在做什么?我在文档中match.call()
看到了
match.call
返回一个调用,其中所有指定的参数都由它们的全名指定。
但我不明白这是什么意思。在这种情况下,什么是“电话”?“参数由它们的全名指定”是什么意思?
最终,重要的部分是存储在dose
和中的内容resp
。这些变量稍后会使用,所以我需要了解它们的值是什么,以便我可以在 Python 中做类似的事情(可能使用 numpy、pandas 和 scipy)。
解决方案
字面上的 R 答案在这里。但你的问题意图似乎是什么是 R's 的惯用 Python 等价物,match.call()
我什么时候应该/不应该使用它?,答案是:
- (function) introspection with
inspect.signature(f)
1,2:检查哪些函数参数与位置关联与关键字/命名关联(与默认值)匹配。在 Python 函数/方法签名中,func(arg_1, *args, **kwargs)
R 的省略号大致相当于...
传递f(args, ...)
未指定的参数(通常继承自super().func()
)。- 永远不要在生产代码中使用自省
- R 的范围界定行为与 Python 不同,也可能存在范围界定问题。一般来说,在 Python 中创建一个类(如果需要,可以自定义子类)并封装对象的数据,以避免麻烦)。
- 但是你为什么认为你需要把这
match.call()
条线移植到 Python 上呢?除了单元测试或调试您正在编写的类之外,您通常不会在 Python 中执行此操作。如果您移植drc::drm()
供自己使用,那么标准建议是实现您出于自己目的所需的绝对最低限度的接口(而不是发布质量,并且您不会为此获得报酬),并忽略所有的花里胡哨. 您可能需要更长的时间才能弄清楚 Rmatch.call()
线在做什么,而不是忽略它或为您的用例拼凑它。 - 实现重载函数原型的 Pythonic 方法是将所有非必要参数默认为
None
,然后任何 arg 解析逻辑为它们提供“智能默认”值(取决于其他 args 被传递/未传递,或者对象的state) 必须进入函数体内。这行得通,Python 用户应该了解您生成的代码的作用。
至于您是否应该首先将drc
其用作参考包,就像我一个月前给您的建议一样,drc 包自 2016 年以来就没有 CRAN 版本,基本上处于休眠状态,只有一两个维护者,没有邮件列表,并没有很好的记录。很可能还有其他 R 包具有更好的代码或更好的文档以用作参考。我几乎无法拼写“bioassay”,所以我建议您在相关列表/用户组(Python 和 R,学术和商业)上询问从哪个参考包开始的建议。
(显然,如果你真的想为drm
维护者贡献 R 文档和单元测试,以及做 Python 移植,你可以提供。但如果你只想要一个基本的 Python 等价物,这听起来太悲伤了。)
(您的问题非常广泛。我还尝试通过评论来解决您的第二个更具体的问题。我不知道这是否会取代这一点,请通过编辑/评论进行更新。)
推荐阅读
- ruby-on-rails - Gemfile 冲突版本
- django - 在 Django 网站的管理页面中创建普通用户后,我无法使用该普通用户登录
- ios - SwiftUI iOS 14 日期选择器奇怪的行为
- python - 切换名字和姓氏顺序的功能
- javascript - 如何使用 discord.js 获取另一个单词之后的单词
- c# - Ubuntu 版本 Edge 上的 Chromium Edge 驱动程序
- python - 遍历列表以在 Jinja、Python-Flask、SQLite 中创建表
- java - Dataproc Spark 作业因 IO 异常而失败,当历史服务器尝试访问 GCS 存储桶时无法初始化 Spark 上下文
- java - 在 Java 中拆分字符串的问题
- java - 如何有效地创建最小 N 大小的分区列表?