:py:mod:`tonic.cached_dataset`
==============================

.. py:module:: tonic.cached_dataset


Module Contents
---------------

Classes
~~~~~~~

.. autoapisummary::

   tonic.cached_dataset.MemoryCachedDataset
   tonic.cached_dataset.DiskCachedDataset
   tonic.cached_dataset.Aug_DiskCachedDataset
   tonic.cached_dataset.CachedDataset



Functions
~~~~~~~~~

.. autoapisummary::

   tonic.cached_dataset.save_to_disk_cache
   tonic.cached_dataset.load_from_disk_cache



.. py:class:: MemoryCachedDataset


   MemoryCachedDataset caches the samples to memory to substantially improve data loading
   speeds. However you have to keep a close eye on memory consumption while loading your samples,
   which can increase rapidly when converting events to rasters/frames. If your transformed
   dataset doesn't fit into memory, yet you still want to cache samples to speed up training,
   consider using `DiskCachedDataset` instead.

   :param dataset: Dataset to be cached to memory.
   :param device: Device to cache to. This is preferably a torch device. Will cache to CPU memory if None (default).
   :param transform: Transforms to be applied on the data
   :param target_transform: Transforms to be applied on the label/targets
   :param transforms: A callable of transforms that is applied to both data and labels at the same time.

   .. py:attribute:: dataset
      :type: collections.abc.Iterable

      

   .. py:attribute:: device
      :type: str | None

      

   .. py:attribute:: transform
      :type: collections.abc.Callable | None

      

   .. py:attribute:: target_transform
      :type: collections.abc.Callable | None

      

   .. py:attribute:: transforms
      :type: collections.abc.Callable | None

      

   .. py:attribute:: samples_dict
      :type: dict

      

   .. py:method:: __getitem__(index)


   .. py:method:: __len__()



.. py:class:: DiskCachedDataset


   DiskCachedDataset caches the data samples to the hard drive for subsequent reads, thereby
   potentially improving data loading speeds. If dataset is None, then the length of this dataset
   will be inferred from the number of files in the caching folder. Pay attention to the cache
   path you're providing, as DiskCachedDataset will simply check if there is a file present with
   the index that it is looking for. When using train/test splits, it is wise to also take that
   into account in the cache path.

   .. note:: When you change the transform that is applied before caching, DiskCachedDataset cannot know about this and will present you
             with an old file. To avoid this you either have to clear your cache folder manually when needed, incorporate all
             transformation parameters into the cache path which creates a tree of cache files or use reset_cache=True.

   .. note:: Caching Pytorch tensors will write numpy arrays to disk, so be careful when loading the sample and you expect a tensor. The recommendation is to defer the transform to tensor as late as possible.

   :param dataset: Dataset to be cached to disk. Can be None, if only files in cache_path should be used.
   :param cache_path: The preferred path where the cache will be written to and read from.
   :param reset_cache: When True, will clear out the cache path during initialisation. Default is False
   :param transform: Transforms to be applied on the data
   :param target_transform: Transforms to be applied on the label/targets
   :param transforms: A callable of transforms that is applied to both data and labels at the same time.
   :param num_copies: Number of copies of each sample to be cached.
                      This is a useful parameter if the dataset is being augmented with slow, random transforms.
   :param compress: Whether to apply lightweight lzf compression, default is True.

   .. py:attribute:: dataset
      :type: collections.abc.Iterable

      

   .. py:attribute:: cache_path
      :type: str

      

   .. py:attribute:: reset_cache
      :type: bool
      :value: False

      

   .. py:attribute:: transform
      :type: collections.abc.Callable | None

      

   .. py:attribute:: target_transform
      :type: collections.abc.Callable | None

      

   .. py:attribute:: transforms
      :type: collections.abc.Callable | None

      

   .. py:attribute:: num_copies
      :type: int
      :value: 1

      

   .. py:attribute:: compress
      :type: bool
      :value: True

      

   .. py:method:: __post_init__()


   .. py:method:: __getitem__(item) -> tuple[object, object]


   .. py:method:: __len__()



.. py:function:: save_to_disk_cache(data, targets, file_path: str | pathlib.Path, compress: bool = True) -> None

   Save data to caching path on disk in an hdf5 file. Can deal with data
   that is a dictionary.
   :param data: numpy ndarray-like or a list thereof.
   :param targets: same as data, can be None.
   :param file_path: caching file path.
   :param compress: Whether to apply compression. (default = True - uses lzf compression)


.. py:function:: load_from_disk_cache(file_path: str | pathlib.Path) -> tuple

   Load data from file cache, separately for (data) and (targets).

   Can assemble dictionaries back together.
   :param file_path: caching file path.

   :returns: data, targets


.. py:class:: Aug_DiskCachedDataset


   Bases: :py:obj:`DiskCachedDataset`

   Aug_DiskCachedDataset is a child class from DiskCachedDataset with further customizations to
   handle augmented copies of a sample. The goal of this customization is to map the indices of
   cached files (copy) to augmentation parameters. This is useful in a category of augmentations
   where the range of parameter is rather disceret and non probabilistic, for instance an audio
   sample is being augmented with noise and SNR can take only N=5 values. Passing copy_index to
   augmentation Class as an init argument ensures that each copy will be a a distinct augmented
   sample with a trackable parameter.

   'generate_all' method generates all augmented vesions of a sample.
   'generate_copy' method generates the missing variant (augmented version)



   Therefore all transforms applied to the dataset are categorized by the keys:  "pre_aug", "augmentations"
    and "post_aug".

    Args:
        'all_transforms' is a dictionarty passed to this class containing information about all transforms.

   .. py:attribute:: all_transforms
      :type: dict | None

      

   .. py:method:: __post_init__()


   .. py:method:: generate_all(item)


   .. py:method:: generate_copy(item, copy)


   .. py:method:: __getitem__(item) -> tuple[object, object]



.. py:class:: CachedDataset(*args, **kwargs)


   Bases: :py:obj:`DiskCachedDataset`

   Deprecated class that points to DiskCachedDataset for now but will be removed in a future
   release.

   Please use MemoryCachedDataset or DiskCachedDataset in the future.


