Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python data loading script for neuropixels 1.0e headstage workflow #135

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions workflows/hardware/np1e/bno055.bonsai
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
<Expression xsi:type="Combinator">
<Combinator xsi:type="onix1:PolledBno055Data">
<onix1:DeviceName>NeuropixelsV1eHeadstage/PolledBno055</onix1:DeviceName>
<onix1:PolledRegisters>All</onix1:PolledRegisters>
</Combinator>
</Expression>
<Expression xsi:type="io:CsvWriter">
<io:FileName>bno055_.csv</io:FileName>
<io:Append>false</io:Append>
<io:Overwrite>false</io:Overwrite>
<io:Suffix>Timestamp</io:Suffix>
<io:Suffix>FileCount</io:Suffix>
<io:IncludeHeader>false</io:IncludeHeader>
<io:Selector>it</io:Selector>
<io:Selector>Clock,EulerAngle,Quaternion,Acceleration,Gravity,Temperature</io:Selector>
</Expression>
<Expression xsi:type="MemberSelector">
<Selector>Quaternion</Selector>
Expand Down
82 changes: 76 additions & 6 deletions workflows/hardware/np1e/configuration.bonsai

Large diffs are not rendered by default.

110 changes: 110 additions & 0 deletions workflows/hardware/np1e/load-np1e.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import numpy as np
import matplotlib.pyplot as plt

# Load data from neuropixels board tutorial workflow
suffix = '4'; # Change to match file names' suffix
np_ap_gain = 1000 # Change to the AP band gain used
np_lfp_gain = 50 # Change to the lfp band gain used

plt.close('all')

#%% Metadata
dt = {'names': ('time', 'acq_clk_hz', 'block_read_sz', 'block_write_sz'),
'formats': ('datetime64[us]', 'u4', 'u4', 'u4')}
meta = np.genfromtxt('start-time_' + suffix + '.csv', delimiter=',', dtype=dt, skip_header=1)
print(f"Recording was started at {meta['time']} GMT")

#%% Hardware FIFO buffer use
dt = {'names': ('clock', 'bytes', 'percent'),
'formats': ('u8', 'u4', 'f8')}
memory_use = np.genfromtxt('memory-use_' + suffix + '.csv', delimiter=',', dtype=dt)

plt.figure()
plt.plot(memory_use['clock'] / meta['acq_clk_hz'], memory_use['percent'])
plt.xlabel("time (sec)")
plt.ylabel("FIFO used (%)")


#%% Neuropixels 1.0
start_t = 9.0 # when to start plotting data (seconds)
dur = 1.0 # duration of data to plot
plot_channel_offset_uV = 100 # Vertical offset between each channel in the time series

npx = {}
npx['spike_time'] = np.fromfile('np1-clock_' + suffix + '.raw', dtype=np.uint64) / meta['acq_clk_hz']
npx['spike'] = np.reshape(np.fromfile('np1-spike_' + suffix + '.raw', dtype=np.uint16), (-1, 384))

npx['lfp_time'] = npx['spike_time'][::12] # 12 spike samples per lfp sample
npx['lfp'] = np.reshape(np.fromfile('np1-lfp_' + suffix + '.raw', dtype=np.uint16), (-1, 384))

# Make arrays for plotting
b = np.bitwise_and(npx['spike_time'] >= start_t, npx['spike_time'] < start_t + dur)
spike_time = npx['spike_time'][b]
spike_wave = npx['spike'][b, :].astype(np.double)
b = np.bitwise_and(npx['lfp_time'] >= start_t, npx['lfp_time'] < start_t + dur)
lfp_time = npx['lfp_time'][b]
lfp_wave = npx['lfp'][b, :].astype(np.double)

# Convert to uV and offset each channel by some plot_channel_offset_uV
ap_uV_per_lsb = 1e6 * 1.2 / 1024 / np_ap_gain
lfp_uV_per_lsb = 1e6 * 1.2 / 1024 / np_lfp_gain
spike_wave = (spike_wave - 512) * ap_uV_per_lsb + np.arange(spike_wave.shape[1])[None, :] * plot_channel_offset_uV
lfp_wave = (lfp_wave - 512) * lfp_uV_per_lsb + np.arange(lfp_wave.shape[1])[None, :] * plot_channel_offset_uV

plt.figure()
plt.subplot(121)
plt.plot(spike_time, spike_wave, 'k', linewidth=0.25)
plt.tick_params(axis='y', which='both', left=False, right=False, labelleft=False)
plt.xlabel("time (sec)")
plt.ylabel("channel")
plt.title('Spike Band')

plt.subplot(122)
plt.plot(lfp_time, lfp_wave, 'k', linewidth=0.25)
plt.tick_params(axis='y', which='both', left=False, right=False, labelleft=False)
plt.xlabel("time (sec)")
plt.ylabel("channel")
plt.title('LFP Band')

#%% Bno055
dt = {'names': ('clock', 'euler', 'quat', 'is_quat_id', 'accel', 'grav', 'temp'),
'formats': ('u8', '(1,3)f8', '(1,4)f8', '?', '(1,3)f8', '(1,3)f8', 'f8')}
bno055 = np.genfromtxt('bno055_' + suffix + '.csv', delimiter=',', dtype=dt)

