Skip to content

Commit ef26f1e

Browse files
authored
Merge branch 'main' into ks4
2 parents 92dc448 + 815b2f3 commit ef26f1e

22 files changed

Lines changed: 688 additions & 347 deletions

File tree

doc/development/development.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,52 @@ After this you need to add a block in `Install Sorters <https://github.com/Spike
397397
to describe your sorter.
398398

399399
Finally, make a pull request so we can review the code and incorporate into the sorters module of SpikeInterface!
400+
401+
402+
403+
How to make a release
404+
---------------------
405+
406+
Checklist
407+
^^^^^^^^^
408+
* pyproject.toml: check that the version is ahead of current release. Also, comment out the @ (git dependencies)
409+
* In the top level ``__init__`` (located at ``src/spikeinterface/__init__.py``) set ``DEV_MODE`` to ``False`` (this is used for the docker installations)
410+
* Create a new release note for the appropriate version on doc/releases/new_version_tag.
411+
412+
There can be large releases like:
413+
414+
``doc/releases/0.101.0.rst``
415+
416+
Which contain a section called "Main Changes" and minor releases which include only bug fixes like:
417+
418+
``doc/releases/0.101.2.rst``
419+
420+
To collect all the PRs and bug fixes we have a script in:
421+
``doc/scripts/``
422+
called ``auto-release-notes.sh``. Run it with ``bash auto-release-notes.sh`` and it will create the release notes for the module specific changes.
423+
424+
The first time you run the script, GitHub will guide you through an authorization process if you've not already done so.
425+
426+
The signature of the script is:
427+
428+
.. code-block:: bash
429+
430+
bash auto-release-notes.sh <start_date> <end_date>
431+
432+
Where the start date is the date of the last release and the end date is the current date. Dates are in YYYY-MM-DD format
433+
434+
The date of the last release can be found on `PyPI <https://pypi.org/project/spikeinterface/>`_.
435+
436+
437+
As a specific example:
438+
.. code-block:: bash
439+
440+
bash auto-release-notes.sh 2025-02-19 2025-03-24
441+
442+
* Finish the release notes and merge
443+
* Locally tag the main branch with the newly merged release notes with the new version
444+
* Push the tag to the remote repository which will trigger the release action (.github/workflows/publish-to-pypi.yml)
445+
* Do an after-release `PR <https://github.com/SpikeInterface/spikeinterface/pull/3828/files>`_:
446+
- Uncomment the git installs in pyproject
447+
- Set ``DEV_MODE`` to ``True`` in the top level ``__init__`` (located at ``src/spikeinterface/__init__.py``)
448+
- Update `pyproject.toml` version one patch ahead or one minor if it is larger one.

