manoharan-lab / holopy

Hologram processing and light scattering in python
GNU General Public License v3.0
133 stars 50 forks source link

Cleantests #301

Closed briandleahy closed 4 years ago

briandleahy commented 4 years ago

This PR primarily deals with some of the warnings and other stuff printed out to the console when the tests are run. Now there are only two tests which print out information to the console, scattering.tets.test_lowlevel.test_multisphere_singlesph and scattering.tets.test_lowlevel.test_asm. Unfortunately, since the code that causes the print statements is in one of the C libraries holopy uses, I can't suppress this without doing something drastic.

As an example, the test output before these commits:

.................S......................................................S...........................................................Exception ignored in: <_io.FileIO name=56 mode='rb+' closefd=True>
ResourceWarning: unclosed file <_io.BufferedRandom name=56>
.........................../home/brian/holopy/holopy/inference/nmpfit.py:130: UserWarning: Minimizer Convergence Failed, your results                                 may not be correct.
  may not be correct.")
....S...S..................................................................................................................................................SSSSSSSSS......... single sphere max. order:           12

 Estimated cluster expansion order:          14
 assembling interaction matrix row:            1

 Solving for incident state            1
 +iteration:            1
 error:    0.0000000000000000     

 Solving for incident state            2
 +iteration:            1
 error:    0.0000000000000000     

  Cluster expansion order:           14
... single sphere max. order:            5

 Estimated cluster expansion order:           9
 assembling interaction matrix row:            1
 assembling interaction matrix row:            2

 Solving for incident state            1
 +iteration:            1
 error:    1.6293636664628852E-002
 +iteration:            2
 error:    2.0851788369929126E-004
 +iteration:            3
 error:    4.0114965420415224E-006
 +iteration:            4
 error:    1.0340134107902293E-007

 Solving for incident state            2
 +iteration:            1
 error:    1.6293636664628852E-002
 +iteration:            2
 error:    2.0851788369929126E-004
 +iteration:            3
 error:    4.0114965420415224E-006
 +iteration:            4
 error:    1.0340134107902293E-007

  Cluster expansion order:            6
.........................................................................../home/brian/holopy/holopy/scattering/scatterer/spherecluster.py:74: OverlapWarning: Spheres(scatterers=[Sphere(n=1.59, r=5e-07, center=[3e-06, 3e-06, 1e-05]), Sphere(n=1.59, r=5e-07, center=[3.4e-06, 3e-06, 1e-05])], ties={}, warn=True) has overlaps between spheres: [(0, 1)]
  warnings.warn(OverlapWarning(self, self.overlaps))
/home/brian/holopy/holopy/scattering/scatterer/spherecluster.py:74: OverlapWarning: Spheres(scatterers=[Sphere(n=1.59, r=5e-07, center=[3e-06, 3e-06, 1e-05]), Sphere(n=1.59, r=5e-07, center=[3.4e-06, 3e-06, 1e-05])], ties={}, warn=True) has overlaps between spheres: [(0, 1)]
  warnings.warn(OverlapWarning(self, self.overlaps))
/home/brian/holopy/holopy/scattering/scatterer/spherecluster.py:74: OverlapWarning: Spheres(scatterers=[Sphere(n=1.59, r=5e-07, center=[3e-06, 3e-06, 1e-05]), Sphere(n=1.59, r=5e-07, center=[3.9e-06, 3e-06, 1e-05])], ties={}, warn=True) has overlaps between spheres: [(0, 1)]
  warnings.warn(OverlapWarning(self, self.overlaps))
/home/brian/holopy/holopy/scattering/scatterer/spherecluster.py:74: OverlapWarning: Spheres(scatterers=[Sphere(n=1.59, r=5e-07, center=[3e-06, 3e-06, 1e-05]), Sphere(n=1.59, r=5e-07, center=[3.9e-06, 3e-06, 1e-05])], ties={}, warn=True) has overlaps between spheres: [(0, 1)]
  warnings.warn(OverlapWarning(self, self.overlaps))
......................................................................S
----------------------------------------------------------------------
Ran 481 tests in 42.708s

OK (SKIP=14)
/usr/local/lib/python3.6/dist-packages/matplotlib/backends/backend_gtk3.py:197: Warning: Source ID 8 was not found when attempting to remove it
  GLib.source_remove(self._idle_draw_id)

And after:

................S......................................................S..........................................................................................S...S..................................................................................................................................................SSSSSSSSS......... single sphere max. order:           12

 Estimated cluster expansion order:          14
 assembling interaction matrix row:            1

 Solving for incident state            1
 +iteration:            1
 error:    0.0000000000000000     

 Solving for incident state            2
 +iteration:            1
 error:    0.0000000000000000     

  Cluster expansion order:           14
... single sphere max. order:            5

 Estimated cluster expansion order:           9
 assembling interaction matrix row:            1
 assembling interaction matrix row:            2

 Solving for incident state            1
 +iteration:            1
 error:    1.6293636664628852E-002
 +iteration:            2
 error:    2.0851788369929126E-004
 +iteration:            3
 error:    4.0114965420415224E-006
 +iteration:            4
 error:    1.0340134107902293E-007

 Solving for incident state            2
 +iteration:            1
 error:    1.6293636664628852E-002
 +iteration:            2
 error:    2.0851788369929126E-004
 +iteration:            3
 error:    4.0114965420415224E-006
 +iteration:            4
 error:    1.0340134107902293E-007

  Cluster expansion order:            6
...................................................................................................................................................S
----------------------------------------------------------------------
Ran 482 tests in 43.056s

OK (SKIP=14)

In addition, I fixed a few bugs in the tests (including one incorrectly failing test), expanded a test, and made some pep8 changes as well.

@barkls I know you're crippled right now but I'd love your feedback on this. If in person is easier lmk!

barkls commented 4 years ago

Hey @briandleahy I have some code already in holopy (probably in multisphere?) that suppresses the print statements. It might be worthwhile to factor this out as a context manager and use that to suppress the additional printing during tests.

barkls commented 4 years ago

it's in holopy.scattering.theory.multisphere, lines 186-206

briandleahy commented 4 years ago

Should be fixed. Now the test output is much tidier:

nosetests3
.................S......................................................S..........................................................................................S...S..................................................................................................................................................SSSSSSSSS...............................................................................................................................................................S
----------------------------------------------------------------------
Ran 483 tests in 42.485s

OK (SKIP=14)

I have a 2 questions / comments in the code before merging; see above.

barkls commented 4 years ago

In conversation with @briandleahy, we agreed to keep the Fortran output in stdout vs. stderr, since it's usually suppressed anyway. The context manager gives a convenient way to to hide/expose the output as necessary.

briandleahy commented 4 years ago

Thanks @barkls . I think maybe down the line we can edit the fortran code to output to stderr, but I don't think it's urgent now.