-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Lightsource enhancements #3291
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
Lightsource enhancements #3291
Conversation
I have not done a full review but this looks like great stuff. Could you have a look at adding some more tests for the code. The only present test that I know of test_light_source_shading_color_range Perhaps you could add some more real tests? Something like your example above but shading a more simplified surface, but one with a curvature perhaps just -x^2 - y^2. You can take inspiration in the existing test_light_source_shading_color_range if you like. I think it would be good to make sure that hillshade cannot do a divide by zero by checking that The present test failures are pep8 style fixes which should not be to difficult to fix. |
@jenshnielsen - Thanks, good points! I made the PEP8 fixes and I'll add some tests tonight. (Should have included both of those originally!) |
This is good to see. There are some places in mplot3d where LightSource is On Wed, Jul 23, 2014 at 8:25 AM, joferkington notifications@github.com
|
@WeatherGod - Good point! I added an example in 31a4239. There's an undocumented |
ls = mcolors.LightSource(315, 45) | ||
elev = dem['elevation'] | ||
cmap = cm.gist_earth | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test raises a unclosed file warning on python3. Could you add a dem.close()
statement here when you are done with the file. We are trying to reduce the number of warnings in the test suite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it wouldn't be dem.close()
, because it is a dictionary of numpy arrays.
It is the cbook.get_sample_data() that returns a file object. So
numpy.load() needs to do the closing, I think.
On Sat, Jul 26, 2014 at 5:21 AM, Jens H Nielsen notifications@github.com
wrote:
In lib/matplotlib/tests/test_colors.py:
+@image_comparison(baseline_images=['light_source_shading_topo'],
+def test_light_source_topo_surface():extensions=['png'])
- """Shades a DEM using different v.e.'s and blend modes."""
- dem = np.load(cbook.get_sample_data('jacksboro_fault_dem.npz'))
Get the true cellsize in meters for accurate vertical exaggeration
Convert from decimal degrees to meters
- dx, dy = dem['dx'], dem['dy']
- dx = 111320.0 * dx * np.cos(dem['ymin'])
- dy = 111320.0 * dy
- ls = mcolors.LightSource(315, 45)
- elev = dem['elevation']
- cmap = cm.gist_earth
The test raises a unclosed file warning on python3. Could you add a
dem.close() statement here when you are done with the file. We are trying
to reduce the number of warnings in the test suite.—
Reply to this email directly or view it on GitHub
https://github.com/matplotlib/matplotlib/pull/3291/files#r15432988.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jenshnielsen - Thanks for the catch!
@WeatherGod - Either one works, actually. dem
is an NpzFile
object. Closing it closes the file. I thought the same thing until I dug a bit deeper, though.
Either way, I went ahead and changed the test and example to use a context manager. (I didn't realize until today that np.load
supported that, but it does for .npz files, and apparently has for the past few versions.) See: 896bd37
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, looks like that actually doesn't close the underlying file (or, more precisely, the file returned by get_sample_data
is still open even though the NpzFile
object is closed). Still getting the warnings. I pushed things a bit too soon. See: bdbc7df for the correct fix.
@Patrick-Cole - I just saw your notebook on hillshading, and if you have a chance, I'd appreciate your input on this. I'm using a completely different algorithm than yours (I hadn't seen Horn (1981) before), but it's basically accomplishing the same thing. This is a more "traditional" approach (i.e. (Side note: a vertical exaggeration of 1 is only accurate if the correct dx and dy are supplied. In the figure at the top of the page, V.E.=0.01 is actually the closest to a true V.E. of 1 because the cellsize is about 90 meters ( Your scipy talk was a large part of the impetus for this pull request. I'd written a lot of this years ago, but never thought to contribute it back upstream, as I figured I was the only one using it. |
@Patrick-Cole Is it possible to use the vmin and vmax arguments to imshow to get rid of the outliers? |
@jenshnielsen - Not directly, because @Patrick-Cole - Thanks for testing things! Actually, you've revealed my ulterior motive in sending in this pull request. What you're asking for is all entirely reasonable, but is rather clunky to do as things are currently structured. In the long run, I'd like to put together an MEP (enhancement proposal) for a A new function like this would provide a simpler interface to the shading functionality. However, I wanted to get this pull request accepted before I proposed larger changes. (Also, like I mentioned, I think a new axes method arguably needs a "full-blown" MEP, rather than just a pull request.) At any rate, to answer your questions, here's how you'd do what you're asking with the current (i.e. this pull request) state of things: Avoid outliers
Shade different data
Display a correct colorbar
At any rate, it's all possible, but often clunkier than it should be. A separate axes method and a separate artist for shaded plots would make this a lot simpler, assuming there's enough interest. |
@joferkington I think it would be great to add your examples to the documentation as part of the pull request. |
Thanks for the examples. I agree with them being added to the documentation. Its definitely looking good so far! |
@joferkington Any progress on adding the examples to the documentation? I would like to add some tests in the style of the original test that you removed (i.e. |
@jenshnielsen - |
@joferkington Great I will have a more careful look later. I agree on removing the original array tests since they were basically broken. So adding new array tests seem sensible to me. The main point is that the array tests will test this piece of code more directly independent from any other code in matplotlib. |
BTW: I think that this is a significant improvement so you may want to add a small entry to the whats new folder too. |
689b3e1
to
ba1f492
Compare
Gah! I managed to mess up my fork and branch somehow... Just a bit, let me see if I can fix this. In the meantime, ignore all the commits at the end. |
The tests in abf8b87 fail due to pep8 issues in the included np arrays. I don't think that fixing this is a good idea since the arrays will be less readable if pep8ed. Personally I think that the best solution is to store the arrays in separate npz files rather than in the source code. @tacaswell @mdboom any thoughts on that? |
I don't think we should be pep8ing the tests. |
I think that I agree with that. We can remove the pep8 on that file but in principle I think that it is a good idea to separate tests from reference data. Also when the reference is not images. |
@jenshnielsen - On a side note, I fixed the pep8 issues on the printed arrays in fe474e7. The result is still quite readable, though I agree I might be better not to have the reference data inline. |
@joferkington thanks missed that due all the other commits |
3cfc33b
to
9fce26c
Compare
@jenshnielsen - Yeah, sorry about those. I believe I have all the duplicate commits fixed now. (On a side note, rebase can do some odd things under certain circumstances!) |
This looks good to me. A great addition with good documentation and testing so I will merge this. Thanks for all your work. |
@jenshnielsen - Thanks!! |
Currently,
colors.LightSource
preforms "hillshading" through methods that appear visually unrealistic in some cases (particularly topographic surfaces). This pull request:LightSource
to allow the hillshade method to be called independently so that illumination maps can be produced.blend_mode='overlay'
andblend_mode='soft'
are both more visually appealing for complex surfaces than the default mode. Effectively, the other blend modes make the surface appear less "shiny".All of the changes made should be fully backwards-compatible and none of the defaults have been changed. This also adds a 170Kb example data file of some SRTM elevation data (public domain) to the sample data.
As a quick example of the effect of some of the changes: