File: test_boxy.py

package info (click to toggle)
python-mne 1.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 131,492 kB
  • sloc: python: 213,302; javascript: 12,910; sh: 447; makefile: 144
file content (219 lines) | stat: -rw-r--r-- 7,614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

import numpy as np
import pytest
import scipy.io as spio
from numpy.testing import assert_allclose, assert_array_equal, assert_array_less

from mne import pick_types
from mne.datasets import testing
from mne.io import read_raw_boxy
from mne.io.tests.test_raw import _test_raw_reader

data_path = testing.data_path(download=False)
boxy_0_40 = (
    data_path / "BOXY" / "boxy_0_40_recording" / "boxy_0_40_notriggers_unparsed.txt"
)
p_pod_0_40 = (
    data_path
    / "BOXY"
    / "boxy_0_40_recording"
    / "p_pod_10_6_3_loaded_data"
    / "p_pod_10_6_3_notriggers_unparsed.mat"
)
boxy_0_84 = (
    data_path
    / "BOXY"
    / "boxy_0_84_digaux_recording"
    / "boxy_0_84_triggers_unparsed.txt"
)
boxy_0_84_parsed = (
    data_path / "BOXY" / "boxy_0_84_digaux_recording" / "boxy_0_84_triggers_parsed.txt"
)
p_pod_0_84 = (
    data_path
    / "BOXY"
    / "boxy_0_84_digaux_recording"
    / "p_pod_10_6_3_loaded_data"
    / "p_pod_10_6_3_triggers_unparsed.mat"
)


def _assert_ppod(raw, p_pod_file):
    have_types = raw.get_channel_types(unique=True)
    assert "fnirs_fd_phase" in raw, have_types
    assert "fnirs_cw_amplitude" in raw, have_types
    assert "fnirs_fd_ac_amplitude" in raw, have_types
    ppod_data = spio.loadmat(p_pod_file)

    # Compare MNE loaded data to p_pod loaded data.
    map_ = dict(
        dc="fnirs_cw_amplitude", ac="fnirs_fd_ac_amplitude", ph="fnirs_fd_phase"
    )
    for key, value in map_.items():
        ppod = ppod_data[key].T
        m = np.median(np.abs(ppod))
        assert 1e-1 < m < 1e5, key  # our atol is meaningful
        atol = m * 1e-10
        py = raw.get_data(value)
        if key == "ph":  # radians
            assert_array_less(-np.pi, py)
            assert_array_less(py, 3 * np.pi)
            py = np.rad2deg(py)
        assert_allclose(py, ppod, atol=atol, err_msg=key)


@testing.requires_testing_data
def test_boxy_load():
    """Test reading BOXY files."""
    raw = read_raw_boxy(boxy_0_40, verbose=True)
    assert raw.info["sfreq"] == 62.5
    _assert_ppod(raw, p_pod_0_40)

    # Grab our different data types.
    mne_ph = raw.copy().pick(picks="fnirs_fd_phase")
    mne_dc = raw.copy().pick(picks="fnirs_cw_amplitude")
    mne_ac = raw.copy().pick(picks="fnirs_fd_ac_amplitude")

    # Check channel names.
    first_chans = [
        "S1_D1",
        "S2_D1",
        "S3_D1",
        "S4_D1",
        "S5_D1",
        "S6_D1",
        "S7_D1",
        "S8_D1",
        "S9_D1",
        "S10_D1",
    ]
    last_chans = [
        "S1_D8",
        "S2_D8",
        "S3_D8",
        "S4_D8",
        "S5_D8",
        "S6_D8",
        "S7_D8",
        "S8_D8",
        "S9_D8",
        "S10_D8",
    ]

    assert mne_dc.info["ch_names"][:10] == [
        i_chan + " " + "DC" for i_chan in first_chans
    ]
    assert mne_ac.info["ch_names"][:10] == [
        i_chan + " " + "AC" for i_chan in first_chans
    ]
    assert mne_ph.info["ch_names"][:10] == [
        i_chan + " " + "Ph" for i_chan in first_chans
    ]

    assert mne_dc.info["ch_names"][70::] == [
        i_chan + " " + "DC" for i_chan in last_chans
    ]
    assert mne_ac.info["ch_names"][70::] == [
        i_chan + " " + "AC" for i_chan in last_chans
    ]
    assert mne_ph.info["ch_names"][70::] == [
        i_chan + " " + "Ph" for i_chan in last_chans
    ]

    # Since this data set has no 'digaux' for creating trigger annotations,
    # let's make sure our Raw object has no annotations.
    assert len(raw.annotations) == 0


