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: statistical functions #1420

Closed weibullguy closed 1 month ago

weibullguy commented 1 month ago

Does this pull request introduce a breaking change?

Purpose of this pull request

This pull request refactors the statistical functions across multiple distribution types (normal, exponential, lognormal, and Weibull) to improve maintainability, abstraction, and testing. The key goals include:

Benefits of the pull request

Any particular area(s) reviewers should focus on

Any other pertinent information

Pull Request Checklist

Summary by Sourcery

Refactor statistical functions to centralize shared logic for hazard rate, MTBF, and survival calculations, improving maintainability and consistency across distribution types. Enhance unit test coverage by adding new test cases for edge conditions to ensure robustness.

Enhancements:

Tests:

sourcery-ai[bot] commented 1 month ago

Reviewer's Guide by Sourcery

This pull request refactors the statistical functions across multiple distribution types (normal, exponential, lognormal, and Weibull) to improve maintainability, abstraction, and testing. The key changes include centralizing common statistical calculations, refining distribution-specific methods, and enhancing unit test coverage.

Class diagram for refactored statistical functions

classDiagram
    class Distributions {
        +calculate_hazard_rate(time: float, location: float, scale: Optional[float], shape: Optional[float], dist_type: str) float
        +calculate_mtbf(shape: float, location: float, scale: float, dist_type: str) float
        +calculate_survival(shape: float, time: float, location: float, scale: float, dist_type: str) float
    }

    class Normal {
        +get_hazard_rate(location: float, scale: float, time: float) float
        +get_mtbf(location: float, scale: float) float
        +get_survival(location: float, scale: float, time: float) float
        +do_fit(data, kwargs) Tuple[float, float]
    }

    class Exponential {
        +get_hazard_rate(scale: float, location: float) float
        +get_mtbf(rate: float, location: float) float
        +get_survival(scale: float, time: float, location: float) float
        +do_fit(data, kwargs) Tuple[float, float]
    }

    class Lognormal {
        +get_hazard_rate(shape: float, location: float, scale: float, time: float) float
        +get_mtbf(shape: float, location: float, scale: float) float
        +get_survival(shape: float, location: float, scale: float, time: float) float
        +do_fit(data, kwargs) Tuple[float, float, float]
    }

    class Weibull {
        +get_hazard_rate(shape: float, location: float, scale: float, time: float) float
        +get_mtbf(shape: float, location: float, scale: float) float
        +get_survival(shape: float, location: float, scale: float, time: float) float
        +do_fit(data, kwargs) Tuple[float, float, float]
    }

    Distributions <|-- Normal
    Distributions <|-- Exponential
    Distributions <|-- Lognormal
    Distributions <|-- Weibull

File-Level Changes

Change Details Files
Centralized common statistical calculations into a new distributions.py file
  • Created calculate_hazard_rate(), calculate_mtbf(), and calculate_survival() functions
  • Implemented support for exponential, lognormal, normal, and Weibull distributions
  • Added error handling for unsupported distribution types
src/ramstk/analyses/statistics/distributions.py
Refactored distribution-specific files to use the new centralized functions
  • Updated get_hazard_rate(), get_mtbf(), and get_survival() functions to use the new centralized calculations
  • Simplified do_fit() functions by removing redundant code
  • Improved error handling for empty datasets
src/ramstk/analyses/statistics/exponential.py
src/ramstk/analyses/statistics/lognormal.py
src/ramstk/analyses/statistics/normal.py
src/ramstk/analyses/statistics/weibull.py
Enhanced unit tests for all distribution types
  • Added tests for edge cases (e.g., zero, negative, and very large parameter values)
  • Implemented tests for empty datasets and invalid distribution types
  • Updated existing tests to use pytest.approx() for float comparisons
tests/analyses/statistics/bounds_unit_test.py
tests/analyses/statistics/exponential_unit_test.py
tests/analyses/statistics/lognormal_unit_test.py
tests/analyses/statistics/normal_unit_test.py
tests/analyses/statistics/weibull_unit_test.py
tests/analyses/statistics/distributions_unit_test.py
Improved code style and documentation
  • Updated function docstrings for clarity and consistency
  • Standardized import statements across files
  • Removed redundant comments and improved existing ones
src/ramstk/analyses/statistics/bounds.py
src/ramstk/analyses/statistics/exponential.py
src/ramstk/analyses/statistics/lognormal.py
src/ramstk/analyses/statistics/normal.py
src/ramstk/analyses/statistics/weibull.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).