Skip to content

Commit c4f5f0a

Browse files
LaurentLMpre-commit-ci[bot]drammocklarsoner
authored
Birthday input cast to datetime.date (#13284)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel McCloy <dan@mccloy.info> Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
1 parent 729ce25 commit c4f5f0a

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

doc/changes/devel/13284.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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`.

mne/_fiff/meas_info.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,19 @@ def _check_types(x, *, info, name, types, cast=None):
10111011
return x
10121012

10131013

1014+
def _check_bday(birthday_input, *, info):
1015+
date = _check_types(
1016+
birthday_input,
1017+
info=info,
1018+
name='subject_info["birthday"]',
1019+
types=(datetime.date, None),
1020+
)
1021+
# test if we have a pd.Timestamp
1022+
if hasattr(date, "date"):
1023+
date = date.date()
1024+
return date
1025+
1026+
10141027
class SubjectInfo(ValidatedDict):
10151028
_attributes = {
10161029
"id": partial(_check_types, name='subject_info["id"]', types=int),
@@ -1022,9 +1035,7 @@ class SubjectInfo(ValidatedDict):
10221035
"middle_name": partial(
10231036
_check_types, name='subject_info["middle_name"]', types=str
10241037
),
1025-
"birthday": partial(
1026-
_check_types, name='subject_info["birthday"]', types=(datetime.date, None)
1027-
),
1038+
"birthday": partial(_check_bday),
10281039
"sex": partial(_check_types, name='subject_info["sex"]', types=int),
10291040
"hand": partial(_check_types, name='subject_info["hand"]', types=int),
10301041
"weight": partial(

mne/_fiff/tests/test_meas_info.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,27 @@ def test_meas_date_convert(stamp, dt):
773773
assert str(dt[0]) in repr(info)
774774

775775

776+
def test_birthday_input():
777+
"""Test that birthday input is handled correctly."""
778+
pd = pytest.importorskip("pandas")
779+
780+
# Test valid date
781+
info = create_info(ch_names=["EEG 001"], sfreq=1000.0, ch_types="eeg")
782+
info["subject_info"] = {}
783+
info["subject_info"]["birthday"] = date(2000, 1, 1)
784+
assert info["subject_info"]["birthday"] == date(2000, 1, 1)
785+
786+
# pandas Timestamp should convert to datetime date
787+
info["subject_info"]["birthday"] = pd.Timestamp("2000-01-01")
788+
assert info["subject_info"]["birthday"] == date(2000, 1, 1)
789+
# Ensure we've converted it during setting
790+
assert not isinstance(info["subject_info"]["birthday"], pd.Timestamp)
791+
792+
# Test invalid date raises error
793+
with pytest.raises(TypeError, match="must be an instance of date"):
794+
info["subject_info"]["birthday"] = "not a date"
795+
796+
776797
def _complete_info(info):
777798
"""Complete the meas info fields."""
778799
for key in ("file_id", "meas_id"):

0 commit comments

Comments
 (0)