| 1 |
import nixio |
| 2 |
import numpy as np |
| 3 |
import matplotlib.pyplot as plt |
| 4 |
import matplotlib.patches as patches |
| 5 |
|
| 6 |
import docutils |
| 7 |
|
| 8 |
|
| 9 |
def create_example_data(nixfile): |
| 10 |
sampling_interval = 0.001 |
| 11 |
total_duration = 3.0 |
| 12 |
time = np.arange(0., total_duration, sampling_interval) |
| 13 |
stim = np.ones(time.shape) |
| 14 |
stim_duration = 0.25 |
| 15 |
intervals = np.arange(0.25, total_duration, stim_duration * 3) |
| 16 |
stim_frequencies = np.zeros_like(intervals) |
| 17 |
stim_onsets = np.zeros_like(intervals) |
| 18 |
stim_extents = np.zeros_like(intervals) |
| 19 |
base_freq = 5 |
| 20 |
|
| 21 |
for i, interval in enumerate(intervals): |
| 22 |
stim[(time >= interval) & (time < interval + stim_duration)] = i + 2 |
| 23 |
stim_frequencies[i] = (i + 2) * base_freq |
| 24 |
stim_onsets[i] = interval |
| 25 |
stim_extents[i] = stim_duration |
| 26 |
stim *= base_freq |
| 27 |
signal = np.sin(time * 2 * np.pi * stim) |
| 28 |
|
| 29 |
block = nixfile.create_block("multiple regions", "nix.session") |
| 30 |
|
| 31 |
data_array = block.create_data_array("signal", "nix.data.sampled", data=signal) |
| 32 |
data_array.label = "voltage" |
| 33 |
data_array.unit = "mV" |
| 34 |
data_array.append_sampled_dimension(sampling_interval, label="time", unit="s") |
| 35 |
|
| 36 |
positions = block.create_data_array("stimulus onsets", "nix.region.onsets", data=stim_onsets) |
| 37 |
positions.append_set_dimension() |
| 38 |
|
| 39 |
extents = block.create_data_array("stimulus extents", "nix.region.extents", data=stim_extents) |
| 40 |
extents.append_set_dimension() |
| 41 |
|
| 42 |
frequencies = block.create_data_array("stimulus frequency", "nix.feature", data=stim_frequencies) |
| 43 |
frequencies.label = "frequency" |
| 44 |
frequencies.unit = "Hz" |
| 45 |
frequencies.append_set_dimension() |
| 46 |
|
| 47 |
mtag = block.create_multi_tag("stimulus segments", "nix.segments.stimulus", positions=positions, extents=extents) |
| 48 |
mtag.references.append(data_array) |
| 49 |
mtag.create_feature(frequencies, nixio.LinkType.Indexed) |
| 50 |
|
| 51 |
|
| 52 |
def plot(nixfile): |
| 53 |
block = nixfile.blocks[0] |
| 54 |
signal_da = block.data_arrays["signal"] |
| 55 |
signal = signal_da[:] |
| 56 |
signal_dim = signal_da.dimensions[0] |
| 57 |
time = np.asarray(signal_dim.axis(len(signal))) |
| 58 |
mtag = block.multi_tags[0] |
| 59 |
fig = plt.figure() |
| 60 |
fig.set_size_inches(5.5, 2.5) |
| 61 |
ax = fig.add_subplot(111) |
| 62 |
|
| 63 |
l, = ax.plot(time, signal, label=signal_da.name) |
| 64 |
ax.set_xlim([0., 3.]) |
| 65 |
ax.set_ylim([-1.45, 1.45]) |
| 66 |
ax.set_xticks(np.arange(0., 3.01, 0.5)) |
| 67 |
ax.set_yticks(np.arange(-1.5, 1.6, 0.5)) |
| 68 |
ax.set_xlabel("%s %s" % (signal_dim.label, "[%s]" % signal_dim.unit if signal_dim.unit else "")) |
| 69 |
ax.set_ylabel("%s %s" % (signal_da.label, "[%s]" % signal_da.unit if signal_da.unit else "")) |
| 70 |
|
| 71 |
for i, (interval, extent) in enumerate(zip(mtag.positions[:], mtag.extents[:])): |
| 72 |
rect = patches.Rectangle((interval, 1.05), extent, -2.1, alpha=0.5, |
| 73 |
facecolor="silver", edgecolor='k', lw=0.75, ls='--') |
| 74 |
ax.add_patch(rect) |
| 75 |
stim_freq = mtag.feature_data(i, "stimulus frequency")[:] |
| 76 |
ax.text(interval + extent / 2, -1.25, |
| 77 |
"%.1f %s" % (stim_freq.item(), |
| 78 |
mtag.features["stimulus frequency"].data.unit), fontsize=8, ha="center") |
| 79 |
ax.legend((l, rect), (signal_da.name, mtag.name), loc=1, frameon=False, ncol=2) |
| 80 |
|
| 81 |
ax.spines["top"].set_visible(False) |
| 82 |
ax.spines["right"].set_visible(False) |
| 83 |
fig.subplots_adjust(bottom=0.175, left=0.15, right=0.95, top=0.95) |
| 84 |
if docutils.is_running_under_pytest(): |
| 85 |
plt.close() |
| 86 |
else: |
| 87 |
|
| 88 |
plt.show() |
| 89 |
|
| 90 |
|
| 91 |
def plot_tagged_data(nixfile): |
| 92 |
block = nixfile.blocks[0] |
| 93 |
mtag = block.multi_tags[0] |
| 94 |
signal_da = mtag.references["signal"] |
| 95 |
signal_dim = signal_da.dimensions[0] |
| 96 |
xlabel = "%s %s" % (signal_dim.label, "[%s]" % signal_dim.unit if signal_dim.unit else "") |
| 97 |
ylabel = "%s %s" % (signal_da.label, "[%s]" % signal_da.unit if signal_da.unit else "") |
| 98 |
|
| 99 |
fig = plt.figure(figsize=(5.5, 2.5)) |
| 100 |
num_segments = len(mtag.positions[:]) |
| 101 |
for i in range(num_segments): |
| 102 |
data = mtag.tagged_data(i, "signal")[:] |
| 103 |
time = signal_dim.axis(len(data), start_position=mtag.positions[i]) |
| 104 |
ax = fig.add_subplot(1, num_segments, i + 1) |
| 105 |
ax.plot(time, data) |
| 106 |
ax.spines["top"].set_visible(False) |
| 107 |
ax.spines["right"].set_visible(False) |
| 108 |
ax.set_xlabel(xlabel) |
| 109 |
if i == 0: |
| 110 |
ax.set_ylabel(ylabel) |
| 111 |
else: |
| 112 |
ax.set_yticklabels([]) |
| 113 |
fig.subplots_adjust(left=0.15, bottom=0.2, wspace=0.5, right=0.975) |
| 114 |
if docutils.is_running_under_pytest(): |
| 115 |
plt.close() |
| 116 |
else: |
| 117 |
|
| 118 |
plt.show() |
| 119 |
|
| 120 |
|
| 121 |
def main(): |
| 122 |
nixfile = nixio.File.open("multiple_regions.nix", nixio.FileMode.Overwrite) |
| 123 |
|
| 124 |
create_example_data(nixfile) |
| 125 |
plot(nixfile) |
| 126 |
plot_tagged_data(nixfile) |
| 127 |
nixfile.close() |
| 128 |
|
| 129 |
|
| 130 |
if __name__ == "__main__": |
| 131 |
main() |