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

fix gkernel #393

Merged
merged 6 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
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
15 changes: 10 additions & 5 deletions pyleoclim/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3976,17 +3976,20 @@ def interp(self, method='linear', keep_log= False, **kwargs):

return new

def gkernel(self, step_type='median', keep_log = False, **kwargs):
def gkernel(self, step_style='max', keep_log = False, step_type=None, **kwargs):
''' Coarse-grain a Series object via a Gaussian kernel.

Like .bin() this technique is conservative and uses the max space between points
as the default spacing. Unlike .bin(), gkernel() uses a gaussian kernel to
calculate the weighted average of the time series over these intervals.

Note that if the series being examined has very low resolution sections with few points,
you may need to tune the parameter for the kernel e-folding scale (h).

Parameters
----------

step_type : str
step_style : str

type of timestep: 'mean', 'median', or 'max' of the time increments

Expand All @@ -4009,10 +4012,12 @@ def gkernel(self, step_type='median', keep_log = False, **kwargs):

pyleoclim.utils.tsutils.gkernel : application of a Gaussian kernel
'''

if step_type is not None:
warnings.warn("step_type is deprecated. Please use step_style instead",
DeprecationWarning, stacklevel=2)

new=self.copy()

ti, vi = tsutils.gkernel(self.time, self.value, **kwargs) # apply kernel
ti, vi = tsutils.gkernel(self.time, self.value, step_style=step_style, **kwargs) # apply kernel
new.time = ti
new.value = vi

Expand Down
12 changes: 12 additions & 0 deletions pyleoclim/tests/test_core_Series.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,19 @@ def test_gkernel_t2(self):

ts2 = pyleo.Series(time=t_unevenly, value=v_unevenly)
ts_interp=ts2.gkernel(h=15)

def test_gkernel_t3(self):
''' Test the gkernel function with specified step_style'''

ts = gen_ts(nt=550, alpha=1.0)
# randomly remove some data pts
n_del = 50
deleted_idx = np.random.choice(range(np.size(ts.time)), n_del, replace=False)
t_unevenly = np.delete(ts.time, deleted_idx)
v_unevenly = np.delete(ts.value, deleted_idx)

ts2 = pyleo.Series(time=t_unevenly, value=v_unevenly)
ts_interp=ts2.gkernel(step_style='median')

class TestUISeriesInterp():
''' Unit tests for the interpolation function
Expand Down
3 changes: 1 addition & 2 deletions pyleoclim/utils/tsutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,7 @@ def gkernel(t,y, h = 3.0, step=None,start=None,stop=None, step_style = None, eve
if len(xslice)>0:
d = xslice-time_axis[i]
weight = kernel(d,h)
yc[i] = sum(weight*yslice)
yc /= sum(weight) # normalize by the sum of weights
yc[i] = sum(weight*yslice)/sum(weight) # normalize by the sum of weights
else:
yc[i] = np.nan

Expand Down