doc/how_to/customize_a_plot.rst

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
Customize a plot
2+
================
3+
4+
The ``SpikeInterface`` widgets are designed to have reasonable default
5+
plotting options, but sometimes you’ll want to make adjustments to the
6+
plots. The plotting functions all return a ``Widget`` object. These
7+
contain and give you access to the underlying matplotlib figure and
8+
axis, which you can apply any matplotlib machinery to. Let’s see how to
9+
do this in an example, by first making some synthetic data and computing
10+
extensions which can be used for plotting.
11+
12+
.. code::
13+
14+
import spikeinterface.full as si
15+
import matplotlib.pyplot as plt
16+
17+
recording, sorting = si.generate_ground_truth_recording(seed=1205)
18+
sorting_analyzer = si.create_sorting_analyzer(sorting=sorting, recording=recording)
19+
sorting_analyzer.compute({"random_spikes": {'seed': 1205}, "templates": {}, "unit_locations": {}})
20+
21+
unit_locations = sorting_analyzer.get_extension("unit_locations").get_data()
22+
23+
24+
25+
.. parsed-literal::
26+
27+
estimate_sparsity (no parallelization): 0%| | 0/10 [00:00<?, ?it/s]
28+
29+
30+
31+
.. parsed-literal::
32+
33+
estimate_templates_with_accumulator (no parallelization): 0%| | 0/10 [00:00<?, ?it/s]
34+
35+
36+
Now we can plot the ``unit_locations`` and ``unit_templates`` using the
37+
appropriate widgets (see the `full list of
38+
widgets <https://spikeinterface.readthedocs.io/en/stable/modules/widgets.html#available-plotting-functions>`__
39+
for more!). These functions output a ``Widget object``. We’ll assign the
40+
unit locations widget to ``fig_units``.
41+
42+
.. code::
43+
44+
fig_units = si.plot_unit_locations(sorting_analyzer)
45+
46+
# Each widget contains a `matplotlib` figure and axis:
47+
print(type(fig_units.figure))
48+
print(type(fig_units.ax))
49+
50+
51+
.. parsed-literal::
52+
53+
<class 'matplotlib.figure.Figure'>
54+
<class 'matplotlib.axes._axes.Axes'>
55+
56+
57+
58+
.. image:: customize_a_plot_files/customize_a_plot_4_1.png
59+
60+
61+
By gaining access to the matplotlib objects, we are able to utilize the
62+
full ``matplotlib`` machinery: adding custom titles, axis labels, ticks,
63+
more plots etc. Let’s customize our unit locations plot. (Note: the
64+
``SpikeInterface`` Team does not endorse the following style
65+
conventions):
66+
67+
.. code::
68+
69+
# Get the widget
70+
fig_units = si.plot_unit_locations(sorting_analyzer)
71+
72+
# Modify the widget's `axis`` to set the title and axes labels
73+
fig_units.ax.set_title("My favorite units", fontname = "Comic Sans MS")
74+
fig_units.ax.set_xlabel("x probe location (um)")
75+
fig_units.ax.set_ylabel("y probe location (um)")
76+
77+
# You can also set custom ticks
78+
fig_units.ax.set_xticks([-60,-30,unit_locations[0,0],30,60])
79+
fig_units.ax.set_xticklabels([-60,-30,"unit_0_x",30,60])
80+
fig_units.ax.set_yticks([-40,-20,0,unit_locations[0,1],40])
81+
fig_units.ax.set_yticklabels([-40,-20,0,"unit_0_y",40])
82+
83+
# Change the limits of the plot
84+
fig_units.ax.set_xlim((-30,50))
85+
fig_units.ax.set_ylim((-50,50))
86+
87+
# And add extra information on the plot
88+
fig_units.ax.text(unit_locations[6,0], unit_locations[6,1]+5, s="UNIT 6!!!", fontname="Courier")
89+
90+
fig_units
91+
92+
93+
94+
95+
.. parsed-literal::
96+
97+
<spikeinterface.widgets.unit_locations.UnitLocationsWidget at 0x147a81520>
98+
99+
100+
101+
102+
.. image:: customize_a_plot_files/customize_a_plot_6_1.png
103+
104+
105+
Beautiful!!!
106+
107+
You can also combine figures into a multi-figure plot. The easiest way
108+
to do this is to set up your figure and axes first, then tell
109+
``SpikeInterface`` which axes it should attach the widget plot to.
110+
Here’s an example of making a unit summary plot.
111+
112+
.. code::
113+
114+
import matplotlib.pyplot as plt
115+
fig, axs = plt.subplots(ncols=2, nrows=1)
116+
117+
unit_id=8
118+
si.plot_unit_locations(sorting_analyzer=sorting_analyzer, ax=axs[0])
119+
si.plot_unit_templates(sorting_analyzer, axes=[axs[1]], unit_ids=[f'{unit_id}'])
120+
121+
axs[0].plot([unit_locations[8,0], unit_locations[8,0]+50], [unit_locations[8,1], unit_locations[8,1]+50])
122+
axs[0].text(unit_locations[8,0]+52, unit_locations[8,1]+52, s=f"Unit {unit_id}")
123+
axs[0].set_title("Unit location", fontsize=10)
124+
125+
fig.suptitle(f"Unit {unit_id} summary", fontfamily="Comic Sans MS", fontsize=20)
126+
127+
fig.tight_layout()
128+
129+
130+
131+
132+
.. image:: customize_a_plot_files/customize_a_plot_8_1.png
133+
134+
135+
For more details on what you can do using matplotlib, check out their
136+
`extensive documentation <https://matplotlib.org/stable/>`__
24.8 KB
Loading
35.2 KB
Loading
53.5 KB
Loading

