-
Notifications
You must be signed in to change notification settings - Fork 4
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
✨feat: dataclasses with converters #20
base: main
Are you sure you want to change the base?
Conversation
56fabe8
to
ae42f0c
Compare
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.
Looks nice. But I'm not quite sure whether we want to necessarily lock ourselves in too much: e.g., for python scalars, we don't really want to do np.asarray
, right? Though I guess we can relax that later, and then having the ability to wrap the scalar in a light class that provides .shape()
etc would be good.
Similarly, in principle one would like to worry as little as possible about the unit class (but again, can relax that later).
Probably also premature, but does this get executed only on first initialization, or also on replace
? I.e., is there a performance penalty for every operation?
Agreed. We probably want to do
Yes, the converter for the units field allows us to delegate determining what's a viable input to the unit constructor, rather than
Yes there is. We can provide a past-path around this. e.g.
Then the only performance penalty is in calling the wrapper init. I think we can mitigate even that function call overhead using |
ae42f0c
to
14f06f7
Compare
Done. I enabled fast-pathing. |
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
14f06f7
to
cfafaff
Compare
@mhvk how does this look now? |
value: Any | ||
unit: u.UnitBase | ||
value: Array = field(converter=_value_converter) | ||
unit: u.UnitBase = field(converter=u.Unit) |
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.
In future, to make this completely unit-type agnostic, we would set the type to be a runtime-checkable protocol and the converter would be something like converter=lambda x: x if isinstance(x, UnitAPI) else config.get("units_backend")(x)
, where the user can configure a preferred units package. Or, if we keep this more closely tied to Astropy, we would have converter=lambda x: x if isinstance(x, UnitAPI) else u.Unit(x)
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.
I think this may be the way to go but worry that it is a bit too soon. Partially because I still think this is a bit too fast in turning python floats etc into numpy arrays -- after all, they promote differently, and also they are not annoyingly eager in converting everything else to ndarray
.
But also because it is not currently strict enough: e.g., what do we do with Quantity(q, unit)
? (with q
either our own quantity or astropy's, or even an astropy Column
) Do we bail? Do we convert, like astropy? If so, one needs access to the unit, and defining __init__
would be more appropriate.
My overall sense would be to postpone deciding on this, and first try to get things working assuming input is sane (though of course that should not stop us trying to reason things through if it isn't!).
Agreed about the promotion. I do think that strictly enforcing that
I think we do what numpy does.
Therefore I would suggest using
or
IMO the lesson to learn from numpy ndarray is that we want a generalized constructor for Quantity, not that |
@mhvk, as promised 😎.
IMO this should be spun off into a separate utility package, so it can be used, e.g. for the coordinates APE. CC @jeffjennings & @adrn.