-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoad_Saved_Data_For_SpikeInterface.py
More file actions
96 lines (77 loc) · 3.04 KB
/
Load_Saved_Data_For_SpikeInterface.py
File metadata and controls
96 lines (77 loc) · 3.04 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 16 16:32:17 2025
@author: tonyd
"""
import json
import spikeinterface.full as si
import probeinterface as pi
from probeinterface.plotting import plot_probe
import matplotlib.pyplot as plt
# This function loads data in a Spikeinterface compatible format saved within Neuromod. It expects a .bin file containing
# amplifier channel data as well as a Meta_Data.json file with meta data (samplerate, event trigger times) and a probe.json containing information about probe geometry.
# specify the paths to those variables and execute this script in a environment in which spikeinterface is installed.
# Upon execution this script plots the probe desing with spikeinterface as well as the raw data trace overlayed by event trigger time points if present
Meta_Data_Path = "Path_to_your_file/Meta_Data.json";
Probe_Path = "Path_to_your_file/probe.json"
ChannelDataBinPath = "Path_to_your_file/SITest.bin";
'''
######################### Load Channel Data and Meta Data #########################
'''
with open(Meta_Data_Path) as f:
metadata = json.load(f)
SamplingRate = metadata["SampleRate"]
recording = si.read_binary(
file_paths=ChannelDataBinPath,
sampling_frequency=SamplingRate,
num_channels=metadata["num_channels"],
dtype=metadata["dtype"]
)
'''
######################### Load Event Data If Present #########################
'''
# --- Attach event data ---
if "EventStruct" in metadata:
for ev in metadata["EventStruct"]:
event_times = ev["times"] # list of samples
channel_name = ev["event_channel_name"]
print("Loaded events for", len(metadata["EventStruct"]), "event channels.")
'''
######################### Load Probe Data #########################
'''
# Load probe
probe_group = pi.read_probeinterface(Probe_Path)
# If you only have one probe, extract it
probe = probe_group.probes[0]
'''
######################### Plot Probe #########################
'''
# Plot the probe
plot_probe(probe, with_contact_id=True)
plt.show()
'''
######################### Plot Data (first channel, all time points) #########################
'''
PlotData = recording.get_traces(channel_ids=[0]).flatten() # shape (num_samples,)
# Plot signal
plt.figure(figsize=(12, 4))
plt.plot(PlotData, label="Signal", color="blue")
if "EventStruct" in metadata:
# Extract EventStruct
ev = metadata["EventStruct"]
# Handle single event channel vs multiple channels
if isinstance(ev, dict): # single channel
event_times = ev["times"]
event_name = ev["event_channel_name"]
elif isinstance(ev, list): # multiple channels
event_times = ev[0]["times"]
event_name = ev[0]["event_channel_name"]
else:
raise ValueError("Unexpected EventStruct format!")
# Plot events as vertical lines
plt.vlines(event_times, ymin=min(PlotData), ymax=max(PlotData), color="red", alpha=0.6, label=f"Events: {event_name}")
plt.xlabel("Sample index")
plt.ylabel("Amplitude")
plt.title("Signal Channel 1 with Events (if present)")
plt.legend()
plt.show()