8000 Fix `Annotations` time formats by tsbinns · Pull Request #13109 · mne-tools/mne-python · GitHub
[go: up one dir, main page]

Skip to content

Fix Annotations time formats #13109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Update tests
  • Loading branch information
tsbinns committed Feb 10, 2025
commit 2015ceb28631da84f090014113b4a2e7b17c360e
26 changes: 26 additions & 0 deletions mne/tests/test_annotations.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,24 @@ def test_broken_csv(tmp_path):
read_annotations(fname)


def test_nanosecond_csv(tmp_path):
"""Test .csv with nanosecond timestamps for onsets read correctly."""
pytest.importorskip("pandas")
import pandas as pd

onset = (
pd.Timestamp(_ORIG_TIME)
.astimezone(None)
.isoformat(sep=" ", timespec="nanoseconds")
)
content = f"onset,duration,description\n{onset},1.0,AA"
fname = tmp_path / "annotations_broken.csv"
with open(fname, "w") as f:
f.write(content)
annot = read_annotations(fname)
assert annot.orig_time == _ORIG_TIME


# Test for IO with .txt files


Expand Down Expand Up @@ -1462,6 +1480,8 @@ def test_repr():
def test_annotation_to_data_frame(time_format):
"""Test annotation class to data frame conversion."""
pytest.importorskip("pandas")
import pandas as pd

onset = np.arange(1, 10)
durations = np.full_like(onset, [4, 5, 6, 4, 5, 6, 4, 5, 6])
description = ["yy"] * onset.shape[0]
Expand All @@ -1481,6 +1501,12 @@ def test_annotation_to_data_frame(time_format):
assert want == got
assert df.groupby("description").count().onset["yy"] == 9

# Check nanoseconds omitted from onset times
if time_format == "datetime":
a.onset += 1e-7 # >6 decimals to trigger nanosecond component
df = a.to_data_frame(time_format=time_format)
assert pd.Timestamp(df.onset[0]).nanosecond == 0


def test_annotation_ch_names():
"""Test annotation ch_names updating and pruning."""
Expand Down
Loading
0