示例#1
0
def test_convert_gif(tmpdir, moviename, remove_movie, gif_palette, gifname, gif_framerate):
    m = tmpdir.join(moviename)
    mpath = m.strpath
    g = tmpdir.join(gifname)
    gpath = g.strpath

    da = test_dataarray()
    mov = Movie(da)
    mov.save_frames_serial(tmpdir)

    combine_frames_into_movie(tmpdir, moviename)

    convert_gif(
        mpath,
        gpath=gpath,
        gif_palette=gif_palette,
        remove_movie=remove_movie,
        gif_framerate=gif_framerate,
    )

    if remove_movie:
        assert ~m.exists()
    else:
        assert m.exists()

    assert g.exists()
    fps = 1000 / Image.open(g.strpath).info["duration"]
    assert np.ceil(fps) == gif_framerate
示例#2
0
def test_Movie(plotfunc, framedim, frame_pattern, dpi, pixelheight,
               pixelwidth):
    da = test_dataarray()
    kwargs = dict(
        plotfunc=plotfunc,
        framedim=framedim,
        frame_pattern=frame_pattern,
        pixelwidth=pixelwidth,
        pixelheight=pixelheight,
        dpi=dpi,
    )
    if framedim == "wrong":
        with pytest.raises(ValueError):
            mov = Movie(da, **kwargs)
    else:
        mov = Movie(da, **kwargs)

        if plotfunc is None:
            assert mov.plotfunc == rotating_globe_dark
        else:
            assert mov.plotfunc == plotfunc
        assert mov.framedim == framedim
        assert mov.pixelwidth == pixelwidth
        assert mov.pixelheight == pixelheight
        assert mov.dpi == dpi
示例#3
0
def test_write_movie(tmpdir, moviename, remove_frames, framerate, ffmpeg_options):

    frame_pattern = "frame_%05d.png"  # the default
    m = tmpdir.join(moviename)
    da = test_dataarray()
    mov = Movie(da)
    mov.save_frames_serial(tmpdir)
    filenames = [tmpdir.join(frame_pattern % ff) for ff in range(len(da.time))]
    combine_frames_into_movie(
        tmpdir,
        moviename,
        remove_frames=remove_frames,
        framerate=framerate,
        ffmpeg_options=ffmpeg_options,
    )

    if remove_frames:
        assert all([~fn.exists() for fn in filenames])
    else:
        assert all([fn.exists() for fn in filenames])
    assert m.exists()  # could test more stuff here I guess.
    video = cv2.VideoCapture(m.strpath)
    fps = video.get(cv2.CAP_PROP_FPS)
    assert fps == framerate
    total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
    assert total_frames == len(da.time)
示例#4
0
def test_Movie(plotfunc, framedim, frame_pattern, dpi, pixelheight,
               pixelwidth):
    da = test_dataarray()
    kwargs = dict(
        plotfunc=plotfunc,
        framedim=framedim,
        frame_pattern=frame_pattern,
        pixelwidth=pixelwidth,
        pixelheight=pixelheight,
        dpi=dpi,
    )
    if framedim == "wrong":
        with pytest.raises(ValueError):
            mov = Movie(da, **kwargs)
    else:
        mov = Movie(da, **kwargs)

        if plotfunc is None:
            assert mov.plotfunc == basic
        else:
            assert mov.plotfunc == plotfunc
        assert mov.plotfunc_n_outargs == _check_plotfunc_output(
            mov.plotfunc, mov.data)
        assert mov.dpi == dpi
        assert mov.framedim == framedim
        assert mov.pixelwidth == pixelwidth
        assert mov.pixelheight == pixelheight
        assert mov.dpi == dpi
示例#5
0
def test_movie_save(tmpdir, filename, gif_palette):
    # Need more tests for progress, verbose, overwriting
    path = tmpdir.join(filename)
    da = test_dataarray()
    mov = Movie(da)
    mov.save(path.strpath, gif_palette=gif_palette)

    assert path.exists()
示例#6
0
def test_movie_save_parallel_wrong_chunk(tmpdir):
    path = tmpdir.join("movie.mp4")
    da = test_dataarray().chunk({"time": 2})
    mov = Movie(da)
    with pytest.raises(ValueError) as excinfo:
        mov.save(
            path.strpath,
            parallel=True,
        )
    assert "Input data needs to be a with single chunks along" in str(excinfo.value)
示例#7
0
def test_movie_save_parallel_no_dask(tmpdir):
    path = tmpdir.join("movie.mp4")
    da = test_dataarray()
    mov = Movie(da)
    with pytest.raises(ValueError) as excinfo:
        mov.save(
            path.strpath,
            parallel=True,
        )

    assert "Input data needs to be a dask array to save in parallel" in str(excinfo.value)
示例#8
0
def test_movie_render_frame(plotfunc, expected_empty):
    da = test_dataarray()
    mov = Movie(da, plotfunc=plotfunc)

    if expected_empty:
        with pytest.warns(UserWarning):
            fig, ax, pp = mov.render_single_frame(1)
            assert ax is None
            assert pp is None
            assert isinstance(fig, mpl.figure.Figure)

    else:
        fig, ax, pp = mov.render_single_frame(1)
        assert isinstance(ax, mpl.axes.Axes)  # this needs to be tested for the projections aswell
        assert isinstance(fig, mpl.figure.Figure)
