[med-svn] [python-mne] 11/52: use __get_item__ instead of __get_slice__, support pythonic indexing, small fixes based on Alex' comments
Yaroslav Halchenko
debian at onerussian.com
Fri Nov 27 17:23:44 UTC 2015
This is an automated email from the git hooks/post-receive script.
yoh pushed a commit to annotated tag v0.2
in repository python-mne.
commit 42feb3c2ca8e351da2bc5ea54b1b150e41dc62b1
Author: Martin Luessi <mluessi at nmr.mgh.harvard.edu>
Date: Wed Sep 28 09:52:57 2011 -0400
use __get_item__ instead of __get_slice__, support pythonic indexing, small fixes based on Alex' comments
---
mne/epochs.py | 37 ++++++++++++++++++++-----------------
mne/tests/test_epochs.py | 2 +-
2 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/mne/epochs.py b/mne/epochs.py
index 6978a58..f4c44e4 100644
--- a/mne/epochs.py
+++ b/mne/epochs.py
@@ -361,35 +361,38 @@ class Epochs(object):
s += ", baseline : %s" % str(self.baseline)
return "Epochs (%s)" % s
- def __getslice__(self, start, end):
- """Return an Epoch object with a subset of epochs.
+ def __len__(self):
+ """Return length (number of events)
"""
- if not self.bad_dropped:
- warnings.warn("Bad epochs have not been dropped, indexing will " \
- "be inccurate. Use drop_bad_epochs() or preload=True")
+ return len(self.events)
- epoch_slice = copy.copy(self)
- epoch_slice.events = self.events[start:end]
+ def __getitem__(self, index):
+ """Return epoch at index or an Epochs object with a slice of epochs
+ """
+ if isinstance(index, slice):
+ # return Epochs object with slice of epochs
+ if not self.bad_dropped:
+ warnings.warn("Bad epochs have not been dropped, indexing "
+ "will be inccurate. Use drop_bad_epochs() "
+ "or preload=True")
- if self.preload:
- epoch_slice._data = self._data[start:end]
+ epoch_slice = copy.copy(self)
+ epoch_slice.events = self.events[index]
- return epoch_slice
+ if self.preload:
+ epoch_slice._data = self._data[index]
- def __getitem__(self, index):
- """Return epoch at index
- """
- if index < 0 or index >= len(self.events):
- raise IndexError("Epoch index out of bounds")
+ return epoch_slice
+ # return single epoch as 2D array
if self.preload:
epoch = epoch = self._data[index]
else:
epoch = self._get_epoch_from_disk(index)
if not self._is_good_epoch(epoch):
- warnings.warn("Bad epoch with index %d returned."\
- "Use drop_bad_epochs() or preload=True "\
+ warnings.warn("Bad epoch with index %d returned. "
+ "Use drop_bad_epochs() or preload=True "
"to prevent this." % (index))
return epoch
diff --git a/mne/tests/test_epochs.py b/mne/tests/test_epochs.py
index 94abf4f..545d91a 100644
--- a/mne/tests/test_epochs.py
+++ b/mne/tests/test_epochs.py
@@ -70,6 +70,7 @@ def test_preload_epochs():
data_no_preload = epochs.get_data()
assert_array_equal(data_preload, data_no_preload)
+
def test_indexing_slicing():
"""Test of indexing and slicing operations
"""
@@ -110,7 +111,6 @@ def test_indexing_slicing():
pos += 1
-
def test_comparision_with_c():
"""Test of average obtained vs C code
"""
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git
More information about the debian-med-commit
mailing list