NumpyAsType#

class tonic.transforms.NumpyAsType(dtype: dtype)[source]#

Change dtype of numpy ndarray to custom dtype. This transform is necessary for example if you want to load raw events using a PyTorch dataloader. The original events coming from any dataset in Tonic are structured numpy arrays, so that they can be indexed as events[“t”] or events[“p”] etc. Pytorch’s dataloader however does not support the conversion from structured numpy arrays to Tensors, that’s why we need to employ at least NumpyAsType(int) to convert the structured array into an unstructured one before handing it to the dataloader.

Parameters:

dtype (dtype) – data type that the array should be cast to.

Example

>>> # indexing the dataset directly provides structured numpy arrays
>>> dataset = tonic.datasets.NMNIST(save_to='data')
>>> events, targets = dataset[100]
>>>
>>> # this doesn't work
>>> dataloader = torch.utils.data.DataLoader(dataset)
>>> events, targets = next(iter(dataloader))
>>>
>>> # we need to convert to unstructured arrays
>>> transform = tonic.transforms.NumpyAsType(int)
>>> dataset = tonic.datasets.NMNIST(save_to='data', transform=transform)
>>> dataloader = torch.utils.data.DataLoader(dataset)
>>> events, targets = next(iter(dataloader))