bno055_time = bno055['clock'] / meta['acq_clk_hz']

plt.figure()
plt.subplot(231)
plt.plot(bno055_time, bno055['euler'].squeeze())
plt.xlabel("time (sec)")
plt.ylabel("angle (deg.)")
plt.ylim(-185, 365)
plt.legend(['yaw', 'pitch', 'roll'])
plt.title('Euler')

plt.subplot(232)
plt.plot(bno055_time, bno055['quat'].squeeze())
plt.xlabel("time (sec)")
plt.ylim(-1.1, 1.1)
plt.legend(['X', 'Y', 'Z', 'W'])
plt.title('Quaternion')

plt.subplot(233)
plt.plot(bno055_time, bno055['accel'].squeeze())
plt.xlabel("time (sec)")
plt.ylabel("accel. (m/s^2)")
plt.legend(['X', 'Y', 'Z'])
plt.title('Lin. Accel.')

plt.subplot(234)
plt.plot(bno055_time, bno055['grav'].squeeze())
plt.xlabel("time (sec)")
plt.ylabel("accel. (m/s^2)")
plt.legend(['X', 'Y', 'Z'])
plt.title('Gravity Vec.')

plt.subplot(235)
plt.plot(bno055_time, bno055['temp'].squeeze())
plt.xlabel("time (sec)")
plt.ylabel("temp. (deg. C)")
plt.title('Headstage Temp.')
31 changes: 31 additions & 0 deletions workflows/hardware/np1e/memory-monitor.bonsai
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<WorkflowBuilder Version="2.8.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:onix1="clr-namespace:OpenEphys.Onix1;assembly=OpenEphys.Onix1"
xmlns:io="clr-namespace:Bonsai.IO;assembly=Bonsai.System"
xmlns="https://bonsai-rx.org/2018/workflow">
<Workflow>
<Nodes>
<Expression xsi:type="Combinator">
<Combinator xsi:type="onix1:MemoryMonitorData">
<onix1:DeviceName>BreakoutBoard/MemoryMonitor</onix1:DeviceName>
</Combinator>
</Expression>
<Expression xsi:type="io:CsvWriter">
<io:FileName>memory-use_.csv</io:FileName>
<io:Append>false</io:Append>
<io:Overwrite>false</io:Overwrite>
<io:Suffix>FileCount</io:Suffix>
<io:IncludeHeader>false</io:IncludeHeader>
<io:Selector>Clock,BytesUsed,PercentUsed</io:Selector>
</Expression>
<Expression xsi:type="MemberSelector">
<Selector>PercentUsed</Selector>
</Expression>
</Nodes>
<Edges>
<Edge From="0" To="1" Label="Source1" />
<Edge From="1" To="2" Label="Source1" />
</Edges>
</Workflow>
</WorkflowBuilder>
6 changes: 3 additions & 3 deletions workflows/hardware/np1e/np1.bonsai
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Expression xsi:type="Combinator">
<Combinator xsi:type="dsp:MatrixWriter">
<dsp:Path>np1-clock_.raw</dsp:Path>
<dsp:Suffix>Timestamp</dsp:Suffix>
<dsp:Suffix>FileCount</dsp:Suffix>
<dsp:Overwrite>false</dsp:Overwrite>
<dsp:Layout>ColumnMajor</dsp:Layout>
</Combinator>
Expand All @@ -29,7 +29,7 @@
<Expression xsi:type="Combinator">
<Combinator xsi:type="dsp:MatrixWriter">
<dsp:Path>np1-spike_.raw</dsp:Path>
<dsp:Suffix>Timestamp</dsp:Suffix>
<dsp:Suffix>FileCount</dsp:Suffix>
<dsp:Overwrite>false</dsp:Overwrite>
<dsp:Layout>ColumnMajor</dsp:Layout>
</Combinator>
Expand All @@ -40,7 +40,7 @@
<Expression xsi:type="Combinator">
<Combinator xsi:type="dsp:MatrixWriter">
<dsp:Path>np1-lfp_.raw</dsp:Path>
<dsp:Suffix>Timestamp</dsp:Suffix>
<dsp:Suffix>FileCount</dsp:Suffix>
<dsp:Overwrite>false</dsp:Overwrite>
<dsp:Layout>ColumnMajor</dsp:Layout>
</Combinator>
Expand Down
127 changes: 108 additions & 19 deletions workflows/hardware/np1e/np1e.bonsai

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions workflows/hardware/np1e/port-status.bonsai
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<io:FileName>port-status_.csv</io:FileName>
<io:Append>false</io:Append>
<io:Overwrite>false</io:Overwrite>
<io:Suffix>Timestamp</io:Suffix>
<io:Suffix>FileCount</io:Suffix>
<io:IncludeHeader>false</io:IncludeHeader>
<io:Selector>Timestamp,Value.Clock,Value.StatusCode,Value.SerdesLocked,Value.SerdesPass</io:Selector>
<io:Selector>Timestamp,Value.Clock,Value.StatusCode</io:Selector>
</Expression>
</Nodes>
<Edges>
Expand Down
Loading