Basic functions to read timeseries files like `.arff` and `.ts` files.
from nbdev.showdoc import *
from fastai2.data.external import *
# #export
# from zipfile import ZipFile 

class TSData[source]

TSData(fnames, has_targets=True, fill_missing='NaN')

Class that loads .arff (soon .ts) files and returns a tuple (data.x , self.y)

TSData.from_arff[source]

TSData.from_arff(fnames, has_targets=True, fill_missing='NaN')

read one or serveral arff files and concatenate them, and returns a TSData object

TSData.get_items[source]

TSData.get_items()

return list of tuples. Each tuple corresponds to a timeserie (nump.ndarray) and a label (string)

TSData.n_channels[source]

return timeserie's number of channels. For arff files it is called dimension. In the case of NATOPS_Train.arff, it returns 24

get_ts_items[source]

get_ts_items(fnames)

get_ts_items return list of tuples. Each tuple corresponds to a timeserie (nump.ndarray) and a label (string)

get_ts_items[source]

get_ts_items(fnames)

get_ts_items return list of tuples. Each tuple corresponds to a timeserie (nump.ndarray) and a label (string)

Plot Timeseries

show_timeseries[source]

show_timeseries(ts, ctx=None, title=None, chs=None, leg=True, **kwargs)

Plot a timeseries.

Args:

title : usually the class of the timeseries

ts : timeseries. It should have a shape of (nb_channels, sequence_length)

chs : array representing a list of channels to plot

leg : Display or not a legend
path_data = Config().data
path_data
Path('/home/farid/.fastai/data')

file_extract_at_filename[source]

file_extract_at_filename(fname, dest)

Extract fname to dest/fname.name folder using tarfile or `zipfile

file_extract_at_filename is used by default in unzip_data to decompress the downloaded file in a folder that has the same name as the zip filename.

unzip_data download and decompress the downloaded file in a folder and decompress it in a folder that has the same name as the zip filename

unzip_data[source]

unzip_data(url, fname=None, dest=None, c_key='data', force_download=False)

Download url to fname if dest doesn't exist, and un-compress to dest/fname.name folder .

class URLs_TS[source]

URLs_TS()

Global constants for dataset and model URLs.

dsname =  'NATOPS' #'NATOPS', 'LSST', 'Wine', 'Epilepsy', 'HandMovementDirection'
path = unzip_data(URLs_TS.NATOPS)
path
Path('/home/farid/.fastai/data/NATOPS')
path.ls()
(#54) [Path('/home/farid/.fastai/data/NATOPS/NATOPS.jpg'),Path('/home/farid/.fastai/data/NATOPS/NATOPS.txt'),Path('/home/farid/.fastai/data/NATOPS/NATOPSDimension10_TEST.arff'),Path('/home/farid/.fastai/data/NATOPS/NATOPSDimension10_TRAIN.arff'),Path('/home/farid/.fastai/data/NATOPS/NATOPSDimension11_TEST.arff'),Path('/home/farid/.fastai/data/NATOPS/NATOPSDimension11_TRAIN.arff'),Path('/home/farid/.fastai/data/NATOPS/NATOPSDimension12_TEST.arff'),Path('/home/farid/.fastai/data/NATOPS/NATOPSDimension12_TRAIN.arff'),Path('/home/farid/.fastai/data/NATOPS/NATOPSDimension13_TEST.arff'),Path('/home/farid/.fastai/data/NATOPS/NATOPSDimension13_TRAIN.arff')...]
fname_train = f'{dsname}_TRAIN.arff'
fname_test = f'{dsname}_TEST.arff'
fnames = [path/fname_train, path/fname_test]
fnames
[Path('/home/farid/.fastai/data/NATOPS/NATOPS_TRAIN.arff'),
 Path('/home/farid/.fastai/data/NATOPS/NATOPS_TEST.arff')]
data = TSData.from_arff(fnames)
data
TSData:
 Datasets names (concatenated): ['NATOPS_TRAIN', 'NATOPS_TEST']
 Filenames:                     [Path('/home/farid/.fastai/data/NATOPS/NATOPS_TRAIN.arff'), Path('/home/farid/.fastai/data/NATOPS/NATOPS_TEST.arff')]
 Data shape: (360, 24, 51)
 Targets shape: (360,)
 Nb Samples: 360
 Nb Channels:           24
 Sequence Length: 51
print(data)
TSData:
 Datasets names (concatenated): ['NATOPS_TRAIN', 'NATOPS_TEST']
 Filenames:                     [Path('/home/farid/.fastai/data/NATOPS/NATOPS_TRAIN.arff'), Path('/home/farid/.fastai/data/NATOPS/NATOPS_TEST.arff')]
 Data shape: (360, 24, 51)
 Targets shape: (360,)
 Nb Samples: 360
 Nb Channels:           24
 Sequence Length: 51
data.dsname, data.fnames, data.n_channels, data.sizes, data.x.shape, data.y.shape
(['NATOPS_TRAIN', 'NATOPS_TEST'],
 [Path('/home/farid/.fastai/data/NATOPS/NATOPS_TRAIN.arff'),
  Path('/home/farid/.fastai/data/NATOPS/NATOPS_TEST.arff')],
 24,
 ((360, 24, 51), (360,)),
 (360, 24, 51),
 (360,))
test_eq(data.dsname, ['NATOPS_TRAIN', 'NATOPS_TEST'])
test_eq(data.n_channels, 24)
test_eq(data.sizes, ((360, 24, 51), (360,)))
test_eq(data.x.shape, (360, 24, 51))
test_eq(data.y.shape, (360,))
type(data.get_items()[1][0]), data.get_items()[1][0]
(numpy.ndarray,
 array([[-0.54737 , -0.546334, -0.549748, ..., -0.533726, -0.528338,
         -0.518618],
        [-1.600105, -1.599419, -1.595734, ..., -1.576063, -1.572246,
         -1.565955],
        [-0.809446, -0.80942 , -0.812398, ..., -0.766209, -0.764902,
         -0.765835],
        ...,
        [ 0.618919,  0.648665,  0.618913, ...,  0.455396,  0.457002,
          0.456688],
        [-1.497652, -1.465919, -1.50323 , ..., -1.435609, -1.422537,
         -1.421817],
        [-0.754927, -0.706829, -0.758939, ..., -0.538306, -0.530174,
         -0.529384]], dtype=float32))
type(data.get_y()[1]), data.get_y()[1]
(numpy.str_, '3.0')
test_eq(data.get_y()[1], '3.0')
idx = 4
ts, title = data.get_items()[idx]
show_timeseries(ts, title=title)
# show_timeseries(ts, title=title, chs=range(0,24,3)) 
<matplotlib.axes._subplots.AxesSubplot at 0x7f58fa7e7710>
fname_train = path_data/f'{dsname}/{dsname}_TRAIN.ts'
fname_train
Path('/home/farid/.fastai/data/NATOPS/NATOPS_TRAIN.ts')
train_x_ts, train_y_ts = load_from_tsfile_to_array(fname_train)
train_x_ts.shape, train_y_ts.shape
((180, 24, 51), (180,))
train_x_ts[1].shape
(24, 51)
train_x_ts[10][0][30]
0.445743

get_UCR_univariate_list[source]

get_UCR_univariate_list()

get_UCR_multivariate_list[source]

get_UCR_multivariate_list()