Skip to content

Commit fff7674

Browse files
author
sprenger
committed
Add neo objects as ephy data sources
1 parent 8728e9f commit fff7674

4 files changed

Lines changed: 67 additions & 1 deletion

File tree

frites/dataset/tests/test_ds_ephy.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ def test_repr(self):
6666
ds.__repr__()
6767
ds._repr_html_()
6868

69+
def test_definition_as_neo(self):
70+
"""Test function definition."""
71+
# test array definition
72+
data, roi, time = sim_multi_suj_ephy(modality="intra", n_times=57,
73+
n_roi=5, n_sites_per_roi=7,
74+
n_epochs=10, n_subjects=5,
75+
random_state=0)
76+
y, _ = sim_mi_cc(data, snr=.8)
77+
z = [np.random.randint(0, 3, (10,)) for _ in range(len(y))]
78+
dt = DatasetEphy(data, y, roi, z=z, times=time)
79+
dt.groupby('roi')
80+
# test mne definition
81+
data, roi, time = sim_multi_suj_ephy(modality="meeg", n_times=57,
82+
n_roi=5, n_sites_per_roi=1,
83+
n_epochs=7, n_subjects=5,
84+
as_mne=False, as_neo=True,
85+
random_state=0)
86+
y, _ = sim_mi_cc(data, snr=.8)
87+
ds = DatasetEphy(data, y, roi, times=time)
88+
6989
def test_multiconditions(self):
7090
"""Test multi-conditions remapping."""
7191
d_3d = self._get_data(3)

frites/simulations/sim_generate_data.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
from scipy.signal import savgol_filter
55
from itertools import product
66

7+
try:
8+
import neo
9+
import quantities as pq
10+
HAS_NEO = True
11+
except ModuleNotFoundError:
12+
HAS_NEO = False
13+
714
MA_NAMES = ['L_VCcm', 'L_VCl', 'L_VCs', 'L_Cu', 'L_VCrm', 'L_ITCm', 'L_ITCr',
815
'L_MTCc', 'L_STCc', 'L_STCr', 'L_MTCr', 'L_ICC', 'L_IPCv',
916
'L_IPCd', 'L_SPC', 'L_SPCm', 'L_PCm', 'L_PCC', 'L_Sv', 'L_Sdl',
@@ -23,7 +30,8 @@
2330

2431
def sim_single_suj_ephy(modality="meeg", sf=512., n_times=1000, n_roi=1,
2532
n_sites_per_roi=1, n_epochs=100, n_sines=100, f_min=.5,
26-
f_max=160., noise=10, as_mne=False, random_state=None):
33+
f_max=160., noise=10, as_mne=False, as_neo=False,
34+
random_state=None):
2735
"""Simulate electrophysiological data of a single subject.
2836
2937
This function generate some illustrative random electrophysiological data
@@ -54,6 +62,8 @@ def sim_single_suj_ephy(modality="meeg", sf=512., n_times=1000, n_roi=1,
5462
Noise level.
5563
as_mne : bool | False
5664
If True, data are converted to a mne.EpochsArray structure
65+
as_neo : bool | False
66+
If True, data are converted to a neo.Block structure
5767
random_state : int | None
5868
Fix the random state for the reproducibility.
5969
@@ -103,6 +113,18 @@ def sim_single_suj_ephy(modality="meeg", sf=512., n_times=1000, n_roi=1,
103113
from mne import create_info, EpochsArray
104114
info = create_info(roi.tolist(), sf, ch_types='seeg')
105115
signal = EpochsArray(signal, info, tmin=float(time[0]), verbose=False)
116+
elif as_neo:
117+
if not HAS_NEO:
118+
raise ImportError('`as_neo` requires neo to be installed.')
119+
block = neo.Block()
120+
for epoch_id in range(len(signal)):
121+
seg = neo.Segment()
122+
anasig = neo.AnalogSignal(signal[epoch_id].T*pq.dimensionless,
123+
t_start=time[0, 0]*pq.s,
124+
sampling_rate=sf*pq.Hz)
125+
seg.analogsignals.append(anasig)
126+
block.segments.append(seg)
127+
signal = block
106128
return signal, roi, time.squeeze()
107129

108130

frites/simulations/sim_mi.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,16 @@ def sim_mi_cc(x, snr=.9):
7575
# if mne types, turn into arrays
7676
if isinstance(x[0], CONFIG["MNE_EPOCHS_TYPE"]):
7777
x = [x[k].get_data() for k in range(len(x))]
78+
elif 'neo.core' in str(type(x[0])):
79+
subject_list = []
80+
for block in x:
81+
subject_data = np.stack([seg.analogsignals[0].magnitude for seg in block.segments])
82+
# reorder dimensions to match (n_epochs, n_channels, n_times)
83+
subject_list.append(subject_data.swapaxes(1, 2))
84+
x = subject_list
85+
7886
n_times = x[0].shape[-1]
87+
7988
# cluster definition (20% length around central point)
8089
cluster = _get_cluster(n_times, location='center', perc=.2)
8190
# ground truth definition

frites/simulations/tests/test_sim_generate_data.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
import numpy as np
33
from mne import EpochsArray
44

5+
try:
6+
import neo
7+
HAS_NEO = True
8+
except ModuleNotFoundError:
9+
HAS_NEO = False
10+
511
from frites.simulations import (sim_single_suj_ephy, sim_multi_suj_ephy)
612

713

@@ -19,6 +25,10 @@ def test_sim_single_suj_ephy(self):
1925
# mne type
2026
data, _, _ = sim_single_suj_ephy(as_mne=True)
2127
assert isinstance(data, EpochsArray)
28+
# neo type
29+
if HAS_NEO:
30+
data, _, _ = sim_single_suj_ephy(as_neo=True)
31+
assert isinstance(data, neo.Block)
2232

2333
def test_sim_multi_suj_ephy(self):
2434
"""Test function sim_multi_suj_ephy."""
@@ -34,3 +44,8 @@ def test_sim_multi_suj_ephy(self):
3444
# mne type
3545
data, _, _ = sim_multi_suj_ephy(n_subjects=5, as_mne=True)
3646
assert all([isinstance(k, EpochsArray) for k in data])
47+
# neo type
48+
if HAS_NEO:
49+
data, _, _ = sim_multi_suj_ephy(n_subjects=5, as_neo=True)
50+
assert all([isinstance(k, neo.Block) for k in data])
51+

0 commit comments

Comments
 (0)