Skip to content

Commit d43f0e9

Browse files
committed
Add a python equivalent to MATLABs smoothdata and use in Bayes plots
1 parent b0f51a9 commit d43f0e9

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

ratapi/utils/plotting.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,9 +826,7 @@ def plot_one_hist(
826826
sd_y = np.std(parameter_chain)
827827

828828
if smooth:
829-
if sigma is None:
830-
sigma = sd_y / 2
831-
counts = gaussian_filter1d(counts, sigma)
829+
counts = moving_avg(counts)
832830
axes.hist(
833831
bins[:-1],
834832
bins,
@@ -1233,3 +1231,30 @@ def plot_bayes(project: ratapi.Project, results: ratapi.outputs.BayesResults):
12331231
plot_corner(results)
12341232
else:
12351233
raise ValueError("Bayes plots are only available for the results of Bayesian analysis (NS or DREAM)")
1234+
1235+
1236+
def moving_avg(data: np.ndarray, window_size: int = 8) -> list[float]:
1237+
"""Calculate the moving average of an array with a given window size.
1238+
1239+
This is a python equivalent to MATLABs smoothdata(A, 'movmean')
1240+
1241+
Parameters
1242+
----------
1243+
data : np.ndarray
1244+
The input array to smooth
1245+
window_size : int
1246+
The window slides down the length of the vector,
1247+
computing an average over the elements within each window.
1248+
1249+
"""
1250+
i = 0
1251+
moving_averages = []
1252+
1253+
while i < len(data):
1254+
start_window_ind = floor(float(i - window_size / 2)) if i - window_size / 2 > 0 else 0
1255+
end_window_ind = floor(float(i + window_size / 2)) if i + window_size / 2 < len(data) else len(data)
1256+
window_average = np.sum(data[start_window_ind:end_window_ind]) / (end_window_ind + 0 - start_window_ind)
1257+
moving_averages.append(window_average)
1258+
i += 1
1259+
1260+
return moving_averages

0 commit comments

Comments
 (0)