CoolFace
Apppublic

JFoz/test_path_analysis

sourceHugging Facebsd-3-clauseupdated 3y agoView on Hugging Face
0likes
test_preprocess.py151 linesDownload Raw Back to tests
1from path_analysis.data_preprocess import *2import numpy as np3import pytest4 5 6def test_thin_points():7    # Define a sample point list8    points = [9        PeakData([0, 0, 0], 10, 0),10        PeakData([1, 1, 1], 8, 1),11        PeakData([10, 10, 10], 12, 2),12        PeakData([10.5, 10.5, 10.5], 5, 3),13        PeakData([20, 20, 20], 15, 4)14    ]15    16    # Call the thin_points function with dmin=5 (for example)17    removed_indices = thin_peaks(points, dmin=5)18    19    # Check results20    # Point at index 1 ([1, 1, 1]) should be removed since it's within 5 units distance of point at index 0 and has lower intensity.21    # Similarly, point at index 3 ([10.5, 10.5, 10.5]) should be removed as it's close to point at index 2 and has lower intensity.22    assert set(removed_indices) == {1, 3}23 24    # Another simple test to check if function does nothing when points are far apart25    far_points = [26        PeakData([0, 0, 0], 10, 0),27        PeakData([100, 100, 100], 12, 1),28        PeakData([200, 200, 200], 15, 2)29    ]30    31    removed_indices_far = thin_peaks(far_points, dmin=5)32    assert len(removed_indices_far) == 0  # Expect no points to be removed33 34 35def test_find_peaks2():36 37    # Basic test38    data = np.array([0, 0, 0, 0, 0, 0, 5, 0, 3, 0])39    peaks, _ = find_peaks2(data)40    assert set(peaks) == {6}  # Expected peaks at positions 641 42    # Basic test43    data = np.array([0, 2, 0, 0, 0, 0, 0, 0, 0, 0])44    peaks, _ = find_peaks2(data)45    assert set(peaks) == {1}  # Expected peaks at positions 146 47 48    # Test with padding impacting peak detection49    data = np.array([3, 2.9, 0, 0, 0, 3])50    peaks, _ = find_peaks2(data)51    assert set(peaks) == {0,5}  # Peaks at both ends52 53    # Test with close peaks54    data = np.array([3, 0, 3])55    peaks, _ = find_peaks2(data)56    assert set(peaks) == {2}  # Peak at right end only57    # Test with close peaks58    59    60    # Test with close peaks61    data = np.array([3, 0, 3])62    peaks, _ = find_peaks2(data, distance=1)63    assert set(peaks) == {0,2}  # Peaks at both ends64 65    # Test with close peaks66    data = np.array([0, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3])67    peaks, _ = find_peaks2(data, distance=1)68    assert set(peaks) == {2,7}  # Peak at centre (rounded to the left) of groups of maximum values69 70    # Test with prominence threshold71    data = np.array([0, 1, 0, 0.4, 0])72    peaks, _ = find_peaks2(data, prominence=0.5)73    assert peaks == [1]  # Only the peak at position 1 meets the prominence threshold74 75 76def test_focus_criterion():77    pos = np.array([0, 1, 2, 3, 4, 6])78    values = np.array([0.1, 0.5, 0.2, 0.8, 0.3, 0.9])79 80    # Basic test81    assert np.array_equal(focus_criterion(pos, values), np.array([1, 3, 6]))  # only values 0.8 and 0.9 exceed 0.4 times the max (which is 0.9)82 83    # Empty test84    assert np.array_equal(focus_criterion(np.array([]), np.array([])), np.array([]))85 86    # Test with custom alpha87    assert np.array_equal(focus_criterion(pos, values, alpha=0.5), np.array([1, 3, 6]))88 89    # Test with a larger alpha90    assert np.array_equal(focus_criterion(pos, values, alpha=1.0), [6])  # No values exceed the maximum value itself91 92    # Test with all values below threshold93    values = np.array([0.1, 0.2, 0.3, 0.4])94 95    assert np.array_equal(focus_criterion(pos[:4], values), [1,2,3])  # All values are below 0.4 times the max (which is 0.4)96 97@pytest.fixture98def mock_data():99    all_paths = [ [ (0,0,0), (0,2,0), (0,5,0), (0,10,0), (0,15,0), (0,20,0)], [ (1,20,0), (1,20,10), (1,20,20)  ] ] # Mock paths100    path_lengths = [ 2.2, 2.3 ]  # Mock path lengths101    measured_trace_fluorescence = [ [100, 8, 3, 2, 3, 49], [38, 2, 20] ]  # Mock fluorescence data102    return all_paths, path_lengths, measured_trace_fluorescence103 104def test_process_cell_traces_return_type(mock_data):105    all_paths, path_lengths, measured_trace_fluorescence = mock_data106    result = process_cell_traces(all_paths, path_lengths, measured_trace_fluorescence)107    assert isinstance(result, CellData), f"Expected CellData but got {type(result)}"108 109def test_process_cell_traces_pathdata_list_length(mock_data):110    all_paths, path_lengths, measured_trace_fluorescence = mock_data111    result = process_cell_traces(all_paths, path_lengths, measured_trace_fluorescence)112    assert len(result.pathdata_list) == len(all_paths), f"Expected {len(all_paths)} but got {len(result.pathdata_list)}"113    114def test_process_cell_traces_pathdata_path_lengths(mock_data):115    all_paths, path_lengths, measured_trace_fluorescence = mock_data116    result = process_cell_traces(all_paths, path_lengths, measured_trace_fluorescence)117    path_lengths = [p.SC_length for p in result.pathdata_list]118    expected_path_lengths = [2.2, 2.3]119    assert  path_lengths == expected_path_lengths, f"Expected {expected_path_lengths} but got {path_lengths}"120    121def test_process_cell_traces_peaks(mock_data):122    all_paths, path_lengths, measured_trace_fluorescence = mock_data123    result = process_cell_traces(all_paths, path_lengths, measured_trace_fluorescence)124    print(result)125    peaks = [p.peaks for p in result.pathdata_list]126    assert peaks == [[0,5],[]]127    128# Mock data129@pytest.fixture130def mock_celldata():131    pathdata1 = PathData(peaks=[0, 5], points=[(0,0,0), (0,2,0), (0,5,0), (0,10,0), (0,15,0), (0,20,0)], removed_peaks=[], o_intensity=[100, 8, 3, 2, 3, 69], SC_length=2.2)132    pathdata2 = PathData(peaks=[2], points=[(1,20,0), (1,20,10), (1,20,20) ], removed_peaks=[RemovedPeakData(0, (0,5))], o_intensity=[38, 2, 20], SC_length=2.3)133    return CellData(pathdata_list=[pathdata1, pathdata2])134 135def test_analyse_celldata(mock_celldata):136    data_frame, foci_absolute_intensity, foci_position_index, dominated_foci_data, trace_median_intensity, trace_thresholds = analyse_celldata(mock_celldata, {'peak_threshold': 0.4, 'threshold_type':'per-trace'})137    assert len(data_frame) == len(mock_celldata.pathdata_list), "Mismatch in dataframe length"138    assert len(foci_absolute_intensity) == len(mock_celldata.pathdata_list), "Mismatch in relative intensities length"139    assert len(foci_position_index) == len(mock_celldata.pathdata_list), "Mismatch in positions length"140 141    assert list(map(list, foci_position_index)) == [[0, 5], [2]]142 143 144def test_analyse_celldata_per_cell(mock_celldata):145    data_frame, foci_absolute_intensity, foci_position_index, dominated_foci_data, trace_median_intensity, trace_thresholds = analyse_celldata(mock_celldata, {'peak_threshold': 0.4, 'threshold_type':'per-cell'})146    assert len(data_frame) == len(mock_celldata.pathdata_list), "Mismatch in relative intensities length"147    assert len(foci_absolute_intensity) == len(mock_celldata.pathdata_list), "Mismatch in positions length"148    assert len(foci_position_index) == len(mock_celldata.pathdata_list), "Mismatch in position indices length"149    assert list(map(list, foci_position_index)) == [[0, 5], []]150 151