Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Plot directive preserve#10149

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

r-barnes
Copy link

PR Summary

The Sphinx plot directive can be used to automagically generate figures for documentation like so:

.. plot::   import matplotlib.pyplot as plt   import matplotlib.image as mpimg   import numpy as np   img = mpimg.imread('_static/stinkbug.png')   imgplot = plt.imshow(img)

But, if you reorder the figures in the documentation then all the figures may need to be rebuilt. This takes time. The names given to the figures are also fairly meaningless, making them more difficult to index by search engines or to find on a filesystem.

Alternatively, if you are compiling on a limited-resource service like ReadTheDocs, you may wish to build imagery locally to avoid hitting resource limits on the server. Using the new changes allows extensive dynamically generated imagery to be used on services like ReadTheDocs.

The:outname: property

These problems are addressed through two new features in the plot directive. The first is the introduction of the:outname: property. It is used like so:

.. plot::   :outname: stinkbug_plot   import matplotlib.pyplot as plt   import matplotlib.image as mpimg   import numpy as np   img = mpimg.imread('_static/stinkbug.png')   imgplot = plt.imshow(img)

Without:outname:, the figure generated above would normally be called, e.g.docfile3-4-01.png or something equally mysterious. With:outname: the figure generated will instead be namedstinkbug_plot-01.png or evenstinkbug_plot.png. This makes it easy to understand which output image is which and, more importantly, uniquely keys output images to code snippets.

Theplot_preserve_dir configuration value

Setting theplot_preserve_dir configuration value to the name of a directory will cause all images with:outname: set to be copied to this directory upon generation.

If an image is already inplot_preserve_dir when documentation is being generated, this image is copied to the build directory thereby pre-empting generation and reducing computation time in low-resource environments.

Related issues were raised on ReadTheDocs (link) and on StackOverflow (link).

PR Checklist

  • Has Pytest style unit tests
  • It would, but I'm not sure how to go about writing them. Sorry :-/
  • Code is PEP 8 compliant
  • Leastwise, nothing new is added to the numerous pre-existing violations. PEP8 adherence makes code difficult to read :-/
  • 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

@tacaswelltacaswell added this to thev2.2 milestoneJan 3, 2018
@tacaswell
Copy link
Member

👍 in principle, but the docs need to build cleanly!

