|
| 1 | +"""Tests for the utils module.""" |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | + |
| 5 | +from cubids import utils |
| 6 | +from cubids.tests.utils import compare_group_assignments |
| 7 | + |
| 8 | + |
| 9 | +def test_round_params(): |
| 10 | + """Test the cubids.utils.round_params function.""" |
| 11 | + # Example DataFrame |
| 12 | + df = pd.DataFrame( |
| 13 | + { |
| 14 | + "A": [1.12345, 2.23456, 3.34567], |
| 15 | + "B": [[1.12345, 2.23456], [3.34567, 4.45678], [5.56789, 6.67890]], |
| 16 | + "C": ["text", "more text", "even more text"], |
| 17 | + "D": [1.12345, 2.23456, 3.34567], |
| 18 | + } |
| 19 | + ) |
| 20 | + |
| 21 | + # Example config |
| 22 | + config = { |
| 23 | + "sidecar_params": { |
| 24 | + "func": { |
| 25 | + "A": {"precision": 2}, |
| 26 | + "B": {"precision": 2}, |
| 27 | + }, |
| 28 | + }, |
| 29 | + "derived_params": { |
| 30 | + "func": {}, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + # Expected DataFrame after rounding |
| 35 | + expected_df = pd.DataFrame( |
| 36 | + { |
| 37 | + "A": [1.12, 2.23, 3.35], |
| 38 | + "B": [[1.12, 2.23], [3.35, 4.46], [5.57, 6.68]], |
| 39 | + "C": ["text", "more text", "even more text"], |
| 40 | + "D": [1.12345, 2.23456, 3.34567], |
| 41 | + } |
| 42 | + ) |
| 43 | + |
| 44 | + # Round columns |
| 45 | + rounded_df = utils.round_params(df, config, "func") |
| 46 | + |
| 47 | + # Assert that the rounded DataFrame matches the expected DataFrame |
| 48 | + pd.testing.assert_frame_equal(rounded_df, expected_df) |
| 49 | + |
| 50 | + |
| 51 | +def test_cluster_single_parameters(): |
| 52 | + """Test the cubids.utils.cluster_single_parameters function. |
| 53 | +
|
| 54 | + We want to test that the function correctly clusters parameters based on the |
| 55 | + configuration dictionary. |
| 56 | + """ |
| 57 | + config = { |
| 58 | + "sidecar_params": { |
| 59 | + "func": { |
| 60 | + "RepetitionTime": {"tolerance": 0.01, "suggest_variant_rename": True}, |
| 61 | + "TaskName": {"suggest_variant_rename": True}, |
| 62 | + "SliceTiming": {"tolerance": 0.01, "suggest_variant_rename": True}, |
| 63 | + "ImageType": {"suggest_variant_rename": True}, |
| 64 | + }, |
| 65 | + }, |
| 66 | + "derived_params": { |
| 67 | + "func": {}, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + # Mock up the input. The variants are explicitly prepared. |
| 72 | + params = [ |
| 73 | + { |
| 74 | + "RepetitionTime": 2.0, |
| 75 | + "TaskName": "rest eyes closed", |
| 76 | + "SliceTiming": [0.0, 1.0, 2.0], |
| 77 | + "ImageType": ["ORIGINAL", "NONE", "M"], |
| 78 | + }, |
| 79 | + { |
| 80 | + "RepetitionTime": 2.0, |
| 81 | + "TaskName": "rest eyes closed", |
| 82 | + "SliceTiming": [0.0, 1.0, 2.0], |
| 83 | + "ImageType": ["ORIGINAL", "NONE", "M"], |
| 84 | + }, |
| 85 | + { |
| 86 | + "RepetitionTime": 2.0, |
| 87 | + # TaskName variant |
| 88 | + "TaskName": "rest eyes open", |
| 89 | + "SliceTiming": [0.0, 1.0, 2.0], |
| 90 | + "ImageType": ["ORIGINAL", "NONE", "M"], |
| 91 | + }, |
| 92 | + { |
| 93 | + # RepetitionTime variant |
| 94 | + "RepetitionTime": 1.9, |
| 95 | + "TaskName": "rest eyes closed", |
| 96 | + "SliceTiming": [0.0, 1.0, 2.0], |
| 97 | + "ImageType": ["ORIGINAL", "NONE", "M"], |
| 98 | + }, |
| 99 | + { |
| 100 | + "RepetitionTime": 2.0, |
| 101 | + "TaskName": "rest eyes closed", |
| 102 | + # SliceTiming variant (length) |
| 103 | + "SliceTiming": [0.0, 0.5, 1.0, 1.5, 2.0], |
| 104 | + "ImageType": ["ORIGINAL", "NONE", "M"], |
| 105 | + }, |
| 106 | + { |
| 107 | + "RepetitionTime": 2.0, |
| 108 | + "TaskName": "rest eyes closed", |
| 109 | + # SliceTiming variant (values) |
| 110 | + "SliceTiming": [0.0, 1.0, 1.9], |
| 111 | + "ImageType": ["ORIGINAL", "NONE", "M"], |
| 112 | + }, |
| 113 | + { |
| 114 | + "RepetitionTime": 2.0, |
| 115 | + "TaskName": "rest eyes closed", |
| 116 | + "SliceTiming": [0.0, 1.0, 2.0], |
| 117 | + # ImageType variant (length) |
| 118 | + "ImageType": ["ORIGINAL", "NONE", "M", "NORM"], |
| 119 | + }, |
| 120 | + { |
| 121 | + "RepetitionTime": 2.0, |
| 122 | + "TaskName": "rest eyes closed", |
| 123 | + "SliceTiming": [0.0, 1.0, 2.0], |
| 124 | + # ImageType variant (values) |
| 125 | + "ImageType": ["ORIGINAL", "NONE", "P"], |
| 126 | + }, |
| 127 | + ] |
| 128 | + files_df = pd.DataFrame(params) |
| 129 | + modality = "func" |
| 130 | + |
| 131 | + # Run the function |
| 132 | + out_df = utils.cluster_single_parameters( |
| 133 | + df=files_df, |
| 134 | + config=config, |
| 135 | + modality=modality, |
| 136 | + ) |
| 137 | + assert isinstance(out_df, pd.DataFrame) |
| 138 | + assert "Cluster_RepetitionTime" in out_df.columns |
| 139 | + assert "Cluster_SliceTiming" in out_df.columns |
| 140 | + assert "Cluster_ImageType" in out_df.columns |
| 141 | + # Non-list columns without tolerance don't get clustered |
| 142 | + assert "Cluster_TaskName" not in out_df.columns |
| 143 | + |
| 144 | + assert compare_group_assignments( |
| 145 | + out_df["Cluster_RepetitionTime"].values.astype(int), |
| 146 | + [0, 0, 0, 1, 0, 0, 0, 0], |
| 147 | + ) |
| 148 | + assert compare_group_assignments( |
| 149 | + out_df["Cluster_SliceTiming"].values.astype(int), |
| 150 | + [0, 0, 0, 0, 2, 1, 0, 0], |
| 151 | + ) |
| 152 | + assert compare_group_assignments( |
| 153 | + out_df["Cluster_ImageType"].values.astype(int), |
| 154 | + [0, 0, 0, 0, 0, 0, 1, 2], |
| 155 | + ) |
| 156 | + |
| 157 | + # Change the tolerance for SliceTiming |
| 158 | + config["sidecar_params"]["func"]["SliceTiming"]["tolerance"] = 0.5 |
| 159 | + out_df = utils.cluster_single_parameters( |
| 160 | + df=files_df, |
| 161 | + config=config, |
| 162 | + modality=modality, |
| 163 | + ) |
| 164 | + assert isinstance(out_df, pd.DataFrame) |
| 165 | + assert "Cluster_RepetitionTime" in out_df.columns |
| 166 | + assert "Cluster_SliceTiming" in out_df.columns |
| 167 | + assert "Cluster_ImageType" in out_df.columns |
| 168 | + # Non-list columns without tolerance don't get clustered |
| 169 | + assert "Cluster_TaskName" not in out_df.columns |
| 170 | + |
| 171 | + assert compare_group_assignments( |
| 172 | + out_df["Cluster_RepetitionTime"].values.astype(int), |
| 173 | + [0, 0, 0, 1, 0, 0, 0, 0], |
| 174 | + ) |
| 175 | + # Different lengths still produce different clusters, |
| 176 | + # but the value-based variants are now the same |
| 177 | + assert compare_group_assignments( |
| 178 | + out_df["Cluster_SliceTiming"].values.astype(int), |
| 179 | + [0, 0, 0, 0, 1, 0, 0, 0], |
| 180 | + ) |
| 181 | + assert compare_group_assignments( |
| 182 | + out_df["Cluster_ImageType"].values.astype(int), |
| 183 | + [0, 0, 0, 0, 0, 0, 1, 2], |
| 184 | + ) |
0 commit comments