.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/howto/optimize_rhythmic.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_howto_optimize_rhythmic.py: ========================================= 08. Optimize simulated rhythmic responses ========================================= This example demonstrates how to optimize the power spectral density (PSD) of a current dipole signal with significant alpha and beta spectral components. .. GENERATED FROM PYTHON SOURCE LINES 10-15 .. code-block:: Python # Authors: Carolina Fernandez import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 16-17 Let us import hnn_core .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. code-block:: Python from hnn_core import (MPIBackend, jones_2009_model, simulate_dipole) # The number of cores may need modifying depending on your current machine. n_procs = 10 .. GENERATED FROM PYTHON SOURCE LINES 24-28 First, we define a function that will tell the optimization routine how to modify the network drive parameters. The function will take in the Network object with no attached drives, and a dictionary of the parameters we wish to optimize. .. GENERATED FROM PYTHON SOURCE LINES 28-63 .. code-block:: Python def set_params(net, params): # Proximal (alpha) weights_ampa_p = {'L2_pyramidal': params['alpha_prox_weight'], 'L5_pyramidal': 4.4e-5} syn_delays_p = {'L2_pyramidal': 0.1, 'L5_pyramidal': 1.} net.add_bursty_drive('alpha_prox', tstart=params['alpha_prox_tstart'], burst_rate=params['alpha_prox_burst_rate'], burst_std=params['alpha_prox_burst_std'], numspikes=2, spike_isi=10, n_drive_cells=10, location='proximal', weights_ampa=weights_ampa_p, synaptic_delays=syn_delays_p) # Distal (beta) weights_ampa_d = {'L2_pyramidal': params['alpha_dist_weight'], 'L5_pyramidal': 4.4e-5} syn_delays_d = {'L2_pyramidal': 5., 'L5_pyramidal': 5.} net.add_bursty_drive('alpha_dist', tstart=params['alpha_dist_tstart'], burst_rate=params['alpha_dist_burst_rate'], burst_std=params['alpha_dist_burst_std'], numspikes=2, spike_isi=10, n_drive_cells=10, location='distal', weights_ampa=weights_ampa_d, synaptic_delays=syn_delays_d) .. GENERATED FROM PYTHON SOURCE LINES 64-72 Then, we define the constraints. The constraints must be a dictionary of tuples where the first value in each tuple is the lower bound and the second value is the upper bound for the corresponding parameter. The following synaptic weight parameter ranges (units of micro-siemens) were chosen so as to keep the model in physiologically realistic regimes. .. GENERATED FROM PYTHON SOURCE LINES 72-83 .. code-block:: Python constraints = dict() constraints.update({'alpha_prox_weight': (4.4e-5, 6.4e-5), 'alpha_prox_tstart': (45, 55), 'alpha_prox_burst_rate': (1, 30), 'alpha_prox_burst_std': (10, 30), 'alpha_dist_weight': (4.4e-5, 6.4e-5), 'alpha_dist_tstart': (45, 55), 'alpha_dist_burst_rate': (1, 30), 'alpha_dist_burst_std': (10, 30)}) .. GENERATED FROM PYTHON SOURCE LINES 84-85 Now we define and fit the optimizer. .. GENERATED FROM PYTHON SOURCE LINES 85-101 .. code-block:: Python from hnn_core.optimization import Optimizer tstop = 300 scale_factor = 3000 smooth_window_len = 20 net = jones_2009_model() optim = Optimizer(net, tstop=tstop, constraints=constraints, set_params=set_params, obj_fun='maximize_psd') # 8-15 Hz (alpha) and 15-30 Hz (beta) are the frequency bands whose # power we wish to maximize in a ratio of 1 to 2. with MPIBackend(n_procs=n_procs, mpi_cmd='mpiexec'): optim.fit(f_bands=[(9, 11), (19, 21)], relative_bandpower=(1, 2), scale_factor=scale_factor, smooth_window_len=smooth_window_len) .. GENERATED FROM PYTHON SOURCE LINES 102-104 Finally, we can plot the optimized dipole, power spectral density (PSD), and convergence plot. .. GENERATED FROM PYTHON SOURCE LINES 104-123 .. code-block:: Python from hnn_core.viz import plot_psd with MPIBackend(n_procs=n_procs, mpi_cmd='mpiexec'): opt_dpl = simulate_dipole(optim.net_, tstop=tstop, n_trials=1)[0] opt_dpl.scale(scale_factor) opt_dpl.smooth(smooth_window_len) fig, axes = plt.subplots(2, 1, figsize=(6, 6)) # plot dipole opt_dpl.plot(ax=axes[0], layer='agg', show=False, color='tab:green') axes[0].legend(['optimized']) # plot psd plot_psd(opt_dpl, fmax=50, ax=axes[1], show=False) # convergence fig1 = optim.plot_convergence() .. _sphx_glr_download_auto_examples_howto_optimize_rhythmic.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/jonescompneurolab/hnn-core/gh-pages?filepath=v0.4/notebooks/auto_examples/howto/optimize_rhythmic.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: optimize_rhythmic.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: optimize_rhythmic.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: optimize_rhythmic.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_