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

Add an option to flip the order by depth #2005

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion src/spikeinterface/core/recording_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def get_chunk_with_margin(
return traces_chunk, left_margin, right_margin


def order_channels_by_depth(recording, channel_ids=None, dimensions=("x", "y")):
def order_channels_by_depth(recording, channel_ids=None, dimensions=("x", "y"), flip=False):
Copy link
Collaborator

@h-mayorquin h-mayorquin Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe using the standard name reversed from the python API would be a good idea?

https://docs.python.org/3/library/functions.html#sorted

It is also supported by the list.sort

But maybe you think that flip describes it better?
Just throwing something that I thought.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, on the other hand we have:

https://numpy.org/doc/stable/reference/generated/numpy.flip.html

Which seems to be aligned withat what you are doing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started with reversed and finally change it to flip at the very last moment. I think flip is ok

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I also think that your docstring is doing the heavy lifting there anyway.

"""
Order channels by depth, by first ordering the x-axis, and then the y-axis.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question out of curiosity, do we have a convention that x goes along the width or the recorder and that y goes in the height / depth (the direction where it is usualy moves) or is it the other way around?

I found myself realizing that I did not know what to expect after reading this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @h-mayorquin

Yes the convention is that x is width and y is depth. The ('x', 'y') tuple is fed into lexsort, which sorts outer to innermost dimensions ;)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @alejoe91.


Expand All @@ -316,6 +316,9 @@ def order_channels_by_depth(recording, channel_ids=None, dimensions=("x", "y")):
If str, it needs to be 'x', 'y', 'z'.
If tuple or list, it sorts the locations in two dimensions using lexsort.
This approach is recommended since there is less ambiguity, by default ('x', 'y')
flip: bool, default False
samuelgarcia marked this conversation as resolved.
Show resolved Hide resolved
If flip is False then the order is bottom first (starting from tip of the probe).
If flip is True then the order is upper first.

Returns
-------
Expand All @@ -341,6 +344,8 @@ def order_channels_by_depth(recording, channel_ids=None, dimensions=("x", "y")):
assert dim < ndim, "Invalid dimensions!"
locations_to_sort += (locations[:, dim],)
order_f = np.lexsort(locations_to_sort)
if flip:
order_f = order_f[::-1]
order_r = np.argsort(order_f, kind="stable")

return order_f, order_r
Expand Down
2 changes: 2 additions & 0 deletions src/spikeinterface/core/tests/test_recording_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ def test_order_channels_by_depth():
order_1d, order_r1d = order_channels_by_depth(rec, dimensions="y")
order_2d, order_r2d = order_channels_by_depth(rec, dimensions=("x", "y"))
locations_rev = locations_copy[order_1d][order_r1d]
order_2d_fliped, order_r2d_fliped = order_channels_by_depth(rec, dimensions=("x", "y"), flip=True)

assert np.array_equal(locations[:, 1], locations_copy[order_1d][:, 1])
assert np.array_equal(locations_copy[order_1d][:, 1], locations_copy[order_2d][:, 1])
assert np.array_equal(locations, locations_copy[order_2d])
assert np.array_equal(locations_copy, locations_copy[order_2d][order_r2d])
assert np.array_equal(order_2d[::-1], order_2d_fliped)


if __name__ == "__main__":
Expand Down
10 changes: 8 additions & 2 deletions src/spikeinterface/preprocessing/depth_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ class DepthOrderRecording(ChannelSliceRecording):
If str, it needs to be 'x', 'y', 'z'.
If tuple or list, it sorts the locations in two dimensions using lexsort.
This approach is recommended since there is less ambiguity, by default ('x', 'y')
flip: bool, default False
samuelgarcia marked this conversation as resolved.
Show resolved Hide resolved
If flip is False then the order is bottom first (starting from tip of the probe).
If flip is True then the order is upper first.
"""

name = "depth_order"
installed = True

def __init__(self, parent_recording, channel_ids=None, dimensions=("x", "y")):
order_f, order_r = order_channels_by_depth(parent_recording, channel_ids=channel_ids, dimensions=dimensions)
def __init__(self, parent_recording, channel_ids=None, dimensions=("x", "y"), flip=False):
order_f, order_r = order_channels_by_depth(
parent_recording, channel_ids=channel_ids, dimensions=dimensions, flip=flip
)
reordered_channel_ids = parent_recording.channel_ids[order_f]
ChannelSliceRecording.__init__(
self,
Expand All @@ -35,6 +40,7 @@ def __init__(self, parent_recording, channel_ids=None, dimensions=("x", "y")):
parent_recording=parent_recording,
channel_ids=channel_ids,
dimensions=dimensions,
flip=flip,
)


Expand Down