|
| 1 | +"""Tests for numpy array read/write and memory-based processing flow (issue #294).""" |
| 2 | +import os |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | +import musicalgestures |
| 6 | +from musicalgestures._utils import extract_subclip |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture(scope="module") |
| 10 | +def testvideo_avi(tmp_path_factory): |
| 11 | + target_name = os.path.join(str(tmp_path_factory.mktemp("data")), "testvideo.avi") |
| 12 | + return extract_subclip(musicalgestures.examples.dance, 5, 6, target_name=target_name) |
| 13 | + |
| 14 | + |
| 15 | +class Test_MgVideo_numpy: |
| 16 | + """Tests for MgVideo.numpy() – read video frames as numpy array.""" |
| 17 | + |
| 18 | + def test_returns_tuple(self, testvideo_avi): |
| 19 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 20 | + result = mg.numpy() |
| 21 | + assert isinstance(result, tuple) |
| 22 | + assert len(result) == 2 |
| 23 | + |
| 24 | + def test_array_shape(self, testvideo_avi): |
| 25 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 26 | + array, fps = mg.numpy() |
| 27 | + # shape should be (N_frames, height, width, 3) |
| 28 | + assert array.ndim == 4 |
| 29 | + assert array.shape[1] == mg.height |
| 30 | + assert array.shape[2] == mg.width |
| 31 | + assert array.shape[3] == 3 |
| 32 | + |
| 33 | + def test_array_dtype(self, testvideo_avi): |
| 34 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 35 | + array, fps = mg.numpy() |
| 36 | + assert array.dtype == np.uint8 |
| 37 | + |
| 38 | + def test_fps_matches(self, testvideo_avi): |
| 39 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 40 | + array, fps = mg.numpy() |
| 41 | + assert fps == mg.fps |
| 42 | + |
| 43 | + def test_frame_count(self, testvideo_avi): |
| 44 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 45 | + array, fps = mg.numpy() |
| 46 | + from musicalgestures._utils import get_framecount |
| 47 | + expected_frames = get_framecount(testvideo_avi) |
| 48 | + assert array.shape[0] == expected_frames |
| 49 | + |
| 50 | + |
| 51 | +class Test_MgAudio_numpy: |
| 52 | + """Tests for MgAudio.numpy() – read audio as numpy array.""" |
| 53 | + |
| 54 | + def test_returns_array(self, testvideo_avi): |
| 55 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 56 | + result = mg.audio.numpy() |
| 57 | + assert isinstance(result, np.ndarray) |
| 58 | + |
| 59 | + def test_array_1d(self, testvideo_avi): |
| 60 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 61 | + result = mg.audio.numpy() |
| 62 | + assert result.ndim == 1 |
| 63 | + |
| 64 | + def test_sample_rate_set(self, testvideo_avi): |
| 65 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 66 | + mg.audio.numpy() |
| 67 | + assert mg.audio.sr > 0 |
| 68 | + |
| 69 | + def test_array_length_matches_duration(self, testvideo_avi): |
| 70 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 71 | + result = mg.audio.numpy() |
| 72 | + # Audio duration = n_samples / sr, should be roughly 1 second (we extracted 5-6 s) |
| 73 | + duration = len(result) / mg.audio.sr |
| 74 | + assert 0.5 < duration < 2.0 |
| 75 | + |
| 76 | + |
| 77 | +class Test_MgVideo_from_numpy: |
| 78 | + """Tests for creating MgVideo from a numpy array (via __init__ array parameter).""" |
| 79 | + |
| 80 | + def test_init_with_array_no_path(self, testvideo_avi, tmp_path): |
| 81 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 82 | + array, fps = mg.numpy() |
| 83 | + out_file = str(tmp_path / "from_arr.avi") |
| 84 | + new_mg = musicalgestures.MgVideo( |
| 85 | + filename=out_file, |
| 86 | + array=array[:30], |
| 87 | + fps=fps, |
| 88 | + ) |
| 89 | + assert os.path.isfile(new_mg.filename) |
| 90 | + assert new_mg.fps == fps |
| 91 | + assert new_mg.width == array.shape[2] |
| 92 | + assert new_mg.height == array.shape[1] |
| 93 | + |
| 94 | + def test_init_with_array_and_path(self, testvideo_avi, tmp_path): |
| 95 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 96 | + array, fps = mg.numpy() |
| 97 | + new_mg = musicalgestures.MgVideo( |
| 98 | + filename="arr_output.avi", |
| 99 | + array=array[:30], |
| 100 | + fps=fps, |
| 101 | + path=str(tmp_path), |
| 102 | + ) |
| 103 | + expected_path = os.path.join(str(tmp_path), "arr_output.avi") |
| 104 | + assert new_mg.filename == expected_path |
| 105 | + assert os.path.isfile(new_mg.filename) |
| 106 | + |
| 107 | + def test_from_numpy_direct_call(self, testvideo_avi, tmp_path): |
| 108 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 109 | + array, fps = mg.numpy() |
| 110 | + target = str(tmp_path / "direct.avi") |
| 111 | + mg.from_numpy(array[:30], fps, target_name=target) |
| 112 | + assert os.path.isfile(target) |
| 113 | + |
| 114 | + def test_roundtrip_frame_count(self, testvideo_avi, tmp_path): |
| 115 | + """Array written to disk should have the same number of frames.""" |
| 116 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 117 | + array, fps = mg.numpy() |
| 118 | + n_frames = 20 |
| 119 | + out_file = str(tmp_path / "roundtrip.avi") |
| 120 | + new_mg = musicalgestures.MgVideo( |
| 121 | + filename=out_file, |
| 122 | + array=array[:n_frames], |
| 123 | + fps=fps, |
| 124 | + ) |
| 125 | + from musicalgestures._utils import get_framecount |
| 126 | + assert get_framecount(new_mg.filename) == n_frames |
| 127 | + |
| 128 | + |
| 129 | +class Test_mg_grid_return_array: |
| 130 | + """Tests for mg_grid() memory-based flow (return_array=True).""" |
| 131 | + |
| 132 | + def test_return_array_type(self, testvideo_avi): |
| 133 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 134 | + result = mg.grid(height=100, rows=2, cols=2, return_array=True) |
| 135 | + assert isinstance(result, np.ndarray) |
| 136 | + |
| 137 | + def test_return_array_dtype(self, testvideo_avi): |
| 138 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 139 | + result = mg.grid(height=100, rows=2, cols=2, return_array=True) |
| 140 | + assert result.dtype == np.uint8 |
| 141 | + |
| 142 | + def test_return_array_shape(self, testvideo_avi): |
| 143 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 144 | + rows, cols, height = 2, 3, 100 |
| 145 | + result = mg.grid(height=height, rows=rows, cols=cols, return_array=True) |
| 146 | + assert result.ndim == 3 |
| 147 | + assert result.shape[0] == height * rows |
| 148 | + assert result.shape[2] == 3 # RGB channels |
| 149 | + |
| 150 | + def test_no_file_written(self, testvideo_avi, tmp_path): |
| 151 | + """return_array=True should not write any file to disk.""" |
| 152 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 153 | + of = os.path.splitext(testvideo_avi)[0] |
| 154 | + expected_file = of + "_grid.png" |
| 155 | + if os.path.exists(expected_file): |
| 156 | + os.remove(expected_file) |
| 157 | + mg.grid(height=100, rows=2, cols=2, return_array=True) |
| 158 | + assert not os.path.exists(expected_file) |
| 159 | + |
| 160 | + def test_return_mgimage_when_no_array(self, testvideo_avi): |
| 161 | + mg = musicalgestures.MgVideo(testvideo_avi) |
| 162 | + result = mg.grid(height=100, rows=2, cols=2, return_array=False) |
| 163 | + assert isinstance(result, musicalgestures.MgImage) |
| 164 | + assert os.path.isfile(result.filename) |
0 commit comments