Skip to content

Contouring 1x1 array (issue #8197) - #8227

Merged
NelleV merged 1 commit into
matplotlib:masterfrom
ianthomas23:8197_contour_1x1_array
Mar 11, 2017
Merged

NelleV merged 1 commit into
matplotlib:masterfrom
ianthomas23:8197_contour_1x1_array

Conversation

@ianthomas23

Copy link
Copy Markdown
Member

There are two underlying problems here. Firstly, we've never bothered to explicitly check if contour/contourf are called with a 2D array that is smaller than 2x2, but have instead relied upon later code to raise an error. There are different code paths for the legacy and new contouring algorithms, hence two different errors reported by OP. Secondly, in the new contour code (and also tricontour too) I haven't correctly handled all errors raised; I've called PyErr_SetString in each case but not necessarily returned -1 or NULL (depending on context) correctly. This shouldn't really matter as all such C++ code is private and covered by checks in the python wrappers - well it should be be, but evidently isn't in this case.

Hence there are two parts to the fix. Firstly, I've added explicit checks in the python contour code for arrays smaller than 2x2, and this is done twice as there are 2 code paths. I've also put a new test in test_contour.py to test that these errors are correctly raised. Secondly, I've corrected _contour_wrapper.cpp and _tri_wrapper.cpp to correctly report errors, and added a set of tests for each so that each raising of a python error in the C++ code is tested at least once.

Comment thread lib/matplotlib/tests/test_contour.py Outdated
try:
plt.contour([[0]])
except TypeError as exc:
assert exc.args[0] == 'Input z must be at least a 2x2 array.'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we have recently changed over to using py.test. You can see how asserts are tested here: http://doc.pytest.org/en/latest/assert.html#assertions-about-expected-exceptions

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Exception-catching tests changed to pytest, including those elsewhere in test_contour.py. Thanks for the link @WeatherGod.

assert_array_almost_equal(cs.levels, np.linspace(-12, 12, 5))


def test_contour_1x1_array():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test might need a cleanup decorator, because while the plt.contour() calls all fail, figures and axes are still produced prior to throwing the error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

master does not need or use the cleanup decorator any more; it's all in the py.test fixture.

Comment thread pytest.ini Outdated
matplotlib/testing/jpl_units/UnitDbl.py E201 E202 E203
matplotlib/testing/jpl_units/UnitDblConverter.py E201 E202 E203 E251 E302 E501 E711
matplotlib/testing/jpl_units/UnitDblFormatter.py E201 E202 E251 E302
matplotlib/tests/test_contour.py E501

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The PEP8 exceptions list should only get smaller, not bigger. Probably will be fixed by using the py.test method noted by @WeatherGod.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@QuLogic: done.

@ianthomas23

Copy link
Copy Markdown
Member Author

Should I squash these commits into one?

@phobson

phobson commented Mar 9, 2017

Copy link
Copy Markdown
Member

@ianthomas23 if you're comfortable with that workflow, go for it.

@ianthomas23
ianthomas23 force-pushed the 8197_contour_1x1_array branch from d716c7b to 3e23c4e Compare March 10, 2017 07:36
@ianthomas23

Copy link
Copy Markdown
Member Author

Squashed commits.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Mar 10, 2017
@tacaswell

Copy link
Copy Markdown
Member

Is this worth backporting to 2.0.x? This seems like a pretty narrow corner that has been broken for a long time?

@efiring

efiring commented Mar 10, 2017

Copy link
Copy Markdown
Member

I would say "no" to backporting.

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.

7 participants