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: stress ratio calculations #1418

Merged
merged 3 commits into from
Oct 13, 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
12 changes: 6 additions & 6 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
## Does this PR introduce a breaking change?
## Does this pull request introduce a breaking change?
- [ ] Yes
- [ ] No

<!-- If yes, describe the impact and migration path below. -->

## Describe the purpose of this pull request.
## Purpose of this pull request


## Describe how this was implemented.
## Benefits of the pull request


## Describe any particular area(s) reviewers should focus on.
## Any particular area(s) reviewers should focus on


## Provide any other pertinent information.
<!-- Provide any other information that is import to this PR such as
## Any other pertinent information
<!-- Provide any other information that is important to this PR such as
screenshots if this impacts the GUI. -->


Expand Down
3 changes: 2 additions & 1 deletion src/ramstk/analyses/similaritem.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ def _do_update_sia_values(
:param values: the list of values to insert into `sia`.
:param start_idx: the start index in the SIA dictionary.
:param end_idx: the end index in the SIA dictionary.
:param default_value: the default value to use when the value list runs out of items.
:param default_value: the default value to use when the value list runs out of
items.
:param value_type: the type to which the values should be converted (int or float).
"""
for _idx in range(start_idx, end_idx + 1):
Expand Down
29 changes: 14 additions & 15 deletions src/ramstk/analyses/stress.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
#
# ramstk.analyses.Stress.py is part of the RAMSTK Project
# ramstk.analyses.stress.py is part of the RAMSTK Project
#
# All rights reserved.
# Copyright 2019 Doyle Rowland doyle.rowland <AT> reliaqual <DOT> com
# Copyright since 2007 Doyle Rowland doyle.rowland <AT> reliaqual <DOT> com
"""Component Stress Calculations Module."""


Expand All @@ -21,23 +21,22 @@ def calculate_stress_ratio(stress_operating, stress_rated):
>>> calculate_stress_ratio(1, 1.29)
0.7751937984496123

Rated stress must not be zero:
>>> calculate_stress_ratio(0.382, 0.0)
Traceback (most recent call last):
...
ZeroDivisionError: float division by zero

Stress inputs must not be strings:
>>> calculate_stress_ratio(0.382, '3.2')
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for /: 'float' and 'str'

:param stress_operating: the device's operating level of the stress.
:param stress_rated: the devices's rated stress.
:return: _stress_ratio; the ratio of operating stress to rated stress.
:return: the ratio of operating stress to rated stress.
:rtype: float
:raise: TypeError if an input value is non-numerical.
:raise: ZeroDivisionError if the rated stress is zero.
"""
if not isinstance(stress_operating, (int, float)) or not isinstance(
stress_rated, (int, float)
):
raise TypeError("Inputs must be numerical (int or float).")

if stress_operating < 0 or stress_rated < 0:
raise ValueError("Stress values must be non-negative.")

if stress_rated == 0:
raise ZeroDivisionError("Rated stress must not be zero.")

return stress_operating / stress_rated
67 changes: 65 additions & 2 deletions tests/analyses/test_stress.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# tests.analyses.test_stress.py is part of The RAMSTK Project
#
# All rights reserved.
# Copyright 2019 Doyle Rowland doyle.rowland <AT> reliaqual <DOT> com
# Copyright since 2007 Doyle Rowland doyle.rowland <AT> reliaqual <DOT> com
"""Test class for the electrical stress module."""

# Third Party Imports
Expand All @@ -21,7 +21,7 @@ def test_calculate_stress_ratio():
"""Return a float stress ratio on success."""
_stress_ratio = stress.calculate_stress_ratio(0.625, 1.25)

assert pytest.approx(_stress_ratio) == 0.5
assert _stress_ratio == pytest.approx(0.5)


@pytest.mark.unit
Expand All @@ -38,3 +38,66 @@ def test_calculate_stress_ratio_zero_rated():
"""Raise a ZeroDivisionError if passed a rated stress of zero."""
with pytest.raises(ZeroDivisionError):
stress.calculate_stress_ratio(0.625, 0.0)


@pytest.mark.unit
@pytest.mark.calculation
def test_calculate_stress_ratio_negative_operating():
"""Raise a ValueError if passed a negative operating stress."""
with pytest.raises(ValueError):
stress.calculate_stress_ratio(-0.625, 1.25)


@pytest.mark.unit
@pytest.mark.calculation
def test_calculate_stress_ratio_negative_rated():
"""Raise a ValueError if passed a negative rated stress."""
with pytest.raises(ValueError):
stress.calculate_stress_ratio(0.625, -1.25)
weibullguy marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.unit
@pytest.mark.calculation
def test_calculate_stress_ratio_zero_operating():
"""Return 0.0 when operating stress is zero."""
_stress_ratio = stress.calculate_stress_ratio(0.0, 1.25)
assert _stress_ratio == pytest.approx(0.0)
weibullguy marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.unit
@pytest.mark.calculation
def test_calculate_stress_ratio_both_zero():
"""Raise a ZeroDivisionError if both operating and rated stresses are zero."""
with pytest.raises(ZeroDivisionError):
stress.calculate_stress_ratio(0.0, 0.0)
weibullguy marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.unit
@pytest.mark.calculation
def test_calculate_stress_ratio_near_zero_operating():
"""Return near 0.0 when operating stress is very small."""
_stress_ratio = stress.calculate_stress_ratio(1e-10, 1.25)
assert _stress_ratio == pytest.approx(8.0e-11)


@pytest.mark.unit
@pytest.mark.calculation
@pytest.mark.parametrize(
"operating, rated", [(0.625, -1.25), (-0.625, 1.25), (-0.625, -1.25)]
)
def test_calculate_stress_ratio_invalid_inputs(operating, rated):
"""Raise a ValueError if passed invalid stress values."""
with pytest.raises(ValueError):
stress.calculate_stress_ratio(operating, rated)


@pytest.mark.unit
@pytest.mark.calculation
def test_calculate_stress_ratio_non_numerical():
"""Raise a TypeError if passed non-numerical inputs."""
with pytest.raises(TypeError):
stress.calculate_stress_ratio("string", 1.25)
with pytest.raises(TypeError):
stress.calculate_stress_ratio(0.625, "string")
with pytest.raises(TypeError):
stress.calculate_stress_ratio(None, 1.25)
Loading