是什么匹配呼叫()你做什么?

2024-09-24 02:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图手动将一些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 returns a call in which all of the specified arguments are specified by their full names.

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

最后,重要的部分是存储在doseresp中的内容。这些变量稍后使用,因此我需要了解它们的值是什么,这样我就可以在Python中执行类似的操作(可能是使用numpy、pandas和scipy)。你知道吗


Tags: oftheinmodelifismatchcall
1条回答
网友
1楼 · 发布于 2024-09-24 02:22:01

字面上的R答案是here。但你的问题意图似乎是What is the idiomatic Python equivalent of R's ^{}, and when should I/not use it?,答案是:

  • (function) introspection with ^{}12:检查哪些函数参数与位置关联vs关键字/命名关联(vs默认值)匹配。在Python函数/方法签名中,func(arg_1, *args, **kwargs)f(args, ...)中R的省略号...的粗略等价物,用于传递未指定的参数(通常从super().func()继承)。
    • Never use introspection in production code
    • R的作用域行为与Python不同,很可能也存在作用域问题。一般来说,在Python中创建一个类(如果需要的话,可以是一个自定义子类)并封装对象的数据,以避免麻烦。你知道吗
  • 但是为什么您认为需要将match.call()行移植到Python呢?除了在单元测试或调试您正在编写的类之外,通常不会在Python中这样做。如果你移植^{}是为了你自己的使用,那么标准的建议是实现你自己的目的所需要的绝对最小的接口(而不是发布质量,你不会为此得到报酬),并且忽略所有的钟声和口哨声。与忽略Rmatch.call()行或在用例中混淆视听相比,弄清楚Rmatch.call()行在做什么可能需要更长的时间。你知道吗
  • 实现重载函数原型的python方法是将所有非必要参数默认为None,然后任何arg解析逻辑都必须进入函数体内部,以给它们提供“智能默认”值(取决于传递了/没有传递的其他arg或对象的状态)。这是可行的,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包有更好的代码或更好的文档作为参考。我几乎拼不出“bioassay”,所以我建议您在相关列表/用户组(Python和R,学术和商业)上询问关于从哪个参考包开始的建议。你知道吗

(显然,如果您真的想将R doc和单元测试贡献给drm维护人员,以及做Python端口,您可以提供。但如果您只想要一个基本的Python等价物,那听起来太悲伤了。)

(这个问题问得很广泛。我还试着用一个评论来回答你的第二个much more specific reasking。我不知道这是否会取代这个,请通过编辑/评论更新。)

相关问题 更多 >