doc/how_to/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ Guides on how to solve specific, short problems in SpikeInterface. Learn how to.
1717
drift_with_lfp
1818
auto_curation_training
1919
auto_curation_prediction
20+
customize_a_plot

examples/how_to/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ with `nbconvert`. Here are the steps (in this example for the `get_started`):
1414

1515
```
1616
>>> jupytext --to notebook get_started.py
17-
>>> jupytext --set-formats ipynb,py get_started.ipynb
17+
>>> jupytext --set-formats ipynb.py get_started.ipynb
1818
```
1919

2020
2. Run the notebook
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# cell_metadata_filter: -all
5+
# formats: py:light,ipynb
6+
# text_representation:
7+
# extension: .py
8+
# format_name: light
9+
# format_version: '1.5'
10+
# jupytext_version: 1.17.0
11+
# kernelspec:
12+
# display_name: .venv
13+
# language: python
14+
# name: python3
15+
# ---
16+
17+
# # Customize a plot
18+
19+
# The `SpikeInterface` widgets are designed to have reasonable default plotting options, but
20+
# sometimes you'll want to make adjustments to the plots. The plotting functions all return
21+
# a `Widget` object. These contain and give you access to the underlying matplotlib figure
22+
# and axis, which you can apply any matplotlib machinery to. Let's see how to do this in an
23+
# example, by first making some synthetic data and computing extensions which can be used for plotting.
24+
25+
# +
26+
import spikeinterface.full as si
27+
import matplotlib.pyplot as plt
28+
29+
recording, sorting = si.generate_ground_truth_recording(seed=1205)
30+
sorting_analyzer = si.create_sorting_analyzer(sorting=sorting, recording=recording)
31+
sorting_analyzer.compute({"random_spikes": {'seed': 1205}, "templates": {}, "unit_locations": {}})
32+
33+
unit_locations = sorting_analyzer.get_extension("unit_locations").get_data()
34+
# -
35+
36+
# Now we can plot the `unit_locations` and `unit_templates` using the appropriate widgets
37+
# (see the [full list of widgets](https://spikeinterface.readthedocs.io/en/stable/modules/widgets.html#available-plotting-functions)
38+
# for more!). These functions output a `Widget object`. We'll assign the unit locations widget to `fig_units`.
39+
40+
# +
41+
fig_units = si.plot_unit_locations(sorting_analyzer)
42+
43+
# Each widget contains a `matplotlib` figure and axis:
44+
print(type(fig_units.figure))
45+
print(type(fig_units.ax))
46+
# -
47+
48+
# By gaining access to the matplotlib objects, we are able to utilize the full `matplotlib`
49+
# machinery: adding custom titles, axis labels, ticks, more plots etc. Let's customize
50+
# our unit locations plot. (Note: the `SpikeInterface` Team does not endorse the following style conventions):
51+
52+
# +
53+
# Get the widget
54+
fig_units = si.plot_unit_locations(sorting_analyzer)
55+
56+
# Modify the widget's `axis`` to set the title and axes labels
57+
fig_units.ax.set_title("My favorite units", fontname = "Comic Sans MS")
58+
fig_units.ax.set_xlabel("x probe location (um)")
59+
fig_units.ax.set_ylabel("y probe location (um)")
60+
61+
# You can also set custom ticks
62+
fig_units.ax.set_xticks([-60,-30,unit_locations[0,0],30,60])
63+
fig_units.ax.set_xticklabels([-60,-30,"unit_0_x",30,60])
64+
fig_units.ax.set_yticks([-40,-20,0,unit_locations[0,1],40])
65+
fig_units.ax.set_yticklabels([-40,-20,0,"unit_0_y",40])
66+
67+
# Change the limits of the plot
68+
fig_units.ax.set_xlim((-30,50))
69+
fig_units.ax.set_ylim((-50,50))
70+
71+
# And add extra information on the plot
72+
fig_units.ax.text(unit_locations[6,0], unit_locations[6,1]+5, s="UNIT 6!!!", fontname="Courier")
73+
74+
fig_units
75+
# -
76+
77+
# Beautiful!!!
78+
#
79+
# You can also combine figures into a multi-figure plot. The easiest way to do this is to set up your
80+
# figure and axes first, then tell `SpikeInterface` which axes it should attach the widget plot to.
81+
# Here's an example of making a unit summary plot.
82+
83+
# +
84+
import matplotlib.pyplot as plt
85+
fig, axs = plt.subplots(ncols=2, nrows=1)
86+
87+
unit_id=8
88+
si.plot_unit_locations(sorting_analyzer=sorting_analyzer, ax=axs[0])
89+
si.plot_unit_templates(sorting_analyzer, axes=[axs[1]], unit_ids=[f'{unit_id}'])
90+
91+
axs[0].plot([unit_locations[8,0], unit_locations[8,0]+50], [unit_locations[8,1], unit_locations[8,1]+50])
92+
axs[0].text(unit_locations[8,0]+52, unit_locations[8,1]+52, s=f"Unit {unit_id}")
93+
axs[0].set_title("Unit location", fontsize=10)
94+
95+
fig.suptitle(f"Unit {unit_id} summary", fontfamily="Comic Sans MS", fontsize=20)
96+
97+
fig.tight_layout()
98+
# -
99+
100+
# For more details on what you can do using matplotlib, check out their [extensive documentation](https://matplotlib.org/stable/)

