`interpolate_motion` loses `interpolation_time_bin_size_s` when the recording is rebuilt from its kwargs

Open Beginner friendly
#4,998 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start in sortingcomponents/motion/motion_interpolation.py and inspect how InterpolateMotionRecording stores and uses interpolation time bin settings when rebuilding from _kwargs. Add a regression test for clone() and parallel save() using the supplied example; done means both rebuilt recordings return traces identical to the original when bin size and edges are provided.

Written by the indexing model from the issue text.

Description

I found this while reading sortingcomponents/motion/motion_interpolation.py, not in my own analyses, so this is a "for the sake of improvement" report.

InterpolateMotionRecording._kwargs does not store interpolation_time_bin_size_s, and the stored interpolation_time_bin_centers_s is None in that case. A recording rebuilt from _kwargs (clone(), or the workers of save(n_jobs=2) on Windows, where they are started with spawn) therefore returns different traces, without an error. Reading the code, it falls back to the time bins of the motion.

import tempfile

import numpy as np
from spikeinterface.core import generate_recording
from spikeinterface.core.motion import Motion
from spikeinterface.sortingcomponents.motion import interpolate_motion

if __name__ == "__main__":
    recording = generate_recording(num_channels=32, durations=[10.0], seed=0)
    motion = Motion([np.linspace(0, 100, 5)[:, None]], [np.arange(5) * 2.0 + 1.0], np.array([100.0]))
    corrected = interpolate_motion(
        recording, motion, border_mode="force_extrapolate", interpolation_time_bin_size_s=0.1
    )
    traces = corrected.get_traces()
    print("in _kwargs:", "interpolation_time_bin_size_s" in corrected._kwargs)
    print("signal std:", traces.std())
    print("clone, max abs diff:", np.abs(corrected.clone().get_traces() - traces).max())
    with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as folder:
        saved = corrected.save(folder=folder + "/saved", n_jobs=2, chunk_duration="1s", progress_bar=False)
        print("save(n_jobs=2), max abs diff:", np.abs(saved.get_traces() - traces).max())
        del saved

# Output on current main (Windows):
# in _kwargs: False
# signal std: 0.8311724
# clone, max abs diff: 4.6557436
# save(n_jobs=2), max abs diff: 4.6557436
# Expected: both differences equal to 0

Fix: store interpolation_time_bin_size_s and interpolation_time_bin_edges_s in _kwargs, with the values given by the user. Reading the code, interpolation_time_bin_edges_s has the same problem: only the centers are stored and the edges are recomputed from them.

I can open a PR with a test if you agree.

Dominant language
Python
Stars
883
Forks
291
Avg merge
3d 17h
Merged PRs (30d)
40

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from SpikeInterface/spikeinterface

All issues in SpikeInterface/spikeinterface

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.