`run_sorter_jobs(engine='slurm')` writes an invalid Python script for a preprocessed recording

未关闭 适合新手
#5,001 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

从 sorters/launcher.py 中约第183–192行的 Slurm 脚本生成代码开始,然后检查录制数据的序列化处理方式。针对原始、已过滤和已切片的录制数据,分别编译生成的脚本以复现问题;报告中包含一个可运行示例。完成标准是三个脚本都能编译,并且包含引号或反斜杠的字符串和路径参数都能得到安全处理。

由索引模型根据 Issue 内容生成。

描述

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

With engine="slurm", the recording is written into the generated script as kwargs["recording"].to_dict() (line 192). Without recursive=True, the parent of a preprocessed or sliced recording is written as its repr, so the script is not valid Python. As sbatch does not parse the script, this would only show up when the job starts.

I have no slurm here, so the example mocks subprocess.run (no sbatch call) and compiles the generated script:

import contextlib
import io
import os
import subprocess
import tempfile
from pathlib import Path
from unittest import mock

from spikeinterface.core import generate_recording
from spikeinterface.preprocessing import bandpass_filter
from spikeinterface.sorters import run_sorter_jobs

if not hasattr(os, "fchmod"):  # only needed on Windows with Python < 3.13
    os.fchmod = lambda fd, mode: None


def slurm_script(recording, folder):
    # sbatch is not called: only the generated script is kept and compiled
    job = dict(sorter_name="simple", recording=recording, folder=(folder / "out").as_posix())
    done = subprocess.CompletedProcess(args=[], returncode=0, stdout="", stderr="")
    with mock.patch("subprocess.run", return_value=done), contextlib.redirect_stdout(io.StringIO()):
        run_sorter_jobs([job], engine="slurm", engine_kwargs=dict(tmp_script_folder=folder))
    script = folder / "si_script_0.py"
    try:
        compile(script.read_text(), str(script), "exec")
        return "compiles"
    except SyntaxError as e:
        return f"SyntaxError: {e.msg} (line {e.lineno})"


if __name__ == "__main__":
    recording = generate_recording(num_channels=4, durations=[5.0], seed=0)
    with tempfile.TemporaryDirectory() as tmp:
        print("raw:     ", slurm_script(recording, Path(tmp) / "raw"))
        print("filtered:", slurm_script(bandpass_filter(recording), Path(tmp) / "filtered"))
        print("sliced:  ", slurm_script(recording.select_channels(recording.channel_ids[:2]), Path(tmp) / "sliced"))

# Output:
# raw:      compiles
# filtered: SyntaxError: invalid decimal literal (line 6)
# sliced:   SyntaxError: invalid decimal literal (line 6)
# Expected: the script compiles in all three cases.

A string or path argument containing a quote or a backslash also breaks the script (lines 183-186).

Fix: write the recording to a JSON or pickle file next to the script and load it from there.

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

主要语言
Python
星标
883
派生
291
平均合并
3 天 17 小时
30 天内合并 PR
40

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

SpikeInterface/spikeinterface 的其他 Issue

查看 SpikeInterface/spikeinterface 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。