.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/00_simulate_recruitment_thresholds.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_00_simulate_recruitment_thresholds.py: Recruitment Thresholds ================================= The first step in using **MyoGen** is to generate the **recruitment thresholds** of the **motor units** (MUs). .. note:: A **recruitment threshold** is the minimum force required to activate a MU. In **MyoGen**, the threshold is defined between ``0`` and ``1``, where ``0`` is the minimum force required to activate a MU and ``1`` is the maximum force required to activate a MU. **MyoGen** offers **4 different models** to generate the recruitment thresholds: * **Fuglevand** model: Classic exponential distribution (*Fuglevand et al., 1993*) * **De Luca** model: Slope-corrected exponential distribution (*De Luca & Contessa, 2012*) * **Konstantin** model: Exponential with explicit maximum threshold control (*Konstantin et al., 2019*) * **Combined** model: Hybrid approach combining De Luca shape with Konstantin scaling (*Ours*) .. GENERATED FROM PYTHON SOURCE LINES 21-23 Import Libraries ---------------- .. GENERATED FROM PYTHON SOURCE LINES 23-30 .. code-block:: Python from pathlib import Path import joblib from myogen import simulator .. GENERATED FROM PYTHON SOURCE LINES 31-56 Define Parameters ----------------- Each recruitment threshold simulation is defined by the following parameters: - ``N``: Number of motor units in the pool - ``recruitment_range``: Recruitment range (max_threshold / min_threshold) .. note:: The **recruitment_range** is defined as the ratio between the ``maximum`` and ``minimum`` recruitment thresholds. For example, if the **recruitment_range** is ``50``, the biggest MU will have a **recruitment threshold** ``50`` times bigger than the smallest MU. With some models you also need to define additional parameters: - De Luca model: - ``deluca__slopes``: Different slope values to demonstrate variety - Konstantin model: - ``konstantin__max_threshold``: Maximum recruitment threshold - Combined model: - ``deluca__slopes``: Different slope values to demonstrate variety - ``konstantin__max_threshold``: Maximum recruitment threshold .. GENERATED FROM PYTHON SOURCE LINES 56-71 .. code-block:: Python n_motor_units = 100 # Number of motor units in the pool recruitment_range = 50 # Recruitment range (max_threshold / min_threshold) # Model specific parameters fuglevand_params = {} # No additional parameters needed deluca_slopes = [0.001, 5, 25, 50] # Different slope values to demonstrate variety konstantin_max_threshold = 1.0 # Maximum recruitment threshold combined_slopes = [0.001, 5, 25, 50] # Slopes for combined model combined_max_threshold = 1.0 # Maximum threshold for combined model .. GENERATED FROM PYTHON SOURCE LINES 72-83 Generate Recruitment Thresholds -------------------------------- Generating the recruitment thresholds is done by calling the ``generate_mu_recruitment_thresholds`` function. .. important:: **MyoGen** is intended to be used with the following API: .. code-block:: python from myogen import simulator .. GENERATED FROM PYTHON SOURCE LINES 83-127 .. code-block:: Python # load_nmodl_files() # Create results directory save_path = Path("./results") save_path.mkdir(exist_ok=True) # 1. Fuglevand Model rt_fuglevand, rtz_fuglevand = simulator.generate_mu_recruitment_thresholds( N=n_motor_units, recruitment_range=recruitment_range, mode="fuglevand" ) # 2. De Luca Model with different slopes deluca_results = {} for slope in deluca_slopes: rt, _ = simulator.generate_mu_recruitment_thresholds( N=n_motor_units, recruitment_range=recruitment_range, deluca__slope=slope, mode="deluca", ) deluca_results[slope] = rt # 3. Konstantin Model rt_konstantin, rtz_konstantin = simulator.generate_mu_recruitment_thresholds( N=n_motor_units, recruitment_range=recruitment_range, konstantin__max_threshold=konstantin_max_threshold, mode="konstantin", ) # 4. Combined Model with different slopes combined_results = {} for slope in combined_slopes: rt, _ = simulator.generate_mu_recruitment_thresholds( N=n_motor_units, recruitment_range=recruitment_range, deluca__slope=slope, konstantin__max_threshold=combined_max_threshold, mode="combined", ) combined_results[slope] = rt .. GENERATED FROM PYTHON SOURCE LINES 128-133 Save Recruitment Thresholds -------------------------- .. note:: All **MyoGen** objects can be saved to a file using ``joblib``. This is useful to **avoid re-running expensive simulations** if you need to use the same parameters. .. GENERATED FROM PYTHON SOURCE LINES 133-136 .. code-block:: Python joblib.dump(combined_results[50], save_path / "thresholds.pkl") .. rst-class:: sphx-glr-script-out .. code-block:: none ['results/thresholds.pkl'] .. GENERATED FROM PYTHON SOURCE LINES 137-148 Plot Recruitment Thresholds -------------------------- The recruitment thresholds can be plotted using the ``plot_recruitment_thresholds`` function. .. note:: **Plotting helper functions** are available in the ``myogen.utils.plotting`` module. .. code-block:: python from myogen.utils.plotting import plot_recruitment_thresholds .. GENERATED FROM PYTHON SOURCE LINES 148-160 .. 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) from myogen.utils.plotting import plot_recruitment_thresholds from matplotlib import pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 161-166 Fuglevand Model Visualization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The Fuglevand model uses a simple exponential distribution for recruitment thresholds. This is the classic approach from Fuglevand et al. (1993). .. GENERATED FROM PYTHON SOURCE LINES 166-176 .. code-block:: Python print("Plotting Fuglevand model...") with plt.xkcd(): _, ax = plt.subplots(figsize=(10, 6)) plot_recruitment_thresholds( rt_fuglevand, [ax], model_name="Fuglevand", colors="#90b8e0" ) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_00_simulate_recruitment_thresholds_001.png :alt: Fuglevand :srcset: /auto_examples/images/sphx_glr_00_simulate_recruitment_thresholds_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Plotting Fuglevand model... Creating Fuglevand model visualization... .. GENERATED FROM PYTHON SOURCE LINES 177-182 De Luca Model Visualization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The De Luca model includes a slope correction parameter that allows control over the shape of the recruitment threshold distribution. .. GENERATED FROM PYTHON SOURCE LINES 182-195 .. code-block:: Python print("Plotting De Luca model...") with plt.xkcd(): _, ax = plt.subplots(figsize=(10, 6)) plot_recruitment_thresholds( deluca_results, [ax], model_name="De Luca", colors=["#90b8e0", "#af8bff", "#90b8e0", "#af8bff"], ) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_00_simulate_recruitment_thresholds_002.png :alt: De Luca :srcset: /auto_examples/images/sphx_glr_00_simulate_recruitment_thresholds_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Plotting De Luca model... Creating De Luca model visualization... .. GENERATED FROM PYTHON SOURCE LINES 196-201 Konstantin Model Visualization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The Konstantin model provides explicit control over the maximum recruitment threshold while maintaining physiological recruitment patterns. .. GENERATED FROM PYTHON SOURCE LINES 201-216 .. code-block:: Python print("Plotting Konstantin model...") with plt.xkcd(): _, ax = plt.subplots(figsize=(10, 6)) plot_recruitment_thresholds( rt_konstantin, [ax], model_name="Konstantin", y_max=konstantin_max_threshold, colors="#90b8e0", markers="s", ) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_00_simulate_recruitment_thresholds_003.png :alt: Konstantin :srcset: /auto_examples/images/sphx_glr_00_simulate_recruitment_thresholds_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Plotting Konstantin model... Creating Konstantin model visualization... .. GENERATED FROM PYTHON SOURCE LINES 217-222 Combined Model Visualization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The Combined model merges De Luca's shape control with Konstantin's scaling, offering the most flexibility for custom recruitment patterns. .. GENERATED FROM PYTHON SOURCE LINES 222-235 .. code-block:: Python print("Plotting Combined model...") with plt.xkcd(): _, ax = plt.subplots(figsize=(10, 6)) plot_recruitment_thresholds( combined_results, [ax], model_name="Combined", y_max=combined_max_threshold, colors=["#90b8e0", "#af8bff", "#90b8e0", "#af8bff"], ) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_00_simulate_recruitment_thresholds_004.png :alt: Combined :srcset: /auto_examples/images/sphx_glr_00_simulate_recruitment_thresholds_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Plotting Combined model... Creating Combined model visualization... .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.590 seconds) .. _sphx_glr_download_auto_examples_00_simulate_recruitment_thresholds.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 00_simulate_recruitment_thresholds.ipynb <00_simulate_recruitment_thresholds.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 00_simulate_recruitment_thresholds.py <00_simulate_recruitment_thresholds.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 00_simulate_recruitment_thresholds.zip <00_simulate_recruitment_thresholds.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_