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

MAINT: Simplify algebra in LightSource.hillshade #8880

Merged
merged 2 commits into from
Jul 24, 2017

Conversation

eric-wieser
Copy link
Contributor

@eric-wieser eric-wieser commented Jul 14, 2017

Don't use trig functions when vector operations will suffice.

This also breaks hillshade down into two smaller functions, since shading based on normal vectors is an operation that's likely helpful elsewhere.

Image tests of this function continue to pass.

Brought up by #8877, after considering shading for #6404

np.cos(az) * np.cos(alt),
np.sin(az) * np.cos(alt),
np.sin(alt)
])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This trig is unavoidable, but it's also on scalars, so is cheap

normal[...,0] = -e_dx
normal[...,1] = -e_dy
normal[...,2] = 1
normal /= _linalg_norm(normal, keepdims=True, axis=-1)
Copy link
Contributor Author

@eric-wieser eric-wieser Jul 14, 2017

Choose a reason for hiding this comment

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

All of this vectorized trig on dx and dy amounts to... concatenating and normalizing.

Expanding np.cos(az - aspect) turns this into a dot product between a bunch of trig stuff, and the light .direction vector.

Moving on from there is done by tedious application of trig identities, or simply by geometric reasoning on how to obtain normal vectors.

@@ -1493,6 +1493,11 @@ def hsv_to_rgb(hsv):
return rgb


# np.linalg.norm doesn't broadcast in numpy 1.7
def _linalg_norm(arr, keepdims=False, axis=None):
return np.sqrt(np.sum(np.square(arr), axis=axis, keepdims=keepdims))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm correct that we're stuck on 1.7, right? Does this helper exist elsewhere?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wasn't enough here - that doesn't preserve masks on a masked array. See the corrected patch

@eric-wieser
Copy link
Contributor Author

Currently failing on masked arrays due to numpy bugs

Don't use trig functions when vector operations will suffice.

Image tests of this function continue to pass
@eric-wieser
Copy link
Contributor Author

Seems that this reduced the number of executable lines in hillshade, which resulted in codecov reporting lower test coverage - in general, simplifying methods that are tested will cause a failure by codecov!

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jul 15, 2017
@dstansby dstansby merged commit 1f8d143 into matplotlib:master Jul 24, 2017
@eric-wieser eric-wieser deleted the tidy-LightSource.hillshade branch March 23, 2019 18:07
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.

None yet

3 participants