Skip to content

Commit

Permalink
Removed unused arguments; Remove hacky pre-calc code
Browse files Browse the repository at this point in the history
  • Loading branch information
iandanforth committed Feb 18, 2019
1 parent 927821a commit 35a2f52
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 85 deletions.
12 changes: 4 additions & 8 deletions pymuscle/muscle.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,11 @@ def __init__(
self,
motor_unit_count: int,
apply_central_fatigue: bool = True,
apply_peripheral_fatigue: bool = True,
pre_calc_firing_rates: bool = False
apply_peripheral_fatigue: bool = True
):
pool = PotvinFuglevand2017MotorNeuronPool(
motor_unit_count,
apply_fatigue=apply_central_fatigue,
pre_calc_firing_rates=pre_calc_firing_rates
apply_fatigue=apply_central_fatigue
)
fibers = PotvinFuglevand2017MuscleFibers(
motor_unit_count,
Expand Down Expand Up @@ -156,8 +154,7 @@ def __init__(
max_force: float = 32.0,
force_conversion_factor: float = 0.0123,
apply_central_fatigue: bool = False,
apply_peripheral_fatigue: bool = True,
pre_calc_firing_rates: bool = False
apply_peripheral_fatigue: bool = True
):

# Maximum voluntary isometric force this muscle will be able to produce
Expand All @@ -173,8 +170,7 @@ def __init__(

pool = PotvinFuglevand2017MotorNeuronPool(
motor_unit_count,
apply_fatigue=apply_central_fatigue,
pre_calc_firing_rates=pre_calc_firing_rates
apply_fatigue=apply_central_fatigue
)
fibers = PyMuscleFibers(
motor_unit_count,
Expand Down
53 changes: 1 addition & 52 deletions pymuscle/potvin_fuglevand_2017_motor_neuron_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ class PotvinFuglevand2017MotorNeuronPool(Model):
Max firing rate for the first motor unit (maxR(1))
:param max_firing_rate_last_unit:
Max firing rate for the last motor unit (maxR(last))
:param pre_calc_firing_rates:
Whether to build a dict mapping excitation levels to firing rates for
each motor neuron. This can speed up simulation at the cost of
additional memory.
:param pre_calc_resolution:
Step size for excitation levels to pre-calculate (res)
:param pre_calc_max: Highest excitation value to pre-calculate
:param derecruitment_delta:
Absolute minimum firing rate = min_firing_rate - derecruitment_delta
(d)
Expand All @@ -45,11 +38,7 @@ class PotvinFuglevand2017MotorNeuronPool(Model):
Longest duration of motor unit activity that will be recorded. The
default value should be >> than the time it takes to fatigue all
fibers. Helps prevent unbounded values.
.. todo::
Make pre_calc_max a function of other values as in the matlab code.
This will also require changing how we look up values if they
are larger than this value.
:apply_fatigue: Whether to calculate and apply central fatigue.
Usage::
Expand All @@ -69,9 +58,6 @@ def __init__(
min_firing_rate: int = 8,
max_firing_rate_first_unit: int = 35,
max_firing_rate_last_unit: int = 25,
pre_calc_firing_rates: bool = False,
pre_calc_resolution: float = 0.1,
pre_calc_max: float = 70.0,
derecruitment_delta: int = 2,
adaptation_magnitude: float = 0.67,
adaptation_time_constant: float = 22.0,
Expand Down Expand Up @@ -115,43 +101,6 @@ def __init__(
# Pre-calculate firing rates for all motor neurons across a range of
# possible excitation levels.
self._firing_rates_by_excitation: Dict[float, Any] = {}
if pre_calc_firing_rates:
self._build_firing_rates_cache(pre_calc_max, pre_calc_resolution)

def _build_firing_rates_cache(
self,
pre_calc_max: float,
pre_calc_resolution: float
) -> None:
"""
Pre-calculate and store firing rates for all motor neurons across a
range of possible excitation levels.
:param pre_calc_max: The maximum excitation value to calculate.
:param pre_calc_resolution:
The step size between values during calculations.
"""

# TODO: This is a hack. Maybe memoize vs pre-calculate?
# Maybe https://docs.python.org/3/library/functools.html#functools.lru_cache
resolution_places = len(str(pre_calc_resolution).split(".")[1])
excitations = np.zeros(self.motor_unit_count)
excitation_values = np.arange(
0.0,
pre_calc_max + pre_calc_resolution,
pre_calc_resolution
)
for i in excitation_values:
i = round(i, resolution_places)
self._firing_rates_by_excitation[i] = \
self._inner_calc_firing_rates(
excitations,
self._recruitment_thresholds,
self._firing_gain,
self._min_firing_rate,
self._peak_firing_rates
)
excitations += pre_calc_resolution

def _calc_adapted_firing_rates(
self,
Expand Down
9 changes: 4 additions & 5 deletions pymuscle/potvin_fuglevand_2017_muscle_fibers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class PotvinFuglevand2017MuscleFibers(Model):
[milliseconds] Maximum contraction time for a motor unit (tL)
:param contraction_time_range:
The scale between the fastest contraction time and the slowest (rt)
:fatigue_factor_first_unit:
The nominal fatigability of the first motor unit in percent / second
:max_fatigue_rate: [percent per second] The rate at which the largest
motor unit will fatigue at maximum excitation.
:fatigability_range:
The scale between the fatigability of the first motor unit and the last
:contraction_time_change_ratio:
For each percent of force lost during fatigue, what percentage should
contraction increase? Based on Shields et al (1997)
:apply_fatigue: Whether to calculate and apply peripheral fatigue.
.. todo::
The argument naming isn't consistent. Sometimes we use 'max' and other
Expand All @@ -48,8 +49,6 @@ def __init__(
max_twitch_amplitude: int = 100,
max_contraction_time: int = 90,
contraction_time_range: int = 3,
max_recruitment_threshold: int = 50,
fatigue_factor_first_unit: float = 0.0125,
max_fatigue_rate: float = 0.0225,
fatigability_range: int = 180,
contraction_time_change_ratio: float = 0.379,
Expand Down Expand Up @@ -81,7 +80,7 @@ def __init__(
self._peak_twitch_forces
)

# Assing other non-public attributes
# Assign other non-public attributes
self._contraction_time_change_ratio = contraction_time_change_ratio
self._apply_fatigue = apply_fatigue
self._max_fatigue_rate = max_fatigue_rate
Expand Down
20 changes: 0 additions & 20 deletions tests/test_potvin_fuglevand_2017_motor_neuron_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,6 @@ def test_step():
assert output_sum == pytest.approx(max_output)


def test_precacl_values():
# Pre calculated values and on-the-fly values should be the same
motor_unit_count = 120
p1 = Pool(motor_unit_count)
p2 = Pool(
motor_unit_count,
pre_calc_firing_rates=True
)
moderate_input = 40.0
input_array = np.full(motor_unit_count, moderate_input)

output1 = p1.step(input_array, 1.0)
output1_sum = np.sum(output1)

output2 = p2.step(input_array, 1.0)
output2_sum = np.sum(output2)

assert output1_sum == pytest.approx(output2_sum)


def test_fatigue_values():
motor_unit_count = 120

Expand Down

0 comments on commit 35a2f52

Please sign in to comment.