Skip to content

Commit

Permalink
MAINT: Compat with MNE 1.8 birthday date
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Jul 17, 2024
1 parent 3c020d9 commit 24a3e90
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ repos:
- id: ruff
name: ruff mne_bids/
files: ^mne_bids/
args: ["--fix"]
- id: ruff
name: ruff examples/
# D103: missing docstring in public function
Expand Down
9 changes: 9 additions & 0 deletions mne_bids/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def pytest_configure(config):
config.addinivalue_line("usefixtures", "monkeypatch_mne")


@pytest.fixture(autouse=True)
def close_all():
"""Close all figures after each test."""
yield
import matplotlib.pyplot as plt

plt.close("all")


@pytest.fixture(scope="session")
def monkeypatch_mne():
"""Monkeypatch MNE to ensure we have download=False everywhere in tests."""
Expand Down
3 changes: 2 additions & 1 deletion mne_bids/tests/data/tiny_bids/code/make_tiny_bids_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# %%
import json
from datetime import date
from pathlib import Path

import mne
Expand Down Expand Up @@ -45,7 +46,7 @@
"last_name": "Musterperson",
"first_name": "Maxi",
"middle_name": "Luka",
"birthday": (1970, 10, 20),
"birthday": date(1970, 10, 20),
"sex": 2,
"hand": 3,
}
Expand Down
5 changes: 1 addition & 4 deletions mne_bids/tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,12 +827,9 @@ def test_handle_chpi_reading(tmp_path):

with (
pytest.warns(RuntimeWarning, match="Defaulting to .* mne.Raw object"),
pytest.warns(
RuntimeWarning, match="This file contains raw Internal Active Shielding"
),
pytest.warns(RuntimeWarning, match="The unit for channel"),
):
raw_read = read_raw_bids(bids_path)
raw_read = read_raw_bids(bids_path, extra_params=dict(allow_maxshield="yes"))

# cHPI "off" according to sidecar, but present in the data
meg_json_data_chpi_mismatch = meg_json_data.copy()
Expand Down
12 changes: 9 additions & 3 deletions mne_bids/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import shutil as sh
import sys
import warnings
from datetime import datetime, timedelta, timezone
from datetime import date, datetime, timedelta, timezone
from glob import glob
from pathlib import Path

Expand Down Expand Up @@ -222,9 +222,12 @@ def test_write_participants(_bids_validate, tmp_path):

# add fake participants data
raw.set_meas_date(datetime(year=1994, month=1, day=26, tzinfo=timezone.utc))
birthday = (1993, 1, 26)
if check_version("mne", "1.8"):
birthday = date(*birthday)
raw.info["subject_info"] = {
"his_id": subject_id2,
"birthday": (1993, 1, 26),
"birthday": birthday,
"sex": 1,
"hand": 2,
}
Expand Down Expand Up @@ -707,9 +710,12 @@ def test_fif(_bids_validate, tmp_path):
# data
# change the gender but don't force overwrite.
raw = _read_raw_fif(raw_fname)
birthday = (1994, 1, 26)
if check_version("mne", "1.8"):
birthday = date(*birthday)
raw.info["subject_info"] = {
"his_id": subject_id2,
"birthday": (1994, 1, 26),
"birthday": birthday,
"sex": 2,
"hand": 1,
}
Expand Down
6 changes: 4 additions & 2 deletions mne_bids/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import sys
import warnings
from collections import OrderedDict, defaultdict
from datetime import datetime, timedelta, timezone
from datetime import date, datetime, timedelta, timezone
from pathlib import Path

import mne
Expand Down Expand Up @@ -456,12 +456,14 @@ def _participants_tsv(raw, subject_id, fname, overwrite=False):

# determine the age of the participant
age = subject_info.get("birthday", None)
if isinstance(age, tuple): # can be removed once MNE >= 1.8 is required
age = date(*age)
meas_date = raw.info.get("meas_date", None)
if isinstance(meas_date, (tuple, list, np.ndarray)):
meas_date = meas_date[0]

if meas_date is not None and age is not None:
bday = datetime(age[0], age[1], age[2], tzinfo=timezone.utc)
bday = datetime(age.year, age.month, age.day, tzinfo=timezone.utc)
if isinstance(meas_date, datetime):
meas_datetime = meas_date
else:
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ full = [
]

# Dependencies for running the test infrastructure
test = ["mne_bids[full]", "pytest", "pytest-cov", "pytest-sugar", "ruff"]
test = ["mne_bids[full]", "pytest >= 8", "pytest-cov", "pytest-sugar", "ruff"]

# Dependencies for building the documentation
doc = [
Expand Down Expand Up @@ -148,4 +148,6 @@ filterwarnings = [
# old MNE _fake_click
"ignore:The .*_event function was deprecated in Matplotlib.*:",
"ignore:datetime\\.datetime\\.utcfromtimestamp.* is deprecated and scheduled for removal in a future version.*:DeprecationWarning",
# matplotlib
"ignore:Figure.*is non-interactive.*cannot be shown:UserWarning",
]

0 comments on commit 24a3e90

Please sign in to comment.