1
1
from math import floor
2
2
import os
3
3
from os .path import join as pjoin
4
- from tempfile import mkdtemp
5
4
from warnings import warn
6
5
6
+ import imageio
7
7
import numpy as np
8
8
from scipy import stats , ndimage , misc
9
9
from scipy .interpolate import interp1d
21
21
22
22
from . import utils , io
23
23
from .utils import (Surface , verbose , create_color_lut , _get_subjects_dir ,
24
- string_types , assert_ffmpeg_is_available , ffmpeg )
24
+ string_types )
25
25
26
26
27
27
import logging
@@ -730,6 +730,35 @@ def _get_display_range(self, scalar_data, min, max, sign):
730
730
731
731
return min , max
732
732
733
+ def _iter_time (self , time_idx , interpolation ):
734
+ """Iterate through time points, then reset to current time
735
+
736
+ Parameters
737
+ ----------
738
+ time_idx : array_like
739
+ Time point indexes through which to iterate.
740
+ interpolation : str
741
+ Interpolation method (``scipy.interpolate.interp1d`` parameter,
742
+ one of 'linear' | 'nearest' | 'zero' | 'slinear' | 'quadratic' |
743
+ 'cubic'). Interpolation is only used for non-integer indexes.
744
+
745
+ Yields
746
+ ------
747
+ idx : int | float
748
+ Current index.
749
+
750
+ Notes
751
+ -----
752
+ Used by movie and image sequence saving functions.
753
+ """
754
+ current_time_idx = self .data_time_index
755
+ for idx in time_idx :
756
+ self .set_data_time_index (idx , interpolation )
757
+ yield idx
758
+
759
+ # Restore original time index
760
+ self .set_data_time_index (current_time_idx )
761
+
733
762
###########################################################################
734
763
# ADDING DATA PLOTS
735
764
def add_overlay (self , source , min = 2 , max = "robust_max" , sign = "abs" ,
@@ -2041,12 +2070,9 @@ def save_image_sequence(self, time_idx, fname_pattern, use_abs_idx=True,
2041
2070
images_written: list
2042
2071
all filenames written
2043
2072
"""
2044
- current_time_idx = self .data_time_index
2045
2073
images_written = list ()
2046
- rel_pos = 0
2047
- for idx in time_idx :
2048
- self .set_data_time_index (idx , interpolation )
2049
- fname = fname_pattern % (idx if use_abs_idx else rel_pos )
2074
+ for i , idx in enumerate (self ._iter_time (time_idx , interpolation )):
2075
+ fname = fname_pattern % (idx if use_abs_idx else i )
2050
2076
if montage == 'single' :
2051
2077
self .save_single_image (fname , row , col )
2052
2078
elif montage == 'current' :
@@ -2055,10 +2081,6 @@ def save_image_sequence(self, time_idx, fname_pattern, use_abs_idx=True,
2055
2081
self .save_montage (fname , montage , 'h' , border_size , colorbar ,
2056
2082
row , col )
2057
2083
images_written .append (fname )
2058
- rel_pos += 1
2059
-
2060
- # Restore original time index
2061
- self .set_data_time_index (current_time_idx )
2062
2084
2063
2085
return images_written
2064
2086
@@ -2172,15 +2194,7 @@ def save_movie(self, fname, time_dilation=4., tmin=None, tmax=None,
2172
2194
bitrate : str | float
2173
2195
Bitrate to use to encode movie. Can be specified as number (e.g.
2174
2196
64000) or string (e.g. '64k'). Default value is 1M
2175
-
2176
- Notes
2177
- -----
2178
- This method requires FFmpeg to be installed in the system PATH. FFmpeg
2179
- is free and can be obtained from `here
2180
- <http://ffmpeg.org/download.html>`_.
2181
2197
"""
2182
- assert_ffmpeg_is_available ()
2183
-
2184
2198
if tmin is None :
2185
2199
tmin = self ._times [0 ]
2186
2200
elif tmin < self ._times [0 ]:
@@ -2206,12 +2220,10 @@ def save_movie(self, fname, time_dilation=4., tmin=None, tmax=None,
2206
2220
2207
2221
logger .debug ("Save movie for time points/samples\n %s\n %s"
2208
2222
% (times , time_idx ))
2209
- tempdir = mkdtemp ()
2210
- frame_pattern = 'frame%%0%id.png' % (np .floor (np .log10 (n_times )) + 1 )
2211
- fname_pattern = os .path .join (tempdir , frame_pattern )
2212
- self .save_image_sequence (time_idx , fname_pattern , False , - 1 , - 1 ,
2213
- 'current' , interpolation = interpolation )
2214
- ffmpeg (fname , fname_pattern , framerate , codec = codec , bitrate = bitrate )
2223
+ images = (self .screenshot () for _ in
2224
+ self ._iter_time (time_idx , interpolation ))
2225
+ imageio .mimwrite (fname , images , fps = framerate , codec = codec ,
2226
+ bitrate = bitrate )
2215
2227
2216
2228
def animate (self , views , n_steps = 180. , fname = None , use_cache = False ,
2217
2229
row = - 1 , col = - 1 ):
0 commit comments