Skip to content
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

ENH: improve API consistency for ProjectionPlot VS SlicePlot #3489

Merged
merged 2 commits into from
Nov 1, 2021

Conversation

neutrinoceros
Copy link
Member

@neutrinoceros neutrinoceros commented Aug 29, 2021

PR Summary

This is another jab at the current API inconsistency between slice plots and projection plots.
This replaces #3450.

main branch

  • SlicePlot is a "factory function" that dispatches between AxisAlignedSlicePlot and OffAxisSlicePlot classes
  • ProjectionPlot is a class, but it only performs "axis aligned" projections, and we have OffAxisProjectionPlot but no AxisAlignedProjectionPlot

this work

  • SlicePlot becomes a class, but does the same exact thing as the original factory function
  • ProjectionPlot becomes AxisAlignedProjectionPlot, making room for a "dispatch class" ProjectionPlot with the same dispatching pattern as SlicePlot

This eleviate the inconsistency, plus it allows users to rely on the fact that SlicePlot and ProjectionPlot are classes (as the camel case suggests).

I've checked that filenames generated by the PWViewerMPL.save method are consistent with the main branch.
The only possible backward compat breakage I can think of would be if anyone downstream relied on SlicePlot being a function, which seems terribly unlikely to me.

Note

The "dispatch class" pattern as implemented here is heavily inspired from the standard lib pathlib.Path class which instantiate pathlib.PosixPath or pathlib.WindowsPath depending on the OS, and provides a general abstraction for the two specialised classes.

PR Checklist

  • New features are documented, with docstrings and narrative docs
  • Adds a test for any bugs fixed. Adds tests for new features.

@neutrinoceros neutrinoceros added api-consistency naming conventions, code deduplication, informative error messages, code smells... enhancement Making something better viz: 2D labels Aug 29, 2021
@neutrinoceros neutrinoceros force-pushed the dispatch_plot_classes branch from 6b0f806 to 90aae3f Compare August 29, 2021 14:37
@neutrinoceros neutrinoceros marked this pull request as ready for review August 29, 2021 14:41
@neutrinoceros
Copy link
Member Author

Same failures as seen in other recent PRs as well as on the daily run for Jenkins these days.

@neutrinoceros
Copy link
Member Author

Close + reopen to retrigger tests

@neutrinoceros neutrinoceros force-pushed the dispatch_plot_classes branch from 90aae3f to 295ee64 Compare August 31, 2021 09:14
@neutrinoceros
Copy link
Member Author

there may be a way to reduce the diff size of this PR using from __future__ import annotations.

@neutrinoceros
Copy link
Member Author

I don't want to make this anymore confusing so I'll ignore my own previous comment.
The apparent diff is much bigger than the actual change, because I needed to move a couple classes and docstrings around to enable my proposed inheritance flow

@neutrinoceros neutrinoceros marked this pull request as draft September 16, 2021 09:18
@neutrinoceros
Copy link
Member Author

Sigh. I realize my current reimplementation of the compatibility layer doesn't work:

class Base:
     def __new__(cls, *args, **kwargs):
         if 'hello' in kwargs:
             val = kwargs['hello']
             print(f"CATPURED VALUE {val}")
             del kwargs["hello"]
         self = object.__new__(cls)
         return self

     def __init__(self, *args, **kwargs):
         if 'hello' in kwargs:
             raise RuntimeError("this is not supposed to be possible")
Base(hello=True)

raises the "impossible" error. I need to reiterate on this

@neutrinoceros
Copy link
Member Author

I think I solved this in the less disruptive fashion possible. Ideally I'd want to really align signatures for OffAxisSlicePlot.__init__ and AxisAlignedSlicePlot.__init__, but the way to do that is to make a handful of existing arguments keyword-only. While I think the sane way to use those is de facto by passing them as keywords, it is a disruption to existing users if anyone is using them as positional. I may propose such a change in the future but I don't want this PR to break backwards compatibility for anyone.

@neutrinoceros
Copy link
Member Author

adding the bug label now that this fixes #3528

matthewturk
matthewturk previously approved these changes Sep 27, 2021
Copy link
Member

@matthewturk matthewturk left a comment

Choose a reason for hiding this comment

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

Seems pretty good. Anything in particular you want eyes on? I've read and it seems OK but there might be things I'm missing?

@neutrinoceros
Copy link
Member Author

This is intended as a no-op for the most part, but I did change (supposedly improved) the way normal vectors are validated. I think this would be the part worth the most attention.

@matthewturk
Copy link
Member

@chummels could you take a look, and if this looks OK to you can you merge?

Copy link
Member

@cphyc cphyc left a comment

Choose a reason for hiding this comment

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

This looks great to me! Especially, I am very happy with the dispatching mechanism inserted in the __new__ method, very clean!

I however left few comments that I think should be addressed before this goes in.

doc/source/cookbook/complex_plots.rst Outdated Show resolved Hide resolved
doc/source/cookbook/simple_off_axis_projection.py Outdated Show resolved Hide resolved
yt/visualization/plot_window.py Outdated Show resolved Hide resolved
yt/visualization/tests/test_plotwindow.py Show resolved Hide resolved
- rename ProjectionPlot -> AxisAlignedProejctionPlot
- make SlicePlot a dispatch class instead of a factory function
- a similar dispatch class with the name ProjectionPlot
- add an abstract layer (NormalPlot) as a common ancestor
  for plot classes with a "normal"/"axis" argument
@neutrinoceros
Copy link
Member Author

@yt-fido test this please

@neutrinoceros
Copy link
Member Author

@yt-fido test this please (this seems to keep hitting a randomly failing test in the athenapp frontend)

@neutrinoceros
Copy link
Member Author

neutrinoceros commented Oct 22, 2021

@yt-fido test this please (if this test fails one more time I'm making a PR to propose deactivating it because it's not just this PR)

edit: here's the PR #3620

@neutrinoceros
Copy link
Member Author

So this has two approvals already but @chummels would like to review it too, so we'll hold this off for the time being :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-consistency naming conventions, code deduplication, informative error messages, code smells... bug enhancement Making something better viz: 2D
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Normal vectors in SlicePlot doesn't understand non-cartesian coordinate systems
3 participants