Skip to content

Cleanup _plot_args_replacer logic #10872

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

Merged
merged 2 commits into from
Jul 10, 2018
Merged

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Mar 23, 2018

PR Summary

This is a follow-up to #10810. Basically it cleans up the code structure. The logic in the PR is the same, just rewritten to be more clear.

Additional follow up question

As part of the cleanup, I realized that we are not exactly checking what we want to know:

  • Want to know whether data as an item named name.
  • Check name in data or name in data.dtype.names.

That works for usual data (dict like and numpy.array), but may fail e.g. if a user would use a string-list for data. I assume that's ok-ish.

I think there is no real "has_item()" function and the only exact way of finding out if data[name] works is really trying and catching a possible exception. I assume we don't want to really pull data as part of this check.

So in total the present check is still the check we want to do. Please correct me if I'm wrong here.

@timhoffm timhoffm force-pushed the arg-replacer branch 2 times, most recently from 8fc43eb to 80cddbb Compare March 23, 2018 07:48
@timhoffm timhoffm added this to the v3.0 milestone Mar 26, 2018
@phobson
Copy link
Member

phobson commented Mar 26, 2018

may fail e.g. if a user would use a string-list for data. I assume that's ok-ish.

I'm worried this might mess categorical axes. I'll make a note to check on that.

@tacaswell
Copy link
Member

This will 'work' if you do ax.plot('foo', 'bar', data=['foo', 'bar'])

@anntzer
Copy link
Contributor

anntzer commented Mar 28, 2018

Why do we not want to pull out the data as part of the test? (i.e. try data[name] and catch attributeerror) "Should" be cheap...

@timhoffm
Copy link
Member Author

timhoffm commented Mar 28, 2018

"Probably" you are right.

In the worst case, it could get a factor 2 slower because we'll have to fetch the data later anyway. But I think this is negligible compared to all the other stuff done during plotting. If someone has data with a slow getitem, they can still resort to plot(data['x'], data['y']) to only access them once.

So I'm +1 on try data[name] and catch AttributeError.

@timhoffm
Copy link
Member Author

timhoffm commented Mar 28, 2018

Is there more support for "try data[name] and catch AttributeError"? If so, I would create an alternative PR.

@tacaswell
Copy link
Member

h5py objects can be slow.

I would lean towards continuing to use __contains__ and documenting that support that is part of the required API on the things passed in to data=.

@timhoffm
Copy link
Member Author

If h5py is the couter-example that item access can be slow, I'd leave the implementation logic as is, just with the cleaned up code from this PR.

I've added a note that the data object must support membership test.

@@ -1529,6 +1529,9 @@ def _replacer(data, key):
following arguments are replaced by **data[<arg>]**:

{replaced}

Objects passed as **data** must support item access (``data[<arg>]``) and
membership test (``<arg> in data``).
Copy link
Member

Choose a reason for hiding this comment

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

This is inconsistent with the actual test, and with the desired behavior; the structured ndarray does not support the membership test.

Copy link
Member

@tacaswell tacaswell Mar 31, 2018

Choose a reason for hiding this comment

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

Maybe should say 'support <arg> in data or be a numpy structured array'?

@anntzer
Copy link
Contributor

anntzer commented Mar 31, 2018

I don't mind the rebase, but note that #10928 will probably make all this obsolete.

@tacaswell
Copy link
Member

Don't we need both?

@anntzer
Copy link
Contributor

anntzer commented Mar 31, 2018

#10928 kills _plot_args_replacer and uses the standard _replacer logic used by everyone else (try to get the item and if it's there, swap it in -- so access is only done one anyways)

(and yes this PR is the original reason why I did #10928)

@timhoffm
Copy link
Member Author

timhoffm commented Jun 6, 2018

Note: Adapted _has_item() to fix #11389

@tacaswell tacaswell added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jul 10, 2018
@phobson phobson merged commit 0544616 into matplotlib:master Jul 10, 2018
@timhoffm timhoffm deleted the arg-replacer branch July 10, 2018 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants