| 1 |
import nixio |
| 2 |
import docutils |
| 3 |
import numpy as np |
| 4 |
import matplotlib.pyplot as plt |
| 5 |
|
| 6 |
|
| 7 |
def create_data(interval=0.001): |
| 8 |
time = np.arange(0., 1.0, interval) |
| 9 |
freq = 5. |
| 10 |
signal = np.sin(time * 2 * np.pi * freq) + np.sin(time * 2 * np.pi * 2 * freq) * 0.4 |
| 11 |
events = time[(signal > 0.5) & (np.roll(signal, -1) <= 0.5)] |
| 12 |
return signal, events |
| 13 |
|
| 14 |
|
| 15 |
def plot(multi_tag): |
| 16 |
event_array = multi_tag.positions |
| 17 |
event_times = multi_tag.positions[:] |
| 18 |
signal_array = multi_tag.references["signal"] |
| 19 |
signal = signal_array[:] |
| 20 |
x_axis = signal_array.dimensions[0] |
| 21 |
time = x_axis.axis(len(signal)) |
| 22 |
|
| 23 |
fig = plt.figure() |
| 24 |
fig.set_size_inches(5.5, 2.5) |
| 25 |
ax = fig.add_subplot(111) |
| 26 |
ax.plot(time, signal, label='voltage', zorder=0) |
| 27 |
ax.scatter(event_times, np.ones(event_times.shape) * 0.5, marker=".", s=75, color='red', |
| 28 |
label=event_array.name, zorder=1) |
| 29 |
|
| 30 |
ax.set_xlim([0., 1.]) |
| 31 |
ax.set_ylim([-1.75, 1.75]) |
| 32 |
ax.set_xticks(np.arange(0, 1.01, 0.25)) |
| 33 |
ax.set_yticks(np.arange(-1.5, 1.6, 0.5)) |
| 34 |
ax.set_xlabel(x_axis.label + ((" [" + x_axis.unit + "]") if x_axis.unit else "")) |
| 35 |
ax.set_ylabel("%s [%s]" % (signal_array.label, signal_array.unit)) |
| 36 |
ax.spines["top"].set_visible(False) |
| 37 |
ax.spines["right"].set_visible(False) |
| 38 |
fig.subplots_adjust(bottom=0.175, left=0.15, right=0.95, top=0.95) |
| 39 |
ax.legend(loc=3, frameon=False, ncol=2) |
| 40 |
if docutils.is_running_under_pytest(): |
| 41 |
plt.close() |
| 42 |
else: |
| 43 |
|
| 44 |
plt.show() |
| 45 |
|
| 46 |
|
| 47 |
def main(): |
| 48 |
sampling_interval = 0.001 |
| 49 |
signal, events = create_data(sampling_interval) |
| 50 |
|
| 51 |
nixfile = nixio.File.open("multiple_points.nix", nixio.FileMode.Overwrite) |
| 52 |
block = nixfile.create_block("recording session", "nix.session") |
| 53 |
signal_array = block.create_data_array("signal", "nix.sampled", data=signal, label="voltage", unit="mV") |
| 54 |
signal_array.append_sampled_dimension(sampling_interval, label="time", unit="s") |
| 55 |
|
| 56 |
event_array = block.create_data_array("threshold crossings", "nix.events.threshold_crossings", data=events, label="time", unit="s") |
| 57 |
event_array.append_range_dimension_using_self() |
| 58 |
|
| 59 |
mtag = block.create_multi_tag("event tag", "nix.tag.events", event_array) |
| 60 |
mtag.references.append(signal_array) |
| 61 |
|
| 62 |
plot(mtag) |
| 63 |
nixfile.close() |
| 64 |
|
| 65 |
|
| 66 |
if __name__ == "__main__": |
| 67 |
main() |