8000 Default to writing animation frames to a temporary directory. by anntzer · Pull Request #16082 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Default to writing animation frames to a temporary directory. #16082

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 1 commit into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/behaviour.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ Setting the same property under multiple aliases now raises a TypeError
Previously, calling e.g. ``plot(..., color=somecolor, c=othercolor)`` would
emit a warning because ``color`` and ``c`` actually map to the same Artist
property. This now raises a TypeError.

`.FileMovieWriter` temporary frames directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`.FileMovieWriter` now defaults to writing temporary frames in a temporary
directory, which is always cleared at exit. In order to keep the individual
frames saved on the filesystem, pass an explicit *frame_prefix*.
31 changes: 19 additions & 12 deletions lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def __init__(self, *args, **kwargs):
MovieWriter.__init__(self, *args, **kwargs)
self.frame_format = mpl.rcParams['animation.frame_format']

def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
def setup(self, fig, outfile, dpi=None, frame_prefix=None,
clear_temp=True):
"""
Perform setup for writing the movie file.
Expand All @@ -449,13 +449,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
controls the size in pixels of the resulting movie file.
Default is fig.dpi.
frame_prefix : str, optional
The filename prefix to use for temporary files. Defaults to
``'_tmp'``.
The filename prefix to use for temporary files. If None (the
default), files are written to a temporary directory which is
deleted by `cleanup` (regardless of the value of *clear_temp*).
clear_temp : bool, optional
If the temporary files should be deleted after stitching
the final result. Setting this to ``False`` can be useful for
debugging. Defaults to ``True``.

"""
self.fig = fig
self.outfile = outfile
Expand All @@ -464,8 +464,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
self.dpi = dpi
self._adjust_frame_size()

if frame_prefix is None:
self._tmpdir = TemporaryDirectory()
self.temp_prefix = str(Path(self._tmpdir.name, 'tmp'))
else:
self._tmpdir = None
self.temp_prefix = frame_prefix
self.clear_temp = clear_temp
self.temp_prefix = frame_prefix
self._frame_counter = 0 # used for generating sequential file names
self._temp_paths = list()
self.fname_format_str = '%s%%07d.%s'
Expand Down Expand Up @@ -527,13 +532,15 @@ def finish(self):

def cleanup(self):
MovieWriter.cleanup(self)

# Delete temporary files
if self.clear_temp:
_log.debug('MovieWriter: clearing temporary paths=%s',
self._temp_paths)
for path in self._temp_paths:
path.unlink()
if self._tmpdir:
_log.debug('MovieWriter: clearing temporary path=%s', self._tmpdir)
self._tmpdir.cleanup()
else:
if self.clear_temp:
_log.debug('MovieWriter: clearing temporary paths=%s',
self._temp_paths)
for path in self._temp_paths:
path.unlink()


@writers.register('pillow')
Expand Down
0