.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_simulate_muscle.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_02_simulate_muscle.py: Muscle Model =============================== The **spike trains** alone are not enough to create **EMG signals**. To create **EMG signals**, we need to create a **muscle model** that will distribute the **motor units** and **fibers** within the muscle volume. .. GENERATED FROM PYTHON SOURCE LINES 11-13 Import Libraries ----------------- .. GENERATED FROM PYTHON SOURCE LINES 13-23 .. code-block:: Python from pathlib import Path import numpy as np import matplotlib.pyplot as plt import joblib from myogen import simulator from myogen.utils.plotting.muscle import plot_mf_centers, plot_innervation_areas_2d .. GENERATED FROM PYTHON SOURCE LINES 24-38 Define Parameters ----------------- The **muscle model** is created using the **Muscle** object. The **Muscle** object takes the following parameters: - ``recruitment_thresholds``: Recruitment thresholds of the motor units - ``radius``: Radius of the muscle in mm - ``fiber_density``: Fiber density per mm² - ``max_innervation_area_to_total_muscle_area__ratio``: Maximum innervation area to total muscle area ratio - ``grid_resolution``: Spatial resolution for muscle discretization Since the **recruitment thresholds** are already generated, we can load them from the previous example using ``joblib``. .. GENERATED FROM PYTHON SOURCE LINES 38-54 .. code-block:: Python # Load recruitment thresholds save_path = Path("./results") recruitment_thresholds = joblib.load(save_path / "thresholds.pkl") # Define muscle parameters for an FDI muscle muscle_radius = 4.9 # Muscle radius in mm mean_fiber_length = 32 # Mean fiber length in mm fiber_length_variation = 3 # Fiber length variation (±) in mm fiber_density = 50 # Fiber density per mm² # Define simulation parameters max_innervation_ratio = 1 / 4 # Maximum motor unit territory size grid_resolution = 256 # Spatial resolution for muscle discretization .. GENERATED FROM PYTHON SOURCE LINES 55-62 Create Muscle Model ------------------- .. note:: Depending on the parameters, the simulation can take a few minutes to run. To **avoid running the simulation every time**, we can save the muscle model using ``joblib``. .. GENERATED FROM PYTHON SOURCE LINES 62-84 .. code-block:: Python # Create muscle model muscle = simulator.Muscle( recruitment_thresholds=recruitment_thresholds[::4], # For faster simulation radius__mm=muscle_radius, fiber_density__fibers_per_mm2=fiber_density, max_innervation_area_to_total_muscle_area__ratio=max_innervation_ratio, grid_resolution=grid_resolution, autorun=True, ) # Save muscle model for future use joblib.dump(muscle, save_path / "muscle_model.pkl") # Display muscle statistics total_fibers = sum(muscle.resulting_number_of_innervated_fibers) print(f"\nMuscle model statistics:") print(f" - Total muscle fibers: {total_fibers}") print(f" - Mean fibers per MU: {total_fibers / len(recruitment_thresholds):.1f}") print(f" - Muscle cross-sectional area: {np.pi * muscle_radius**2:.1f} mm²") .. rst-class:: sphx-glr-script-out .. code-block:: none Calculating out-of-circle coefficients: 0%| | 0/25 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02_simulate_muscle.py <02_simulate_muscle.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 02_simulate_muscle.zip <02_simulate_muscle.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_