Skip to content

Simplify units.Registry.get_converter.#9314

Merged
timhoffm merged 1 commit into
matplotlib:masterfrom
anntzer:simpler-units
Feb 28, 2019
Merged

Simplify units.Registry.get_converter.#9314
timhoffm merged 1 commit into
matplotlib:masterfrom
anntzer:simpler-units

Conversation

@anntzer

@anntzer anntzer commented Oct 8, 2017

Copy link
Copy Markdown
Contributor

The caching of the converter was disabled 10 years ago
(be73ef4) and is unlikely to ever work
as the correct converter for ndarrays depends on the type of the
contents, not on the container, so just drop the cache-related code.

When a masked array is passed, it's OK to just get the first underlying
data, even if it is masked (as it should have the same type as the
unmasked entries), for the purpose of getting the converter.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@dstansby dstansby added this to the 2.2 (next feature release) milestone Oct 8, 2017

@dstansby dstansby 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.

A couple of things that I think need leaving in.

Comment thread lib/matplotlib/units.py
def get_converter(self, x):
'get the converter interface instance for x, or None'

if not len(self):

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 it would be good to keep this in, to avoid stepping through the following logic if there aren't any convertors registered in the first place.

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 think the cost of an instance check, a dict lookup and attempting to get the first element is quite small. For code like this I would like to see at least a microbenchmark showing at least some minor improvement (not necessarily a big one) before making it more complex than necessary.

Comment thread lib/matplotlib/units.py
converter = None
classx = getattr(x, '__class__', None)

if classx is not None:

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.

Shouldn't this also stay in, as there's no guarantee that unit data is a subclass of numpy array or an iterable?

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.

Should be handled by return self[type(x)], no?

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.

Ah yes

@dstansby

Copy link
Copy Markdown
Member

(but I think simplifying the numpy logic is a good thing to do!)

@tacaswell tacaswell modified the milestones: v2.2, v3.0 Feb 10, 2018
@tacaswell

Copy link
Copy Markdown
Member

Please hold of on merging this for 2.2.

@jklymak

jklymak commented Jul 9, 2018

Copy link
Copy Markdown
Member

I think this should wait for the units MEP, if such is forthcoming...

@tacaswell tacaswell added this to the v3.1 milestone Jul 10, 2018
@anntzer

anntzer commented Jul 10, 2018

Copy link
Copy Markdown
Contributor Author

AFAICT this should not change any semantics.

@jklymak

jklymak commented Feb 27, 2019

Copy link
Copy Markdown
Member

Can this get a rebase?

I'm still nervous about any changes to units without more thorough testing, i.e. w/ pandas, datetime, pint, etc. OTOH, this seems a lot simpler, so I'm inclined to approve given that tghe existing tests do pass...

@anntzer

anntzer commented Feb 27, 2019

Copy link
Copy Markdown
Contributor Author

rebased

@jklymak

jklymak commented Feb 27, 2019

Copy link
Copy Markdown
Member

... good thing we added some tests 😉

@anntzer

anntzer commented Feb 27, 2019

Copy link
Copy Markdown
Contributor Author

good catches, should be fixed now.

@jklymak jklymak 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.

This looks right to me. Could have an extra comment or two, for instance after the except KeyError, but I supposes its clear enough

Comment thread lib/matplotlib/units.py

# If x is an array, look inside the array for data with units
"""Get the converter interface instance for *x*, or None."""
if hasattr(x, "values"):

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.

@ImportanceOfBeingErnest had some concerns about this way to check for pandas:
#11664 (comment)

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.

One could do something like type(x).__module__.startswith("pandas.") and hasattr(x, "values") but that should be a separate PR; this PR doesn't change the way pandas testing is done.

The caching of the converter was disabled 10 years ago
(be73ef4) and is unlikely to ever work
as the correct converter for ndarrays depends on the type of the
contents, not on the container, so just drop the cache-related code.

When a masked array is passed, it's OK to just get the first underlying
data, even if it is masked (as it should have the same type as the
unmasked entries), for the purpose of getting the converter.
@anntzer

anntzer commented Feb 28, 2019

Copy link
Copy Markdown
Contributor Author

added comments

@timhoffm timhoffm 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.

This looks right, but I would be more comfortable if there were some tests.

@anntzer

anntzer commented Feb 28, 2019

Copy link
Copy Markdown
Contributor Author

There are already tests (as noted by Jody anbove, an earlier version of this failed them).

@timhoffm timhoffm merged commit cd3ed7a into matplotlib:master Feb 28, 2019
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Feb 28, 2019
@anntzer anntzer deleted the simpler-units branch February 28, 2019 17:00
@jklymak

jklymak commented Feb 28, 2019

Copy link
Copy Markdown
Member

There are some tests. I'd still argue there are not enough, but I guess we will find out what breaks...

dstansby added a commit that referenced this pull request Feb 28, 2019
…14-on-v3.1.x

Backport PR #9314 on branch v3.1.x (Simplify units.Registry.get_converter.)
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.

5 participants