Skip to content

Remove use of np.matrix in coordinates, version 2 - #5104

Merged
eteq merged 18 commits into
astropy:masterfrom
mhvk:coord-no-np-matrix-use-matmul
Jul 29, 2016
Merged

eteq merged 18 commits into
astropy:masterfrom
mhvk:coord-no-np-matrix-use-matmul

Conversation

@mhvk

@mhvk mhvk commented Jun 15, 2016

Copy link
Copy Markdown
Contributor

This is the second of two possible ways to remove the use of np.matrix in coordinates (see #5088).

In this PR, the matrices are replaced with normal arrays, and a new matmul function is defined that either just uses the numpy function of that name, or, for numpy <= 1.10, the equivalent einsum. With this implementation, in all the transformations, multiplication has to be replaced with matmul, so this affects a lot more files. To get general broadcasting to work, it will be necessary to also replace any .T on the rotation matrices with .swapaxes(-2, -1).

@mhvk

mhvk commented Jun 15, 2016

Copy link
Copy Markdown
Contributor Author

p.s. If we go this route, matmul should probably be defined in utils.compat.numpy rather than in angles, but this can obviously be done later.

mat3 = rotation_matrix(Galactic._ngp_J2000.ra.degree, 'z')

return mat1 * mat2 * mat3 * pmat
return matmul(matmul(matmul(mat1, mat2), mat3), pmat)

@astrofrog astrofrog Jun 16, 2016 •

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.

I wonder if we should have a standard way of indicating 'review this when we only support python 3.5 and higher', or 'review this when we only support numpy 1.11 or higher', etc.?

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.

What about having a very distinct color and naming for a label? Something like "revise after: np 1.11"?

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.

I think this should be something in the code though, not just labels. But yes, a syntax like:

# REVISE PY35: ...

could work

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It indeed needs to be something within a comment, since one cannot have @ as part of the code, even if protected by an if statement.

I do think, though, that the code will look clean enough if I define matrix_product (which repeatedly multiplies matrices together) and perhaps also a matrix_transpose.

@MSeifert04 MSeifert04 Jun 16, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You could wrap it in an if py35: eval("a @ b") but granted that's not exactly good style.

But the @ operator is really bad for backwards-compatibility because implementing it means that older python are not just not supported anymore but it will be impossible to use them with astropy because of the SyntaxErrors. So maybe using eval() until 2020 (?) would be the best way if astropy doesn't want to exclude any older python version.

@mhvk

mhvk commented Jun 16, 2016

Copy link
Copy Markdown
Contributor Author

Since the move to matmul seemed to be preferred, I went ahead and simplified expressions here by using matrix_product (which multiplies its arguments in sequence using reduce). I also made a matrix_transpose to have at least a hope multidimensional matrices will work (though I haven't added any test cases yet; PRs to my PR more than welcome...).

p.s. The only place where I did not yet remove .T is in ecliptic_transforms, since for that I feel #4293 should be resolved first.

@mhvk
mhvk force-pushed the coord-no-np-matrix-use-matmul branch from b8ace7d to 36e0ad2 Compare June 16, 2016 19:54
@astrofrog

Copy link
Copy Markdown
Member

@mhvk - have you done any kind of performance benchmarking to see how things change with this PR?

@mhvk
mhvk force-pushed the coord-no-np-matrix-use-matmul branch from 36e0ad2 to c733163 Compare June 16, 2016 20:29
@mhvk

mhvk commented Jun 16, 2016

Copy link
Copy Markdown
Contributor Author

Not beyond checking that np.einsum(...) is faster than multiplying to np.matrix instances. I'll need to remind myself how to do asv...

@mhvk

mhvk commented Jun 22, 2016

Copy link
Copy Markdown
Contributor Author

@eteq, @StuartLittlefair - would you be able to review this PR to remove the use of np.matrix? It passes all tests, and I think it is an improvement in itself. Next up would be to add full tests for broadcasting of coordinates and times, etc., but my own sense is that that is better done as a separate PR, since this one is already quite large.

Comment thread astropy/coordinates/angles.py Outdated
Either ``'x'``, ``'y'``, ``'z'``, or a (x,y,z) specifying the axis to
rotate about. If ``'x'``, ``'y'``, or ``'z'``, the rotation sense is
counterclockwise looking down the + axis (e.g. positive rotations obey
left-hand-rule). If given as an array, the last dimension should be 3.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it a good idea to also allow an array of strs? For example ['x', 'y', 'z']?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

'x', 'y', and 'z' are mostly meant as short-cuts to speed up those common cases; the speed-up would certainly be lost for multiple values, and since this is used mostly internally anyway, I'd tend to leave it alone.

@StuartLittlefair

Copy link
Copy Markdown
Contributor

@mhvk this is a 👍 from me.

@mhvk

mhvk commented Jun 27, 2016

Copy link
Copy Markdown
Contributor Author

@eteq - do you want to review this, or are you happy with @StuartLittlefair's review?

@astrofrog - on possible speed regressions: I think this should only lead to improvements, but still haven't figured out how to easily run asv. However, a quick test seems promising:

from astropy.coordinates import SkyCoord, Galactic
import astropy.units as u
sc = SkyCoord(np.arange(100.)*u.hourangle, np.zeros(100)*u.deg)
%timeit sc.transform_to(Galactic)
# Debian [1.1.2]: 10 loops, best of 3: 16.5 ms per loop
# This PR:       100 loops, best of 3: 14.3 ms per loop

@mhvk

mhvk commented Jul 4, 2016

Copy link
Copy Markdown
Contributor Author

@eteq - with @StuartLittlefair's review, I'm confident enough to merge this removal of the use of np.matrix in coordinates, but it might still be good if you could have a look as well. If not, one question: do you think this needs an entry in the API change section for 1.3? Obviously, it only changes internal routines, so strictly it is not necessary, but it might still be good to warn that rotation_matrix now returns ndarray just in case anybody has been using the routine for their own coordinate system transforms.

@eteq

eteq commented Jul 5, 2016 •

Copy link
Copy Markdown
Member

I definitely plan to have a look at this, @mhvk. It might take me a few more days but I'll try to bump it up in the queue seeing as how you think it's pretty much there.

One possible logistical complication: I think I've worked out a fix to #5133 that would make be made easier by use of this machinery. But ideally we would fix that in 1.2.x and this PR is certainly 1.3.x. I might just try for a "quick-and-dirty" fix that would work in 1.2.x, and then a better version to merge after this, but in that case we'd want to get the #5133 fix in before this.

@mhvk

mhvk commented Jul 5, 2016

Copy link
Copy Markdown
Contributor Author

@eteq - sounds good. I'm off on holidays at the end of the week, but happy to rebase beforehand with the fix for #5133 beforehand.

@StuartLittlefair

Copy link
Copy Markdown
Contributor

I've just discovered the transform method of CartesianRepresentation.

Given the changes here, is this PR a sensible place to update that method to use the code contained within this PR?

@mhvk
mhvk force-pushed the coord-no-np-matrix-use-matmul branch from 99e16d1 to 27d3930 Compare July 27, 2016 14:48
@mhvk

mhvk commented Jul 27, 2016

Copy link
Copy Markdown
Contributor Author

@eteq, @adrn - could you check you're happy with the deprecation warning, and with how I moved routines to a new matrix_utilities file? I'd like to merge this, as it is holding up further work on shape changes, etc.

@adrn

adrn commented Jul 27, 2016

Copy link
Copy Markdown
Member

This looks good to me -- thanks @mhvk !

@eteq

eteq commented Jul 29, 2016

Copy link
Copy Markdown
Member

Looks good to me too except for one super minor thing: I think this should go in the "other" section of the changelog, as it isn't really a bug fix (but isn't exactly an API change due to it not explicitly being public API). Will just do that in a manual merge though so as not to delay things further.

Thanks for all the work on this, @mhvk!

@eteq

eteq commented Jul 29, 2016

Copy link
Copy Markdown
Member

and just for the record, if anyone else is looking at this and wondering, the travis failures were a separate problem, reported in #5198 and fixed in #5203

@eteq
eteq merged commit 27d3930 into astropy:master Jul 29, 2016
eteq added a commit that referenced this pull request Jul 29, 2016
Remove use of np.matrix in coordinates, version 2
@eteq

eteq commented Jul 29, 2016

Copy link
Copy Markdown
Member

@mhvk - didn't think this needed delaying the merge, but I see you modified the Sgr example to use the new matrices but left in this:

SGR_MATRIX = np.array(B.dot(C).dot(D))

That could be changed to

SGR_MATRIX = matrix_product(B, C, D)

right?

@mhvk
mhvk deleted the coord-no-np-matrix-use-matmul branch July 29, 2016 20:44
@mhvk

mhvk commented Jul 29, 2016

Copy link
Copy Markdown
Contributor Author

Yes, sorry I missed that... Will look at #5210 tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants