Experiment ACRIM1 and HF

[1]:
import datetime
import os

import gpflow as gpf
import numpy as np
import pandas as pd
import tensorflow as tf

from tsipy.fusion import LocalGPModel, SVGPModel
from tsipy.fusion.kernels import MultiWhiteKernel
from tsipy.fusion.utils import (
    build_and_concat_label_mask,
    build_and_concat_label_mask_output,
)
from tsipy.utils import (
    plot_signals,
    plot_signals_and_confidence,
    pprint,
    sort_inputs,
    transform_time_to_unit,
)

Parameters

[2]:
# Widths in years
pred_window_width = 1.5
fit_window_width = 4.5

normalization = True
clipping = True

num_inducing_pts = 500
max_iter = 5000

Helpers

[3]:
def load_dataset(dataset_name: str) -> pd.DataFrame:
    data_ = pd.read_csv(
        os.path.join("../data", f"{dataset_name}.txt"),
        delimiter=" ",
        header=None,
    )
    data_ = data_.rename(
        columns={
            0: "t",
            1: "a",
            2: "b",
        }
    )

    data_["t_org"] = data_["t"].values.copy()
    data_["t"] = transform_time_to_unit(
        data_["t"] - data_["t"][0],
        t_label="year",
        start=datetime.datetime(1980, 1, 1),
    )

    result_ = pd.read_csv(
        os.path.join("../data", f"{dataset_name}_results.txt"),
        delimiter=",",
        header=None,
    )
    result_ = result_.rename(
        columns={
            0: "t_org",
            1: "t",
            2: "s_out_mean",
            3: "s_out_std",
        }
    )
    return data_, result_

Load dataset

[4]:
data, result = load_dataset("acrim1_hf")
t_a = t_b = data["t"].values
a = data["a"].values
b = data["b"].values

res_t = result["t"].values
res_s_mean = result["s_out_mean"].values
res_s_std = result["s_out_std"].values

pprint("Input Signal", level=0)
pprint("- t_a", t_a.shape, level=1)
pprint("- a", a.shape, level=1)

pprint("Input Signal", level=0)
pprint("- t_b", t_b.shape, level=1)
pprint("- b", b.shape, level=1)

pprint("Result Signal", level=0)
pprint("- res_t", t_b.shape, level=1)
pprint("- res_s_mean", res_s_mean.shape, level=1)
pprint("- res_s_std", res_s_std.shape, level=1)

_ = plot_signals(
    [
        (t_a, a, r"$a$", {}),
        (t_b, b, r"$b$", {}),
    ],
    x_ticker=1,
)

_ = plot_signals_and_confidence(
    [(res_t, res_s_mean, res_s_std, "SVGP")], x_ticker=1,
)
Input Signal
    - t_a                                         (3436,)
    - a                                           (3436,)
Input Signal
    - t_b                                         (3436,)
    - b                                           (3436,)
Result Signal
    - res_t                                       (3436,)
    - res_s_mean                                  (3436,)
    - res_s_std                                   (3436,)
../_images/notebooks_exp_acrim1_hf_7_1.png
../_images/notebooks_exp_acrim1_hf_7_2.png

Data Fusion

[5]:
t_a = build_and_concat_label_mask(t_a, label=1)
t_b = build_and_concat_label_mask(t_b, label=2)
t_out = build_and_concat_label_mask_output(t_a)

t = np.vstack((t_a, t_b))
s = np.reshape(np.hstack((a, b)), newshape=(-1, 1))
t, s = sort_inputs(t, s, sort_axis=0)

pprint("Training data", level=0)
pprint("- t", t.shape, level=1)
pprint("- s", s.shape, level=1)
Training data
    - t                                           (6872, 2)
    - s                                           (6872, 1)

Kernel and Fusion Model

[6]:
# Kernel
matern_kernel = gpf.kernels.Matern12(active_dims=[0])
white_kernel = MultiWhiteKernel(labels=(1, 2), active_dims=[1])  # Sensors 1 and 2
kernel = matern_kernel + white_kernel

# Fusion and local model
local_model = SVGPModel(
    kernel=kernel,
    num_inducing_pts=num_inducing_pts,
)
fusion_model = LocalGPModel(
    model=local_model,
    pred_window_width=pred_window_width,
    fit_window_width=fit_window_width,
    normalization=normalization,
    clipping=clipping,
)

Training and Inference

[7]:
# Train
fusion_model.fit(t, s, max_iter=max_iter, verbose=True)

# Predict
s_out_mean, s_out_std = fusion_model(t_out)

pprint("Output Signal", level=0)
pprint("- t_out", t_out.shape, level=1)
pprint("- s_out_mean", s_out_mean.shape, level=1)
pprint("- s_out_std", s_out_std.shape, level=1)
Window
    - Prediction:                                 -inf, 1981.500
    - Training:                                   1980.000, 1983.000
    - Data indices:                               0,     2192
    - x, y:                                       (2193, 2), (2193, 1)
    - n_ind_pts/time_unit                         111.333
    - Step      1/5000:                           -4695.529
    - Step   1000/5000:                           -2869.061
    - Step   2000/5000:                           -2308.389
    - Step   3000/5000:                           -2498.535
    - Step   4000/5000:                           -2055.930
    - Step   5000/5000:                           -2198.182
