|
import numpy as np |
|
import pytest |
|
from utils import filter_and_average_mst, verify_image_patterns, compute_vox_rels, compute_avg_repeat_corrs |
|
|
|
|
|
|
|
def test_no_mst_images(): |
|
vox = np.array([[1,2,3], [4,5,6], [7,8,9]]) |
|
vox_image_dict = {0: 'image1.jpg', 1: 'image2.jpg', 2: 'image3.jpg'} |
|
|
|
filtered_vox, kept_indices = filter_and_average_mst(vox, vox_image_dict) |
|
|
|
np.testing.assert_array_equal(filtered_vox, vox) |
|
np.testing.assert_array_equal(kept_indices, [0, 1, 2]) |
|
|
|
def test_single_mst_image_set(): |
|
vox = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]]) |
|
vox_image_dict = {0: 'image1.jpg', 1: 'MST_pairs/image2.jpg', 2: 'image3.jpg', 3: 'MST_pairs/image2.jpg'} |
|
|
|
filtered_vox, kept_indices = filter_and_average_mst(vox, vox_image_dict) |
|
|
|
expected_vox = np.array([[1,2,3], [7,8,9], [7,8,9]]) |
|
expected_indices = [0, 1, 2] |
|
|
|
np.testing.assert_array_equal(filtered_vox, expected_vox) |
|
np.testing.assert_array_equal(kept_indices, expected_indices) |
|
|
|
def test_multiple_mst_image_sets(): |
|
vox = np.array([[1,2,3], [4,5,6], [7,8,9], [7,8,9], [10,11,12], [12,15,12]]) |
|
vox_image_dict = { |
|
0: 'image1.jpg', |
|
1: 'MST_pairs/image2.jpg', |
|
2: 'image3.jpg', |
|
3: 'MST_pairs/image2.jpg', |
|
4: 'MST_pairs/image4.jpg', |
|
5: 'MST_pairs/image4.jpg' |
|
} |
|
|
|
filtered_vox, kept_indices = filter_and_average_mst(vox, vox_image_dict) |
|
|
|
expected_vox = np.array([[1,2,3], [5.5, 6.5, 7.5], [7,8,9], [11,13,12]]) |
|
expected_indices = [0, 1, 2, 4] |
|
|
|
np.testing.assert_array_equal(filtered_vox, expected_vox) |
|
np.testing.assert_array_equal(kept_indices, expected_indices) |
|
|
|
def test_empty_input(): |
|
vox = np.array([]) |
|
vox_image_dict = {} |
|
|
|
filtered_vox, kept_indices = filter_and_average_mst(vox, vox_image_dict) |
|
|
|
assert len(filtered_vox) == 0 |
|
assert len(kept_indices) == 0 |
|
|
|
def test_input_shape(): |
|
vox = np.random.rand(5, 3) |
|
vox_image_dict = {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'} |
|
|
|
filtered_vox, _ = filter_and_average_mst(vox, vox_image_dict) |
|
|
|
assert filtered_vox.shape[1] == vox.shape[1] |
|
|
|
|
|
|
|
|
|
def test_valid_special515(): |
|
image_to_indices = { |
|
"all_stimuli/special515/image1.jpg": [[1, 2, 3], []], |
|
"all_stimuli/special515/image2.jpg": [[], [10, 11, 12]], |
|
} |
|
failures = verify_image_patterns(image_to_indices) |
|
assert failures == [] |
|
|
|
def test_invalid_special515(): |
|
image_to_indices = { |
|
"all_stimuli/special515/image1.jpg": [[1, 2], []], |
|
"all_stimuli/special515/image2.jpg": [[1, 2], [3]], |
|
} |
|
failures = verify_image_patterns(image_to_indices) |
|
assert len(failures) == 2 |
|
|
|
def test_valid_MST_pairs(): |
|
image_to_indices = { |
|
"all_stimuli/MST_pairs/image1.png": [[4, 5], [6, 7]], |
|
} |
|
failures = verify_image_patterns(image_to_indices) |
|
assert failures == [] |
|
|
|
def test_invalid_MST_pairs(): |
|
image_to_indices = { |
|
"all_stimuli/MST_pairs/image1.png": [[4, 5, 6], [7]], |
|
} |
|
failures = verify_image_patterns(image_to_indices) |
|
assert len(failures) == 1 |
|
|
|
def test_valid_other_images(): |
|
image_to_indices = { |
|
"all_stimuli/other/image1.png": [[123], []], |
|
"all_stimuli/other/image2.png": [[], [456]], |
|
} |
|
failures = verify_image_patterns(image_to_indices) |
|
assert failures == [] |
|
|
|
def test_invalid_other_images(): |
|
image_to_indices = { |
|
"all_stimuli/other/image1.png": [[123, 124], []], |
|
"all_stimuli/other/image2.png": [[123], [456]], |
|
} |
|
failures = verify_image_patterns(image_to_indices) |
|
assert len(failures) == 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_basic_case(): |
|
"""Test with 2 repeats and 2 voxels, with basic correlation""" |
|
vox_repeats = np.random.rand(30, 50) |
|
breakpoint() |
|
rels = compute_avg_repeat_corrs(vox_repeats) |
|
|
|
|
|
assert rels.shape == (2,) |
|
|
|
|
|
assert np.all(np.isfinite(rels)) |
|
|
|
for v in range(2): |
|
expected_corr = np.corrcoef(vox_repeats[:, v])[0, 1] |
|
assert np.allclose(rels[v], expected_corr, atol=1e-5) |
|
|
|
def test_multiple_repeats(): |
|
"""Test with more repeats (3) and multiple voxels (3)""" |
|
vox_repeats = np.array([[1, 2, 3], [2, 3, 4], [3, 4, 5]]) |
|
rels = compute_avg_repeat_corrs(vox_repeats) |
|
|
|
assert rels.shape == (3,) |
|
for v in range(3): |
|
assert not np.isnan(rels[v]) |
|
|
|
def test_identical_repeats(): |
|
"""Test with all identical repeats (perfect correlation)""" |
|
vox_repeats = np.array([[1, 1], [1, 1]]) |
|
rels = compute_avg_repeat_corrs(vox_repeats) |
|
|
|
assert rels.shape == (2,) |
|
assert np.allclose(rels, 1) |
|
|
|
def test_anticorrelation(): |
|
"""Test with perfect anti-correlation (correlation = -1)""" |
|
vox_repeats = np.array([[1, 2], [2, 1]]) |
|
rels = compute_avg_repeat_corrs(vox_repeats) |
|
|
|
assert rels.shape == (2,) |
|
assert np.allclose(rels, -1) |
|
|
|
def test_zero_variance_repeats(): |
|
"""Test with repeats having zero variance (e.g., all values are the same)""" |
|
vox_repeats = np.array([[1, 1], [1, 1], [1, 1]]) |
|
rels = compute_avg_repeat_corrs(vox_repeats) |
|
|
|
assert rels.shape == (2,) |
|
|
|
assert np.all(np.isnan(rels)) |
|
|
|
def test_edge_case_two_repeats_and_one_voxel(): |
|
"""Test with only 2 repeats and 1 voxel (minimal edge case)""" |
|
vox_repeats = np.array([[1], [2]]) |
|
rels = compute_avg_repeat_corrs(vox_repeats) |
|
|
|
assert rels.shape == (1,) |
|
assert np.allclose(rels[0], np.corrcoef([1], [2])[1, 0]) |
|
|