Skip to content

Commit

Permalink
Update warning
Browse files Browse the repository at this point in the history
  • Loading branch information
tsbinns committed Feb 10, 2025
1 parent c6d075d commit 924bebf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
19 changes: 13 additions & 6 deletions mne/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,19 @@ class Annotations:
def __init__(self, onset, duration, description, orig_time=None, ch_names=None):
self._orig_time = _handle_meas_date(orig_time)
if isinstance(orig_time, str) and self._orig_time is None:
warnings.warn(
"The format of the `orig_time` string is not recognised. It must "
"conform to the ISO8601 format with at most microsecond precision and "
"where the delimiter between date and time is ' '.",
RuntimeWarning,
)
try: # only warn if `orig_time` is not the default '1970-01-01 00:00:00'
if _handle_meas_date(0) == datetime.strptime(
orig_time, "%Y-%m-%d %H:%M:%S"
).replace(tzinfo=timezone.utc):
pass
except ValueError: # error if incorrect datetime format AND not the default
warn(
"The format of the `orig_time` string is not recognised. It "
"must conform to the ISO8601 format with at most microsecond "
"precision and where the delimiter between date and time is "
"' '.",
RuntimeWarning,
)
self.onset, self.duration, self.description, self.ch_names = _check_o_d_s_c(
onset, duration, description, ch_names
)
Expand Down
14 changes: 14 additions & 0 deletions mne/tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def windows_like_datetime(monkeypatch):

def test_basics():
"""Test annotation class."""
pytest.importorskip("pandas")
import pandas as pd

raw = read_raw_fif(fif_fname)
assert raw.annotations is not None
assert len(raw.annotations.onset) == 0
Expand All @@ -95,6 +98,17 @@ def test_basics():
assert isinstance(annot.orig_time, datetime)
assert annot.orig_time.tzinfo is timezone.utc

# Test bad format `orig_time` str -> `None` raises warning
with pytest.warns(
RuntimeWarning, match="The format of the `orig_time` string is not recognised."
):
bad_orig_time = (
pd.Timestamp(_ORIG_TIME)
.astimezone(None)
.isoformat(sep=" ", timespec="nanoseconds")
)
Annotations(onset, duration, description, bad_orig_time)

pytest.raises(ValueError, Annotations, onset, duration, description[:9])
pytest.raises(ValueError, Annotations, [onset, 1], duration, description)
pytest.raises(ValueError, Annotations, onset, [duration, 1], description)
Expand Down

0 comments on commit 924bebf

Please sign in to comment.