Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] A reconstruct() call alters the sys.stdout #125

Open
patquem opened this issue Jul 1, 2021 · 10 comments
Open

[BUG] A reconstruct() call alters the sys.stdout #125

patquem opened this issue Jul 1, 2021 · 10 comments

Comments

@patquem
Copy link

patquem commented Jul 1, 2021

Hello,

I am working on a pysap test with unitest.
But my unitest can't be correctly executed due to the following AttributeError:

File "/home/300.86-METRO_CARAC/PYTHON/miniconda3/lib/python3.7/unittest/case.py", line 647, in run
result.stopTest(self)
File "/home/300.86-METRO_CARAC/PYTHON/pycharm-community-2021.1.2/plugins/python-ce/helpers/pycharm/teamcity/unittestpy.py", line 263, in stopTest
output = sys.stdout.getvalue()

AttributeError: '_io.TextIOWrapper' object has no attribute 'getvalue'

This error is related to a mri.reconstructors.base.ReconstructorBase.reconstruct() function call.

When this function is not called, the sys.stdout is :
image
and the unitest is correctly executed.

But when this function is called, the sys.stdout becomes :
image
and the unitest goes wrong (AttributeError).

Could you please investigate and fix this ?
(this could be related to the buffer or a file not correctly closed when passing in the reconstruct() function ?)

Note : The following workaround which consists in :

import sys
from importlib import reload
reload(sys) 

after calling the function didn't work for me.

Thank you,
Patrick

@chaithyagr
Copy link
Contributor

Thank you for pointing this issue. However I am afraid this isn't related to pysap-mri.
If I am right, you are using pycharm and this seems to be a known issue in running tests on pycharm and I have indeed faced this in past.

Here is some more details: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000019990-Unittest-Error-AttributeError-file-object-has-no-attribute-getvalue-
I would recommend to switch to pytest for testing within pycharm as I don't think I ever faced this issue there.

Can you please run the test from terminal and not use pycharm to ensure that pysap-mri isn't causing it? From what I see, the same tests pass at CI, hence I am closing this issue here. Feel free to ping here if you still have the issues.

@chaithyagr
Copy link
Contributor

Also, which version of pycharm are you using?
I would recommend updating it as these parts of pycharm have been unstable and constantly undergo changes

@patquem
Copy link
Author

patquem commented Jul 2, 2021

Hello @chaithyagr ,

The problem is not related to PyCharm.
When I execute my unitest in console mode (out of PyCharm) I am faced to the same problem.

File "init.py", line 72, in run_tests
xmlrunner.XMLTestRunner(output=rslt_path, verbosity=3).run(test_suite)
File "/home/300.86-METRO_CARAC/PYTHON/envs/env_aar114/lib/python3.7/site-packages/xmlrunner/xmlrunner.py", line 468, in run
self._restore_standard_output()
File "/home/300.86-METRO_CARAC/PYTHON/envs/env_aar114/lib/python3.7/site-packages/xmlrunner/xmlrunner.py", line 397, in _restore_standard_output
sys.stdout = sys.stdout.delegate
AttributeError: 'io.TextIOWrapper' object has no attribute 'delegate'

the reconstruct() function introduces something wrong for the sys.stdout interpretation by xmlrunner afterwards.

Patrick

@chaithyagr
Copy link
Contributor

Reopening this here, will have a look to be sure

@chaithyagr chaithyagr reopened this Jul 2, 2021
@chaithyagr
Copy link
Contributor

Can you please post how did you run the test, also which particular test is failing. The stacktrace given above indicates the issue is in xmlrunner more than pysap-mri. Further, I could run all the tests locally and also we have successful test at travis CI here

@patquem
Copy link
Author

patquem commented Jul 5, 2021

I am sorry, I can't do it (reasonably).
The test is not a one from the pysap release but a test that belongs to our test-suite and based on specific developments we realise here in CEA-Grenoble.
We didn't modify mri.reconstruct(). We just observe that the sys.stdout is altered after passing in this function and makes our xmlrunner test-suite report down.
As previously said, this could be related to a File that is not correctly closed in mri.reconstruct() (or something wrong with the Buffer ?).

@chaithyagr
Copy link
Contributor

I understand that. However, I am sorry but without a minimum reproducible example, I cant be able to reproduce and hence fix the bug, that too if any. I am quite confident the codes at pysap-mri is fine as it is constantly used by many users and also the codes are constantly tested with rigid CI pipelines.
In fact, the reconstruct function is a simple wrapper to the Modopt functions to optimize. (see here) . We dont have any buffers or file opening here.
Please make sure you have latest version of modopt.

If possible, please do try to setup a minimum reproducible example which isnt directly related to your internal codes. Or you can send in an email with the minimum reproducible codes if you are comfortable with it.

@patquem
Copy link
Author

patquem commented Jul 9, 2021

Hello,
We have finally found what was the cause of the disturbances. This was the same as for modopt I met a while.
The progress bar.
Could you please report the following changes to make the progress bar a 'user' argument ?
in the mri context, we have done in primal_dual.py :
line 30 : progress=True added in the condatvu arguments
line 181 : progress=progress added in the Condat arguments

Maybe it could be relevant to do the same for the other optimisers, if not :)
Thanks,
Patrick

@chaithyagr
Copy link
Contributor

Sure, I can take some time and open a PR for this. While I still don't understand how this can cause that issue, it surely can be a good feature to add.

@chaithyagr
Copy link
Contributor

Thank you for helping with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants