File size: 1,788 Bytes
0f830b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Gradio interface for SpikeCascade. Drop a .npy file containing a
(n_channels, n_samples) voltage array, and the Space returns the
sorted units, the calibration report, and the activity heatmap.
"""

import gradio as gr
import numpy as np
import matplotlib.pyplot as plt

from modeling_spikecascade import SpikeCascadeForSorting


MODEL = SpikeCascadeForSorting.from_pretrained("your-org/SpikeCascade")


def sort(voltage_file):
    voltage = np.load(voltage_file.name).astype(np.float32)
    report = MODEL.infer(voltage)

    # Raster plot
    fig, ax = plt.subplots(figsize=(8, 3))
    for uid, times in report.units.items():
        ax.scatter(times, [uid] * len(times), s=4, marker="|")
    ax.set_xlabel("time (ms)")
    ax.set_ylabel("unit")
    ax.set_title(
        f"activity={report.activity_fraction:.3f}  "
        f"stage3={report.stage3_fraction:.3f}  "
        f"latency={report.latency_ms:.2f} ms  "
        f"corr={report.correlation:.3f}"
    )

    return (
        fig,
        report.activity_fraction,
        report.stage3_fraction,
        report.latency_ms,
        report.correlation,
    )


demo = gr.Interface(
    fn=sort,
    inputs=gr.File(label="voltage (.npy, shape = n_channels × n_samples)"),
    outputs=[
        gr.Plot(label="Spike raster"),
        gr.Number(label="activity_fraction"),
        gr.Number(label="stage3_fraction"),
        gr.Number(label="latency_ms"),
        gr.Number(label="correlation"),
    ],
    title="SpikeCascade — real-time Neuropixels spike sorting",
    description=(
        "Three-stage fidelity cascade. The calibration report is printed "
        "with every sort: activity_fraction, stage3_fraction, latency_ms, "
        "and correlation (NaN if the input contains zero spikes)."
    ),
)

demo.launch()