示例#9
0
def test_movie_write_movie(tmpdir, moviename, remove_frames):

    frame_pattern = "frame_%05d.png"  # the default
    m = tmpdir.join(moviename)
    mpath = m.strpath

    da = test_dataarray()
    mov = Movie(da)
    mov.save_frames(tmpdir)
    filenames = [tmpdir.join(frame_pattern % ff) for ff in range(len(da.time))]
    write_movie(tmpdir, moviename, remove_frames=remove_frames)

    if remove_frames:
        assert all([~fn.exists() for fn in filenames])
    else:
        assert all([fn.exists() for fn in filenames])
    assert m.exists()  # could test more stuff here I guess.
示例#10
0
def test_Movie(plotfunc, framedim, frame_pattern, dpi, pixelheight,
               pixelwidth):
    da = test_dataarray()
    kwargs = dict(
        plotfunc=plotfunc,
        framedim=framedim,
        frame_pattern=frame_pattern,
        pixelwidth=pixelwidth,
        pixelheight=pixelheight,
        dpi=dpi,
    )

    # if not time, hide it to test changing default
    if framedim != "time":
        da = da.rename({"time": "something_else"})
    mov = Movie(da, **kwargs)

    if plotfunc is None:
        assert mov.plotfunc == basic
    else:
        assert mov.plotfunc == plotfunc
    assert mov.plotfunc_n_outargs == _check_plotfunc_output(
        mov.plotfunc, mov.data, framedim)

    assert mov.dpi == dpi
    assert mov.framedim == framedim
    assert mov.pixelwidth == pixelwidth
    assert mov.pixelheight == pixelheight
    assert mov.dpi == dpi
    assert mov.kwargs == _parse_plot_defaults(mov.data, mov.raw_kwargs)
    # assures that none of the input options are not parsed

    # now test exceptions:
    # non existent framedim
    with pytest.raises(ValueError):
        mov = Movie(da, framedim="wrong")
    # passing dataset without plot_variable (this should error out)
    with pytest.raises(ValueError):
        ds = xr.Dataset({"some": test_dataarray(), "stuff": test_dataarray()})
        mov = Movie(ds)

    # this should work (this way one could pass a totally custom function)
    mov = Movie(ds, input_check=False)
    assert mov.kwargs == {
    }  # there are no kwargs set that are not used by Movie
示例#11
0
def test_movie_save(tmpdir, parallel, filename, gif_palette, framerate, gif_framerate, ffmpeg_options):
    print(gif_palette)
    # Need more tests for progress, verbose, overwriting
    path = tmpdir.join(filename)
    da = test_dataarray()
    if parallel:
        da = da.chunk({"time": 1})
    mov = Movie(da)
    mov.save(
        path.strpath,
        gif_palette=gif_palette,
        framerate=framerate,
        gif_framerate=gif_framerate,
        ffmpeg_options=ffmpeg_options,
        parallel=parallel,
    )

    assert path.exists()
    # I should also check if no other files were created. For later

    # Check relevant fps of output file
    if ".mp4" in filename:
        fps = cv2.VideoCapture(path.strpath).get(cv2.CAP_PROP_FPS)
        assert fps == framerate
    elif ".gif" in filename:
        fps = 1000 / Image.open(path.strpath).info["duration"]
        assert np.ceil(fps) == gif_framerate

    # Check overwriting
    print(path.exists())
    with pytest.raises(RuntimeError):
        mov.save(path.strpath, overwrite_existing=False)
示例#12
0
def test_convert_gif(tmpdir, moviename, remove_movie, gif_palette, gifname):
    m = tmpdir.join(moviename)
    mpath = m.strpath
    g = tmpdir.join(gifname)
    gpath = g.strpath

    da = test_dataarray()
    mov = Movie(da)
    mov.save_frames(tmpdir)

    write_movie(tmpdir, moviename)

    convert_gif(mpath,
                gpath=gpath,
                gif_palette=gif_palette,
                remove_movie=remove_movie)

    if remove_movie:
        assert ~m.exists()
    else:
        assert m.exists()

    assert g.exists()
示例#13
0
def test_plotfunc_kwargs(tmpdir):
    """Test if kwargs are properly
    propagated to the  plotfunction"""
    def plotfunc(ds, fig, tt, framedim="time", test1=None, **kwargs):
        if test1 is None:
            raise RuntimeError("test1 cannot be None")

    da = test_dataarray()
    mov = Movie(da, plotfunc=plotfunc, test1=3)
    mov.preview(0)
    mov.save_frames(tmpdir)
示例#14
0
def test_plotfunc_kwargs_xfail(tmpdir):
    pytest.xfail("if **kwargs is not in the function signature \
        and the input is checked, this should error out.")

    def plotfunc(ds, fig, tt, test1=None):
        if test1 is None:
            raise RuntimeError("test1 cannot be None")

    da = test_dataarray()
    mov = Movie(da, plotfunc=plotfunc, test1=3)
    mov.preview(0)
    mov.save_frames(tmpdir)
示例#15
0
def test_movie_save_frames(tmpdir, frame_pattern):
    da = test_dataarray()
    mov = Movie(da, frame_pattern=frame_pattern)
    mov.save_frames_serial(tmpdir)
    filenames = [tmpdir.join(frame_pattern % ff) for ff in range(len(da.time))]
    assert all([fn.exists() for fn in filenames])
示例#16
0
def test_movie_preview():
    da = test_dataarray()
    mov = Movie(da)
    mov.preview(0)
    fig = plt.gcf()
    assert mov.dpi == fig.dpi