9  UnitMatch (coding)

In the presentation, we saw why we want to track single neurons across recording sessions, and how UnitMatch and DeepUnitMatch identify matching units from their waveforms alone, without needing to concatenate and re-sort recordings together.

In this session, we will use UnitMatchPy and DeepUnitMatch to match units between two recording sessions from the same mouse, recorded roughly 24 hours apart.

9.1 Setup

Install UnitMatchPy UnitMatchPy should already be installed as part of the course setup, but in general it can be installed in any Python environment with:

pip install "UnitMatchPy[full,notebooks]"

Download the example data from this Dropbox folder (you can click “continue without signing up”, no Dropbox account needed).

The main dataset we will use below is AL032: two sessions from one mouse (also available on figshare). You should download this dataset.

You can optionally download the EB055 dataset (four sessions from one mouse, two of which include raw SpikeGLX data) which will be used in the extended exercises. However, it is a large dataset so don’t worry if you cannot download as we will not use it in the main exercises.

9.2 Exercise 1: Matching neurons with UnitMatchPy (~10 min)

  1. Open UMPy_example.ipynb in your favourite Python interpreter, pointed at the two AL032 sessions.
  2. Follow the demo until you reach a plot that looks like a matrix of black and white dots.
TipThings to try
  • Inspect the black-and-white matrix plot. How many neurons do you think were matched?
  • Run the UnitMatch GUI and browse through some of the matches. Do they look convincing to you?
  • Are there any matches within the same session? What would that mean (hint: think splits and merges)?

9.3 Exercise 2: Matching neurons with DeepUnitMatch (~15 min)

Now let’s compare against DeepUnitMatch, which replaces the hand-engineered matching step with a trained neural network.

  1. Open DeepUnitMatch.ipynb.
  2. For the same two AL032 sessions, use the pretrained DUM model to infer matches.
TipThings to try
  • How do the DeepUnitMatch matches compare to what you got from UnitMatchPy in Exercise 1?
  • Can you find a way to run the GUI on the DeepUnitMatch output? Are the results comparable to UnitMatchPy’s?
  • Do you understand the functional validation? What do the specific AUC values tell you about the matching quality?
  • Do you understand the tracking algorithm(s) at the end?

9.4 If you have more time

The exercises below go beyond the core session. They are here as a self-paced reference, not something we expect everyone to reach today.

TipExtension: multi-day tracking and training your own model
  • Multi-day tracking (~10-15 min): use DeepUnitMatch.ipynb on the EB055 dataset (four sessions) to see how DUM performs when tracking across more than two days.
  • Training your own model (~10-15 min): use DUM_training.ipynb to train a new DeepUnitMatch model on all available EB055 sessions, then infer matches across days with it. How does it compare to the pretrained model, and why might that be?
  • Extracting raw waveforms (~15 min): use extract_raw_data_demo.ipynb (or extract_raw_data_demo_open_ephys.ipynb for OpenEphys data) on the raw SpikeGLX EB055 sessions (or your own data), then run UnitMatchPy or DeepUnitMatch on the extracted waveforms.
  • Bonus - Use the spike interface integration (~10-15 min): use ‘UMPy_spike_interface_demo.ipynb’.
  • Bonus - try it on your own data (~15-30 min): if you don’t have chronic recordings, run UnitMatch on a single session instead, to check for oversplits.