Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/devel/13284.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed bug where saving FIFF files failed when ``info["subject_info"]["birthday"]`` was a :class:`pandas.Timestamp` instead of :class:`datetime.date`, by :newcontrib:`Laurent Le Mentec`.
17 changes: 14 additions & 3 deletions mne/_fiff/meas_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,19 @@ def _check_types(x, *, info, name, types, cast=None):
return x


def _check_bday(birthday_input, *, info):
date = _check_types(
birthday_input,
info=info,
name='subject_info["birthday"]',
types=(datetime.date, None),
)
# test if we have a pd.Timestamp
if hasattr(date, "date"):
date = date.date()
return date


class SubjectInfo(ValidatedDict):
_attributes = {
"id": partial(_check_types, name='subject_info["id"]', types=int),
Expand All @@ -1022,9 +1035,7 @@ class SubjectInfo(ValidatedDict):
"middle_name": partial(
_check_types, name='subject_info["middle_name"]', types=str
),
"birthday": partial(
_check_types, name='subject_info["birthday"]', types=(datetime.date, None)
),
"birthday": partial(_check_bday),
"sex": partial(_check_types, name='subject_info["sex"]', types=int),
"hand": partial(_check_types, name='subject_info["hand"]', types=int),
"weight": partial(
Expand Down
21 changes: 21 additions & 0 deletions mne/_fiff/tests/test_meas_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,27 @@ def test_meas_date_convert(stamp, dt):
assert str(dt[0]) in repr(info)


def test_birthday_input():
"""Test that birthday input is handled correctly."""
pd = pytest.importorskip("pandas")

# Test valid date
info = create_info(ch_names=["EEG 001"], sfreq=1000.0, ch_types="eeg")
info["subject_info"] = {}
info["subject_info"]["birthday"] = date(2000, 1, 1)
assert info["subject_info"]["birthday"] == date(2000, 1, 1)

# pandas Timestamp should convert to datetime date
info["subject_info"]["birthday"] = pd.Timestamp("2000-01-01")
assert info["subject_info"]["birthday"] == date(2000, 1, 1)
# Ensure we've converted it during setting
assert not isinstance(info["subject_info"]["birthday"], pd.Timestamp)

# Test invalid date raises error
with pytest.raises(TypeError, match="must be an instance of date"):
info["subject_info"]["birthday"] = "not a date"


def _complete_info(info):
"""Complete the meas info fields."""
for key in ("file_id", "meas_id"):
Expand Down