|
| 1 | +"""Tests for ephys.extra_features_utils""" |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +import numpy |
| 6 | +import pytest |
| 7 | + |
| 8 | +from bluepyopt import ephys |
| 9 | + |
| 10 | + |
| 11 | +testdata_dir = os.path.join( |
| 12 | + os.path.dirname(os.path.abspath(__file__)), 'testdata' |
| 13 | +) |
| 14 | +waveforms_fpath = os.path.join(testdata_dir, 'mean_waveforms.dat') |
| 15 | +waveforms = numpy.loadtxt(waveforms_fpath) |
| 16 | +waveform = numpy.array([waveforms[0]]) |
| 17 | +sampling_freq = 10000 |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.unit |
| 21 | +def test_peak_to_valley(): |
| 22 | + """ephys.extra_features_utils: Test peak_to_valley""" |
| 23 | + ptv = ephys.extra_features_utils.peak_to_valley(waveform, sampling_freq) |
| 24 | + assert len(ptv) == 1 |
| 25 | + assert ptv[0] == pytest.approx(0.0013) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.unit |
| 29 | +def test_peak_trough_ratio(): |
| 30 | + """ephys.extra_features_utils: Test peak_trough_ratio""" |
| 31 | + ptratio = ephys.extra_features_utils.peak_trough_ratio(waveform) |
| 32 | + assert len(ptratio) == 1 |
| 33 | + print(ptratio) |
| 34 | + assert ptratio[0] == pytest.approx(0.53804035) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.unit |
| 38 | +def test_halfwidth(): |
| 39 | + """ephys.extra_features_utils: Test halfwidth""" |
| 40 | + ret = ephys.extra_features_utils.halfwidth(waveform, sampling_freq, True) |
| 41 | + assert len(ret) == 3 |
| 42 | + |
| 43 | + hw = ephys.extra_features_utils.halfwidth(waveform, sampling_freq) |
| 44 | + assert len(hw) == 1 |
| 45 | + assert hw[0] == pytest.approx(0.0015) |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.unit |
| 49 | +def test_repolarization_slope(): |
| 50 | + """ephys.extra_features_utils: Test repolarization_slope""" |
| 51 | + ret = ephys.extra_features_utils.repolarization_slope( |
| 52 | + waveform, sampling_freq, True |
| 53 | + ) |
| 54 | + assert len(ret) == 2 |
| 55 | + |
| 56 | + rslope = ephys.extra_features_utils.repolarization_slope( |
| 57 | + waveform, sampling_freq |
| 58 | + ) |
| 59 | + assert len(rslope) == 1 |
| 60 | + assert rslope[0] == pytest.approx(73.12572131) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.unit |
| 64 | +def test_recovery_slope(): |
| 65 | + """ephys.extra_features_utils: Test recovery_slope""" |
| 66 | + window = 0.7 |
| 67 | + rslope = ephys.extra_features_utils.recovery_slope( |
| 68 | + waveform, sampling_freq, window=window |
| 69 | + ) |
| 70 | + assert len(rslope) == 1 |
| 71 | + assert rslope[0] == pytest.approx(-3.63355521) |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.unit |
| 75 | +def test_peak_image(): |
| 76 | + """ephys.extra_features_utils: Test peak_image""" |
| 77 | + rel_peaks = ephys.extra_features_utils.peak_image( |
| 78 | + waveforms, sign="negative" |
| 79 | + ) |
| 80 | + assert len(rel_peaks) == 209 |
| 81 | + assert rel_peaks[0] == pytest.approx(0.06084468) |
| 82 | + |
| 83 | + rel_peaks = ephys.extra_features_utils.peak_image( |
| 84 | + waveforms, sign="positive" |
| 85 | + ) |
| 86 | + assert len(rel_peaks) == 209 |
| 87 | + assert rel_peaks[0] == pytest.approx(0.10850117) |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.unit |
| 91 | +def test_relative_amplitude(): |
| 92 | + """ephys.extra_features_utils: Test relative_amplitude""" |
| 93 | + rel_amp = ephys.extra_features_utils.relative_amplitude( |
| 94 | + waveforms, sign="negative" |
| 95 | + ) |
| 96 | + assert len(rel_amp) == 209 |
| 97 | + assert rel_amp[0] == pytest.approx(0.09513392) |
| 98 | + |
| 99 | + rel_amp = ephys.extra_features_utils.relative_amplitude( |
| 100 | + waveforms, sign="positive" |
| 101 | + ) |
| 102 | + assert len(rel_amp) == 209 |
| 103 | + assert rel_amp[0] == pytest.approx(0.2135929) |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.unit |
| 107 | +def test_peak_time_diff(): |
| 108 | + """ephys.extra_features_utils: Test peak_time_diff""" |
| 109 | + peak_t = ephys.extra_features_utils.peak_time_diff( |
| 110 | + waveforms, sampling_freq, sign="negative" |
| 111 | + ) |
| 112 | + assert len(peak_t) == 209 |
| 113 | + assert peak_t[0] == pytest.approx(0.0009) |
| 114 | + |
| 115 | + peak_t = ephys.extra_features_utils.peak_time_diff( |
| 116 | + waveforms, sampling_freq, sign="positive" |
| 117 | + ) |
| 118 | + assert len(peak_t) == 209 |
| 119 | + assert peak_t[0] == pytest.approx(0.0007) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.unit |
| 123 | +def test__get_trough_and_peak_idx(): |
| 124 | + """ephys.extra_features_utils: Test _get_trough_and_peak_idx""" |
| 125 | + t_idx, p_idx = ephys.extra_features_utils._get_trough_and_peak_idx( |
| 126 | + waveform |
| 127 | + ) |
| 128 | + assert t_idx == 102 |
| 129 | + assert p_idx == 115 |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.unit |
| 133 | +def test_calculate_features(): |
| 134 | + """ephys.extra_features_utils: Test calculate_features""" |
| 135 | + feats = ephys.extra_features_utils.calculate_features( |
| 136 | + waveforms, sampling_freq |
| 137 | + ) |
| 138 | + for feature_name in ephys.extra_features_utils.all_1D_features: |
| 139 | + assert feature_name in feats |
0 commit comments