Skip to content

Commit

Permalink
updated tsai to Pytorch 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
oguiza committed Nov 13, 2023
1 parent 8da0578 commit a449b0b
Show file tree
Hide file tree
Showing 9 changed files with 1,446 additions and 1,459 deletions.
323 changes: 159 additions & 164 deletions nbs/002_utils.ipynb

Large diffs are not rendered by default.

2,407 changes: 1,195 additions & 1,212 deletions nbs/004_data.preparation.ipynb

Large diffs are not rendered by default.

110 changes: 60 additions & 50 deletions nbs/012_data.image.ipynb

Large diffs are not rendered by default.

Binary file modified nbs/data/TSCategoricalEncoder.joblib
Binary file not shown.
Binary file modified nbs/data/TSMissingnessEncoder.joblib
Binary file not shown.
Binary file modified nbs/models/test.pth
Binary file not shown.
58 changes: 29 additions & 29 deletions tsai/data/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ def __getitem__(self, idx):
return res.as_subclass(type(self))

@property
def vars(self):
def vars(self):
return self.shape[-3]

@property
def len(self): return self.shape[-2:]

def __repr__(self):
if self.ndim == 0: return f'{self.data}'
else: return f'TSImage(shape:{self.shape})'

def show(self, **kwargs):
if self.ndim < 3:
if self.ndim < 3:
while True:
self = self[None]
if self.ndim == 3: break
elif self.ndim > 3:
while True:
self = self[0]
if self.ndim == 3: break
if self[:3].shape[0] == 3 and kwargs == {}:
if self[:3].shape[0] == 3 and kwargs == {}:
display(to_image(self[:3]))
return
else:
else:
TensorImage(self[:3]).show(**kwargs)
return

Expand Down Expand Up @@ -78,7 +78,7 @@ def encodes(self, o: TSTensor):
output = []
for oi in o:
start = time.time()
ax.plot(oi.T, lw=self.lw, **self.kwargs)
ax.plot(oi.T.numpy(), lw=self.lw, **self.kwargs)
canvas.draw()
buf = np.asarray(canvas.buffer_rgba())[..., :3]
output.append(tensor(buf / 255).permute(2,0,1)[None])
Expand Down Expand Up @@ -106,7 +106,7 @@ def encodes(self, o: TSTensor):
canvas = FigureCanvasAgg(fig)
output = []
for oi in o:
if output == []: im = ax.imshow(oi, aspect=aspect, vmin=-1, vmax=1, cmap=self.cmap, **self.kwargs)
if output == []: im = ax.imshow(oi, aspect=aspect, vmin=-1, vmax=1, cmap=self.cmap, **self.kwargs)
else: im.set_data(oi)
canvas.draw()
buf = np.asarray(canvas.buffer_rgba())[..., :3]
Expand All @@ -121,7 +121,7 @@ class TSToGADF(Transform):
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):
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='d', **kwargs)

Expand All @@ -130,22 +130,22 @@ def encodes(self, o: TSTensor):
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:
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:
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)


@delegates(GramianAngularField.__init__)
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):
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)

Expand All @@ -154,22 +154,22 @@ def encodes(self, o: TSTensor):
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:
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:
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)



@delegates(MarkovTransitionField.__init__)
class TSToMTF(Transform):
r"""Transforms a time series batch to a 4d TSImage (bs, n_vars, size, size) by applying Markov Transition Field"""
order = 98

def __init__(self, size=224, cmap=None, n_bins=5, **kwargs):
def __init__(self, size=224, cmap=None, n_bins=5, **kwargs):
self.size,self.cmap = size,cmap
self.encoder = MarkovTransitionField(n_bins=n_bins, **kwargs)

Expand All @@ -178,22 +178,22 @@ def encodes(self, o: TSTensor):
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:
else:
o = o.reshape(-1, seq_len)
output = self.encoder.fit_transform(o.cpu().numpy()).reshape(bs, -1, size, size)
if self.cmap and output.shape[1] == 1:
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)


@delegates(RecurrencePlot.__init__)
class TSToRP(Transform):
r"""Transforms a time series batch to a 4d TSImage (bs, n_vars, size, size) by applying Recurrence Plot.
r"""Transforms a time series batch to a 4d TSImage (bs, n_vars, size, size) by applying Recurrence Plot.
It requires input to be previously normalized between -1 and 1"""
order = 98

def __init__(self, size=224, cmap=None, **kwargs):
def __init__(self, size=224, cmap=None, **kwargs):
self.size,self.cmap = size,cmap
self.encoder = RecurrencePlot(**kwargs)

Expand All @@ -202,22 +202,22 @@ def encodes(self, o: TSTensor):
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:
else:
o = o.reshape(-1, seq_len)
output = self.encoder.fit_transform(o.cpu().numpy()) / 2
output = output.reshape(bs, -1, size, size)
if self.cmap and output.shape[1] == 1:
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)


@delegates(JointRecurrencePlot.__init__)
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):
def __init__(self, size=224, cmap=None, **kwargs):
self.size,self.cmap = size,cmap
self.encoder = JointRecurrencePlot(**kwargs)

Expand All @@ -227,7 +227,7 @@ def encodes(self, o: TSTensor):
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:
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)
2 changes: 1 addition & 1 deletion tsai/data/preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ def _prepare_forecasting_data(df, x_vars, y_vars):
X, y = _prepare_forecasting_data(df[x_vars], x_vars=None, y_vars=None)
else:
X, y = _prepare_forecasting_data(df, x_vars=x_vars, y_vars=y_vars)
if y == []:
if not isinstance(y, (torch.Tensor, np.ndarray)):
y = None
return X, y

Expand Down
5 changes: 2 additions & 3 deletions tsai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,7 @@ def assign_in_chunks(a, b, chunksize='auto', inplace=True, verbose=True):
b: may be an integer, float, str, 'rand' (for random data), or another array like object.
chunksize: is the size of chunks. If 'auto' chunks will have around 1GB each.
"""

if b != 'rand' and not isinstance(b, (Iterable, Generator)):
if not (isinstance(b, str) and b == 'rand') and not isinstance(b, (Iterable, Generator)):
a[:] = b
else:
shape = a.shape
Expand All @@ -1014,7 +1013,7 @@ def assign_in_chunks(a, b, chunksize='auto', inplace=True, verbose=True):
for i in progress_bar(range((shape[0] - 1) // chunksize + 1), display=verbose, leave=False):
start, end = i * chunksize, min(shape[0], (i + 1) * chunksize)
if start >= shape[0]: break
if b == 'rand':
if (isinstance(b, str) and b == 'rand'):
a[start:end] = np.random.rand(end - start, *shape[1:])
else:
if is_dask(b):
Expand Down

0 comments on commit a449b0b

Please sign in to comment.