def test_fail_movie_size(): with pytest.raises(ValueError) as ex: m = Movie() img = np.arange(100).reshape(20, 5) m.add_image(img, animation_type='movie') assert 'Expected 3d numpy array when animation type is movie' in str( ex.value)
def test_fail_window_size_rgb(): with pytest.raises(ValueError) as ex: m = Movie() img = np.arange(100).reshape(4, 5, 5) m.add_image(img, animation_type='window', is_rgb=True) assert 'Expected 3d numpy array when animation type is window and is_rgb is True' in str( ex.value)
def test_ffmpeg(tmpdir): path = tmpdir.join('test2').relto('') m = Movie(dt=1.0 / 14, height_ratio=2) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') m.save(path) assert os.path.isfile(path + '.mp4')
def test_imagemagick(tmpdir): path = tmpdir.join('test').relto('') m = Movie(dt=1.0 / 14, height_ratio=2) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') m.save(path, writer_name='imagemagick') assert os.path.isfile(path + '.gif')
def test_save_fail(tmpdir): path = tmpdir.join('test').relto('') writers.avail = [] m = Movie(dt=1.0 / 14, height_ratio=2) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') with pytest.raises(ValueError) as ex: m.save(path) assert 'Could not find' in str(ex.value)
def test_set_ylim_axis_same_fail(): m = Movie(dt=1.0 / 14) img = np.arange(250).reshape(10, 5, 5) m.add_image(img, style='dark_img') with pytest.raises(RuntimeError) as ex: m.add_axis('test', 'test', ylim_type='same', ylim_value=1) m.add_trace(np.arange(10)) a = Animation(m) a._init_draw() assert 'Tried to have same y limits ' in str(ex.value)
def test_set_ylim_axis_set_fail(): m = Movie(dt=1.0 / 14) img = np.arange(250).reshape(10, 5, 5) m.add_image(img, style='dark_img') data = np.arange(10) with pytest.raises(RuntimeError) as ex: m.add_axis('test', 'test', ylim_type='set', ylim_value=10) m.add_trace(data) a = Animation(m) a._init_draw() assert 'ylim type set to set but len of ylim_value is not len 2' in str( ex.value)
def test_set_ylim_axis_both(): m = Movie(dt=1.0 / 14) img = np.arange(250).reshape(10, 5, 5) m.add_image(img, style='dark_img') data = np.arange(10) m.add_axis('test', 'test', ylim_type='p_both', ylim_value=10) m.add_trace(data) a = Animation(m) a._init_draw() ax = a.trace_axes[-1] y_min, y_max = ax.get_ylim() assert np.isclose(y_min, 0.9) assert np.isclose(y_max, 8.1)
def test_time_label_image(): m = Movie(dt=1.0 / 14, height_ratio=2) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') m.add_time_label(color='blue') assert m.labels[0]['x'] == 0.01 assert m.labels[0]['y'] == 0.08 assert np.allclose(m.labels[0]['values'], np.arange(4) / 14.0) assert m.labels[0]['axis'] == 0 assert m.labels[0]['s_format'] == '%.2fs' assert m.labels[0]['size'] == 14 kwargs = m.labels[0]['kwargs'] assert kwargs['color'] == 'blue'
def test_x_data(): m = Movie(dt=1.0 / 14, height_ratio=1.5) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') a = Animation(m) assert np.allclose(a.x_data, np.arange(4) * 1.0 / 14) m = Movie(dt=0.5, height_ratio=1.5) img = np.arange(1000).reshape(40, 5, 5) m.add_image(img, style='dark_img') a = Animation(m) assert np.allclose(a.x_data, np.arange(40) * 0.5) m = Movie(dt=1) img = np.arange(100).reshape(5, 20) m.add_image(img, animation_type='window', window_size=5) m.add_axis('x', 'y') m.add_trace(np.arange(20)) a = Animation(m) assert np.allclose(a.x_data, np.arange(20) - 5 // 2) m = Movie(dt=1) img = np.arange(100).reshape(5, 20) m.add_image(img, animation_type='window', window_size=7, window_step=2) m.add_axis('x', 'y') m.add_trace(np.arange(20)) a = Animation(m) assert np.allclose(a.x_data, np.arange(20) - 7 // 2)
def test_default_values(): m = Movie() img = np.arange(100).reshape(4, 5, 5) m.add_image(img) m_img = m.images[0] assert np.allclose(m_img['data'], img) assert m_img['animation_type'] == 'movie' assert m_img['style'] == 'dark_img' assert m_img['c_title'] is None assert m_img['c_style'] == 'dark_background' assert np.isclose(m_img['ymin'], 0) assert np.isclose(m_img['ymax'], 98.901) assert m_img['window_size'] == 29 assert m_img['window_step'] == 1 assert m_img['is_rgb'] is False
def test_set_ylim_axis_same(): m = Movie(dt=1.0 / 14) img = np.arange(250).reshape(10, 5, 5) m.add_image(img, style='dark_img') data = np.arange(10) m.add_axis('test', 'test', ylim_type='set', ylim_value=(2, 10)) m.add_trace(data) m.add_axis('test', 'test', ylim_type='same', ylim_value=0) m.add_trace(data) m.add_trace(data, axis=1) a = Animation(m) a._init_draw() ax = a.trace_axes[-1] y_min, y_max = ax.get_ylim() assert np.isclose(y_min, 2) assert np.isclose(y_max, 10)
def test_var_annotation(): m = Movie() img = np.arange(100).reshape(4, 5, 5) m.add_image(img) array = [[0, 0], [1, 1], [2, 2], [3, 3]] array2 = [[3, 3], [2, 2], [1, 1], [0, 0]] arrow_props = dict(arrowstyle="->", color='r', connectionstyle="arc3", lw=3) m.add_variable_annotation(axis=0, xy_array=array, xy_text_array=array2, text_array=('', '', '', ''), axis_type='image', arrowprops=arrow_props) an = m.annotations[0] assert an['axis'] == 0 assert np.allclose(an['xy_array'], array) assert np.allclose(an['xy_text_array'], array2) assert an['axis_type'] == 'image' an_kwargs = an['kwargs'] assert an_kwargs['arrowprops'] == arrow_props
def test_ratio_1p5(): m = Movie(dt=1.0 / 14, height_ratio=1.5) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') a = Animation(m) a._init_draw() assert a.gs.get_geometry() == (1, 1) assert a.gs.get_height_ratios() is None m = Movie(dt=1.0 / 14, height_ratio=1.5) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') m.add_axis('x', 'y') m.add_trace(np.arange(4)) a = Animation(m) a._init_draw() assert a.gs.get_geometry() == (2, 1) assert a.gs.get_height_ratios() == (1.5, 1) m = Movie(dt=1.0 / 14, height_ratio=1.5) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') m.add_axis('x', 'y') m.add_trace(np.arange(4)) m.add_axis('x', 'y') m.add_trace(np.arange(4), axis=1) a = Animation(m) a._init_draw() assert a.gs.get_geometry() == (3, 1) assert a.gs.get_height_ratios() == (3, 1, 1)
def test_n_axis(): with pytest.raises(RuntimeError) as ex: m = Movie(dt=1.0 / 14, height_ratio=1.5) _ = Animation(m) assert 'At least one image is needed' in str(ex.value) m = Movie(dt=1.0 / 14, height_ratio=1.5) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') a = Animation(m) a._init_draw() assert a.n_axes == 0 assert a.n_images == 1 m = Movie(dt=1.0 / 14, height_ratio=1.5) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') m.add_image(img, style='dark_img') a = Animation(m) a._init_draw() assert a.n_axes == 0 assert a.n_images == 2 m = Movie(dt=1.0 / 14, height_ratio=1.5) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') m.add_axis('x', 'y') with pytest.raises(RuntimeError) as ex: a = Animation(m) a._init_draw() assert 'Axis 0 with no traces' in str(ex.value) m = Movie(dt=1.0 / 14, height_ratio=1.5) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img') m.add_axis('x', 'y') m.add_trace(np.arange(4)) a = Animation(m) a._init_draw() assert a.n_axes == 1 assert a.n_images == 1
def test_fail_type(): with pytest.raises(ValueError) as ex: m = Movie() img = np.arange(100).reshape(4, 5, 5) m.add_image(img, animation_type='fail') assert 'animation type should be movie or window' in str(ex.value)
def test_set_ylim_image(): m = Movie(dt=1.0 / 14) img = np.arange(100).reshape(4, 5, 5) m.add_image(img, style='dark_img', ylim_type='p_top', ylim_value=50) m_img = m.images[-1] assert m_img['ymin'] == 0 assert np.isclose(m_img['ymax'], 49.5) m.add_image(img, style='dark_img', ylim_type='p_bottom', ylim_value=50) m_img = m.images[-1] assert np.isclose(m_img['ymin'], 49.5) assert m_img['ymax'] == 99 m.add_image(img, style='dark_img', ylim_type='p_both', ylim_value=10) m_img = m.images[-1] assert np.isclose(m_img['ymin'], 9.9) assert np.isclose(m_img['ymax'], 89.1) m.add_image(img, style='dark_img', ylim_type='set', ylim_value=(2, 10)) m_img = m.images[-1] assert m_img['ymin'] == 2 assert m_img['ymax'] == 10 with pytest.raises(RuntimeError) as ex: m.add_image(img, style='dark_img', ylim_type='set', ylim_value=10) assert 'ylim type set to set but len of ylim_value is not len 2' in str( ex.value) m.add_image(img, style='dark_img', ylim_type='same', ylim_value=len(m.images) - 1) m_img = m.images[-1] assert m_img['ymin'] == 2 assert m_img['ymax'] == 10 with pytest.raises(RuntimeError) as ex: m.add_image(img, style='dark_img', ylim_type='same', ylim_value=len(m.images)) assert 'Tried to have same y' in str(ex.value) with pytest.raises(RuntimeError) as ex: m.add_image(img, style='dark_img', ylim_type='error', ylim_value=len(m.images)) assert "Expected 'p_top'" in str(ex.value)