From d9e2f7fa1534e81447c2068d65ff8481a0e10b5c Mon Sep 17 00:00:00 2001 From: Alexander James Date: Wed, 19 Apr 2023 12:04:15 -0700 Subject: [PATCH 1/6] fix gkernel --- pyleoclim/utils/tsutils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pyleoclim/utils/tsutils.py b/pyleoclim/utils/tsutils.py index 221d38f4..d0a7ed13 100644 --- a/pyleoclim/utils/tsutils.py +++ b/pyleoclim/utils/tsutils.py @@ -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 From 2837e363ed39e34d4edb911b7b69132f9f0c3be3 Mon Sep 17 00:00:00 2001 From: Alexander James Date: Wed, 19 Apr 2023 14:29:09 -0700 Subject: [PATCH 2/6] change gkernel default step style --- pyleoclim/core/series.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyleoclim/core/series.py b/pyleoclim/core/series.py index ae5cb454..0edd3e9a 100644 --- a/pyleoclim/core/series.py +++ b/pyleoclim/core/series.py @@ -3976,7 +3976,7 @@ 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_type='max', keep_log = False, **kwargs): ''' Coarse-grain a Series object via a Gaussian kernel. Like .bin() this technique is conservative and uses the max space between points @@ -4012,7 +4012,7 @@ def gkernel(self, step_type='median', keep_log = False, **kwargs): 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_type, **kwargs) # apply kernel new.time = ti new.value = vi From f2b6585639e35df6c19ec1a86b00f6c0ecc5a6f1 Mon Sep 17 00:00:00 2001 From: Alexander James Date: Wed, 19 Apr 2023 14:32:34 -0700 Subject: [PATCH 3/6] update gkernel docstring --- pyleoclim/core/series.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyleoclim/core/series.py b/pyleoclim/core/series.py index 0edd3e9a..69c49b24 100644 --- a/pyleoclim/core/series.py +++ b/pyleoclim/core/series.py @@ -3983,6 +3983,9 @@ def gkernel(self, step_type='max', keep_log = False, **kwargs): 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 ---------- From c211c0dc8f26843c9fb4c78a2f3bab730eebad73 Mon Sep 17 00:00:00 2001 From: CommonClimate Date: Wed, 19 Apr 2023 15:52:55 -0700 Subject: [PATCH 4/6] deprecate step_type --- pyleoclim/core/series.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pyleoclim/core/series.py b/pyleoclim/core/series.py index 69c49b24..c030d4af 100644 --- a/pyleoclim/core/series.py +++ b/pyleoclim/core/series.py @@ -3976,7 +3976,7 @@ def interp(self, method='linear', keep_log= False, **kwargs): return new - def gkernel(self, step_type='max', 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 @@ -3989,7 +3989,7 @@ def gkernel(self, step_type='max', keep_log = False, **kwargs): Parameters ---------- - step_type : str + step_style : str type of timestep: 'mean', 'median', or 'max' of the time increments @@ -4012,10 +4012,12 @@ def gkernel(self, step_type='max', 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, step_style=step_type, **kwargs) # apply kernel + ti, vi = tsutils.gkernel(self.time, self.value, step_style=step_style, **kwargs) # apply kernel new.time = ti new.value = vi From f471901815ed3b2009030253b5d32652952c01f7 Mon Sep 17 00:00:00 2001 From: CommonClimate Date: Wed, 19 Apr 2023 15:59:35 -0700 Subject: [PATCH 5/6] added test for step_style --- pyleoclim/tests/test_core_Series.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pyleoclim/tests/test_core_Series.py b/pyleoclim/tests/test_core_Series.py index c28c4988..4e6fc786 100644 --- a/pyleoclim/tests/test_core_Series.py +++ b/pyleoclim/tests/test_core_Series.py @@ -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 bandwidth''' + + 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 From de6508e9bf1da3180e041616221da13f0f494cab Mon Sep 17 00:00:00 2001 From: CommonClimate Date: Wed, 19 Apr 2023 16:03:09 -0700 Subject: [PATCH 6/6] Update test_core_Series.py --- pyleoclim/tests/test_core_Series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyleoclim/tests/test_core_Series.py b/pyleoclim/tests/test_core_Series.py index 4e6fc786..70bce0ea 100644 --- a/pyleoclim/tests/test_core_Series.py +++ b/pyleoclim/tests/test_core_Series.py @@ -801,7 +801,7 @@ def test_gkernel_t2(self): ts_interp=ts2.gkernel(h=15) def test_gkernel_t3(self): - ''' Test the gkernel function with specified bandwidth''' + ''' Test the gkernel function with specified step_style''' ts = gen_ts(nt=550, alpha=1.0) # randomly remove some data pts