Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 34 additions & 0 deletions neo/core/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,37 @@ def as_quantity(self):
Return the event times as a quantities array.
"""
return self.view(pq.Quantity)

# def to_epoch(self):
# """
# Transform Events into an array of Epoch
# """
# from neo.core import Epoch, Event, Segment

# i = 0
# duration = []
# for t in self.times:
# if i==0 :
# duration.append(self.times[i])
# else :
# duration.append(self.times[i] - self.times[i-1])
# i+=1

# epc = Epoch(times=self.times, durations=duration, labels=self.labels)
# return epc

def to_epoch(self, durations=None):
from neo.core import Epoch, Event, Segment
if durations is None:
# times = self.times[:]
# durations = np.diff(self.times)[:]
# labels = self.labels[:]

times = self.times[:-1]
durations = np.diff(self.times)
# labels = self.labels[:-1]
labels = np.array(["{}-{}".format(a, b) for a, b in zip(self.labels[:-1], self.labels[1:])])
else:
times = self.times
labels = self.labels
return Epoch(time=times, durations=durations, labels=labels)
15 changes: 15 additions & 0 deletions neo/test/coretest/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
HAVE_IPYTHON = True

from neo.core.event import Event
from neo import Event, Segment
from neo.core import Segment
from neo.test.tools import (assert_neo_object_is_compliant,
assert_arrays_equal,
Expand Down Expand Up @@ -433,5 +434,19 @@ def test__pickle(self):
fobj.close()
assert_array_equal(event1.times, event2.times)
os.remove('./pickle')

def test_event_to_epoch(self):
seg = Segment(name="test")
# event = Event(times=np.array([5.0, 12.0, 23.0, 45.0]), units="ms", labels=np.array(["A", "B", "C", "D"]))
event = Event(times=np.array([5.0, 12.0, 23.0, 45.0]), units="ms", labels=np.array(["A", "B", "C", "D"]))
print("event : " + str(event))
event.segment = seg
epoch = event.to_epoch()
print("epoch.magnitude : " + str(epoch.durations.magnitude))
print("np.array([7.0, 11.0, 22.0]) : " + str(np.array([7.0, 11.0, 22.0])))
assert_array_equal(epoch.durations.magnitude, np.array([7.0, 11.0, 22.0]))
self.assertEqual(str(type(epoch).__name__), 'Epoch')
pass

if __name__ == "__main__":
unittest.main()