Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Catch specgram warnings in testing #7866

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

Closed
wants to merge 1 commit into from

Conversation

dstansby
Copy link
Member

@dstansby dstansby commented Jan 18, 2017

#7845 introduced lots of warnings when running tests. This gets rid of them in the easiest way I could think of, but is the first time I've done any decorating so there may be some room for improvement.

@dstansby dstansby changed the title Catch spectram warnings in testing Catch specgram warnings in testing Jan 18, 2017
@@ -1491,6 +1492,32 @@ def check_maxfreq(self, spec, fsp, fstims):
del fstimst[-1]
spect[maxind-5:maxind+5] = 0

class catch_specgram_warnings(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can have one fewer level of nesting with the following standard trick:

def catch_specgram_warnings(n_specgram_calls, func=None):
    if func is None:
        return functools.partial(catch_specgram_warnings, n_specgram_calls)
    @functools.wraps(func)
    def wrapped(...): ...
    return wrapped

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, so I now have

    def catch_specgram_warnings(n_specgram_calls, func=None):
        if func is None:
            return functools.partial(catch_specgram_warnings, n_specgram_calls)

        @functools.wraps(func)
        def wrapped(self_obj):
            # Decide how many warnings will be thrown
            n_warnings = 0
            if self_obj.NFFT_specgram is None:
                if 256 >= len(self_obj.y):
                    n_warnings = 1
            elif self_obj.NFFT_specgram >= len(self_obj.y):
                n_warnings = 1

            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                expected_warnings = self.n_specgram_calls * n_warnings
                # Run test
                test_function(self_obj)
                assert len(w) == expected_warnings,\
                    'warnings:%s, expected:%s' % (len(w), expected_warnings)

        return wrapped

but this is erroring with

======================================================================
ERROR: Failure: NameError (name 'catch_specgram_warnings' is not defined)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/dstansby/miniconda3/lib/python3.5/site-packages/nose/failure.py", line 39, in runTest
    raise self.exc_val.with_traceback(self.tb)
  File "/Users/dstansby/miniconda3/lib/python3.5/site-packages/nose/loader.py", line 407, in loadTestsFromName
    module = resolve_name(addr.module)
  File "/Users/dstansby/miniconda3/lib/python3.5/site-packages/nose/util.py", line 312, in resolve_name
    module = __import__('.'.join(parts_copy))
  File "/Users/dstansby/matplotlib/lib/matplotlib/tests/test_mlab.py", line 1313, in <module>
    class Test_spectral_nosig_real_onesided(CleanupTestCase):
  File "/Users/dstansby/matplotlib/lib/matplotlib/tests/test_mlab.py", line 1964, in Test_spectral_nosig_real_onesided
    @catch_specgram_warnings(1)
  File "/Users/dstansby/matplotlib/lib/matplotlib/tests/test_mlab.py", line 1497, in catch_specgram_warnings
    return functools.partial(catch_specgram_warnings, n_specgram_calls)
NameError: name 'catch_specgram_warnings' is not defined

----------------------------------------------------------------------

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is because it is defined in a class, so it becomes a method of that class rather than a freestanding function. You need to hoist it out.

But I agree with @efiring that it'd be better to just try not triggering the warnings in the first place.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jan 18, 2017
@tacaswell
Copy link
Member

👍 thank you! I had meant to create an issue for this but it got lost.

@efiring
Copy link
Member

efiring commented Jan 18, 2017

Would it be harder to handle this via supplying arguments that don't trigger the warning?

@dstansby
Copy link
Member Author

I've raised an issue (#7967), would recommend supplying arguments that don't trigger the warning to all but one test, and then just use a simple warnings filter to test that one case.

@dstansby dstansby closed this Jan 28, 2017
@dstansby dstansby deleted the specgram-test-warnings branch January 28, 2017 12:53
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

Successfully merging this pull request may close these issues.

4 participants