@@ -656,6 +674,9 @@ def render_figures(code, code_path, output_dir, output_base, context,
for format, dpi in formats:
try:
figman.canvas.figure.savefig(img.filename(format), dpi=dpi)
if config.plot_preserve_dir and outname:
print("Preserving '{0}' into '{1}'".format(img.filename(format), config.plot_preserve_dir))
Copy link
Member

Choose a reason for hiding this comment

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

Not reviewing, but I don't think you wantprint in here do you? I'd do_log.info or_log.debug?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, that's probably what I want, but I didn't know about them. I'll update.

Copy link
Author

Choose a reason for hiding this comment

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

Actually, I don't see_log as a variable in theplot_directive.py namespace, so I'm not sure how to make that adjustment.

Copy link
Member

@jklymakjklymakJan 3, 2018
edited
Loading

Choose a reason for hiding this comment

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

If its not already in this module, then up at the top:

# ... other importsimportlogging# ... other imports_log=logging.getLogger(__name__)

Copy link
Author

Choose a reason for hiding this comment

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

Thanks: got that up.

@r-barnes
Copy link
Author

Eliminated some spaces

@jklymakjklymak modified the milestones:needs sorting,v3.0Mar 16, 2018
@jklymakjklymak modified the milestones:v3.0,v3.1Jul 9, 2018
@tacaswelltacaswell modified the milestones:v3.1,v3.2Feb 1, 2019
@jklymak
Copy link
Member

@r-barnes sorry this didn't get a second review yet. If you give it a rebase and ping me I'll give it a quick look and merge on the basis of@tacaswell 's approval. We are about to branch 3.2, so it'd be nice to get this in

@jklymakjklymak added the Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. labelJul 24, 2019
@timhoffm
Copy link
Member

Wouldn'tplot_preserve_dir better be namedplot_cache_dir?

tacaswell
tacaswell previously requested changesAug 11, 2019
Copy link
Member

@tacaswelltacaswell left a comment

Choose a reason for hiding this comment

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

Mostly the indentation needs to be fixed.

@r-barnes I am going to do the rebase and force-push to your branch

if config.plot_preserve_dir and outname:
outfiles = glob.glob(os.path.join(config.plot_preserve_dir, outname) + '*')
for of in outfiles:
_log.info("Copying preserved copy of '{0}' into '{1}'".format(of, build_dir))
Copy link
Member

Choose a reason for hiding this comment

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

we are py36+ so we can use f-strings 😄

@tacaswelltacaswellforce-pushed theplot_directive_preserve branch frome222429 toe238368CompareAugust 12, 2019 00:34
@tacaswell
Copy link
Member

sorry, I read the year when this was opened wrong and thought it was from this January, not from 2 years ago.

@tacaswelltacaswell dismissed theirstale reviewAugust 12, 2019 00:43

I did the re-base and added the leading spaces

Copy link
Contributor

@anntzeranntzer left a comment

Choose a reason for hiding this comment

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

modulo tests passing

@tacaswelltacaswellforce-pushed theplot_directive_preserve branch frome9fe4f7 to725f25dCompareAugust 19, 2019 19:39
@tacaswelltacaswell removed the Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. labelSep 4, 2019
@tacaswelltacaswell modified the milestones:v3.2.0,v3.3.0Sep 4, 2019
@timhoffm
Copy link
Member

I took the liberty of pushing an update to fix the doc issues.

Copy link
Member

@timhoffmtimhoffm left a comment

Choose a reason for hiding this comment

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

Needs a rebase on master. Then move whats new entry to our new whats new scheme.

@@ -519,7 +548,7 @@ def get_plot_formats(config):

def render_figures(code, code_path, output_dir, output_base, context,
function_name, config, context_reset=False,
close_figs=False):
close_figs=False, outname=''):
Copy link
Member

Choose a reason for hiding this comment

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

Would go withoutname=None, which is IMHO a more standard way for "not defined" than an empty string.

@@ -0,0 +1,61 @@
Plot Directive `outname` and `plot_preserve_dir`
Copy link
Member

Choose a reason for hiding this comment

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

I propose to renameplot_preserve_dir toplot_cache_dir.

@QuLogicQuLogic modified the milestones:v3.3.0,v3.4.0May 1, 2020
@jklymakjklymak marked this pull request as draftAugust 24, 2020 14:20
@QuLogicQuLogic modified the milestones:v3.4.0,v3.5.0Jan 21, 2021
@QuLogicQuLogic modified the milestones:v3.5.0,v3.6.0Aug 23, 2021
@timhoffmtimhoffm modified the milestones:v3.6.0,unassignedApr 30, 2022
@story645story645 modified the milestones:unassigned,needs sortingOct 6, 2022
@dstansby
Copy link
Member

See#25091 (comment) for context on closing.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ivanovivanovivanov left review comments

@has2k1has2k1has2k1 left review comments

@jklymakjklymakjklymak left review comments

@timhoffmtimhoffmtimhoffm left review comments

@anntzeranntzeranntzer approved these changes

@tacaswelltacaswelltacaswell left review comments

@alexrudyalexrudyAwaiting requested review from alexrudy

Assignees
No one assigned
Projects
None yet
Milestone
future releases
Development

Successfully merging this pull request may close these issues.

10 participants
@r-barnes@tacaswell@jklymak@timhoffm@dstansby@ivanov@has2k1@anntzer@QuLogic@story645

[8]ページ先頭

©2009-2025 Movatter.jp