-
Notifications
You must be signed in to change notification settings - Fork 473
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
Split Quantity into scalar and sequence classes #764
Conversation
So that's with BaseQuantity having the methods/attributes shared across sequence and scalar, with a mixin (is that the right name?) having the sequence specific ones. Quantity inherits from BaseQuantity and returns either a QuantityScalar or QuantitySequence when initiated, depending on the value. QuantitySequence and QuantityScalar both inherit from Quantity, with QuantitySequence also inheriting the QuantitySequenceMixin |
Great start. I do have a lot of questions (I put them within the code). |
@@ -78,7 +78,7 @@ def wrapped(self, *args, **kwargs): | |||
|
|||
|
|||
@fix_str_conversions | |||
class _Quantity(PrettyIPython, SharedRegistryObject): | |||
class BaseQuantity(PrettyIPython, SharedRegistryObject): |
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.
Why is this classe "public" (i.e. no underscore)? Is this the class users should check against when using isinstance
?
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.
Hadn't thought of this when going through it!
It should be used when the user does know the UnitRegistry. In the current version users should be checking with _Quantity (as done in pint-pandas)
Using isinstance with a Quantity from one UnitRegistry and another UnitRegistry yields false:
'''
ureg=pint.UnitRegistry()
ureg2 = pint.UnitRegistry()
isinstance(my_array,ureg2.Quantity)
False
'''
Should this be public?
|
||
def build_quantity_class(registry, force_ndarray=False): | ||
|
||
class Quantity(BaseQuantity): |
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.
Or this is class to be used when calling isinstance
I guess that all the questions point to the same thing: How much can we simplify the code and making it more maintainable with this change? |
So this was very much a 'get it behaving as you expect' before spending time on those more specific things - I was just moving lines around before spending time working out what paths change/need changing. 1, 2, If the behavior for ndarrays is what's expected from a sequence then yes. I guess this applies everywhere ndarrays are treated differently.
4.Yea, missed them - are there any others?
There should be little difference with this change, from here onwards I can only see moving things around / removing checks like 2. |
And how about the connection to pandas ... will this make pint-pandas easier to implement? |
Came across this when looking for numpy functions y = np.arange(0, 1 * ureg.meter, 0.1 * ureg.meter)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence. The main reason I'd like this was because of that error message - I hit it many times in pint-pandas and spent a while working around it, so it'll make pint-pandas easier, along with checking for scalar/sequence. Back to that error message - I think it's because somewhere in numpy there's a check for iter or getitem, which makes it think it's a sequence and take a wrong path. Splitting into QuantityScalar fails that check so takes a different path: y = np.arange(0, 1 * ureg.meter, 0.1 * ureg.meter)
y
array([0, <Quantity(0.1, 'meter')>, <Quantity(0.2, 'meter')>,
<Quantity(0.30000000000000004, 'meter')>, <Quantity(0.4, 'meter')>,
<Quantity(0.5, 'meter')>, <Quantity(0.6, 'meter')>,
<Quantity(0.7, 'meter')>, <Quantity(0.7999999999999999, 'meter')>,
<Quantity(0.8999999999999999, 'meter')>], dtype=object) I'm surprised that works with 0 and not 0*ureg.m |
So that's the tests passing again, except the uncertainties ones. What's the plan there - are they being split into another module? I'm in two minds as to how to go forward with Alternatively we could convert only the args that are quantities to consistent/root units. This would make it easy to implement many functions https://github.com/hgrecco/pint/pull/764/files#diff-d9924213798d0fc092b8cff13928d747R115 but prevents function specific checks. The other thing I've noticed is the double underscored attributes, eg __used have become _BaseQuantity__used with the inheritance. Not sure if this is an issue. |
Are there any libraries that see such a change between supporting/not supporting
Assuming people are happy to only support numpy >=1.16 in the future, I'd suggest the following roadmap: 0.10 - Split Quantity into scalar and sequence classes (would split this PR into one splitting classes and another implementing |
Maybe. This will break many functions that work but aren't explicitly handled. Are people likely to submit PRs to handle these? |
I think it depends on how much you consider "such a change." With any numpy function that internally calls
Now that you mention it, I like the idea of splitting this PR into one splitting classes and another implementing My one concern with putting off Otherwise, this roadmap makes sense to me!
I would think this will break non-explicitly-handled ufuncs just like the |
what is the status on this? Now that both pydata/xarray#3238 and pydata/xarray#3447 are either merged or close to being merged, the support for I would be interested in and/or willing to help with getting this moving again or starting a new PR that only works on |
If all the interested parts are sattisfied with the currenbt solution, I think we should merge it. (after fixing the pending conflict) |
There are a couple reasons I think this shouldn't be merged yet:
Instead, I'd be in favor of taking the |
To be clear, I am happy to advise based off my cursory understanding of
pint, but don't take my advice too seriously -- I'm not the one actually
implementing this! You can certainly make __array_function__ overrides work
with either a single class or separate classes for scalars and arrays.
Ultimately this is a decision for pint's maintainers and whoever is
implementing this.
…On Wed, Oct 30, 2019 at 3:13 PM keewis ***@***.***> wrote:
if I remember correctly there was some discussion about whether the split
into scalars / sequences was a good idea, so I think we should probably
remove all code relating to that first (see #753
<#753> and #875
<#875>).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#764?email_source=notifications&email_token=AAJJFVSD5ISP5SCJ6QOOE63QRIBINA5CNFSM4GXA3XUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECV6CGY#issuecomment-548135195>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJJFVSAPITG7ZUXUY3BTVLQRIBINANCNFSM4GXA3XUA>
.
|
I just made my comments on #880 Short version, I am happy to merge it after a minor change. |
In case it is useful to anyone at this point, a branch I have with a rough implementation of Current To-Do List:
|
given that #880 has been merged, how do we want to continue with this? Would it be better to wait on a decision on #753 and #875 or should we go ahead and implement support for |
@keewis My apologies for the delays this past week! I'd like to submit the branch I have as a PR, I just haven't had the time to complete the remaining checklist items yet. I should though have the time in the next few days (just probably not today). |
I don't think you have to have completed those items when you create the PR (but good to hear you are working on this) |
A status update: unfortunately, the current implementation of this PR (with Also, a couple side questions:
|
@jthielen, I tried to use your branch to run the tests from xarray and ran into some issues (among others something about having Considering official python 2.7 support expires in a little more than a month, I'd say let's drop it, at least on master (but that's only my personal opinion). @hgrecco, what do you think? |
@keewis I'm sorry for the continued delays! I've opened #905 as a draft PR with my current status and some notes about what I see as remaining high-level tasks for it. I really need to be making progress on it (if there is still to be hope of having this in a Pint release by the end of the year), but I'm not sure when I'll get the time needed with all my other end-of-semester tasks...hopefully soon though. As far as suggesting changes, in absence of a better system, I think PRs to my branch would be best for large changes, whereas suggested changes in a review to #905 would be best for small changes? |
See #906 |
No description provided.