Skip to content

Commit

Permalink
Merge pull request #2364 from h-mayorquin/add_rename_recording
Browse files Browse the repository at this point in the history
Add `rename_channels` method to recording extractors
  • Loading branch information
alejoe91 authored Dec 20, 2023
2 parents 0b821f3 + 5e011e0 commit 9f24c15
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/spikeinterface/core/baserecording.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,23 @@ def _extra_metadata_to_folder(self, folder):
if time_vector is not None:
np.save(folder / f"times_cached_seg{segment_index}.npy", time_vector)

def rename_channels(self, new_channel_ids: list | np.array | tuple):
"""
Returns a new recording object with renamed channel ids.
Parameters
----------
new_channel_ids : list or np.array or tuple
The new channel ids. They are mapped positionally to the old channel ids.
"""
from .channelslice import ChannelSliceRecording

assert len(new_channel_ids) == self.get_num_channels(), (
"new_channel_ids must have the same length as the " "number of channels in the recording"
)

return ChannelSliceRecording(self, renamed_channel_ids=new_channel_ids)

def _channel_slice(self, channel_ids, renamed_channel_ids=None):
from .channelslice import ChannelSliceRecording

Expand Down
7 changes: 7 additions & 0 deletions src/spikeinterface/core/tests/test_baserecording.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,12 @@ def test_BaseRecording():
assert np.allclose(rec_u.get_traces(cast_unsigned=True), rec_i.get_traces().astype("float"))


def test_rename_channels():
recording = generate_recording(durations=[1.0], num_channels=3)
renamed_recording = recording.rename_channels(new_channel_ids=["a", "b", "c"])
renamed_channel_ids = renamed_recording.get_channel_ids()
assert np.array_equal(renamed_channel_ids, ["a", "b", "c"])


if __name__ == "__main__":
test_BaseRecording()

0 comments on commit 9f24c15

Please sign in to comment.