@testing.requires_testing_data
@pytest.mark.parametrize("fname", (boxy_0_84, boxy_0_84_parsed))
def test_boxy_filetypes(fname):
    """Test reading parsed and unparsed BOXY data files."""
    # BOXY data files can be saved in two formats (parsed and unparsed) which
    # mostly determines how the data is organised.
    # For parsed files, each row is a single timepoint and all
    # source/detector combinations are represented as columns.
    # For unparsed files, each row is a source and each group of n rows
    # represents a timepoint. For example, if there are ten sources in the raw
    # data then the first ten rows represent the ten sources at timepoint 1
    # while the next set of ten rows are the ten sources at timepoint 2.
    # Detectors are represented as columns.

    # Since p_pod is designed to only load unparsed files, we will first
    # compare MNE and p_pod loaded data from an unparsed data file. If those
    # files are comparable, then we will compare the MNE loaded data between
    # parsed and unparsed files.
    raw = read_raw_boxy(fname, verbose=True)
    assert raw.info["sfreq"] == 79.4722
    _assert_ppod(raw, p_pod_0_84)

    # Grab our different data types.
    unp_dc = raw.copy().pick("fnirs_cw_amplitude")
    unp_ac = raw.copy().pick("fnirs_fd_ac_amplitude")
    unp_ph = raw.copy().pick("fnirs_fd_phase")

    # Check channel names.
    chans = ["S1_D1", "S2_D1", "S3_D1", "S4_D1", "S5_D1", "S6_D1", "S7_D1", "S8_D1"]

    assert unp_dc.info["ch_names"] == [i_chan + " " + "DC" for i_chan in chans]
    assert unp_ac.info["ch_names"] == [i_chan + " " + "AC" for i_chan in chans]
    assert unp_ph.info["ch_names"] == [i_chan + " " + "Ph" for i_chan in chans]


@testing.requires_testing_data
@pytest.mark.parametrize("fname", (boxy_0_84, boxy_0_84_parsed))
def test_boxy_digaux(fname):
    """Test reading BOXY files and generating annotations from digaux."""
    srate = 79.4722
    raw = read_raw_boxy(fname, verbose=True)

    # Grab our different data types.
    picks_dc = pick_types(raw.info, fnirs="fnirs_cw_amplitude")
    picks_ac = pick_types(raw.info, fnirs="fnirs_fd_ac_amplitude")
    picks_ph = pick_types(raw.info, fnirs="fnirs_fd_phase")
    assert_array_equal(picks_dc, np.arange(0, 8) * 3 + 0)
    assert_array_equal(picks_ac, np.arange(0, 8) * 3 + 1)
    assert_array_equal(picks_ph, np.arange(0, 8) * 3 + 2)

    # Check that our event order matches what we expect.
    event_list = ["1.0", "2.0", "3.0", "4.0", "5.0"]
    assert_array_equal(raw.annotations.description, event_list)

    # Check that our event timings are what we expect.
    event_onset = [i_time * (1.0 / srate) for i_time in [105, 185, 265, 344, 424]]
    assert_allclose(raw.annotations.onset, event_onset, atol=1e-6)

    # Now let's compare parsed and unparsed events to p_pod loaded digaux.
    # Load our p_pod data.
    ppod_data = spio.loadmat(p_pod_0_84)
    ppod_digaux = np.transpose(ppod_data["digaux"])[0]

    # Now let's get our triggers from the p_pod digaux.
    # We only want the first instance of each trigger.
    prev_mrk = 0
    mrk_idx = list()
    duration = list()
    tmp_dur = 0
    for i_num, i_mrk in enumerate(ppod_digaux):
        if i_mrk != 0 and i_mrk != prev_mrk:
            mrk_idx.append(i_num)
        if i_mrk != 0 and i_mrk == prev_mrk:
            tmp_dur += 1
        if i_mrk == 0 and i_mrk != prev_mrk:
            duration.append((tmp_dur + 1) * (1.0 / srate))
            tmp_dur = 0
        prev_mrk = i_mrk
    onset = np.asarray([i_mrk * (1.0 / srate) for i_mrk in mrk_idx])
    description = np.asarray([str(float(i_mrk)) for i_mrk in ppod_digaux[mrk_idx]])
    assert_array_equal(raw.annotations.description, description)
    assert_allclose(raw.annotations.onset, onset, atol=1e-6)


@testing.requires_testing_data
@pytest.mark.parametrize("fname", (boxy_0_40, boxy_0_84, boxy_0_84_parsed))
def test_raw_properties(fname):
    """Test raw reader properties."""
    _test_raw_reader(read_raw_boxy, fname=fname, boundary_decimal=1)