### Install Dependencies and Build Cython Wrapper Source: https://github.com/mtegu/aep-model/blob/master/README.md This snippet shows the commands to set up a virtual environment, install project dependencies, and build the Cython wrapper for the AN model. Ensure you are in the correct directory before running. ```bash python -m venv venv # macOS/Linux source venv/bin/activate # Windows (PowerShell) .\venv\Scripts\Activate.ps1 pip install -r requirements.txt cd BEZ2018model python setup.py build_ext --inplace ``` -------------------------------- ### Run Basic Auditory Nerve Simulation Source: https://github.com/mtegu/aep-model/blob/master/README.md Execute a basic simulation of the auditory nerve model after installation. The output, containing simulated auditory nerve activity per CF, will be saved as 'results.pkl'. ```bash cd .. python test_run_ANmodel.py ``` -------------------------------- ### Initialize Simulated CAP Arrays Source: https://github.com/mtegu/aep-model/blob/master/test_derive_UR.ipynb Initializes arrays to store simulated click-evoked CAPs and sets up the corresponding time vector. The simulated CAPs will be generated by convolving auditory nerve responses with the derived unitary responses. ```python # Initialize the simulated click-evoked CAPs simul_CAP = np.zeros((8, 20, an_click.shape[2] + ur.shape[1] - 1)) t_simul = np.arange(0, simul_CAP.shape[2])/output_fs*1000 - 10 - offset print(f"Simulated CAP array shape: {simul_CAP.shape}") print(f"Number of stimulus levels: {simul_CAP.shape[0]}") print(f"Number of subjects: {simul_CAP.shape[1]}") print(f"Simulated CAP length: {simul_CAP.shape[2]} samples") print(f"Simulated time vector range: {t_simul[0]:.2f} to {t_simul[-1]:.2f} ms") ``` -------------------------------- ### Prepare Data for Unitary Response Derivation Source: https://github.com/mtegu/aep-model/blob/master/test_derive_UR.ipynb Prepares the auditory nerve model output for unitary response derivation by setting up time vectors, defining offset parameters, and selecting an appropriate time window. Sums responses across characteristic frequencies. ```python # Prepare the AN model output offset = 1 # ms - This allows for a smoother start of the UR t_vect = np.arange(0, an_click.shape[2])/output_fs*1000 - 10 # 10 ms silence pre stimulus onset t_X_0 = np.argmin(np.abs(t_vect - (-10+offset))) # Experimental CAPs start at -10 ms t_X_10 = np.argmin(np.abs(t_vect - (30+offset))) # Experimental CAPs end at 30 ms X = np.sum(an_click[:,:, t_X_0:t_X_10], axis=1) # add across all CFs print(f"Time vector range: {t_vect[0]:.2f} to {t_vect[-1]:.2f} ms") print(f"Selected time window: {t_vect[t_X_0]:.2f} to {t_vect[t_X_10-1]:.2f} ms") print(f"Prepared data shape for UR derivation: {X.shape}") print(f"Offset parameter: {offset} ms") ``` -------------------------------- ### Import Libraries for Auditory Nerve Modeling Source: https://github.com/mtegu/aep-model/blob/master/test_derive_UR.ipynb Imports necessary libraries including NumPy, pickle, matplotlib, and custom utility functions for auditory nerve modeling and visualization. Sets the matplotlib backend for Windows compatibility. ```python import numpy as np import pickle import matplotlib as mpl if 'Qt5Agg' in mpl.rcsetup.all_backends: # for Windows mpl.use('Qt5Agg') from utils import plot_tens_output, plot_group_average_CAPs from simulator import derive_unitary_response import matplotlib.pyplot as plt ``` -------------------------------- ### Load Experimental Evoked Data Source: https://github.com/mtegu/aep-model/blob/master/test_derive_UR.ipynb Loads experimental evoked data from a .npz file, including the sampling rate, click-evoked CAP waveform, and time vector. Prints the details of the loaded experimental data. ```python # Load experimental data data = np.load("experimental_dataset/evoked_data.npz") fs = data["fs"] # Important that this matches the output_fs experimental_cap = data["waveform_click_cap"] # shape (20, n_time) t_exp_data = data["time_vector"] print(f"Experimental data sampling rate: {fs} Hz") print(f"Experimental CAP waveform shape: {experimental_cap.shape}") print(f"Time vector length: {len(t_exp_data)}") print(f"Time range: {t_exp_data[0]:.2f} to {t_exp_data[-1]:.2f} ms") ``` -------------------------------- ### Launch Jupyter Notebook for Unitary Response Derivation Source: https://github.com/mtegu/aep-model/blob/master/README.md This command launches the Jupyter Notebook used for deriving unitary responses (UR) by convolving AN fiber activity with a unitary response. This process is used to generate AEPs. ```bash jupyter notebook test_derive_UR.ipynb ``` ```bash jupyter lab test_derive_UR.ipynb ``` -------------------------------- ### Load Simulated Auditory Nerve Data Source: https://github.com/mtegu/aep-model/blob/master/test_derive_UR.ipynb Loads simulated auditory nerve data from a pickle file. Extracts averaged responses, characteristic frequency vector, and sampling rate. Prints the shapes and parameters of the loaded data. ```python with open('simulation_results/results_clicks.pkl', 'rb') as f: results = pickle.load(f) an_click = results['tens_dense_ave'] cf_vect = results['cf_vect'] output_fs = 10e3 print(f"Loaded auditory nerve data with shape: {an_click.shape} levels x channels x time points") print(f"Characteristic frequency vector length: {len(cf_vect)}") print(f"Output sampling rate: {output_fs} Hz") ``` -------------------------------- ### Visualize Derived Unitary Response Source: https://github.com/mtegu/aep-model/blob/master/test_derive_UR.ipynb Creates a detailed plot of the grand average unitary response, including axis labels and formatting. This visualization illustrates the final derived unitary response characterizing the neural-to-electrical transformation. ```python # Plot the derived unitary response fig, ax = plt.subplots(figsize=(5, 5)) t_UR = np.arange(0, ur.shape[1])/output_fs*1000 - offset ax.plot(t_UR, ur.mean(axis=0), color='k', linewidth=2) ax.set_xlabel('Time (ms)') ax.set_xlim([-offset, 9]) ax.set_ylim([-4e-4, 2e-4]) ax.set_ylabel('(a.u.)', y=0.25, x=1) ax.set_title('Grand Average Unitary Response') ax.set_yticks([ax.get_ylim()[0], ax.get_ylim()[0] + 5e-4]) ax.set_yticklabels(['0', '0.5e-4']) ax.grid(True, alpha=0.3) fig.tight_layout() plt.show() ``` -------------------------------- ### Visualize Auditory Nerve Model Output Source: https://github.com/mtegu/aep-model/blob/master/test_derive_UR.ipynb Visualizes the auditory nerve model output using the plot_tens_output function. Displays the response patterns across different characteristic frequencies and intensity levels. ```python # Plot the AN model output by level plot_tens_output(an_click, cf_vect, output_fs, title='AN Output', xlim=(0, 40), xlim2=(0, .1), ylim=(0, 1.5e3)) ``` -------------------------------- ### Derive Unitary Response Source: https://github.com/mtegu/aep-model/blob/master/test_derive_UR.ipynb Derives the unitary response using the derive_unitary_response function. This function optimizes to find the best unitary response that, when convolved with the AN model output, matches the experimental CAP data. Parameters for regularization are set. ```python # Derive the unitary response # Parameters: max_level=7, lambda=5 (minimum MSE regularization parameter) ur = derive_unitary_response(7, X[:,:experimental_cap.shape[2]], experimental_cap, 5, plot=False) print(f"Derived unitary response shape: {ur.shape}") print(f"Number of subjects: {ur.shape[0]}") print(f"Unitary response length: {ur.shape[1]} samples") print(f"Unitary response duration: {ur.shape[1]/output_fs*1000:.2f} ms") ``` -------------------------------- ### Convolve Auditory Nerve Responses with Unitary Responses Source: https://github.com/mtegu/aep-model/blob/master/test_derive_UR.ipynb Convolves auditory nerve responses with derived unitary responses for each stimulus level and subject to generate simulated CAPs. This is used to compare simulated data with experimental results. ```python # Convolve click AN responses with URs for each level and subject for lvl_idx in range(8): for sub_idx in range(20): signal = an_click[lvl_idx, :, :].sum(axis=0) # add across all CFs simul_CAP[lvl_idx, sub_idx, :] = np.convolve(signal, ur[sub_idx, :]) print("Convolution completed for all stimulus levels and subjects") print(f"Generated simulated CAPs with shape: {simul_CAP.shape}") ``` -------------------------------- ### Plot Group Average CAPs Source: https://github.com/mtegu/aep-model/blob/master/test_derive_UR.ipynb Visualizes and compares experimental and simulated group average CAPs using the plot_group_average_CAPs function. This comparison helps validate the quality of the derived unitary response. ```python # Plot the experimental and simulated CAPs plot_group_average_CAPs(experimental_cap, t_exp_data, simul_CAP, t_simul, scale=0.6, title='Group Average Click CAPs') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.