Window
    - Prediction:                                 1981.500, 1983.000
    - Training:                                   1980.000, 1984.500
    - Data indices:                               0,     3289
    - x, y:                                       (3290, 2), (3290, 1)
    - n_ind_pts/time_unit                         111.111
    - Step      1/5000:                           -7810.377
    - Step   1000/5000:                           -4188.844
    - Step   2000/5000:                           -3571.496
    - Step   3000/5000:                           -3294.711
    - Step   4000/5000:                           -2971.427
    - Step   5000/5000:                           -3180.513
Window
    - Prediction:                                 1983.000, 1984.500
    - Training:                                   1981.500, 1986.000
    - Data indices:                               1096,     4383
    - x, y:                                       (3288, 2), (3288, 1)
    - n_ind_pts/time_unit                         111.111
    - Step      1/5000:                           -7800.074
    - Step   1000/5000:                           -3799.285
    - Step   2000/5000:                           -3598.185
    - Step   3000/5000:                           -3274.213
    - Step   4000/5000:                           -2655.067
    - Step   5000/5000:                           -3012.281
Window
    - Prediction:                                 1984.500, 1986.000
    - Training:                                   1983.000, 1987.500
    - Data indices:                               2192,     5478
    - x, y:                                       (3287, 2), (3287, 1)
    - n_ind_pts/time_unit                         111.111
    - Step      1/5000:                           -7321.062
    - Step   1000/5000:                           -4113.775
    - Step   2000/5000:                           -3388.062
    - Step   3000/5000:                           -3210.353
    - Step   4000/5000:                           -3376.418
    - Step   5000/5000:                           -3240.131
Window
    - Prediction:                                 1986.000, 1987.500
    - Training:                                   1984.500, 1989.000
    - Data indices:                               3289,     6574
    - x, y:                                       (3286, 2), (3286, 1)
    - n_ind_pts/time_unit                         111.111
    - Step      1/5000:                           -7255.371
    - Step   1000/5000:                           -3321.593
    - Step   2000/5000:                           -3651.196
    - Step   3000/5000:                           -3488.856
    - Step   4000/5000:                           -3051.031
    - Step   5000/5000:                           -2572.454
Window
    - Prediction:                                 1987.500, 1989.000
    - Training:                                   1986.000, 1989.405
    - Data indices:                               4383,     6871
    - x, y:                                       (2489, 2), (2489, 1)
    - n_ind_pts/time_unit                         111.323
    - Step      1/5000:                           -5862.596
    - Step   1000/5000:                           -2799.543
    - Step   2000/5000:                           -2457.265
    - Step   3000/5000:                           -2400.312
    - Step   4000/5000:                           -2183.402
    - Step   5000/5000:                           -1719.989
Window
    - Prediction:                                 1989.000,      inf
    - Training:                                   1987.500, 1989.405
    - Data indices:                               5478,     6871
    - x, y:                                       (1394, 2), (1394, 1)
    - n_ind_pts/time_unit                         111.314
    - Step      1/5000:                           -3240.337
    - Step   1000/5000:                           -1679.898
    - Step   2000/5000:                           -1411.971
    - Step   3000/5000:                           -1210.601
    - Step   4000/5000:                           -1278.131
    - Step   5000/5000:                           -1116.003
Output Signal
    - t_out                                       (3436, 3)
    - s_out_mean                                  (3436,)
    - s_out_std                                   (3436,)

Results

[8]:
fig, ax = plot_signals_and_confidence(
    [(t_out[:, 0], s_out_mean, s_out_std, "LocalGP")], x_ticker=1,
)
ax.scatter(
    t_a[:, 0],
    a,
    label=r"$a$",
    s=3,
)
ax.scatter(
    t_b[:, 0],
    b,
    label=r"$b$",
    s=3,
)
[8]:
<matplotlib.collections.PathCollection at 0x1dffed32e50>
../_images/notebooks_exp_acrim1_hf_15_1.png
[9]:
_ = plot_signals_and_confidence(
    [(t_out[:, 0], s_out_mean, s_out_std, "LocalGP")], x_ticker=1,
)

_ = plot_signals_and_confidence(
    [(res_t, res_s_mean, res_s_std, "SVGP")], x_ticker=1,
)
../_images/notebooks_exp_acrim1_hf_16_0.png
../_images/notebooks_exp_acrim1_hf_16_1.png
[10]:
_ = plot_signals_and_confidence(
    [
        (res_t, res_s_mean, res_s_std, "SVGP"),
        (t_out[:, 0], s_out_mean, s_out_std, "LocalGP"),
    ],
    legend="lower left",
    x_ticker=1,
)
../_images/notebooks_exp_acrim1_hf_17_0.png

Training

[11]:
for i, window in enumerate(fusion_model.windows):
    elbo = window.model.iter_elbo
    plot_signals([(np.arange(elbo.size), elbo, r"ELBO", {})])
../_images/notebooks_exp_acrim1_hf_19_0.png
../_images/notebooks_exp_acrim1_hf_19_1.png
../_images/notebooks_exp_acrim1_hf_19_2.png
../_images/notebooks_exp_acrim1_hf_19_3.png
../_images/notebooks_exp_acrim1_hf_19_4.png
../_images/notebooks_exp_acrim1_hf_19_5.png
../_images/notebooks_exp_acrim1_hf_19_6.png
[ ]: