|
15 | 15 | import pathlib |
16 | 16 | import tempfile |
17 | 17 | import urllib |
| 18 | +import warnings |
18 | 19 | import zipfile |
19 | | -from typing import List |
| 20 | +from typing import List, Union |
20 | 21 |
|
| 22 | +import numpy as np |
| 23 | +import numpy.typing as npt |
21 | 24 | import requests |
| 25 | +import torch |
22 | 26 |
|
23 | 27 | import cebra.data |
24 | 28 |
|
25 | 29 |
|
26 | | -def get_loader_options(dataset: cebra.data.Dataset) -> List[str]: |
27 | | - """Return all possible dataloaders for the given dataset.""" |
28 | | - |
29 | | - loader_options = [] |
30 | | - if isinstance(dataset, cebra.data.SingleSessionDataset): |
31 | | - mixed = True |
32 | | - if dataset.continuous_index is not None: |
33 | | - loader_options.append(cebra.data.ContinuousDataLoader) |
34 | | - else: |
35 | | - mixed = False |
36 | | - if dataset.discrete_index is not None: |
37 | | - loader_options.append(cebra.data.DiscreteDataLoader) |
38 | | - else: |
39 | | - mixed = False |
40 | | - if mixed: |
41 | | - loader_options.append(cebra.data.MixedDataLoader) |
42 | | - elif isinstance(dataset, cebra.data.MultiSessionDataset): |
43 | | - mixed = True |
44 | | - if dataset.continuous_index is not None: |
45 | | - loader_options.append(cebra.data.ContinuousMultiSessionDataLoader) |
46 | | - else: |
47 | | - mixed = False |
48 | | - if dataset.discrete_index is not None: |
49 | | - pass # not implemented yet |
50 | | - else: |
51 | | - mixed = False |
52 | | - if mixed: |
53 | | - pass # not implemented yet |
54 | | - else: |
55 | | - raise TypeError(f"Invalid dataset type: {dataset}") |
56 | | - return loader_options |
57 | | - |
58 | | - |
59 | 30 | def download_file_from_url(url: str) -> str: |
60 | 31 | """Download a fole from ``url``. |
61 | 32 |
|
@@ -88,3 +59,53 @@ def download_file_from_zip_url(url, file="montblanc_tracks.h5"): |
88 | 59 | except zipfile.error: |
89 | 60 | pass |
90 | 61 | return pathlib.Path(foldername) / "data" / file |
| 62 | + |
| 63 | + |
| 64 | +def _is_integer(y: Union[npt.NDArray, torch.Tensor]) -> bool: |
| 65 | + """Check if the values in ``y`` are :py:class:`int`. |
| 66 | +
|
| 67 | + Args: |
| 68 | + y: An array, either as a :py:func:`numpy.array` or a :py:class:`torch.Tensor`. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + ``True`` if ``y`` contains :py:class:`int`. |
| 72 | + """ |
| 73 | + return (isinstance(y, np.ndarray) and np.issubdtype(y.dtype, np.integer) |
| 74 | + ) or (isinstance(y, torch.Tensor) and |
| 75 | + (not torch.is_floating_point(y) and not torch.is_complex(y))) |
| 76 | + |
| 77 | + |
| 78 | +def _is_floating(y: Union[npt.NDArray, torch.Tensor]) -> bool: |
| 79 | + """Check if the values in ``y`` are :py:class:`int`. |
| 80 | +
|
| 81 | + Note: |
| 82 | + There is no ``torch`` method to check that the ``dtype`` of a :py:class:`torch.Tensor` |
| 83 | + is a :py:class:`float`, consequently, we check that it is not :py:class:`int` nor |
| 84 | + :py:class:`complex`. |
| 85 | +
|
| 86 | + Args: |
| 87 | + y: An array, either as a :py:func:`numpy.array` or a :py:class:`torch.Tensor`. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + ``True`` if ``y`` contains :py:class:`float`. |
| 91 | + """ |
| 92 | + |
| 93 | + return (isinstance(y, np.ndarray) and |
| 94 | + np.issubdtype(y.dtype, np.floating)) or (isinstance( |
| 95 | + y, torch.Tensor) and torch.is_floating_point(y)) |
| 96 | + |
| 97 | + |
| 98 | +def get_loader_options(dataset: "cebra.data.Dataset") -> List[str]: |
| 99 | + """Return all possible dataloaders for the given dataset. |
| 100 | +
|
| 101 | + Notes: |
| 102 | + This function is deprecated and will be removed in an upcoming version of CEBRA. |
| 103 | + Please use :py:mod:`cebra.data.helper.get_loader_options` instead, which is an |
| 104 | + exact copy. |
| 105 | + """ |
| 106 | + |
| 107 | + import cebra.data.helper |
| 108 | + warnings.warn( |
| 109 | + "The 'get_loader_options' function has been moved to 'cebra.data.helpers' module. " |
| 110 | + "Please update your imports.", DeprecationWarning) |
| 111 | + return cebra.data.helper.get_loader_options |
0 commit comments