|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | +import pytest |
| 4 | + |
| 5 | +from voxelwise_tutorials import delays_toy |
| 6 | + |
| 7 | + |
| 8 | +def test_simulate_bold(): |
| 9 | + """Test that simulate_bold function works correctly""" |
| 10 | + # Create a simple stimulus array with an onset |
| 11 | + stimulus = np.zeros(50) |
| 12 | + stimulus[10:15] = 1 # activation for 5 time points |
| 13 | + |
| 14 | + # Test with default parameters |
| 15 | + bold = delays_toy.simulate_bold(stimulus) |
| 16 | + |
| 17 | + # Check that output has the same length as input |
| 18 | + assert len(bold) == len(stimulus) |
| 19 | + |
| 20 | + # Check that there is activation (values > 0) after stimulus onset |
| 21 | + assert np.any(bold[10:] > 0) |
| 22 | + |
| 23 | + # Test with different TR |
| 24 | + bold_tr1 = delays_toy.simulate_bold(stimulus, TR=1.0) |
| 25 | + assert len(bold_tr1) == len(stimulus) |
| 26 | + |
| 27 | + |
| 28 | +def test_create_voxel_data(): |
| 29 | + """Test that create_voxel_data function works correctly""" |
| 30 | + # Test with default parameters |
| 31 | + X, Y, times = delays_toy.create_voxel_data() |
| 32 | + |
| 33 | + # Check shapes |
| 34 | + assert X.shape == (50,) |
| 35 | + assert Y.shape == (50,) |
| 36 | + assert times.shape == (50,) |
| 37 | + |
| 38 | + # Check that activation happens at expected time |
| 39 | + onset_idx = int(30 / 2.0) # 30 seconds at TR=2.0 |
| 40 | + assert np.all( |
| 41 | + X[onset_idx : onset_idx + 5] == 1 |
| 42 | + ) # Should be active for 5 TRs (10s duration) |
| 43 | + |
| 44 | + # Test with different parameters |
| 45 | + X2, Y2, times2 = delays_toy.create_voxel_data( |
| 46 | + n_trs=100, TR=1.0, onset=20, duration=5 |
| 47 | + ) |
| 48 | + assert X2.shape == (100,) |
| 49 | + assert Y2.shape == (100,) |
| 50 | + assert times2.shape == (100,) |
| 51 | + |
| 52 | + # Test error cases |
| 53 | + with pytest.raises(ValueError): |
| 54 | + delays_toy.create_voxel_data(n_trs=50, onset=120) # onset > n_trs * TR |
| 55 | + |
| 56 | + with pytest.raises(ValueError): |
| 57 | + delays_toy.create_voxel_data(duration=-5) # negative duration |
| 58 | + |
| 59 | + |
| 60 | +def test_plot_delays_toy(): |
| 61 | + """Test that plot_delays_toy function works correctly""" |
| 62 | + # Create sample data |
| 63 | + X, Y, times = delays_toy.create_voxel_data() |
| 64 | + |
| 65 | + # Test with 1D array (single delay) |
| 66 | + axs = delays_toy.plot_delays_toy(X, Y, times) |
| 67 | + assert len(axs) == 2 # Should have 2 subplots |
| 68 | + plt.close() |
| 69 | + |
| 70 | + # Test with 2D array (multiple delays) |
| 71 | + X_delayed = np.column_stack([X, np.roll(X, 1), np.roll(X, 2)]) |
| 72 | + axs = delays_toy.plot_delays_toy(X_delayed, Y, times) |
| 73 | + assert len(axs) == 4 # Should have 4 subplots (1 for Y, 3 for X) |
| 74 | + plt.close() |
| 75 | + |
| 76 | + # Test with highlight |
| 77 | + axs = delays_toy.plot_delays_toy(X, Y, times, highlight=30) |
| 78 | + plt.close() |
0 commit comments