gpt4 book ai didi

python - match.call() 的作用是什么?

转载 作者:行者123 更新时间:2023-12-01 07:10:35 26 4
gpt4 key购买 nike

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

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

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

最佳答案

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) 1 , 2 :检查哪些函数参数通过位置关联与关键字/命名关联(与默认值)匹配。在 Python 函数/方法签名中,func(arg_1, *args, **kwargs) 大致相当于 f(args) 中的 R 省略号 ... , ...) 传递未指定的参数(通常继承自 super().func())。
    • Never use introspection in production code
    • R 的作用域行为与 Python 不同,也可能存在作用域问题。一般来说,在 Python 中创建一个类(自定义子类,如果需要的话)并封装对象的数据以避免麻烦)会比较省事。
  • 但是为什么您认为需要将 match.call() 行移植到 Python?除了单元测试或调试类之外你正在编写,通常不会在 Python 中这样做。如果您要移植drc::drm()供您自己使用,那么标准建议是实现您自己的目的所需的绝对最小接口(interface)(而不是发布质量,并且您不会为此获得报酬),并忽略所有花哨的东西。您可能需要更长的时间才能弄清楚 R match.call() 行正在做什么,而不是忽略它或将其混杂到您的用例中。
  • 实现重载函数原型(prototype)的 Pythonic 方法是将所有非必要参数默认为 None,然后使用任何 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,学术和商业)询问从哪个引用包开始的建议。

(显然,如果你真的想向 drm 维护者贡献 R 文档和单元测试,以及做 Python 移植,你可以提供。但是如果你只想要一个基本的 Python 等价物。)

(您提出的问题非常广泛。我还尝试通过评论来解决您的第二个 much more specific reasking 。我不知道这是否会取代这个问题,请通过编辑/评论进行更新。)

关于python - match.call() 的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58239851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com