Skip to content

Allow array with any dimension equal to 3 to initialize CartesianRepresentation - #5439

Merged
mhvk merged 7 commits into
astropy:masterfrom
mhvk:cartesian-representation-init
Oct 31, 2016
Merged

mhvk merged 7 commits into
astropy:masterfrom
mhvk:cartesian-representation-init

Conversation

@mhvk

@mhvk mhvk commented Oct 28, 2016 •

Copy link
Copy Markdown
Contributor

EDIT: this is now a PR!

For arrays holding xyz values, the dimension that holds xyz is in numpy often the last, but in CartesianRepresentation we only easily handle the case where it is the first. In #5434 (comment), it was suggested to consider having a way to handle both cases, perhaps by having axis (and unit?) arguments (this would be for Cartesian only).

@mhvk mhvk changed the title Allow array with last dimension equal to 3 to initialize CartesianRepresentation? Allow array with any dimension equal to 3 to initialize CartesianRepresentation Oct 28, 2016
@mhvk mhvk added this to the v1.3.0 milestone Oct 28, 2016
@mhvk

mhvk commented Oct 28, 2016

Copy link
Copy Markdown
Contributor Author

This PR is based on #5434; to see what it adds, just look at the seond commit.

The main idea is to have the ability to tell for an input vector array which axis the coordinate triples are stored in. Furthermore, it gives the option to pass in a unit (mostly for regular arrays returned by erfa but also if one wants to change the unit). Finally, one use get_xyz(axis) to get out a vector array with x,y,z at the given axis. As one can see, with this quite a bit of the transformation code is simplified.

cc @StuartLittlefair, @eteq.

@eteq eteq left a comment

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.

👍 to this, modulo my naming/docs comments below.

Comment thread astropy/coordinates/representation.py Outdated
('z', u.Quantity)])

def __init__(self, x, y=None, z=None, copy=True):
def __init__(self, x, y=None, z=None, unit=None, axis=0, copy=True):

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.

Here (and in the get_xyz), I think it might be better to call it something like xyz_axis. With just "axis" it's not really clear at a glance what that means.

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.

I like it for __init__ certainly. For get_xyz it is a bit less obvious, but for consistency obviously both should be the same. So, done.

Comment thread astropy/coordinates/representation.py Outdated
be in this unit if not given.
axis : int, optional
The axis along which the coordinates are stored for the case an array
of vectors is passed in (default: 0).

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.

maybe case an array of vectors is passed in -> when a single array is provided rather than distinct x y, and z``? To me, at least, that seems clearer in the context

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.

Much better. done.

y = self.attr_classes['y'](y, copy=copy)
z = self.attr_classes['z'](z, copy=copy)
raise ValueError("x, y, and z are required to instantiate {0}"
.format(self.__class__.__name__))

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 maybe there should be an exception now raised also in the case where axis !=0 but y and z are provided. Right now axis is just silently ignored in that case, right? Catching that will help users understand what the point of axis is.

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.

Yes, correct. Done.

mhvk added 2 commits October 29, 2016 01:08
Also allow extracting such arrays along any axis.
And to set a unit.
@mhvk
mhvk force-pushed the cartesian-representation-init branch 2 times, most recently from 0de7102 to da6e898 Compare October 29, 2016 23:59
@mhvk
mhvk force-pushed the cartesian-representation-init branch from da6e898 to 1a7cd3b Compare October 30, 2016 00:36
@mhvk

mhvk commented Oct 30, 2016

Copy link
Copy Markdown
Contributor Author

@eteq - after a bit of a hassle to understand and solve the numpy 1.7 failures, I now got to implement your comments -- all very good, thanks! With this, I guess ready to merge?

earth_heliocentric = (earth_pv[0] -
sun).get_xyz(xyz_axis=-1).to(u.au).value

# also prepare earth_pv for passing to erfa.

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.

I think this could benefit from the same explanatory comment used in line 124 of icrs_cirs_transforms.py.

Comment thread astropy/coordinates/representation.py Outdated
Returns
-------
xyz : `~astropy.units.Quantity`
With dimension 3 along ``axis``.

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.

typo: should be "along xyz_axis"?

@StuartLittlefair

Copy link
Copy Markdown
Contributor

@mhvk Excellent work! I like the way this closes the loop and means the conversion code is really tidy now.

@mhvk

mhvk commented Oct 31, 2016

Copy link
Copy Markdown
Contributor Author

@StuartLittlefair - OK, good suggestions. I made the changes made.

@eteq - since you were happy modulo comments that I addressed, I'll go ahead and merge.

Thanks both!

@mhvk
mhvk merged commit 3de25bd into astropy:master Oct 31, 2016
@mhvk
mhvk deleted the cartesian-representation-init branch October 31, 2016 16:55
@eteq

eteq commented Oct 31, 2016

Copy link
Copy Markdown
Member

👍

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.

3 participants