Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions ratapi/utils/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,7 @@ def plot_one_hist(
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

sigma = sd_y / 2
counts = gaussian_filter1d(counts, sigma)
counts = moving_avg(counts)
axes.hist(
bins[:-1],
bins,
Expand Down Expand Up @@ -1233,3 +1231,30 @@ def plot_bayes(project: ratapi.Project, results: ratapi.outputs.BayesResults):
plot_corner(results)
else:
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

"""Calculate the moving average of an array with a given window size.

This is a python equivalent to MATLABs smoothdata(A, 'movmean')

Parameters
----------
data : np.ndarray
The input array to smooth
window_size : int
The window slides down the length of the vector,
computing an average over the elements within each window.

"""
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

``

start_window_ind = floor(float(i - window_size / 2)) if i - window_size / 2 > 0 else 0
end_window_ind = floor(float(i + window_size / 2)) if i + window_size / 2 < len(data) else len(data)
window_average = np.sum(data[start_window_ind:end_window_ind]) / (end_window_ind + 0 - start_window_ind)
moving_averages.append(window_average)
i += 1

return moving_averages
Loading