-
Notifications
You must be signed in to change notification settings - Fork 249
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
EDFIO: Alleviate EDF single handle problem #1584
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,15 +83,14 @@ def _parse_header(self): | |
# or continuous EDF+ files ('EDF+C' in header) | ||
if ("EDF+" in file_version_header) and ("EDF+C" not in file_version_header): | ||
raise ValueError("Only continuous EDF+ files are currently supported.") | ||
|
||
self.edf_reader = EdfReader(self.filename) | ||
self._open_reader() | ||
# load headers, signal information and | ||
self.edf_header = self.edf_reader.getHeader() | ||
self.signal_headers = self.edf_reader.getSignalHeaders() | ||
|
||
# add annotations to header | ||
annotations = self.edf_reader.readAnnotations() | ||
self.signal_annotations = [[s, d, a] for s, d, a in zip(*annotations)] | ||
self._edf_annotations = self.edf_reader.readAnnotations() | ||
self.signal_annotations = [[s, d, a] for s, d, a in zip(*self._edf_annotations)] | ||
|
||
# 1 stream = 1 sampling rate | ||
stream_characteristics = [] | ||
|
@@ -120,7 +119,7 @@ def _parse_header(self): | |
signal_channels.append((ch_name, chan_id, sr, dtype, units, gain, offset, stream_id, buffer_id)) | ||
|
||
# convert channel index lists to arrays for indexing | ||
self.stream_idx_to_chidx = {k: np.array(v) for k, v in self.stream_idx_to_chidx.items()} | ||
self.stream_idx_to_chidx = {k: np.asarray(v) for k, v in self.stream_idx_to_chidx.items()} | ||
|
||
signal_channels = np.array(signal_channels, dtype=_signal_channel_dtype) | ||
|
||
|
@@ -174,6 +173,15 @@ def _parse_header(self): | |
for array_key in array_keys: | ||
array_anno = {array_key: [h[array_key] for h in self.signal_headers]} | ||
seg_ann["signals"].append({"__array_annotations__": array_anno}) | ||
|
||
# We store the following attributes for rapid access without needing the reader | ||
|
||
self._t_stop = self.edf_reader.datarecord_duration * self.edf_reader.datarecords_in_file | ||
# use sample count of first signal in stream | ||
self._stream_index_samples = {stream_index : self.edf_reader.getNSamples()[chidx][0] for stream_index, chidx in self.stream_idx_to_chidx.items()} | ||
self._number_of_events = len(self.edf_reader.readAnnotations()[0]) | ||
|
||
self.close() | ||
|
||
def _get_stream_channels(self, stream_index): | ||
return self.header["signal_channels"][self.stream_idx_to_chidx[stream_index]] | ||
|
@@ -183,14 +191,11 @@ def _segment_t_start(self, block_index, seg_index): | |
return 0.0 # in seconds | ||
|
||
def _segment_t_stop(self, block_index, seg_index): | ||
t_stop = self.edf_reader.datarecord_duration * self.edf_reader.datarecords_in_file | ||
# this must return an float scale in second | ||
return t_stop | ||
return self._t_stop | ||
|
||
def _get_signal_size(self, block_index, seg_index, stream_index): | ||
chidx = self.stream_idx_to_chidx[stream_index][0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zm711 here is the zero. The idea is to use the first channel of the stream to get the samples. |
||
# use sample count of first signal in stream | ||
return self.edf_reader.getNSamples()[chidx] | ||
return self._stream_index_samples[stream_index] | ||
|
||
def _get_signal_t_start(self, block_index, seg_index, stream_index): | ||
return 0.0 # EDF does not provide temporal offset information | ||
|
@@ -219,12 +224,13 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea | |
|
||
# load data into numpy array buffer | ||
data = [] | ||
self._open_reader() | ||
for i, channel_idx in enumerate(selected_channel_idxs): | ||
# use int32 for compatibility with pyedflib | ||
buffer = np.empty(n, dtype=np.int32) | ||
self.edf_reader.read_digital_signal(channel_idx, i_start, n, buffer) | ||
data.append(buffer) | ||
|
||
self._close_reader() | ||
# downgrade to int16 as this is what is used in the edf file format | ||
# use fortran (column major) order to be more efficient after transposing | ||
data = np.asarray(data, dtype=np.int16, order="F") | ||
|
@@ -247,11 +253,11 @@ def _get_spike_raw_waveforms(self, block_index, seg_index, spike_channel_index, | |
return None | ||
|
||
def _event_count(self, block_index, seg_index, event_channel_index): | ||
return len(self.edf_reader.readAnnotations()[0]) | ||
return self._number_of_events | ||
|
||
def _get_event_timestamps(self, block_index, seg_index, event_channel_index, t_start, t_stop): | ||
# these time should be already in seconds | ||
timestamps, durations, labels = self.edf_reader.readAnnotations() | ||
timestamps, durations, labels = self._edf_annotations | ||
if t_start is None: | ||
t_start = self.segment_t_start(block_index, seg_index) | ||
if t_stop is None: | ||
|
@@ -281,6 +287,9 @@ def _rescale_event_timestamp(self, event_timestamps, dtype, event_channel_index) | |
def _rescale_epoch_duration(self, raw_duration, dtype, event_channel_index): | ||
return np.asarray(raw_duration, dtype=dtype) | ||
|
||
def _open_reader(self): | ||
self.edf_reader = EdfReader(self.filename) | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is
[0]
coming from?this whole block seems like a great idea, but I don't see the extra [0] in previous
_get_signal_size
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/NeuralEnsemble/python-neo/pull/1584/files#r1806665680