Repository: mne-tools/mne-python

Issue:
BUG: EpochsTFR bug popping up in MNE-BIDS-Pipeline
@drammock see https://app.circleci.com/pipelines/github/mne-tools/mne-bids-pipeline/4395/workflows/4c871947-2971-4fca-8d92-b7a2892c91db/jobs/62041

To reproduce you can `pip install -e .` mne-bids-pipeline and do (it will download some GB of data to `~/mne_data`):
```
pytest --download mne_bids_pipeline/ -k 247 --pdb
```

Discussion:

Patch:
diff --git a/mne/time_frequency/tfr.py b/mne/time_frequency/tfr.py
index 97df892ad..5a16cac80 100644
--- a/mne/time_frequency/tfr.py
+++ b/mne/time_frequency/tfr.py
@@ -4222,8 +4222,12 @@ def read_tfrs(fname, condition=None, *, verbose=None):
     hdf5_dict = read_hdf5(fname, title="mnepython", slash="replace")
     # single TFR from TFR.save()
     if "inst_type_str" in hdf5_dict:
-        inst_type_str = hdf5_dict["inst_type_str"]
-        Klass = dict(Epochs=EpochsTFR, Raw=RawTFR, Evoked=AverageTFR)[inst_type_str]
+        if hdf5_dict["data"].ndim == 4:
+            Klass = EpochsTFR
+        elif "nave" in hdf5_dict:
+            Klass = AverageTFR
+        else:
+            Klass = RawTFR
         out = Klass(inst=hdf5_dict)
         if getattr(out, "metadata", None) is not None:
             out.metadata = _prepare_read_metadata(out.metadata)


Test patch:
diff --git a/mne/time_frequency/tests/test_tfr.py b/mne/time_frequency/tests/test_tfr.py
index 5087a8c46..9383aaec8 100644
--- a/mne/time_frequency/tests/test_tfr.py
+++ b/mne/time_frequency/tests/test_tfr.py
@@ -659,6 +659,18 @@ def test_tfr_io(inst, average_tfr, request, tmp_path):
         tfr.save(fname, overwrite=False)
 
 
+def test_roundtrip_from_legacy_func(epochs, tmp_path):
+    """Test save/load with TFRs generated by legacy method (gh-12512)."""
+    pytest.importorskip("h5io")
+
+    fname = tmp_path / "temp_tfr.hdf5"
+    tfr = tfr_morlet(
+        epochs, freqs=freqs_linspace, n_cycles=7, average=True, return_itc=False
+    )
+    tfr.save(fname, overwrite=True)
+    assert read_tfrs(fname) == tfr
+
+
 def test_raw_tfr_init(raw):
     """Test the RawTFR and RawTFRArray constructors."""
     one = RawTFR(inst=raw, method="morlet", freqs=freqs_linspace)