朱莉娅语言:重定向标准输出不会影响每个println // //如何从标准输出中提取值

时间:2019-10-20 15:54:59

标签: julia stdout p-value t-test

我最初的目的是执行ttest并获得pvalue。 我使用了OneSampleTTest,它看上去很有希望,但结果最终以如下所示在stdout中出现:

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

我想掌握这个值:

two-sided p-value: 0.8944

要重定向标准输出,我在这里的网站上找到了this。但是似乎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检验,并使用文件存储结果,并在每次听起来像是一次糟糕的演奏时都重新读取它们。

2 个答案:

答案 0 :(得分:3)

我建议您信任Julia和您的操作系统来快速执行此类操作,并且只有在遇到瓶颈后才尝试进行优化。

以下代码将p值作为 strings 打印到文本文件中,而无需任何类型的stdout-redirection,而且速度很快。一百万次迭代需要 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

您也可以在https://discourse.julialang.org/进行讨论,以了解是否可以改善处理数据的整体方法。

答案 1 :(得分:1)

OneSampleTTest的调用实际上不会打印任何内容。如果在行的末尾放置分号,则会看到未显示任何输出。

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

julia>

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

如果出于某种原因需要存储show的输出,则可以使用Base.repr,它会将show的输出作为String。 / p>

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方法,只需在测试对象上使用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