Skip to content
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

NEW: TrialIndexer #422

Merged
merged 3 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions syncopy/datatype/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import getpass
import socket
from datetime import datetime
from numbers import Number

# Syncopy imports
from syncopy import __storage__, __storagelimit__, __sessionid__
Expand All @@ -29,11 +30,14 @@ def __init__(self, data_object, idx_list):
idx_list : list
List of valid trial indices for `_get_trial`
"""

self.data_object = data_object
self.idx_list = idx_list
self._len = len(idx_list)

def __getitem__(self, trialno):
if not isinstance(trialno, Number):
raise SPYTypeError(trialno, "trial index", "single number to index a single trial")
if trialno not in self.idx_list:
lgl = "index of existing trials"
raise SPYValueError(lgl, "trial index", trialno)
Expand Down
38 changes: 38 additions & 0 deletions syncopy/tests/test_basedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,44 @@ def test_trialdef(self):
assert np.array_equal(dummy._t0, self.trl[dclass][:, 2])
assert np.array_equal(dummy.trialinfo.flatten(), self.trl[dclass][:, 3])

def test_trials_property(self):

# 3 trials, trial index = data values
data = AnalogData([i * np.ones((2,2)) for i in range(3)], samplerate=1)

# single index access
assert np.all(data.trials[0] == 0)
assert np.all(data.trials[1] == 1)
assert np.all(data.trials[2] == 2)

# iterator
all_trials = [trl for trl in data.trials]
assert len(all_trials) == 3
assert all([np.all(all_trials[i] == i) for i in range(3)])

# selection
data.selectdata(trials=[0, 2], inplace=True)
all_selected_trials = [trl for trl in data.selection.trials]
assert data.selection.trial_ids == [0, 2]
assert len(all_selected_trials) == 2
assert all([np.all(data.selection.trials[i] == i) for i in data.selection.trial_ids])

# check that non-existing trials get catched
with pytest.raises(SPYValueError, match='existing trials'):
data.trials[999]
# selections have absolute trial indices!
with pytest.raises(SPYValueError, match='existing trials'):
data.selection.trials[1]

# check that invalid trial indexing gets catched
with pytest.raises(SPYTypeError, match='trial index'):
data.trials[range(4)]
with pytest.raises(SPYTypeError, match='trial index'):
data.trials[2:3]
with pytest.raises(SPYTypeError, match='trial index'):
data.trials[np.arange(3)]
return data
KatharineShapcott marked this conversation as resolved.
Show resolved Hide resolved

# Test ``_gen_filename`` with `AnalogData` only - method is independent from concrete data object
def test_filename(self):
# ensure we're salting sufficiently to create at least `numf`
Expand Down