.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/05_simulate_currents.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_05_simulate_currents.py: Current Generation ================== **MyoGen** provides various functions to generate different types of input currents for motor neuron pool simulations. These currents can be used to drive motor unit recruitment and firing patterns. .. note:: **Input currents** are the electrical stimuli applied to motor neuron pools to simulate muscle activation. Different current shapes can produce different recruitment and firing patterns. **MyoGen** offers **5 different current waveform types**: * **Sinusoidal**: Smooth oscillatory currents for rhythmic activation * **Sawtooth**: Asymmetric ramp currents with adjustable rise/fall characteristics * **Step**: Constant amplitude pulses for simple on/off activation * **Ramp**: Linear increase/decrease for gradual force changes * **Trapezoid**: Complex waveforms with rise, plateau, and fall phases .. GENERATED FROM PYTHON SOURCE LINES 22-24 Import Libraries ---------------- .. GENERATED FROM PYTHON SOURCE LINES 24-40 .. code-block:: Python from pathlib import Path import joblib import numpy as np from matplotlib import pyplot as plt from myogen.utils.currents import ( create_sinusoidal_current, create_sawtooth_current, create_step_current, create_ramp_current, create_trapezoid_current, ) from myogen.utils.plotting.currents import plot_input_current__matrix .. GENERATED FROM PYTHON SOURCE LINES 41-51 Define Parameters ----------------- Each current simulation is defined by common timing parameters: - ``n_pools``: Number of motor neuron pools - ``simulation_duration__ms``: Total simulation time in milliseconds - ``timestep__ms``: Time step for simulation in milliseconds Additional parameters are specific to each current type and control amplitude, frequency, timing, and shape characteristics. .. GENERATED FROM PYTHON SOURCE LINES 51-62 .. code-block:: Python # Common simulation parameters n_pools = 3 simulation_duration__ms = 2000.0 # 2 seconds timestep__ms = 0.1 # 0.1 ms time step t_points = int(simulation_duration__ms / timestep__ms) # Create results directory save_path = Path("./results") save_path.mkdir(exist_ok=True) .. GENERATED FROM PYTHON SOURCE LINES 63-68 Generate Sinusoidal Currents ----------------------------- Sinusoidal currents are useful for simulating rhythmic muscle activations, such as those occurring during locomotion or tremor. .. GENERATED FROM PYTHON SOURCE LINES 68-87 .. code-block:: Python print("Generating sinusoidal currents...") # Parameters for sinusoidal currents sin_amplitudes = [50.0, 75.0, 100.0] # Different amplitudes for each pool sin_frequencies = [1.0, 2.0, 3.0] # Different frequencies (Hz) sin_offsets = [120.0, 130.0, 140.0] # DC offsets (µV) sin_phases = [0.0, np.pi / 3, 2 * np.pi / 3] # Phase shifts (rad) sinusoidal_currents = create_sinusoidal_current( n_pools=n_pools, t_points=t_points, timestep__ms=timestep__ms, amplitudes__muV=sin_amplitudes, frequencies__Hz=sin_frequencies, offsets__muV=sin_offsets, phases__rad=sin_phases, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Generating sinusoidal currents... .. GENERATED FROM PYTHON SOURCE LINES 88-93 Generate Sawtooth Currents --------------------------- Sawtooth currents provide asymmetric ramps useful for simulating force-variable contractions with different rise and fall characteristics. .. GENERATED FROM PYTHON SOURCE LINES 93-112 .. code-block:: Python print("Generating sawtooth currents...") # Parameters for sawtooth currents saw_amplitudes = [80.0, 60.0, 100.0] saw_frequencies = [0.5, 1.0, 1.5] # Slow sawtooth waves saw_offsets = [120.0, 125.0, 130.0] saw_widths = [0.3, 0.5, 0.7] # Width of rising edge (0-1) sawtooth_currents = create_sawtooth_current( n_pools=n_pools, t_points=t_points, timestep_ms=timestep__ms, amplitudes__muV=saw_amplitudes, frequencies__Hz=saw_frequencies, offsets__muV=saw_offsets, widths=saw_widths, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Generating sawtooth currents... .. GENERATED FROM PYTHON SOURCE LINES 113-118 Generate Step Currents ----------------------- Step currents are useful for simulating sudden muscle activations, such as those occurring during ballistic movements. .. GENERATED FROM PYTHON SOURCE LINES 118-135 .. code-block:: Python print("Generating step currents...") # Parameters for step currents step_heights = [100.0, 75.0, 125.0] # Step amplitudes step_durations = [500.0, 800.0, 300.0] # Step durations (ms) step_offsets = [120.0, 115.0, 125.0] # Baseline currents step_currents = create_step_current( n_pools=n_pools, t_points=t_points, timestep_ms=timestep__ms, step_heights__muV=step_heights, step_durations__ms=step_durations, offsets__muV=step_offsets, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Generating step currents... .. GENERATED FROM PYTHON SOURCE LINES 136-141 Generate Ramp Currents ----------------------- Ramp currents simulate gradual force changes, useful for modeling slow force development or fatigue scenarios. .. GENERATED FROM PYTHON SOURCE LINES 141-157 .. code-block:: Python print("Generating ramp currents...") # Parameters for ramp currents ramp_start = [0.0, 25.0, 50.0] # Starting current levels ramp_end = [150.0, 100.0, 200.0] # Ending current levels ramp_offsets = [120.0, 130.0, 110.0] # DC offsets ramp_currents = create_ramp_current( n_pools=n_pools, t_points=t_points, start_currents__muV=ramp_start, end_currents__muV=ramp_end, offsets__muV=ramp_offsets, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Generating ramp currents... .. GENERATED FROM PYTHON SOURCE LINES 158-164 Generate Trapezoid Currents ---------------------------- Trapezoid currents provide complex activation patterns with distinct phases: rise, plateau, and fall. These are useful for simulating controlled muscle contractions. .. GENERATED FROM PYTHON SOURCE LINES 164-187 .. code-block:: Python print("Generating trapezoid currents...") # Parameters for trapezoid currents trap_amplitudes = [100.0, 80.0, 120.0] # Peak amplitudes trap_rise_times = [200.0, 150.0, 300.0] # Rise durations (ms) trap_plateau_times = [800.0, 1000.0, 600.0] # Plateau durations (ms) trap_fall_times = [300.0, 200.0, 400.0] # Fall durations (ms) trap_offsets = [120.0, 125.0, 115.0] # Baseline currents trap_delays = [100.0, 200.0, 50.0] # Initial delays (ms) trapezoid_currents = create_trapezoid_current( n_pools=n_pools, t_points=t_points, timestep_ms=timestep__ms, amplitudes__muV=trap_amplitudes, rise_times__ms=trap_rise_times, plateau_times__ms=trap_plateau_times, fall_times__ms=trap_fall_times, offsets__muV=trap_offsets, delays__ms=trap_delays, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Generating trapezoid currents... .. GENERATED FROM PYTHON SOURCE LINES 188-195 Save Current Matrices ---------------------- .. note:: All **MyoGen** current matrices can be saved to files using ``joblib``. This is useful to **avoid re-generating currents** if you need to use the same parameters in multiple simulations. .. GENERATED FROM PYTHON SOURCE LINES 195-204 .. code-block:: Python print("Saving current matrices...") joblib.dump(sinusoidal_currents, save_path / "sinusoidal_currents.pkl") joblib.dump(sawtooth_currents, save_path / "sawtooth_currents.pkl") joblib.dump(step_currents, save_path / "step_currents.pkl") joblib.dump(ramp_currents, save_path / "ramp_currents.pkl") joblib.dump(trapezoid_currents, save_path / "trapezoid_currents.pkl") .. rst-class:: sphx-glr-script-out .. code-block:: none Saving current matrices... ['results/trapezoid_currents.pkl'] .. GENERATED FROM PYTHON SOURCE LINES 205-217 Plot Current Waveforms ----------------------- The current waveforms can be visualized to understand their characteristics and verify they match the intended stimulation patterns. .. note:: **Plotting helper functions** are available in the ``myogen.utils.plotting`` module. .. code-block:: python from myogen.utils.plotting.currents import plot_input_currents .. GENERATED FROM PYTHON SOURCE LINES 217-226 .. code-block:: Python # Suppress font warnings to keep output clean import warnings import logging warnings.filterwarnings("ignore", message=".*Font family.*not found.*") warnings.filterwarnings("ignore", message=".*findfont.*") logging.getLogger("matplotlib.font_manager").setLevel(logging.ERROR) .. GENERATED FROM PYTHON SOURCE LINES 227-232 Sinusoidal Currents Visualization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sinusoidal currents show smooth oscillations with different frequencies and phase relationships between pools. .. GENERATED FROM PYTHON SOURCE LINES 232-240 .. code-block:: Python print("Plotting sinusoidal currents...") with plt.xkcd(): _, axs = plt.subplots(figsize=(10, 6), nrows=n_pools, sharex=True) plot_input_current__matrix(sinusoidal_currents, timestep__ms, axs, color="#90b8e0") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_05_simulate_currents_001.png :alt: Pool 1 Input Current, Pool 2 Input Current, Pool 3 Input Current :srcset: /auto_examples/images/sphx_glr_05_simulate_currents_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Plotting sinusoidal currents... .. GENERATED FROM PYTHON SOURCE LINES 241-246 Sawtooth Currents Visualization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sawtooth currents demonstrate asymmetric ramps with different widths controlling the rise/fall characteristics. .. GENERATED FROM PYTHON SOURCE LINES 246-254 .. code-block:: Python print("Plotting sawtooth currents...") with plt.xkcd(): _, axs = plt.subplots(figsize=(10, 6), nrows=n_pools, sharex=True) plot_input_current__matrix(sawtooth_currents, timestep__ms, axs, color="#90b8e0") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_05_simulate_currents_002.png :alt: Pool 1 Input Current, Pool 2 Input Current, Pool 3 Input Current :srcset: /auto_examples/images/sphx_glr_05_simulate_currents_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Plotting sawtooth currents... .. GENERATED FROM PYTHON SOURCE LINES 255-260 Step Currents Visualization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Step currents show sudden changes in amplitude with different durations for each pool. .. GENERATED FROM PYTHON SOURCE LINES 260-268 .. code-block:: Python print("Plotting step currents...") with plt.xkcd(): _, axs = plt.subplots(figsize=(10, 6), nrows=n_pools, sharex=True) plot_input_current__matrix(step_currents, timestep__ms, axs, color="#90b8e0") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_05_simulate_currents_003.png :alt: Pool 1 Input Current, Pool 2 Input Current, Pool 3 Input Current :srcset: /auto_examples/images/sphx_glr_05_simulate_currents_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Plotting step currents... .. GENERATED FROM PYTHON SOURCE LINES 269-274 Ramp Currents Visualization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ramp currents demonstrate linear changes from start to end values with different slopes for each pool. .. GENERATED FROM PYTHON SOURCE LINES 274-282 .. code-block:: Python print("Plotting ramp currents...") with plt.xkcd(): _, axs = plt.subplots(figsize=(10, 6), nrows=n_pools, sharex=True) plot_input_current__matrix(ramp_currents, timestep__ms, axs, color="#90b8e0") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_05_simulate_currents_004.png :alt: Pool 1 Input Current, Pool 2 Input Current, Pool 3 Input Current :srcset: /auto_examples/images/sphx_glr_05_simulate_currents_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Plotting ramp currents... .. GENERATED FROM PYTHON SOURCE LINES 283-288 Trapezoid Currents Visualization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Trapezoid currents show complex multi-phase patterns with distinct rise, plateau, and fall periods. .. GENERATED FROM PYTHON SOURCE LINES 288-295 .. code-block:: Python print("Plotting trapezoid currents...") with plt.xkcd(): _, axs = plt.subplots(figsize=(10, 6), nrows=n_pools, sharex=True) plot_input_current__matrix(trapezoid_currents, timestep__ms, axs, color="#90b8e0") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_05_simulate_currents_005.png :alt: Pool 1 Input Current, Pool 2 Input Current, Pool 3 Input Current :srcset: /auto_examples/images/sphx_glr_05_simulate_currents_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Plotting trapezoid currents... .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.333 seconds) .. _sphx_glr_download_auto_examples_05_simulate_currents.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 05_simulate_currents.ipynb <05_simulate_currents.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 05_simulate_currents.py <05_simulate_currents.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 05_simulate_currents.zip <05_simulate_currents.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_