Julia language: Redirecting stdout does not affect every println // How to extract value from stdout

2

My original aim was to perform a ttest and gaining a pvalue. I used OneSampleTTest which looked promising yet the results ended up in stdout like this:

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

I wanted to get hold of this value:

two-sided p-value: 0.8944

To redirect an stdout I found this on our website here. But it seems that the output from the OneSampleTTest is unaffected by this.

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> 

And if did another s = readline(rd) it would get stuck, because there is nothing in rd. (I assume)

My only other idea to solve this would have been to try and write the results of the test into a file and parse that file again. But I want to do millions of t-tests and to use a file to store the results and reread them everytime sounds like a terrible performance effort.

julia
stdout
p-value
t-test
asked on Stack Overflow Oct 20, 2019 by grimbar

2 Answers

3

I'd suggest to trust Julia and your OS to do such things fast and only try to optimize after you experience a bottleneck.

Following code prints p-values as a strings into a text file without any kind of stdout-redirection and is fast. For a million iterations it takes 2.5 seconds.

 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

You might also carry your discussion to https://discourse.julialang.org/ to find out if your overall approach of handling the data could be improved.

answered on Stack Overflow Oct 20, 2019 by laborg
2

The call to OneSampleTTest does not actually print anything. If you put a semicolon at the end of the line, you will see that no output is shown.

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

julia>

What OneSampleTTest() does is to return a value of type OneSampleTTest. It does not even do the test, merely creating a test object. Since you did not put a semicolon at the end, Julia will call the method Base.show to write an informative text about this value to the current output stream. OneSampleTTest is a subtype of HypothesisTest which extends Base.show method to write the output you see on your console.

If you need, for some reason, to store the output of show you can use Base.repr which would give you the output of show as a String.

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"

Note that you do not need to extract the p-value by parsing a text. OneSampleTTest implements pvalue method and simply using pvalue on your test object will compute and return the p-value.

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
answered on Stack Overflow Oct 20, 2019 by hckr

User contributions licensed under CC BY-SA 3.0