Julia language:重定向标准输出不会影响每个println//如何从标准输出中提取值

Julia language:重定向标准输出不会影响每个println//如何从标准输出中提取值,julia,stdout,p-value,t-test,Julia,Stdout,P Value,T Test,我最初的目标是执行ttest并获得一个pvalue。 我使用了看起来很有希望的方法,但结果是这样的: julia> OneSampleTTest([1,2,3,4,5],3.1) One sample t-test ----------------- Population details: parameter of interest: Mean value under h_0: 3.1 point estimate: 3.0

我最初的目标是执行ttest并获得一个pvalue。 我使用了看起来很有希望的方法,但结果是这样的:

julia> OneSampleTTest([1,2,3,4,5],3.1)
One sample t-test
-----------------
Population details:
    parameter of interest:   Mean
    value under h_0:         3.1
    point estimate:          3.0
    95% confidence interval: (1.0368, 4.9632)

Test summary:
    outcome with 95% confidence: fail to reject h_0
    two-sided p-value:           0.8944

Details:
    number of observations:   5
    t-statistic:              -0.14142135623730961
    degrees of freedom:       4
    empirical standard error: 0.7071067811865476
我想掌握这个价值观:

双面p值:0.8944

重定向我在我们的网站上找到的标准输出。但OneSampleTTest的输出似乎不受此影响

julia> using HypothesisTests

julia> original_stdout = stdout
Base.TTY(RawFD(0x0000001b) open, 0 bytes waiting)

julia> (rd, wr) = redirect_stdout()
(Base.PipeEndpoint(RawFD(0x00000020) open, 0 bytes waiting), Base.PipeEndpoint(RawFD(0x00000025) open, 0 bytes waiting))

julia> println("test")

julia> s = readline(rd)
"test"

julia> s == "test"
true

julia> OneSampleTTest([1,2,3,4,5],3.1)
One sample t-test
-----------------
Population details:
    parameter of interest:   Mean
    value under h_0:         3.1
    point estimate:          3.0
    95% confidence interval: (1.0368, 4.9632)

Test summary:
    outcome with 95% confidence: fail to reject h_0
    two-sided p-value:           0.8944

Details:
    number of observations:   5
    t-statistic:              -0.14142135623730961
    degrees of freedom:       4
    empirical standard error: 0.7071067811865476


julia> 
如果做了另一个
s=readline(rd)
,它会被卡住,因为rd中没有任何内容


我唯一能解决这个问题的另一个想法是尝试将测试结果写入一个文件,然后再次解析该文件。但是我想做数以百万计的t测试,并使用一个文件来存储结果,每次听起来都像是一个糟糕的性能测试。

我建议相信Julia和您的操作系统能够快速完成这些事情,并且只有在遇到瓶颈后才尝试优化

下面的代码将p值作为字符串打印到文本文件中,无需任何类型的标准输出重定向,而且速度很快。对于一百万次迭代,它需要2.5秒

 open("pvalues.txt","w") do io
    for i in 1:1000000 

        # do the test
        test = might be a better place.OneSampleTTest(rand(Int64,5),3.1)

        # transform only the pvalue of the test to a string an write it
        # take note of the function "pvalue", which extract, well, the p-value!
        write(io,string(pvalue(test),"\n"))

    end
end

您还可以进行讨论,以了解您处理数据的总体方法是否可以改进

调用
OneSampleTTest
实际上不会打印任何内容。如果在行的末尾放一个分号,您将看到没有显示任何输出

julia> OneSampleTTest([1,2,3,4,5],3.1);

julia>
OneSampleTTest()
所做的是返回
OneSampleTTest
类型的值。它甚至不做测试,只是创建一个测试对象。由于没有在末尾加分号,Julia将调用该方法将有关该值的信息性文本写入当前输出流
OneSampleTTest
是扩展了
Base的
hypositionistest
的子类型。show
方法可写入您在控制台上看到的输出

如果出于某种原因需要存储
show
的输出,可以使用
Base.repr
show
的输出作为
字符串

julia> result = repr(OneSampleTTest([1,2,3,4,5],3.1));

julia> result
"One sample t-test\n-----------------\nPopulation details:\n    parameter of interest:   Mean\n    value under h_0:         3.1\n    point estimate:          3.0\n    95% confidence interval: (1.0368, 4.9632)\n\nTest summary:\n    outcome with 95% confidence: fail to reject h_0\n    two-sided p-value:           0.8944\n\nDetails:\n    number of observations:   5\n    t-statistic:              -0.14142135623730961\n    degrees of freedom:       4\n    empirical standard error: 0.7071067811865476\n"
请注意,您不需要通过解析文本来提取p值
OneSampleTTest
实现该方法,只需在测试对象上使用
pvalue
即可计算并返回p值

julia> test = OneSampleTTest([1,2,3,4,5],3.1); # this only creates an object, does not compute p-value

julia> pvalue(test) # computes and `show`s the value

回答得好!不仅纠正了我关于println正在进行的错误假设,以及如何获取取而代之的返回对象的字符串表示,而且还向我指出了调用pvalue(test)的简单解决方案,这是非常有帮助的。我觉得“OneSampleTTest”的文档可以提供一些示例,说明如何从结果中提取所有相关的内容。