-
Notifications
You must be signed in to change notification settings - Fork 223
Add a new widget to show the units topological distribution on the probe #3142
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
Open
FrancescoNegri
wants to merge
7
commits into
SpikeInterface:main
Choose a base branch
from
barbaLab:feature/unit-spatial-widget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6071518
Add UnitSpatialDistributionsWidget class
FrancescoNegri cfda649
Add KDE keyword arguments
FrancescoNegri 64f9788
Revert wrong changes in the widgets list
FrancescoNegri 574955a
Run pre-commit locally
FrancescoNegri 9e5330a
Add seaborn to widgets dependencies
FrancescoNegri ee76d37
Move plotting dependencies inside plot_matplotlib function
FrancescoNegri e679568
Add warning and error messages on Probe object
FrancescoNegri 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,152 @@ | ||
from __future__ import annotations | ||
|
||
import numpy as np | ||
from probeinterface import Probe | ||
from probeinterface.plotting import get_auto_lims | ||
from seaborn import color_palette | ||
from warnings import warn | ||
from .base import BaseWidget, to_attr | ||
|
||
|
||
class UnitSpatialDistributionsWidget(BaseWidget): | ||
""" | ||
Placeholder documentation to be changed. | ||
|
||
Parameters | ||
---------- | ||
sorting_analyzer : SortingAnalyzer | ||
The SortingAnalyzer object | ||
depth_axis : int, default: 1 | ||
The dimension of unit_locations that is depth | ||
""" | ||
|
||
def __init__( | ||
self, | ||
sorting_analyzer, | ||
probe=None, | ||
depth_axis=1, | ||
bins=None, | ||
cmap="viridis", | ||
kde=False, | ||
depth_hist=True, | ||
groups=None, | ||
kde_kws=None, | ||
backend=None, | ||
**backend_kwargs, | ||
): | ||
sorting_analyzer = self.ensure_sorting_analyzer(sorting_analyzer) | ||
|
||
self.check_extensions(sorting_analyzer, "unit_locations") | ||
ulc = sorting_analyzer.get_extension("unit_locations") | ||
unit_locations = ulc.get_data(outputs="numpy") | ||
x, y = unit_locations[:, 0], unit_locations[:, 1] | ||
|
||
if type(probe) is Probe: | ||
if sorting_analyzer.recording.has_probe(): | ||
# TODO: throw warning saying that sorting_analyzer has a probe and it will be overwritten | ||
pass | ||
elif sorting_analyzer.recording.has_probe(): | ||
probe = sorting_analyzer.get_probe() | ||
else: | ||
# TODO: throw error or warning, no probe available | ||
pass | ||
|
||
xrange, yrange, _ = get_auto_lims(probe, margin=0) | ||
if bins is None: | ||
bins = ( | ||
np.round(np.diff(xrange).squeeze() / 75).astype(int), | ||
np.round(np.diff(yrange).squeeze() / 75).astype(int), | ||
) | ||
# TODO: change behaviour, if bins is not defined, bin only along the depth axis | ||
|
||
if type(cmap) is str: | ||
cmap = color_palette(cmap, as_cmap=True) | ||
|
||
plot_data = dict( | ||
probe=probe, | ||
x=x, | ||
y=y, | ||
depth_axis=depth_axis, | ||
xrange=xrange, | ||
yrange=yrange, | ||
bins=bins, | ||
kde=kde, | ||
cmap=cmap, | ||
depth_hist=depth_hist, | ||
groups=groups, | ||
kde_kws=kde_kws, | ||
) | ||
|
||
BaseWidget.__init__(self, plot_data, backend=backend, **backend_kwargs) | ||
|
||
def plot_matplotlib(self, data_plot, **backend_kwargs): | ||
import matplotlib.patches as patches | ||
import matplotlib.path as path | ||
from seaborn import kdeplot, histplot | ||
from .utils_matplotlib import make_mpl_figure | ||
|
||
dp = to_attr(data_plot) | ||
|
||
self.figure, self.axes, self.ax = make_mpl_figure(**backend_kwargs) | ||
|
||
ax = self.ax | ||
|
||
custom_shape = path.Path(dp.probe.probe_planar_contour) | ||
patch = patches.PathPatch(custom_shape, facecolor="none", edgecolor="none") | ||
ax.add_patch(patch) | ||
|
||
if dp.kde is not True: | ||
hist, xedges, yedges = np.histogram2d(dp.x, dp.y, bins=dp.bins, range=[dp.xrange, dp.yrange]) | ||
pcm = ax.pcolormesh(xedges, yedges, hist.T, cmap=dp.cmap) | ||
else: | ||
kde_kws = dict(levels=100, thresh=0, fill=True, bw_adjust=0.1) | ||
if dp.kde_kws is not None: | ||
kde_kws.update(dp.kde_kws) | ||
data = dict(x=dp.x, y=dp.y) | ||
bg = ax.add_patch( | ||
patches.Rectangle( | ||
[dp.xrange[0], dp.yrange[0]], | ||
np.diff(dp.xrange).squeeze(), | ||
np.diff(dp.yrange).squeeze(), | ||
facecolor=dp.cmap.colors[0], | ||
fill=True, | ||
) | ||
) | ||
bg.set_clip_path(patch) | ||
kdeplot(data, x="x", y="y", clip=[dp.xrange, dp.yrange], cmap=dp.cmap, ax=ax, **kde_kws) | ||
pcm = ax.collections[0] | ||
ax.set_xlabel(None) | ||
ax.set_ylabel(None) | ||
|
||
pcm.set_clip_path(patch) | ||
|
||
xlim, ylim, _ = get_auto_lims(dp.probe, margin=10) | ||
ax.set_xlim(*xlim) | ||
ax.set_ylim(*ylim) | ||
ax.spines["top"].set_visible(False) | ||
ax.spines["bottom"].set_visible(False) | ||
ax.spines["right"].set_visible(False) | ||
ax.set_xticks([]) | ||
ax.set_xlabel("") | ||
ax.set_ylabel("Depth (um)") | ||
|
||
if dp.depth_hist is True: | ||
bbox = ax.get_window_extent() | ||
hist_height = 1.5 * bbox.width | ||
|
||
ax_hist = ax.inset_axes([1, 0, hist_height / bbox.width, 1]) | ||
data = dict(y=dp.y) | ||
data["group"] = np.ones(dp.y.size) if dp.groups is None else dp.groups | ||
palette = color_palette("bright", n_colors=1 if dp.groups is None else np.unique(dp.groups).size) | ||
histplot( | ||
data=data, | ||
y="y", | ||
hue="group", | ||
bins=dp.bins[1], | ||
binrange=dp.yrange, | ||
palette=palette, | ||
ax=ax_hist, | ||
legend=False, | ||
) | ||
ax_hist.axis("off") | ||
ax_hist.set_ylim(*ylim) |
This file contains hidden or 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
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.
you should move these to the
plot_matplotlib
function. Alsoseaborn
is not installed by default. Could you use a matplotlib palette?Uh oh!
There was an error while loading. Please reload this page.
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.
I can definitely use a
matplotlib
palette, but I was thinking to add an optionalseaborn
import (if available) to enable alsoseaborn
palettes. I usedseaborn
also in other parts of the function, I will try to fix it, though I am not sure I will be able to easily plot kernel desnity estimates withmatplotlib
only.Is there a specific reason to move probeinterface imports to the
plot_matplotlib
function? I use itsget_auto_lims
function to computexrange
andyrange
, that are independent on the visualization backend.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.
yes that souns good! The reason are the failing tests ;)
Core tests only install minimal dependencies. Upon collecting tests across modules, if something is not installed it'll throw an error.
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.
I checked the core tests. It seems that
probeinterface
is installed, butmatplotlib
is not, thusprobeinterface.plotting
cannot be imported. Is that right?