src/spikeinterface/core/analyzer_extension_core.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ class ComputeNoiseLevels(AnalyzerExtension):
709709
depend_on = []
710710
need_recording = True
711711
use_nodepipeline = False
712-
need_job_kwargs = False
712+
need_job_kwargs = True
713713
need_backward_compatibility_on_load = True
714714

715715
def __init__(self, sorting_analyzer):
@@ -729,9 +729,12 @@ def _merge_extension_data(
729729
# this does not depend on units
730730
return self.data.copy()
731731

732-
def _run(self, verbose=False):
732+
def _run(self, verbose=False, **job_kwargs):
733733
self.data["noise_levels"] = get_noise_levels(
734-
self.sorting_analyzer.recording, return_scaled=self.sorting_analyzer.return_scaled, **self.params
734+
self.sorting_analyzer.recording,
735+
return_scaled=self.sorting_analyzer.return_scaled,
736+
**self.params,
737+
**job_kwargs,
735738
)
736739

737740
def _get_data(self):

src/spikeinterface/core/binaryrecordingextractor.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ def get_binary_description(self):
148148
)
149149
return d
150150

151+
def __del__(self):
152+
"""
153+
Ensures that all segment resources are properly cleaned up when this recording extractor is deleted.
154+
Closes any open file handles in the recording segments.
155+
"""
156+
# Close all recording segments
157+
if hasattr(self, "_recording_segments"):
158+
for segment in self._recording_segments:
159+
# This will trigger the __del__ method of the BinaryRecordingSegment
160+
# which will close the file handle
161+
del segment
162+
151163

152164
BinaryRecordingExtractor.write_recording.__doc__ = BinaryRecordingExtractor.write_recording.__doc__.format(
153165
_shared_job_kwargs_doc
@@ -223,6 +235,15 @@ def get_traces(
223235

224236
return traces
225237

238+
def __del__(self):
239+
# Ensure that the file handle is closed when the segment is garbage-collected
240+
try:
241+
if hasattr(self, "file") and self.file and not self.file.closed:
242+
self.file.close()
243+
except Exception as e:
244+
warnings.warn(f"Error closing file handle in BinaryRecordingSegment: {e}")
245+
pass
246+
226247

227248
# For backward compatibility (old good time)
228249
BinDatRecordingExtractor = BinaryRecordingExtractor

0 commit comments

Comments
 (0)