Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rolling_mean_pad Performance Improvements #249

Merged
merged 5 commits into from
Jul 12, 2024
Merged
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
28 changes: 14 additions & 14 deletions flepimop/gempyor_pkg/src/gempyor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy.typing as npt
import pandas as pd
import pyarrow as pa
import scipy.ndimage
import scipy.stats
import sympy.parsing.sympy_parser

Expand Down Expand Up @@ -430,20 +431,19 @@ def rolling_mean_pad(
[20.2, 21.2, 22.2, 23.2],
[22.6, 23.6, 24.6, 25.6]])
"""
padding_size = (window - 1) // 2
padded_data = np.pad(data, ((padding_size, padding_size), (0, 0)), mode="edge")

# Allocate space for the result
result = np.zeros_like(data)

# Perform convolution along the days axis (axis 0) using a loop
for i in range(data.shape[0]):
# Extract the current day's data from the padded array
window_data = padded_data[i : i + window, :]
# Calculate the rolling mean for this day's data
result[i, :] = np.mean(window_data, axis=0)

return result
weights = (1. / window) * np.ones(window)
output = scipy.ndimage.convolve1d(data, weights, axis=0, mode="nearest")
if window % 2 == 0:
rows, cols = data.shape
i = rows - 1
output[i, :] = 0.
window -= 1
weight = 1. / window
for l in range(-((window - 1) // 2), 1 + (window // 2)):
i_star = min(max(i + l, 0), i)
for j in range(cols):
output[i, j] += weight * data[i_star, j]
return output


def print_disk_diagnosis():
Expand Down
Loading