def joint_recurrence_plot(df, threshold_val, percentage_val): """[summary] Input: dataframe Args: df ([type]): [description] """ result = df.copy() for area in tqdm_notebook(result.keys(), desc="to image"): X = result[area] X[np.isnan(X)] = -1 X = X.to_numpy() arr = np.array(X[0:24].T) # arr = np.append(arr,arr,axis=0) a = int(len(X) / 24) for col in range(1, a): # print(col) arr = np.append(arr, X[24 * col:24 * col + 24].T, axis=0) X = arr.reshape(-1, 9, 24) # Recurrence plot transformation jrp = JointRecurrencePlot(threshold=threshold_val, percentage=percentage_val) X_jrp = jrp.fit_transform(X) result[area] = X_jrp return result
def test_actual_results_single_value(params): """Test that the actual results are the expected ones.""" arr_actual = JointRecurrencePlot(**params).transform(X) arr_desired = [] for i in range(n_features): arr_desired.append(RecurrencePlot(**params).transform(X[:, i])) arr_desired = np.prod(arr_desired, axis=0) np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)
def test_actual_results_lists(params): """Test that the actual results are the expected ones.""" arr_actual = JointRecurrencePlot(**params).transform(X) arr_desired = [] for i, (threshold, percentage) in enumerate( zip(params['threshold'], params['percentage'])): arr_desired.append( RecurrencePlot(threshold=threshold, percentage=percentage).transform(X[:, i])) arr_desired = np.prod(arr_desired, axis=0) np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)
class TSToJRP(Transform): r"""Transforms a time series batch to a 4d TSImage (bs, n_vars, size, size) by applying Joint Recurrence Plot""" order = 98 def __init__(self, size=224, cmap=None, **kwargs): self.size,self.cmap = size,cmap self.encoder = JointRecurrencePlot(**kwargs) def encodes(self, o: TSTensor): o = to3d(o) bs, *_, seq_len = o.shape size = ifnone(self.size, seq_len) if size != seq_len: o = F.interpolate(o, size=size, mode='linear', align_corners=False) output = self.encoder.fit_transform(o.cpu().numpy()).reshape(bs, -1, size, size) if self.cmap and output.shape[1] == 1: output = TSImage(plt.get_cmap(self.cmap)(output)[..., :3]).squeeze(1).permute(0,3,1,2) else: output = TSImage(output) return output.to(device=o.device)
def test_shapes(params, shape_desired): """Test that the shape of the output is the expected one.""" transformer = JointRecurrencePlot(**params) assert transformer.fit(X).transform(X).shape == shape_desired assert transformer.fit_transform(X).shape == shape_desired
def test_parameter_check(params, error, err_msg): """Test parameter validation.""" transformer = JointRecurrencePlot(**params) with pytest.raises(error, match=re.escape(err_msg)): transformer.transform(X)
===================== A joint recurrence plot is an extension of recurrence plots ( implemented as :class:`pyts.image.RecurrencePlot`) for multivariate time series. A recurrence plot is built for each feature of the multivariate time series, then the set of recurrence plots is reduced to one single recurrence plot using the Hadamard product. This example illustrates this transformation. It is implemented as :class:`pyts.multivariate.image.JointRecurrencePlot`. """ # Author: Johann Faouzi <*****@*****.**> # License: BSD-3-Clause import matplotlib.pyplot as plt from pyts.multivariate.image import JointRecurrencePlot from pyts.datasets import load_basic_motions X, _, _, _ = load_basic_motions(return_X_y=True) # Recurrence plot transformation jrp = JointRecurrencePlot(threshold='point', percentage=50) X_jrp = jrp.fit_transform(X) # Show the results for the first time series plt.figure(figsize=(5, 5)) plt.imshow(X_jrp[0], cmap='binary', origin='lower') plt.title('Joint Recurrence Plot', fontsize=18) plt.tight_layout() plt.show()
def __init__(self, size=224, cmap=None, **kwargs): self.size, self.cmap = size, cmap self.encoder = JointRecurrencePlot(**kwargs)
def jrp_encode_3_to_3(arr_3d): transformer = JointRecurrencePlot() jrp_iss_3d = (transformer.transform(array.swapaxes(1, 2)) for array in arr_3d) return jrp_iss_3d
X_multi, _, y_multi, _ = load_basic_motions(return_X_y=True) @pytest.mark.parametrize('estimator, X, y', [ (SymbolicFourierApproximation(n_bins=2), X_uni, None), (SymbolicFourierApproximation(n_bins=2, strategy='entropy'), X_uni, y_uni) ]) def test_univariate_transformer_mixin(estimator, X, y): sfa_1 = clone(estimator) sfa_2 = clone(estimator) np.testing.assert_array_equal(sfa_1.fit_transform(X, y), sfa_2.fit(X, y).transform(X)) @pytest.mark.parametrize('estimator, X, y', [(JointRecurrencePlot(), X_multi, None), (JointRecurrencePlot(), X_multi, y_multi)]) def test_multivariate_transformer_mixin(estimator, X, y): jrp_1 = clone(estimator) jrp_2 = clone(estimator) np.testing.assert_allclose(jrp_1.fit_transform(X, y), jrp_2.fit(X, y).transform(X)) @pytest.mark.parametrize( 'sample_weight', [None, np.ones_like(y_uni), np.random.uniform(size=y_uni.size)]) def test_univariate_classifier_mixin(sample_weight): clf = SAXVSM().fit(X_uni, y_uni) assert isinstance(clf.score(X_uni, y_uni, sample_weight),