code: def test_pad_trunc():
      Nt = 100
      width = 5
      nvars = 5
      seg = transform.PadTrunc(width=width)
  
      # multivariate ts data without context data
      X = [
          np.random.rand(Nt, nvars),
          np.random.rand(Nt, nvars),
          np.random.rand(Nt, nvars),
      ]
      y = [np.random.rand(Nt), np.random.rand(Nt), np.random.rand(Nt)]
      seg.fit(X, y)
      Xs, ys, _ = seg.transform(X, y)
      N = len(ys)
      assert Xs.shape == (N, width, nvars)
      assert np.all(
          [np.equal(X[i][0:width], Xs[i]) for i in range(len(X))]
      )
      assert np.all(
          [np.equal(y[i][0:width], ys[i]) for i in range(len(y))]
      )
  
      # univariate ts data without context data
      X = [
          np.random.rand(Nt),
          np.random.rand(2 * Nt),
          np.random.rand(3 * Nt),
      ]
      y = [
          np.random.rand(Nt),
          np.random.rand(2 * Nt),
          np.random.rand(3 * Nt),
      ]
      seg.fit(X, y)
      Xs, ys, _ = seg.transform(X, y)
      N = len(ys)
      assert Xs.shape == (N, width)
      assert np.all(
          [np.equal(X[i][0:width], Xs[i]) for i in range(len(X))]
      )
      assert np.all(
          [np.equal(y[i][0:width], ys[i]) for i in range(len(y))]
      )
  
      # multivariate ts data with context data
      Xt = [
          np.random.rand(Nt, nvars),
          np.random.rand(2 * Nt, nvars),
          np.random.rand(Nt, nvars),
      ]
      Xc = np.random.rand(3, 4)
      y = [
          np.random.rand(Nt),
          np.random.rand(2 * Nt),
          np.random.rand(Nt),
      ]
      X = TS_Data(Xt, Xc)
      seg.fit(X, y)
      Xs, ys, _ = seg.transform(X, y)
      Xst, Xsc = get_ts_data_parts(Xs)
      N = len(ys)
      assert Xst.shape == (N, width, nvars)
      assert Xsc.shape == (N, 4)
      assert np.all(
          [np.equal(Xt[i][0:width], Xst[i]) for i in range(len(Xt))]
      )
      assert np.all([np.equal(Xc[i], Xsc[i]) for i in range(len(Xt))])
      assert np.all(
          [np.equal(y[i][0:width], ys[i]) for i in range(len(y))]
      )
  
      # ts data with univariate context data
      Xt = [
          np.random.rand(Nt, nvars),
          np.random.rand(2 * Nt, nvars),
          np.random.rand(Nt, nvars),
      ]
      Xc = np.random.rand(3)
      y = [
          np.random.rand(Nt),
          np.random.rand(2 * Nt),
          np.random.rand(Nt),
      ]
      X = TS_Data(Xt, Xc)
      seg.fit(X, y)
      Xs, ys, _ = seg.transform(X, y)
      Xst, Xsc = get_ts_data_parts(Xs)
      N = len(ys)
      assert Xst.shape == (N, width, nvars)
      assert Xsc.shape == (N,)
      assert np.all(
          [np.equal(Xt[i][0:width], Xst[i]) for i in range(len(Xt))]
      )
      assert np.all([np.equal(Xc[i], Xsc[i]) for i in range(len(Xt))])
      assert np.all(
          [np.equal(y[i][0:width], ys[i]) for i in range(len(y))]
      )
  
      # same number as context vars and time vars
      # this would cause broadcasting failure before implementation of TS_Data class
      Xt = [
          np.random.rand(Nt, nvars),
          np.random.rand(2 * Nt, nvars),
          np.random.rand(Nt, nvars),
      ]
      Xc = np.random.rand(3, nvars)
      y = [
          np.random.rand(Nt),
          np.random.rand(2 * Nt),
          np.random.rand(Nt),
      ]
      X = TS_Data(Xt, Xc)
      seg.fit(X, y)
      Xs, ys, _ = seg.transform(X, y)
      Xst, Xsc = get_ts_data_parts(Xs)
      N = len(ys)
      assert Xst.shape == (N, width, nvars)
      assert Xsc.shape == (N, 5)
      assert np.all(
          [np.equal(Xt[i][0:width], Xst[i]) for i in range(len(Xt))]
      )
      assert np.all([np.equal(Xc[i], Xsc[i]) for i in range(len(Xt))])
      assert np.all(
          [np.equal(y[i][0:width], ys[i]) for i in range(len(y))]
      )
  
      width = 5
      nvars = 5
      seg = transform.PadTrunc(width=width)
  
      # multivariate ts data without context data
      X = [
          np.random.rand(100, nvars),
          np.random.rand(100, nvars),
          np.random.rand(100, nvars),
      ]
      y = np.random.rand(3)
      seg.fit(X, y)
      Xs, ys, _ = seg.transform(X, y)
      N = len(ys)
      assert Xs.shape == (N, width, nvars)
      assert np.all(
          [np.equal(X[i][0:width], Xs[i]) for i in range(len(Xt))]
      )
      assert np.all([np.equal(y[i], ys[i]) for i in range(len(y))])
  
      # univariate ts data without context
      X = [
          np.random.rand(100),
          np.random.rand(100),
          np.random.rand(100),
      ]
      y = np.random.rand(3)
      seg.fit(X, y)
      Xs, ys, _ = seg.transform(X, y)
      N = len(ys)
      assert Xs.shape == (N, width)
      assert np.all(
          [np.equal(X[i][0:width], Xs[i]) for i in range(len(Xt))]
      )
      assert np.all([np.equal(y[i], ys[i]) for i in range(len(y))])
  
      # multivariate ts data with context data
      Xt = [
          np.random.rand(100, nvars),
          np.random.rand(200, nvars),
          np.random.rand(50, nvars),
      ]
      Xc = np.random.rand(3, 4)
      y = np.random.rand(3)
      X = TS_Data(Xt, Xc)
      seg.fit(X, y)
      Xs, ys, _ = seg.transform(X, y)
      Xst, Xsc = get_ts_data_parts(Xs)
      N = len(ys)
      assert Xst.shape == (N, width, nvars)
      assert Xsc.shape == (N, 4)
      assert np.all(
          [np.equal(Xt[i][0:width], Xst[i]) for i in range(len(Xt))]
      )
      assert np.all([np.equal(Xc[i], Xsc[i]) for i in range(len(Xt))])
      assert np.all([np.equal(y[i], ys[i]) for i in range(len(y))])
  
file_path: seglearn/seglearn/tests/test_transform.py
imports: import warnings
  import numpy
  import pytest
  import seglearn.transform
  from seglearn.base import TS_Data
  from seglearn.feature_functions import all_features, mean
  from seglearn.util import get_ts_data_parts
label_desc: Use image node_id 7 for calling a global function with example usage: test_pad_trunc() without return types