Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

implemented inverse transform for Mollweide axes #1624

Merged
merged 4 commits into from

4 participants

@lpsinger

This patch adds an inverse transform to the class matplotlib.projections.geo.MollweideAxes.

@pelson
Collaborator

Looks good to me. I'd want to check the numerical accuracy of forward -> inverse -> forward before merging, but I have no problems with this in principle. Good stuff @lpsinger!

lpsinger added some commits
@lpsinger lpsinger added unit tests for Mollweide transforms
The new test functions test_mollweide_forward_inverse_closure and test_mollweide_inverse_forward_closure test that the forward->inverse and inverse->forward compositions are approximate identities.
40a9d48
@lpsinger lpsinger use ax.transProjection instead of ax.transData in Mollweide axes unit…
… test

The goal was to exercise the MollweideAxes.MollweideTransform and MollweideAxes.InvertedMollweideTransform classes. The ax.transData property does not make use of either of these classes; the ax.transProjection property does.
2ba7aef
@lpsinger lpsinger reduce precision to the level at which the test passes 6903325
@lpsinger

I added the unit tests. The previous three commits (40a9d48, 2ba7aef, and 6903325) could be squashed together.

@mdboom
Owner

:+1:

@dmcdougall dmcdougall merged commit 3f76b21 into matplotlib:master
@lpsinger lpsinger deleted the lpsinger:mollweide_inverse_transform branch
@lpsinger

Could this be merged into the next release?

@pelson
Collaborator

Could this be merged into the next release?

It is already merged and on master, so yes, this will be in v1.3 (~June time).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 27, 2012
  1. @lpsinger
Commits on Dec 29, 2012
  1. @lpsinger

    added unit tests for Mollweide transforms

    lpsinger authored
    The new test functions test_mollweide_forward_inverse_closure and test_mollweide_inverse_forward_closure test that the forward->inverse and inverse->forward compositions are approximate identities.
Commits on Dec 30, 2012
  1. @lpsinger

    use ax.transProjection instead of ax.transData in Mollweide axes unit…

    lpsinger authored
    … test
    
    The goal was to exercise the MollweideAxes.MollweideTransform and MollweideAxes.InvertedMollweideTransform classes. The ax.transData property does not make use of either of these classes; the ax.transProjection property does.
  2. @lpsinger
This page is out of date. Refresh to see the latest.
View
12 lib/matplotlib/projections/geo.py
@@ -476,8 +476,16 @@ def __init__(self, resolution):
self._resolution = resolution
def transform_non_affine(self, xy):
- # MGDTODO: Math is hard ;(
- return xy
+ x = xy[:, 0:1]
+ y = xy[:, 1:2]
+
+ # from Equations (7, 8) of
+ # http://mathworld.wolfram.com/MollweideProjection.html
+ theta = np.arcsin(y / np.sqrt(2))
+ lon = (np.pi / (2 * np.sqrt(2))) * x / np.cos(theta)
+ lat = np.arcsin((2 * theta + np.sin(2 * theta)) / np.pi)
+
+ return np.concatenate((lon, lat), 1)
transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__
def inverted(self):
View
43 lib/matplotlib/tests/test_axes.py
@@ -933,6 +933,49 @@ def test_transparent_markers():
ax = fig.add_subplot(111)
ax.plot(data, 'D', mfc='none', markersize=100)
+@cleanup
+def test_mollweide_forward_inverse_closure():
+ # test that the round-trip Mollweide forward->inverse transformation is an
+ # approximate identity
+ fig = plt.figure()
+ ax = fig.add_subplot(111, projection='mollweide')
+
+ # set up 1-degree grid in longitude, latitude
+ lon = np.linspace(-np.pi, np.pi, 360)
+ lat = np.linspace(-np.pi / 2.0, np.pi / 2.0, 180)
+ lon, lat = np.meshgrid(lon, lat)
+ ll = np.vstack((lon.flatten(), lat.flatten())).T
+
+ # perform forward transform
+ xy = ax.transProjection.transform(ll)
+
+ # perform inverse transform
+ ll2 = ax.transProjection.inverted().transform(xy)
+
+ # compare
+ np.testing.assert_array_almost_equal(ll, ll2, 3)
+
+@cleanup
+def test_mollweide_inverse_forward_closure():
+ # test that the round-trip Mollweide inverse->forward transformation is an
+ # approximate identity
+ fig = plt.figure()
+ ax = fig.add_subplot(111, projection='mollweide')
+
+ # set up grid in x, y
+ x = np.linspace(0, 1, 500)
+ x, y = np.meshgrid(x, x)
+ xy = np.vstack((x.flatten(), y.flatten())).T
+
+ # perform inverse transform
+ ll = ax.transProjection.inverted().transform(xy)
+
+ # perform forward transform
+ xy2 = ax.transProjection.transform(ll)
+
+ # compare
+ np.testing.assert_array_almost_equal(xy, xy2, 3)
+
if __name__=='__main__':
import nose
nose.runmodule(argv=['-s','--with-doctest'], exit=False)
Something went wrong with that request. Please try again.