match.call()有什么作用?

时间:2019-10-04 16:03:45

标签: python r translate

我正在尝试将一些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)
    # ...
}

这是做什么的?我在the documentation for match.call()中看到

  

match.call返回一个调用,其中所有指定的参数均由其全名指定。

但是我不明白这意味着什么。在这种情况下,什么是“通话”? “参数由全名指定”是什么意思?

最终,重要的部分是存储在doseresp中的内容。这些变量将在以后使用,因此我需要了解它们的值,以便可以在Python中做类似的事情(可能使用numpy,pandas和scipy)。

1 个答案:

答案 0 :(得分:1)

原义R答案为here。但是您的问题意图似乎是What is the idiomatic Python equivalent of R's match.call(), and when should I/not use it?,答案是:

  • (function) introspection with inspect.signature(f) 12:检查哪些函数参数通过位置关联与关键字/命名关联(与默认值)相匹配。在Python函数/方法签名中,func(arg_1, *args, **kwargs)相当于...中R的省略号f(args, ...)传递给未指定的args(通常从super().func()继承)。
    • Never use introspection in production code
    • R的范围定义可能与Python不同,也可能存在范围问题。通常,在Python中创建一个类(如果需要,可以创建一个自定义子类)并封装对象的数据以避免麻烦就可以了。
  • 但是为什么您认为根本需要将match.call()行移植到Python?除了进行单元测试或调试正在编写的类之外,通常不要在Python中执行此操作。如果您要移植drc::drm()供自己使用,则标准建议是实现出于自身目的所需的绝对最小接口(而不是发布质量,并且您不会为此得到报酬),并忽略所有的钟声和口哨声。您可能需要花费更长的时间来弄清楚R match.call()行的作用,而不是忽略它或将其融合成您的用例。
  • 实现重载函数原型的Python方法是将所有非必需参数默认设置为None,然后使用任何arg解析逻辑为它们提供“智能默认”值(取决于是否已传递/未传递其他args,或对象的状态)必须进入函数体内。这行得通,Python用户应该了解您生成的代码的作用。

关于您是否首先应该甚至将drc用作参考软件包,我一个月前给您的相同建议drc package has not had a CRAN release since 2016, is essentially dormant, only has one or two maintainers, no mailing-list, and isn't that well-documented。可能还有其他具有更好的代码或更好的文档的R软件包可以用作参考。我几乎不能拼写“生物测定法”,所以我建议您在相关列表/用户组(Python和R,学术界和商业界)中询问从哪个参考包开始的建议。

(很明显,如果您确实想为drm维护者贡献R文档和单元测试,以及提供Python端口,那么您可以提供。但是,如果您只想要基本的基础知识,这听起来实在太悲痛了。相当于Python。)

(这个问题的询问范围很广。我还尝试用评论来回答第二个much more specific reasking。我不知道是否可以代替此评论,请通过编辑/评论进行更新。)