Note
Click here to download the full example code
Resampling methods#
An example to compare different resampling methods for age models. Usually, age models are not sampled on regular depth intervals. It can often be useful to have age models that are regularly sampled. There are several methods in paleos for achieving this including:
linear resample
cubic resample
spline resample
LOESS resample (local least squares)
LOWESS resample (locally weighted least squares)
Each of these will give a different result. Expert discretion should be used when choosing the best method to use. Each method will have pros and cons.
import numpy as np
import pandas as pd
import plotly
import plotly.graph_objects as go
import paleos.common as pcm
import paleos.agemodel as pam
Read in the original age model data
age_model = pd.read_csv("data/NathanLeckie_1146_GTS12.csv", header=None)
age_model.columns=["Depth", "Age"]
age_model = age_model.set_index("Depth").squeeze("columns")
print(age_model)
Out:
Depth
319.88 5.53
322.88 5.57
361.18 6.14
381.76 6.20
338.04 6.60
408.13 8.58
410.62 8.93
433.13 9.83
444.58 10.46
472.67 11.63
470.28 11.79
504.85 13.41
521.43 13.77
533.45 14.24
541.38 14.53
541.38 14.38
541.38 14.66
541.38 15.10
617.16 16.38
622.30 17.54
636.31 17.59
641.95 19.09
Name: Age, dtype: float64
There are some duplicated depths, let’s average out the ages for those depths
age_model = age_model.sort_index().dropna()
age_model = pcm.average_duplicated(age_model)
print(age_model)
Out:
2022-07-25 22:23:22.816 | INFO | paleos.common:average_duplicated:39 - Averaging values for duplicated indices
Depth
319.88 5.5300
322.88 5.5700
338.04 6.6000
361.18 6.1400
381.76 6.2000
408.13 8.5800
410.62 8.9300
433.13 9.8300
444.58 10.4600
470.28 11.7900
472.67 11.6300
504.85 13.4100
521.43 13.7700
533.45 14.2400
541.38 14.6675
617.16 16.3800
622.30 17.5400
636.31 17.5900
641.95 19.0900
Name: Age, dtype: float64
Define the new depths to interpolate/resample on to
new_depths = np.arange(300.0, 650.0, step=10.0)
Do the resampling with a selection of methods
linear = pam.resample_linear(age_model, new_depths)
cubic = pam.resample_cubic(age_model, new_depths)
spline = pam.resample_spline(age_model, new_depths)
loess = pam.resample_loess(age_model, new_depths, smoothing_factor=0.4)
lowess = pam.resample_lowess(age_model, new_depths, smoothing_factor=0.4)
Out:
2022-07-25 22:23:22.821 | INFO | paleos.interpolate:loess_ext_interpolate:312 - Series size 19, smoothing factor 0.4, points = 7
2022-07-25 22:23:22.827 | INFO | paleos.interpolate:lowess_sm_interpolate:240 - Series size 19, smoothing factor 0.4, points = 7.6000000000000005
Compare the original age model with the resampled data
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=age_model.values, y=age_model.index, mode="lines+markers", name="original"
)
)
fig.add_trace(
go.Scatter(
x=linear.values, y=linear.index, mode="lines+markers", name="Linear resample"
)
)
fig.add_trace(
go.Scatter(
x=cubic.values, y=cubic.index, mode="lines+markers", name="Cubic resample"
)
)
fig.add_trace(
go.Scatter(
x=spline.values, y=spline.index, mode="lines+markers", name="Spline resample"
)
)
fig.add_trace(
go.Scatter(
x=loess.values, y=loess.index, mode="lines+markers", name="LOESS resample"
)
)
fig.add_trace(
go.Scatter(
x=lowess.values, y=lowess.index, mode="lines+markers", name="LOWESS resample"
)
)
fig.update_yaxes(autorange="reversed")
fig.update_layout(width=700, height=1000, margin=go.layout.Margin(l=0, r=0, t=40, b=20))
fig.update_layout(
title="Age model resampling", xaxis_title="Age (Ma)", yaxis_title="Depth (m)"
)
plotly.io.show(fig)
Total running time of the script: ( 0 minutes 2.622 seconds)