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_label(): m = Movie(dt=1.0 / 14, height_ratio=2) m.add_label(1, 2, [1, 2], color='blue') assert m.labels[0]['x'] == 1 assert m.labels[0]['y'] == 2 assert m.labels[0]['values'] == [1, 2] assert m.labels[0]['axis'] == 0 assert m.labels[0]['s_format'] == '%s' assert m.labels[0]['size'] == 14 kwargs = m.labels[0]['kwargs'] assert kwargs['color'] == 'blue'
def test_defaults(): m = Movie() m.add_annotation(axis=0, xy=(0, 1), xy_text=(2, 3), text='4', color='blue') a = m.annotations[0] assert a['type'] == 'annotation' assert a['axis'] == 0 assert a['text'] == '4' assert a['xy'] == (0, 1) assert a['xy_text'] == (2, 3) assert a['axis_type'] == 'image' kwargs = a['kwargs'] assert kwargs['color'] == 'blue'
def test_circle(): m = Movie() m.add_circle_annotation(axis=0, x=0, y=1, radius=3, color='blue') a = m.annotations[0] assert a['type'] == 'circle' assert a['axis'] == 0 assert a['x'] == 0 assert a['y'] == 1 assert a['radius'] == 3 assert a['axis_type'] == 'image' kwargs = a['kwargs'] assert kwargs['color'] == 'blue'
def test_trace(): with pytest.raises(RuntimeError) as ex: m = Movie() m.add_trace(data=[1, 2, 3]) assert 'Please create axis ' in str(ex.value) m = Movie() m.add_axis(x_label='x', y_label='y') m.add_trace(data=[1, 2, 3], color='blue') trace = m.traces[0] assert trace['axis'] == 0 assert np.allclose(trace['data'], [1, 2, 3]) assert trace['kwargs']['color'] == 'blue'
def test_set_ylim_axis_two(): 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_top', ylim_value=10) m.add_trace(data) m.add_trace(np.arange(10, 20)) a = Animation(m) a._init_draw() ax = a.trace_axes[-1] y_min, y_max = ax.get_ylim() assert np.isclose(y_min, 0) assert np.isclose(y_max, 17.1)
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_scale_bar_kwargs(): m = Movie() m.add_scale_bar(text_kwargs={'color': 'blue'}, line_kwargs={'color': 'blue'}) line = m.annotations[0] assert line['axis'] == 0 assert line['x'] == (0, 40) assert line['y'] == (2, 2) line_kwargs = line['kwargs'] assert line_kwargs['color'] == 'blue' text = m.annotations[1] assert text['axis'] == 0 assert text['x'] == 20 assert text['y'] == 1 assert text['text'] == '20um' text_kwargs = text['kwargs'] assert text_kwargs['color'] == 'blue'
def test_fail_axis(): with pytest.raises(ValueError) as ex: m = Movie() m.add_annotation(axis='1', xy=(0, 1), xy_text=(2, 3), text='') assert 'Axis must be an integer got' in str(ex.value) with pytest.raises(ValueError) as ex: m = Movie() m.add_annotation(axis=-1, xy=(0, 1), xy_text=(2, 3), text='') assert 'Axis must be positive got' 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_scale_bar_fail(): with pytest.raises(ValueError) as ex: m = Movie() m.add_scale_bar(axis=0, pixel_width=40, um_width='20', y=2, text_offset=1, line_kwargs=(1,), text_kwargs=None) assert 'line_kwargs should be a dictionary' in str(ex.value) with pytest.raises(ValueError) as ex: m = Movie() m.add_scale_bar(axis=0, pixel_width=40, um_width='20', y=2, text_offset=1, line_kwargs={'color': 'blue'}, text_kwargs=[2]) assert 'text_kwargs should be a dictionary' 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_time_label_axis(): m = Movie(dt=1.0 / 14, height_ratio=2) m.add_axis(x_label='x', y_label='y') m.add_trace(data=np.arange(10)) 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(10) / 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_scale_bar_default(): m = Movie() m.add_scale_bar() line = m.annotations[0] assert line['axis'] == 0 assert line['x'] == (0, 40) assert line['y'] == (2, 2) line_kwargs = line['kwargs'] assert line_kwargs['color'] == 'white' assert line_kwargs['lw'] == 3 text = m.annotations[1] assert text['axis'] == 0 assert text['x'] == 20 assert text['y'] == 1 assert text['text'] == '20um' text_kwargs = text['kwargs'] assert text_kwargs['ha'] == 'center' assert text_kwargs['fontsize'] == 14 assert text_kwargs['color'] == 'white'
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_text(): with pytest.raises(ValueError) as ex: m = Movie() m.add_text_annotation(axis=0, x=0, y=1, text=3) assert 'text should be a string' in str(ex.value) m = Movie() m.add_text_annotation(axis=0, x=0, y=1, text='3', color='blue') a = m.annotations[0] assert a['type'] == 'text' assert a['axis'] == 0 assert a['x'] == 0 assert a['y'] == 1 assert a['text'] == '3' assert a['axis_type'] == 'image' kwargs = a['kwargs'] assert kwargs['color'] == 'blue'
def test_axis(): with pytest.raises(ValueError) as ex: m = Movie() m.add_axis(x_label='x', y_label='y', tight_x='1') assert 'tight_x should be a boolean' in str(ex.value) m = Movie() m.add_axis(x_label='x', y_label='y') axis = m.axes[0] assert axis['x_label'] == 'x' assert axis['y_label'] == 'y' assert axis['style'] == 'dark_trace' assert axis['running_line'] == {'color': 'white', 'lw': 2} assert axis['bottom_left_ticks'] assert axis['ylim_type'] == 'p_top' assert axis['ylim_value'] == 0.1 assert axis['tight_x'] assert axis['label_kwargs'] == {'fontsize': 16} assert axis['legend_kwargs'] == {'frameon': False}
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_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_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)
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_fail_positions(): with pytest.raises(ValueError) as ex: m = Movie() m.add_annotation(axis=0, xy=(0,), xy_text=(2, 3), text='') assert 'xy should be an iterable' in str(ex.value) with pytest.raises(ValueError) as ex: m = Movie() m.add_annotation(axis=0, xy='123', xy_text=(2, 3), text='') assert 'xy should be an iterable' in str(ex.value) with pytest.raises(ValueError) as ex: m = Movie() m.add_annotation(axis=0, xy=(0, 1), xy_text=(2,), text='') assert 'xy_text should be an iterable' in str(ex.value) with pytest.raises(ValueError) as ex: m = Movie() m.add_annotation(axis=0, xy=(0, 1), xy_text='123', text='') assert 'xy_text should be an iterable' in str(ex.value)
def test_fail_axis_type(): with pytest.raises(ValueError) as ex: m = Movie() m.add_annotation(axis=0, xy=(0, 1), xy_text=(2, 3), text='', axis_type='fail') assert 'axis_type should be image or trace' in str(ex.value)
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_rectangle(): with pytest.raises(ValueError) as ex: m = Movie() m.add_rectangle_annotation(axis=0, xy=(0, 1), width='1', height=2, angle=0) assert 'width should be a number' in str(ex.value) with pytest.raises(ValueError) as ex: m = Movie() m.add_rectangle_annotation(axis=0, xy=(0, 1), width=1, height='2', angle=0) assert 'height should be a number' in str(ex.value) with pytest.raises(ValueError) as ex: m = Movie() m.add_rectangle_annotation(axis=0, xy=(0, 1), width=1, height=2, angle='0') assert 'angle should be a number' in str(ex.value) m = Movie() m.add_rectangle_annotation(axis=0, xy=(0, 1), width=1, height=2, angle=0, color='blue') a = m.annotations[0] assert a['type'] == 'rectangle' assert a['axis'] == 0 assert a['xy'] == (0, 1) assert a['width'] == 1 assert a['height'] == 2 assert a['angle'] == 0 assert a['axis_type'] == 'image' kwargs = a['kwargs'] assert kwargs['color'] == 'blue'
def test_line(): with pytest.raises(ValueError) as ex: m = Movie() m.add_line_annotation(axis=0, x='1', y=[0, 1]) assert 'x should be an iterable of locations' in str(ex.value) with pytest.raises(ValueError) as ex: m = Movie() m.add_line_annotation(axis=0, x=(1, 2), y={'1': 2}) assert 'y should be an iterable of locations' in str(ex.value) m = Movie() m.add_line_annotation(axis=0, x=(1, 2), y=np.array([2, 3]), color='blue') a = m.annotations[0] assert a['type'] == 'line' assert a['axis'] == 0 assert a['x'] == (1, 2) assert np.allclose(a['y'], np.array([2, 3])) assert a['axis_type'] == 'image' kwargs = a['kwargs'] assert kwargs['color'] == 'blue'
def test_time_label_fail(): m = Movie(dt=1.0 / 14, height_ratio=2) with pytest.raises(RuntimeError) as ex: m.add_time_label() assert 'Can not add time labels' in str(ex.value)