Skip to content

Add a python equivalent to MATLABs smoothdata and use in Bayes plots#208

Open
MikeSullivan7 wants to merge 1 commit into
RascalSoftware:mainfrom
MikeSullivan7:bayes_plots_smoothing_moving_average
Open

Add a python equivalent to MATLABs smoothdata and use in Bayes plots#208
MikeSullivan7 wants to merge 1 commit into
RascalSoftware:mainfrom
MikeSullivan7:bayes_plots_smoothing_moving_average

Conversation

@MikeSullivan7
Copy link
Copy Markdown
Collaborator

@MikeSullivan7 MikeSullivan7 commented May 19, 2026

This PR fixes the issue with the Bayes plots smoothing not functioning correctly for background parameters. The issue comes from the use of scipy.ndimage.gaussian_filter1d instead of a moving average smoothing as is used in MATLAB: smoothdata(N, 'movmean').

This PR fixes the issue by implimenting a python version of smoothdata and the function has been benchmarked against results from MATLAB.
smoothdata automatically calculates the averaging window to use but there is no real explanation on how this is calculated. This has been set to 8 as default as this is the value used by MATLAB for RAT data most commonly.

Benchmark for noisy data:
MATLAB vs Python smoothing

Example of new smoothing for Substrate Roughness example:
image

Old smoothing:
python Gaussian smoothing

New smoothing:
python mov_avg smoothing

@MikeSullivan7 MikeSullivan7 marked this pull request as ready for review May 20, 2026 12:25
Copy link
Copy Markdown
Contributor

@StephenNneji StephenNneji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix, it works as advertised. Please see comments below

Comment thread ratapi/utils/plotting.py
raise ValueError("Bayes plots are only available for the results of Bayesian analysis (NS or DREAM)")


def moving_avg(data: np.ndarray, window_size: int = 8) -> list[float]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the function can be called moving_average as its not too long and slightly improves readability

Comment thread ratapi/utils/plotting.py
i = 0
moving_averages = []

while i < len(data):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it can be replaced by a for loop for i in range(len(data)) which I tend to prefer over a while loop but also means you can get rid of the index initialization and increment

``

Comment thread ratapi/utils/plotting.py
sd_y = np.std(parameter_chain)

if smooth:
if sigma is None:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update docstring to reflect switch to moving average, remove sigma. I also think we should expose the window size parameter since we don't know if 8 will work for all cases

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing the window size means the moving average function will need a check for valid window sizes and a unit test will be helpful

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