|
1 | 1 | """Test nimare.io (Dataset IO/transformations).""" |
2 | 2 |
|
| 3 | +import copy |
3 | 4 | import os |
4 | 5 |
|
5 | 6 | import pytest |
@@ -52,16 +53,107 @@ def test_convert_nimads_to_dataset_single_sample_size( |
52 | 53 | assert "sample_sizes" in dset.metadata.columns |
53 | 54 |
|
54 | 55 |
|
55 | | -def test_analysis_to_dict_invalid_sample_sizes_type(example_nimads_studyset): |
56 | | - """Test _analysis_to_dict raises ValueError when sample_sizes is not a list/tuple.""" |
| 56 | +@pytest.mark.parametrize( |
| 57 | + "sample_sizes_val,sample_size_val,expect_col,expect_warning", |
| 58 | + [ |
| 59 | + (5, None, False, True), |
| 60 | + ([5, "6", "7.3", "not_a_number"], None, False, True), |
| 61 | + (None, 10, True, False), |
| 62 | + (None, 10.5, True, False), |
| 63 | + (None, "12", True, True), |
| 64 | + (None, "13.5", True, True), |
| 65 | + (None, "not_a_number", False, True), |
| 66 | + (None, None, False, False), |
| 67 | + ([], 7, True, False), |
| 68 | + ([], None, False, False), |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_analysis_to_dict_sample_size( |
| 72 | + example_nimads_studyset, sample_sizes_val, sample_size_val, expect_col, expect_warning, caplog |
| 73 | +): |
| 74 | + """Test conversion of nimads JSON to nimare dataset with different sample_size(s) values.""" |
57 | 75 | studyset = Studyset(example_nimads_studyset) |
58 | | - # Set sample_sizes to an int rather than list/tuple |
59 | 76 | for study in studyset.studies: |
60 | 77 | for analysis in study.analyses: |
61 | | - analysis.metadata["sample_sizes"] = 5 |
62 | | - with pytest.raises(TypeError): |
63 | | - # Trigger conversion which internally calls _analysis_to_dict |
64 | | - io.convert_nimads_to_dataset(studyset) |
| 78 | + analysis.metadata.clear() |
| 79 | + if sample_sizes_val is not None: |
| 80 | + analysis.metadata["sample_sizes"] = sample_sizes_val |
| 81 | + if sample_size_val is not None: |
| 82 | + analysis.metadata["sample_size"] = sample_size_val |
| 83 | + |
| 84 | + with caplog.at_level("WARNING"): |
| 85 | + dset = io.convert_nimads_to_dataset(studyset) |
| 86 | + assert isinstance(dset, nimare.dataset.Dataset) |
| 87 | + if expect_col: |
| 88 | + assert "sample_sizes" in dset.metadata.columns |
| 89 | + else: |
| 90 | + assert "sample_sizes" not in dset.metadata.columns |
| 91 | + if expect_warning: |
| 92 | + assert any( |
| 93 | + "sample_size" in record.message or "sample_sizes" in record.message |
| 94 | + for record in caplog.records |
| 95 | + ) |
| 96 | + else: |
| 97 | + assert not any( |
| 98 | + "sample_size" in record.message or "sample_sizes" in record.message |
| 99 | + for record in caplog.records |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize( |
| 104 | + "annotation_mod,expect_success,expect_typeerror", |
| 105 | + [ |
| 106 | + # No annotation at all |
| 107 | + (None, True, False), |
| 108 | + # Annotation with empty notes |
| 109 | + (lambda ann: ann.update({"notes": []}), True, False), |
| 110 | + # Annotation with extra irrelevant key |
| 111 | + (lambda ann: ann.update({"extra_key": 123}), True, False), |
| 112 | + # Annotation with missing 'notes' key (should fail) |
| 113 | + (lambda ann: ann.pop("notes", None), False, True), |
| 114 | + # Annotation with mismatched analysis id in notes (should warn/fail) |
| 115 | + ( |
| 116 | + lambda ann: ann["notes"].append( |
| 117 | + { |
| 118 | + "analysis_name": "Fake", |
| 119 | + "publication": "Fake", |
| 120 | + "study": ann["notes"][0]["study"], |
| 121 | + "study_year": 2025, |
| 122 | + "analysis": "not_in_studyset", |
| 123 | + "authors": "Nobody", |
| 124 | + "note": {"include": False}, |
| 125 | + "study_name": "Fake", |
| 126 | + } |
| 127 | + ), |
| 128 | + True, |
| 129 | + True, |
| 130 | + ), |
| 131 | + ], |
| 132 | +) |
| 133 | +def test_analysis_to_dict_annotation( |
| 134 | + example_nimads_studyset, |
| 135 | + example_nimads_annotation, |
| 136 | + annotation_mod, |
| 137 | + expect_success, |
| 138 | + expect_typeerror, |
| 139 | +): |
| 140 | + """Test conversion of nimads JSON to nimare dataset with various annotation modifications.""" |
| 141 | + studyset = Studyset(example_nimads_studyset) |
| 142 | + if annotation_mod is not None: |
| 143 | + annotation = copy.deepcopy(example_nimads_annotation) |
| 144 | + annotation_mod(annotation) |
| 145 | + if expect_typeerror: |
| 146 | + with pytest.raises((TypeError, ValueError, KeyError)): |
| 147 | + studyset.annotations = annotation |
| 148 | + io.convert_nimads_to_dataset(studyset) |
| 149 | + else: |
| 150 | + studyset.annotations = annotation |
| 151 | + dset = io.convert_nimads_to_dataset(studyset) |
| 152 | + assert expect_success |
| 153 | + else: |
| 154 | + # No annotation |
| 155 | + dset = io.convert_nimads_to_dataset(studyset) |
| 156 | + assert isinstance(dset, nimare.dataset.Dataset) |
65 | 157 |
|
66 | 158 |
|
67 | 159 | def test_convert_sleuth_to_dataset_smoke(): |
|
0 commit comments