Skip to content

Residual plot tests are not actually exercised #48

Description

@baogorek

Summary

The TestResidualPlots tests in tests/test_plotting.py are not actually testing the residual plotting functionality because the fixture produces results where has_residuals is False.

Details

The tests have conditional guards like:

def test_plot_residuals_available(self, result_with_residuals):
    if result_with_residuals.has_residuals:
        plotter = Plotter(result_with_residuals)
        fig = plotter.plot_residuals()
        assert isinstance(fig, ControlChartFigure)

But the result_with_residuals fixture returns a result where has_residuals is False:

@pytest.fixture
def result_with_residuals(self):
    np.random.seed(42)
    df = pd.DataFrame({
        'value': np.random.normal(100, 5, 100),
        'subgroup': np.repeat(range(20), 5),
        'time': range(100)
    })
    pdf = ProcessDataFrame(df)
    study = pdf.formulate(response=pdf.columns.value, factors=[pdf.columns.subgroup])
    return study.analyze()  # has_residuals is False!

This means the tests pass by doing nothing - plot_residuals() is never called.

Affected tests

  • test_plot_residuals_available
  • test_plot_residuals_histogram
  • test_plot_residuals_qq
  • test_plot_residuals_sequence

Suggested fix

  1. Fix the fixture to produce results that actually have residuals

  2. Consider eliminating scipy entirely from plot_residuals() to keep the package lightweight. The current scipy usage is minimal:

    • norm.pdf() for histogram overlay - trivial to replace:

      (1 / (std * math.sqrt(2 * math.pi))) * math.exp(-0.5 * ((x - mean) / std) ** 2)
    • norm.ppf() for Q-Q plot quantiles - can use well-known approximations (e.g., Abramowitz & Stegun or Beasley-Springer-Moro algorithm)

    This would match the approach taken in PR fix: Resolve ruff linting errors and remove scipy dependency #47 where scipy.special.loggamma was replaced with math.lgamma in spc_constants.py.

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions