Skip to content

fix: handle missing scatter_data in plot_centiles_advanced#374

Merged
contsili merged 2 commits intoamarquand:devfrom
divye-joshi:fix/plot-centiles-advanced-save-crash
Feb 19, 2026
Merged

fix: handle missing scatter_data in plot_centiles_advanced#374
contsili merged 2 commits intoamarquand:devfrom
divye-joshi:fix/plot-centiles-advanced-save-crash

Conversation

@divye-joshi
Copy link

Fix: handle missing scatter_data in plot_centiles_advanced

Closes #373

Problem

plot_centiles_advanced crashes when called without scatter_data in two scenarios:

  1. Saving plots: plotname is only defined inside if scatter_data:, but the save_dir logic at the end uses it unconditionally → NameError
  2. Multi-covariate models: The midpoint computation for non-plotted covariates accesses scatter_data.X unconditionally → AttributeError

Changes

Minimal two-line fix in plotter.py:

  1. Default plotname: Added
    plotname = f"centiles_{response_var}"
    before the if scatter_data: block so it always has a value when saving.

  2. Covariate fallback: When scatter_data is None, the non-plotted covariate value now falls back to the midpoint of the model's stored covariate range instead of trying to compute the mean from scatter data.

Verification

After the fix, the following calls work without error:

# Save centile-only plot
plot_centiles_advanced(model, save_dir="./plots")

# Multi-covariate model without scatter data  
plot_centiles_advanced(multi_cov_model)

Two fixes for plot_centiles_advanced crashing when scatter_data is None:

1. Multi-covariate models: the midpoint computation for non-plotted
   covariates used scatter_data.X unconditionally. Now falls back to
   the midpoint of the model's covariate range when scatter_data is
   not provided.

2. Saving plots: plotname was only defined inside the 'if scatter_data'
   block, causing a NameError when save_dir was set without scatter_data.
   Now plotname has a default value before the scatter_data check.
@contsili
Copy link
Collaborator

the same errors happen with the plot_centiles too. I will fix them in this branch and merge everything tomorrow. Thank you @divye-joshi!

@contsili contsili self-assigned this Feb 18, 2026
@divye-joshi
Copy link
Author

@contsili good point! thanks! I encountered this when using the notebooks where plot_centiles_advanced was used so i overlooked plot_centiles.

- Avoid errors when scatter_data is None
- Use the mean of scatter_data for other covariates when available (same logic as with the plot_centiles_advanced)
@contsili contsili merged commit 58989e9 into amarquand:dev Feb 19, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants