| 1 |
import nixio |
| 2 |
import numpy as np |
| 3 |
import scipy.signal as signal |
| 4 |
import matplotlib.pyplot as plt |
| 5 |
|
| 6 |
import docutils |
| 7 |
|
| 8 |
interval = 0.001 |
| 9 |
duration = 3.5 |
| 10 |
stim_on = 0.5 |
| 11 |
stim_off = 2.5 |
| 12 |
stim_amplitude = 1.0 |
| 13 |
|
| 14 |
|
| 15 |
def butter_lowpass(highcut, fs, order=5): |
| 16 |
nyq = 0.5 * fs |
| 17 |
high = highcut / nyq |
| 18 |
b, a = signal.butter(order, high, btype='low') |
| 19 |
return b, a |
| 20 |
|
| 21 |
|
| 22 |
def butter_highpass(lowcut, fs, order=5): |
| 23 |
nyq = 0.5 * fs |
| 24 |
low = lowcut / nyq |
| 25 |
b, a = signal.butter(order, low, btype='high') |
| 26 |
return b, a |
| 27 |
|
| 28 |
|
| 29 |
def butter_bandpass_filter(data, lowcut, highcut, fs, lporder=1, hporder=1): |
| 30 |
lpb, lpa = butter_lowpass(highcut, fs, order=lporder) |
| 31 |
hpb, hpa = butter_highpass(lowcut, fs, order=hporder) |
| 32 |
y = signal.lfilter(lpb, lpa, data) |
| 33 |
y = signal.lfilter(hpb, hpa, y) |
| 34 |
return y |
| 35 |
|
| 36 |
|
| 37 |
def plot(time, response): |
| 38 |
fig = plt.figure() |
| 39 |
fig.set_size_inches(5.5, 2.5) |
| 40 |
ax = fig.add_subplot(111) |
| 41 |
ax.plot(time, response, label="response") |
| 42 |
ax.set_xlabel("time [s]") |
| 43 |
ax.set_ylabel("voltage [mV]") |
| 44 |
ax.plot([stim_on, stim_off], [1.0, 1.0], ls="solid", lw=2., color="red", label="stimulus on") |
| 45 |
ax.set_ylim([-1.2, 1.2]) |
| 46 |
ax.set_yticks([-1., -0.5, 0, 0.5, 1.]) |
| 47 |
ax.spines["top"].set_visible(False) |
| 48 |
ax.spines["right"].set_visible(False) |
| 49 |
ax.legend(loc=3, frameon=False) |
| 50 |
fig.subplots_adjust(left=0.15, bottom=0.175, right=0.95, top=0.95) |
| 51 |
|
| 52 |
|
| 53 |
ax.scatter(stim_on, 1.15, marker="o", color='silver', lw=0.1) |
| 54 |
ax.plot([stim_on, stim_off], [1.15, 1.15], lw=0.5, color="silver") |
| 55 |
ax.plot([stim_off, stim_off], [1.1, 1.2], lw=0.5, color="silver") |
| 56 |
ax.text(1.3, 1.175, "extent") |
| 57 |
ax.annotate('position', xy=(stim_on, 1.15), xytext=(-0.15, 1.15), |
| 58 |
arrowprops=dict(facecolor='silver', connectionstyle="arc3", arrowstyle="->"), |
| 59 |
) |
| 60 |
|
| 61 |
if docutils.is_running_under_pytest(): |
| 62 |
plt.close() |
| 63 |
else: |
| 64 |
plt.show() |
| 65 |
|
| 66 |
|
| 67 |
def create_data(): |
| 68 |
time = np.arange(0., 3.5, interval) |
| 69 |
stimulus = np.zeros(time.shape) |
| 70 |
stimulus[(time >= stim_on) & (time < stim_off)] = stim_amplitude |
| 71 |
response = butter_bandpass_filter(stimulus, .25, 10., 1. / interval) |
| 72 |
return time, stimulus, response |
| 73 |
|
| 74 |
|
| 75 |
def read_all_data(data_array): |
| 76 |
print(data_array.shape) |
| 77 |
data = data_array[:] |
| 78 |
time = data_array.dimensions[0].axis(len(data)) |
| 79 |
return time, data |
| 80 |
|
| 81 |
|
| 82 |
def main(): |
| 83 |
time, _, response = create_data() |
| 84 |
|
| 85 |
f = nixio.File.open("tagging1.nix", nixio.FileMode.Overwrite) |
| 86 |
block = f.create_block("demo block", "nix.demo") |
| 87 |
data = block.create_data_array("response", "nix.sampled", data=response) |
| 88 |
data.label = "voltage" |
| 89 |
data.unit = "mV" |
| 90 |
|
| 91 |
data.append_sampled_dimension(interval, label="time", unit="s") |
| 92 |
|
| 93 |
stim_tag = block.create_tag("stimulus", "nix.stimulus_segment", position=[stim_on]) |
| 94 |
stim_tag.extent = [stim_off - stim_on] |
| 95 |
stim_tag.references.append(data) |
| 96 |
|
| 97 |
plot(time, response) |
| 98 |
read_partial_data(data) |
| 99 |
f.close() |
| 100 |
|
| 101 |
|
| 102 |
def read_partial_data(data_array): |
| 103 |
|
| 104 |
partial_data = data_array[100:1000] |
| 105 |
time = data_array.dimensions[0].axis(900, 100) |
| 106 |
print(partial_data.shape, len(time)) |
| 107 |
|
| 108 |
|
| 109 |
partial_data = data_array.get_slice([0.5], [1.25], nixio.DataSliceMode.Data)[:] |
| 110 |
time = np.arange(0.5, 0.5 + 1.25, data_array.dimensions[0].sampling_interval) |
| 111 |
print(partial_data.shape, len(time)) |
| 112 |
|
| 113 |
|
| 114 |
if __name__ == "__main__": |
| 115 |
main() |