class TSToGASF(Transform): r"""Transforms a time series batch to a 4d TSImage (bs, n_vars, size, size) by applying Gramian Angular Summation Field. It requires either input to be previously normalized between -1 and 1 or set range to (-1, 1)""" order = 98 def __init__(self, size=224, cmap=None, range=None, **kwargs): self.size, self.cmap, self.range = size, cmap, range self.encoder = GramianAngularField(image_size=1., sample_range=self.range, method='s', **kwargs) def encodes(self, o: TSTensor): bs, *_, seq_len = o.shape size = ifnone(self.size, seq_len) if size != seq_len: o = F.interpolate(o.reshape(-1, 1, seq_len), size=size, mode='linear', align_corners=False)[:, 0] else: o = o.reshape(-1, seq_len) output = self.encoder.fit_transform(o.cpu().numpy()).reshape( bs, -1, size, size) / 2 + .5 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 __init__(self, size=224, cmap=None, range=None, **kwargs): self.size, self.cmap, self.range = size, cmap, range self.encoder = GramianAngularField(image_size=1., sample_range=self.range, method='s', **kwargs)