|
19 | 19 | assert_equal, |
20 | 20 | ) |
21 | 21 |
|
| 22 | +import mne.channels.montage |
22 | 23 | from mne import ( |
23 | 24 | __file__ as _mne_file, |
24 | 25 | ) |
|
56 | 57 | _BUILTIN_STANDARD_MONTAGES, |
57 | 58 | _check_get_coord_frame, |
58 | 59 | transform_to_head, |
| 60 | + write_dig, |
59 | 61 | ) |
60 | 62 | from mne.coreg import get_mni_fiducials |
61 | 63 | from mne.datasets import testing |
@@ -138,7 +140,8 @@ def test_dig_montage_trans(tmp_path): |
138 | 140 | _ensure_trans(trans) |
139 | 141 | # ensure that we can save and load it, too |
140 | 142 | fname = tmp_path / "temp-mon.fif" |
141 | | - _check_roundtrip(montage, fname, "mri") |
| 143 | + with pytest.warns(RuntimeWarning, match="MNE naming conventions"): |
| 144 | + _check_roundtrip(montage, fname, "mri") |
142 | 145 | # test applying a trans |
143 | 146 | position1 = montage.get_positions() |
144 | 147 | montage.apply_trans(trans) |
@@ -1074,12 +1077,12 @@ def _ensure_fid_not_nan(info, ch_pos): |
1074 | 1077 |
|
1075 | 1078 |
|
1076 | 1079 | @testing.requires_testing_data |
1077 | | -def test_fif_dig_montage(tmp_path): |
| 1080 | +def test_fif_dig_montage(tmp_path, monkeypatch): |
1078 | 1081 | """Test FIF dig montage support.""" |
1079 | | - dig_montage = read_dig_fif(fif_dig_montage_fname) |
| 1082 | + dig_montage = read_dig_fif(fif_dig_montage_fname, verbose="error") |
1080 | 1083 |
|
1081 | 1084 | # test round-trip IO |
1082 | | - fname_temp = tmp_path / "test.fif" |
| 1085 | + fname_temp = tmp_path / "test-dig.fif" |
1083 | 1086 | _check_roundtrip(dig_montage, fname_temp) |
1084 | 1087 |
|
1085 | 1088 | # Make a BrainVision file like the one the user would have had |
@@ -1119,16 +1122,32 @@ def test_fif_dig_montage(tmp_path): |
1119 | 1122 | # Roundtrip of non-FIF start |
1120 | 1123 | montage = make_dig_montage(hsp=read_polhemus_fastscan(hsp), hpi=read_mrk(hpi)) |
1121 | 1124 | elp_points = read_polhemus_fastscan(elp) |
1122 | | - ch_pos = {f"EEG{k:03d}": pos for k, pos in enumerate(elp_points[8:], 1)} |
1123 | | - montage += make_dig_montage( |
| 1125 | + ch_pos = {f"ECoG{k:03d}": pos for k, pos in enumerate(elp_points[3:], 1)} |
| 1126 | + assert len(elp_points) == 8 # there are only 8 but pretend the last are ECoG |
| 1127 | + other = make_dig_montage( |
1124 | 1128 | nasion=elp_points[0], lpa=elp_points[1], rpa=elp_points[2], ch_pos=ch_pos |
1125 | 1129 | ) |
| 1130 | + assert other.ch_names[0].startswith("ECoG") |
| 1131 | + montage += other |
| 1132 | + assert montage.ch_names[0].startswith("ECoG") |
1126 | 1133 | _check_roundtrip(montage, fname_temp, "unknown") |
1127 | 1134 | montage = transform_to_head(montage) |
1128 | 1135 | _check_roundtrip(montage, fname_temp) |
1129 | 1136 | montage.dig[0]["coord_frame"] = FIFF.FIFFV_COORD_UNKNOWN |
1130 | 1137 | with pytest.raises(RuntimeError, match="Only a single coordinate"): |
1131 | | - montage.save(fname_temp) |
| 1138 | + montage.save(fname_temp, overwrite=True) |
| 1139 | + montage.dig[0]["coord_frame"] = FIFF.FIFFV_COORD_HEAD |
| 1140 | + |
| 1141 | + # Check that old-style files can be read, too, using EEG001 etc. |
| 1142 | + def write_dig_no_ch_names(*args, **kwargs): |
| 1143 | + kwargs["ch_names"] = None |
| 1144 | + return write_dig(*args, **kwargs) |
| 1145 | + |
| 1146 | + monkeypatch.setattr(mne.channels.montage, "write_dig", write_dig_no_ch_names) |
| 1147 | + montage.save(fname_temp, overwrite=True) |
| 1148 | + montage_read = read_dig_fif(fname_temp) |
| 1149 | + default_ch_names = [f"EEG{ii:03d}" for ii in range(1, 6)] |
| 1150 | + assert montage_read.ch_names == default_ch_names |
1132 | 1151 |
|
1133 | 1152 |
|
1134 | 1153 | @testing.requires_testing_data |
@@ -1175,8 +1194,8 @@ def test_egi_dig_montage(tmp_path): |
1175 | 1194 | atol=1e-4, |
1176 | 1195 | ) |
1177 | 1196 |
|
1178 | | - # test round-trip IO |
1179 | | - fname_temp = tmp_path / "egi_test.fif" |
| 1197 | + # test round-trip IO (with GZ) |
| 1198 | + fname_temp = tmp_path / "egi_test-dig.fif.gz" |
1180 | 1199 | _check_roundtrip(dig_montage, fname_temp, "unknown") |
1181 | 1200 | _check_roundtrip(dig_montage_in_head, fname_temp) |
1182 | 1201 |
|
@@ -1330,7 +1349,7 @@ def test_read_dig_captrak(tmp_path): |
1330 | 1349 | ) |
1331 | 1350 |
|
1332 | 1351 | montage = transform_to_head(montage) # transform_to_head has to be tested |
1333 | | - _check_roundtrip(montage=montage, fname=str(tmp_path / "bvct_test.fif")) |
| 1352 | + _check_roundtrip(montage=montage, fname=tmp_path / "bvct_test-dig.fif") |
1334 | 1353 |
|
1335 | 1354 | fid, _ = _get_fid_coords(montage.dig) |
1336 | 1355 | assert_allclose( |
@@ -1495,15 +1514,15 @@ def test_montage_positions_similar(fname, montage, n_eeg, n_good, bads): |
1495 | 1514 | assert_array_less(0, ang) # but not equal |
1496 | 1515 |
|
1497 | 1516 |
|
1498 | | -# XXX: this does not check ch_names + it cannot work because of write_dig |
1499 | 1517 | def _check_roundtrip(montage, fname, coord_frame="head"): |
1500 | 1518 | """Check roundtrip writing.""" |
1501 | 1519 | montage.save(fname, overwrite=True) |
1502 | 1520 | montage_read = read_dig_fif(fname=fname) |
1503 | 1521 |
|
1504 | | - assert_equal(repr(montage), repr(montage_read)) |
1505 | | - assert_equal(_check_get_coord_frame(montage_read.dig), coord_frame) |
| 1522 | + assert repr(montage) == repr(montage_read) |
| 1523 | + assert _check_get_coord_frame(montage_read.dig) == coord_frame |
1506 | 1524 | assert_dig_allclose(montage, montage_read) |
| 1525 | + assert montage.ch_names == montage_read.ch_names |
1507 | 1526 |
|
1508 | 1527 |
|
1509 | 1528 | def test_digmontage_constructor_errors(): |
@@ -1910,7 +1929,7 @@ def test_get_montage(): |
1910 | 1929 |
|
1911 | 1930 | # 4. read in BV test dataset and make sure montage |
1912 | 1931 | # fulfills roundtrip on non-standard montage |
1913 | | - dig_montage = read_dig_fif(fif_dig_montage_fname) |
| 1932 | + dig_montage = read_dig_fif(fif_dig_montage_fname, verbose="error") |
1914 | 1933 |
|
1915 | 1934 | # Make a BrainVision file like the one the user would have had |
1916 | 1935 | # with testing dataset 'test.vhdr' |
|
0 commit comments