Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: dormancy calculations #1413

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/ramstk/analyses/dormancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
# ramstk.analyses.Dormancy.py is part of the RAMSTK Project
#
# All rights reserved.
# Copyright 2007 - 2024 Doyle Rowland doyle.rowland <AT> reliaqual <DOT> com
# Copyright since 2007 Doyle Rowland doyle.rowland <AT> reliaqual <DOT> com
"""Dormancy Calculations Module."""

# Standard Library Imports
from typing import List, Union
from typing import List, Optional, Union

DORMANT_HR_MULTIPLIER = {
"ground": {
Expand Down Expand Up @@ -50,20 +50,24 @@
]


def get_environment_type(env_id: int, is_active: bool) -> str:
def get_environment_type(env_id: int, is_active: bool) -> Optional[str]:
weibullguy marked this conversation as resolved.
Show resolved Hide resolved
"""Get the environment type based on the environment ID.

:param env_id: the index in the environment list.
:param is_active: indicates whether or not to use the ENVIRONMENTS_ACTIVE list.
:return: the name of the environment associated with the end_id.
:rtype: str
"""
_index = env_id - 1
if is_active and _index < len(ENVIRONMENTS_ACTIVE):
return ENVIRONMENTS_ACTIVE[_index]
if env_id <= 0:
return None # Invalid env_id, must be positive

if _index < len(ENVIRONMENTS_DORMANT):
_index = env_id - 1
if is_active:
if _index < len(ENVIRONMENTS_ACTIVE):
return ENVIRONMENTS_ACTIVE[_index]
elif _index < len(ENVIRONMENTS_DORMANT):
return ENVIRONMENTS_DORMANT[_index]
return None


def get_dormant_hr_multiplier(
Expand Down Expand Up @@ -165,11 +169,14 @@ def do_calculate_dormant_hazard_rate(
:rtype: float
:raise: IndexError if an indexing argument asks for a non-existent index.
"""
_env_active_id = get_environment_type(env_info[0], True)
_env_dormant_id = get_environment_type(env_info[1], False)
_env_active = get_environment_type(env_info[0], True)
_env_dormant = get_environment_type(env_info[1], False)

if _env_active is None or _env_dormant is None:
return 0.0

_dormant_hr_multiplier = get_dormant_hr_multiplier(
hw_info, _env_active_id, _env_dormant_id
hw_info, _env_active, _env_dormant
)

return _dormant_hr_multiplier * hw_info[2]
65 changes: 61 additions & 4 deletions tests/analyses/test_dormancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ def test_get_environment_type_dormant(dormant_env):
)


@pytest.mark.unit
@pytest.mark.parametrize("env_id", [-1, 0, 14])
def test_get_environment_type_invalid(env_id):
"""get_environment_type() should return None for invalid environment IDs."""
assert get_environment_type(env_id, True) is None
assert get_environment_type(env_id, False) is None


@pytest.mark.unit
@pytest.mark.parametrize("category_id", [1, 2, 3, 4, 5, 6, 7, 8])
def test_get_dormant_hr_multiplier_ground(category_id):
Expand Down Expand Up @@ -208,15 +216,21 @@ def test_get_dormant_hr_multiplier_space(category_id):
def test_get_dormant_hr_multiplier_no_category():
"""get_dormant_hr_multiplier() should return 0.0 if the category ID is not in
1-8."""
assert get_dormant_hr_multiplier([10, 1, 0.0], "ground", "ground") == 0.0
assert get_dormant_hr_multiplier([10, 1, 0.0], "ground", "ground") == pytest.approx(
0.0
)


@pytest.mark.unit
def test_get_dormant_hr_multiplier_no_environment():
"""get_dormant_hr_multiplier() should return 0.0 if the active/dormant environments
combination is not valid."""
assert get_dormant_hr_multiplier([1, 1, 0.0], "missile", "ground") == 0.0
assert get_dormant_hr_multiplier([1, 1, 0.0], "ground", "airborne") == 0.0
assert get_dormant_hr_multiplier([1, 1, 0.0], "missile", "ground") == pytest.approx(
0.0
)
assert get_dormant_hr_multiplier(
[1, 1, 0.0], "ground", "airborne"
) == pytest.approx(0.0)


@pytest.mark.unit
Expand All @@ -241,12 +255,55 @@ def test_dormant_hazard_rate(category_id, subcategory_id, expected):
assert _hr_dormant == pytest.approx(expected)


@pytest.mark.unit
def test_get_dormant_hr_multiplier_invalid_subcategory():
"""get_dormant_hr_multiplier() should return 0.0 if the subcategory is out of range
for semiconductors."""
# Test with invalid subcategory for semiconductors.
assert get_dormant_hr_multiplier([2, 5, 0.0], "ground", "ground") == pytest.approx(
0.0
)


@pytest.mark.unit
@pytest.mark.calculation
def test_dormant_hazard_rate_bad_index():
"""do_calculate_dormant_hazard_rate() should raise an IndexError when a bad index
value is passed."""
# with pytest.raises(ValueError):
_hr_dormant = do_calculate_dormant_hazard_rate(
hw_info=[4, 5, 0.008642374], env_info=[3, 12]
)


@pytest.mark.unit
def test_dormant_hazard_rate_invalid_environment():
"""do_calculate_dormant_hazard_rate() should return 0.0 when an invalid environment
is passed."""
_hr_dormant = do_calculate_dormant_hazard_rate(
hw_info=[1, 1, 0.008642374], env_info=[14, 1]
)
assert _hr_dormant == pytest.approx(0.0)

_hr_dormant = do_calculate_dormant_hazard_rate(
hw_info=[1, 1, 0.008642374], env_info=[3, 5]
)
assert _hr_dormant == pytest.approx(0.0)


@pytest.mark.unit
def test_dormant_hazard_rate_zero_hazard_rate():
"""do_calculate_dormant_hazard_rate() should return 0.0 when the hazard rate is
0."""
_hr_dormant = do_calculate_dormant_hazard_rate(hw_info=[1, 1, 0.0], env_info=[3, 1])
assert _hr_dormant == pytest.approx(0.0)


@pytest.mark.unit
@pytest.mark.parametrize("subcategory_id", [1, 2, 3, 4])
def test_dormant_hazard_rate_category_1(subcategory_id):
"""do_calculate_dormant_hazard_rate() should handle different subcategories of
category 1."""
_hr_dormant = do_calculate_dormant_hazard_rate(
hw_info=[1, subcategory_id, 0.008642374], env_info=[3, 1]
)
assert isinstance(_hr_dormant, float)
Loading