ReliaQualAssociates / ramstk

Reliability, Availability, Maintainability, Safety (RAMS) analysis program.
https://www.reliaqual.com/rtk/
BSD 3-Clause "New" or "Revised" License
46 stars 15 forks source link

refactor: dormancy calculations #1413

Closed weibullguy closed 1 month ago

weibullguy commented 1 month ago

Does this PR introduce a breaking change?

Describe the purpose of this pull request.

Added detailed type hints throughout the code to ensure clarity on what each parameter and return value is expected to be. The get_environment_type function now returns Optional[str], indicating that it may return None if the environment ID is invalid.

Instead of potentially allowing the get_environment_type function to return an invalid environment string, it now explicitly returns None if the env_id is out of bounds. The do_calculate_dormant_hazard_rate function handles this case by returning 0.0 if either environment is invalid.

Clarified hw_info indices in the docstrings to help understand what each index represents.

The code that handles the special case for semiconductors (category 1) was clarified, reducing unnecessary branches and making the logic easier to follow.

Describe how this was implemented.

Ran code through ChatGPT.

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

None

Provide any other pertinent information.

Pull Request Checklist

Summary by Sourcery

Refactor dormancy calculations to improve clarity and robustness by handling invalid environment IDs and simplifying logic for semiconductors. Enhance type hinting and expand test coverage to ensure correct behavior in edge cases.

Enhancements:

Tests:

sourcery-ai[bot] commented 1 month ago

Reviewer's Guide by Sourcery

This pull request refactors the dormancy calculations in the RAMSTK project, focusing on improving type hints, error handling, and test coverage. The changes primarily affect the dormancy.py file in the src/ramstk/analyses/ directory and its corresponding test file test_dormancy.py in the tests/analyses/ directory.

Updated class diagram for dormancy calculations

classDiagram
    class DormancyCalculations {
        +get_environment_type(env_id: int, is_active: bool) Optional~str~
        +get_dormant_hr_multiplier(hw_info: List~Union~int, float~~, env_active: str, env_dormant: str) float
        +do_calculate_dormant_hazard_rate(hw_info: List~Union~int, float~~, env_info: List~int~) float
    }
    DormancyCalculations : -DORMANT_HR_MULTIPLIER
    DormancyCalculations : +get_environment_type
    DormancyCalculations : +get_dormant_hr_multiplier
    DormancyCalculations : +do_calculate_dormant_hazard_rate
    DormancyCalculations : +ENVIRONMENTS_ACTIVE
    DormancyCalculations : +ENVIRONMENTS_DORMANT

File-Level Changes

Change Details Files
Improved type hinting and error handling in dormancy calculations
  • Added Optional[str] return type to get_environment_type function
  • Implemented explicit None return for invalid environment IDs
  • Updated do_calculate_dormant_hazard_rate to handle None environment types
  • Modified get_environment_type to check for positive env_id
src/ramstk/analyses/dormancy.py
Enhanced test coverage for dormancy calculations
  • Added tests for invalid environment IDs in get_environment_type
  • Implemented tests for invalid subcategories in get_dormant_hr_multiplier
  • Added tests for invalid environments in do_calculate_dormant_hazard_rate
  • Included test for zero hazard rate in do_calculate_dormant_hazard_rate
  • Added parametrized test for different subcategories of category 1
tests/analyses/test_dormancy.py
Improved code clarity and consistency
  • Updated copyright year format
  • Replaced direct comparisons with pytest.approx for floating-point assertions
  • Clarified variable names (e.g., _env_active_id to _env_active)
src/ramstk/analyses/dormancy.py
tests/analyses/test_dormancy.py

Tips and commands #### Interacting with Sourcery - **Trigger a new review:** Comment `@sourcery-ai review` on the pull request. - **Continue discussions:** Reply directly to Sourcery's review comments. - **Generate a GitHub issue from a review comment:** Ask Sourcery to create an issue from a review comment by replying to it. - **Generate a pull request title:** Write `@sourcery-ai` anywhere in the pull request title to generate a title at any time. - **Generate a pull request summary:** Write `@sourcery-ai summary` anywhere in the pull request body to generate a PR summary at any time. You can also use this command to specify where the summary should be inserted. #### Customizing Your Experience Access your [dashboard](https://app.sourcery.ai) to: - Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others. - Change the review language. - Add, remove or edit custom review instructions. - Adjust other review settings. #### Getting Help - [Contact our support team](mailto:support@sourcery.ai) for questions or feedback. - Visit our [documentation](https://docs.sourcery.ai) for detailed guides and information. - Keep in touch with the Sourcery team by following us on [X/Twitter](https://x.com/SourceryAI), [LinkedIn](https://www.linkedin.com/company/sourcery-ai/) or [GitHub](https://github.com/sourcery-ai).