Loading NWB files using Neo =========================== This notebook demonstrates how to load electrophysiology data using Neo for eFEL eFeatures extraction. .. code:: ipython3 import efel import numpy %matplotlib notebook %matplotlib inline from matplotlib import pyplot as plt plt.rcParams['figure.figsize'] = 10, 10 We will use the voltage trace recordings obtained from a mouse thalamic cell classified as a bursting accommodating (bAC) etype. The data is stored in an NWB (Neurodata Without Borders) file, and we will use the `Python Neo `__ library to load it. .. code:: ipython3 test_data = "../../tests/testdata/JY180308_A_1.nwb" stim_start = 250 stim_end = 600 blocks = efel.io.load_neo_file(test_data, stim_start, stim_end) traces = blocks[0][0] Let’s get a trace that includes a burst .. code:: ipython3 trace = traces[5] .. code:: ipython3 time = trace['T'] voltage = trace['V'] .. code:: ipython3 plt.rcParams['figure.figsize'] = 10, 10 fig1, ax1 = plt.subplots(1) ax1.plot(time, voltage) ax1.set_xlabel('Time (ms)') ax1.set_ylabel('Membrane voltage (mV)'); .. image:: load_nwb_files/load_nwb_7_0.png We can now use the eFEL to extract eFeature values from the trace shown above. We will use the ``get_feature_values()`` function, which accepts a list of trace and the requested eFeature names as input. Let’s extract some burst-related features .. code:: ipython3 efel.set_setting("ignore_first_ISI", False) # Don't ignore the first spike efel.set_setting("strict_burst_factor", 4.0) # The burst detection can be fine-tuned by changing the setting strict_burst_factor. Default value is 2.0. feature_values = efel.get_feature_values([trace], ['spikes_per_burst', 'strict_burst_number', 'strict_burst_mean_freq', 'peak_time', 'AP_height', 'peak_indices', 'burst_begin_indices', 'burst_end_indices'])[0] feature_values .. parsed-literal:: {'spikes_per_burst': array([4]), 'strict_burst_number': array([1]), 'strict_burst_mean_freq': array([28.96451846]), 'peak_time': array([330.4, 343.2, 382.7, 468.5]), 'AP_height': array([11.40000057, 11.71249962, 12.23750019, 12.19999981]), 'peak_indices': array([3304, 3432, 3827, 4685]), 'burst_begin_indices': array([0]), 'burst_end_indices': array([3])} .. code:: ipython3 burst_begin_indices = feature_values['burst_begin_indices'][0] burst_end_indices = feature_values['burst_end_indices'][0] peak_times = feature_values['peak_time'] ap_heights = feature_values['AP_height'] burst_mean_freq = feature_values['strict_burst_mean_freq'] time_spike_indices = numpy.where((time > stim_start) & (time < stim_end)) time_spike = time[time_spike_indices] voltage_spike = voltage[time_spike_indices] plt.figure(figsize=(10, 6)) plt.plot(time_spike, voltage_spike, label='Voltage Trace') burst_start = peak_times[burst_begin_indices] burst_end = peak_times[burst_end_indices] mean_frequency = burst_mean_freq[0] plt.axvspan(burst_start, burst_end, color='yellow', alpha=0.3, label='Burst Interval') for spike_time in peak_times[burst_begin_indices:burst_end_indices+1]: plt.axvline(x=spike_time, color='red', linestyle='--', label='Spike' if 'Spike' not in plt.gca().get_legend_handles_labels()[1] else "") plt.text((burst_start + burst_end) / 2, max(voltage_spike), f'Burst Mean Freq: {mean_frequency:.2f} Hz', horizontalalignment='center', color='black') plt.legend() plt.xlabel('Time (ms)') plt.ylabel('Voltage (mV)') plt.show() .. image:: load_nwb_files/load_nwb_10_0.png We can save the feature values obtained from ``get_feature_values`` for later use. Two functions, ``save_feature_values_to_json`` and ``save_feature_values_to_csv``, are provided for this purpose. They save the feature values as a JSON file and a CSV file, respectively. .. code:: ipython3 efel.io.save_feature_to_json(feature_values, 'output.json') efel.io.save_feature_to_csv(feature_values, 'output.csv')