Proj id and proj name access
Reference issue
Fixes #13252.
What does this implement/fix?
Allows user to set proj_idand proj_name through direct dict access.
Ensures that proj_id is either an integer or None.
Ensures that proj_name is either a string or None.
Additional information
Example:
import mne
info = mne.create_info(
ch_names=['EEG 001', 'EEG 002'],
sfreq=1000,
ch_types='eeg',
)
info['proj_name'] = None
info['proj_name'] = 123 #TypeError: 123 is not a string
info['proj_name'] = 'test'
info['proj_id'] = None
info['proj_id'] = 123
info['proj_id'] = '123' #TypeError: '123' is not an int
Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴
This looks like it's headed in the right direction!
If you look at one CI failure, you'll see:
________________ ERROR collecting mne/stats/tests/test_erp.py _________________
mne\stats\tests\test_erp.py:14: in <module>
raw = read_raw_fif(base_dir / "test_raw.fif")
mne\io\fiff\raw.py:543: in read_raw_fif
return Raw(
<decorator-gen-364>:12: in __init__
???
mne\io\fiff\raw.py:103: in __init__
raw, next_fname, buffer_size_sec = self._read_raw_file(
<decorator-gen-365>:12: in _read_raw_file
???
mne\io\fiff\raw.py:209: in _read_raw_file
info, meas = read_meas_info(fid, tree, clean_bads=True)
<decorator-gen-31>:12: in read_meas_info
???
mne\_fiff\meas_info.py:2540: in read_meas_info
info["proj_id"] = proj_id
mne\_fiff\meas_info.py:960: in __setitem__
val = self._attributes[key](
mne\_fiff\meas_info.py:1008: in _check_types
_validate_type(x, types, name)
mne\utils\check.py:640: in _validate_type
raise TypeError(
E TypeError: proj_id must be an instance of int or None, got <class 'numpy.ndarray'> instead.
I would suggest to modify mne\_fiff\meas_info.py:2540: in read_meas_info to have a .item() to get it to be a float rather than an ndarray with one element.
Any new change would ideally include some changed test, could you modify mne/_fiff/tests/test_meas_info.py somewhere to make sure you can set these two new values? And maybe even a couple with pytest.raises(TypeError, match="...") that you can't set them to something of the wrong type?
Finally, we'll need a doc/changes/devel/13261.newfeature.rst that mentions the ability to set these values. In there you should use the :newcontrib: directive with your name and add a link to doc/changes/names.inc as well.
Hi @larsoner,
Thanks for your guidance, I'm very new to this.
Still a bit confused as to why the test failed. proj_id should be either an int or None. It makes sense to me that an error is returned if someone tries to set proj_id with a numpy.ndarray.
Having said that, I made the change you suggested and it seems to work fine.
I have added tests and contribution information, hopefully it's all in the right place and the right format. Let me know if not :-)
I think I am a bit stuck with this. I fail to understand why info["proj_id"] is converted into an ndarray here:
https://github.com/mne-tools/mne-python/blob/68b5523a1d2e301fb0e4f2460c67d42c465e46b7/mne/_fiff/meas_info.py#L3501
It would make more sense to me to set it as 0 (as an integer), the same way as subject_info["id"] is anonymized.
It would make more sense to me to set it as 0 (as an integer), the same way as subject_info["id"] is anonymized.
Agreed, I think you can make this change
🎉 Congrats on merging your first pull request! 🥳 Looking forward to seeing more from you in the future! 💪