# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/63_trch.ipynb.

# %% auto 0
__all__ = ['isdevice', 'istensor', 'istrc', 'istens', 'istorchtype', 'isdtype', 'isttype', 'istrctype', 'iscpu', 'ismps',
           'iscuda', 'iscputensor', 'ismpstensor', 'isdataset', 'isds', 'isdataloader', 'isdl', 'isdatamodule', 'isdm',
           'isdfds', 'isdfdataset', 'isdataframedataset']

# %% ../nbs/63_trch.ipynb 4
from typing import TypeGuard
from typing import Literal

# %% ../nbs/63_trch.ipynb 5
from .atyp import (dtype, device, Tensor, pttype, ptdevice, pttensor)
from .atyp import (Dataset, DataLoader, ptdataset, ptdataloader)
from .atyp import (pldatamodule, DataModule)

from .core import isstr, isnone

# %% ../nbs/63_trch.ipynb 6
from .ispd import isdf

# %% ../nbs/63_trch.ipynb 9
def isdevice(x) -> TypeGuard[ptdevice]:
    '''Check if `x` is a `torch.device`'''
    return not isnone(x) and ((not isstr(x) and hasattr(x, 'type')) or isinstance(x, device))

def istensor(x) -> TypeGuard[pttensor]:
    '''Check if `x` is a `torch.Tensor`
    
    See Also
    --------
    istrc : Alias for `istensor`
    istens : Alias for `istensor`
    '''
    return not isnone(x) and isinstance(x, Tensor) and hasattr(x, 'device')

def istrc(x) -> TypeGuard[ptdevice]:
    '''Check if `x` is a `torch.Tensor` with a `torch.device`.
    
    See Also
    --------
    istensor : Alias for `istrc`
    '''
    return istensor(x)

def istens(x) -> TypeGuard[pttensor]:
    '''Check if `x` is a `torch.Tensor` with a `torch.device`.
    
    See Also
    --------
    istensor : Check if `x` is a `torch.Tensor` with a `torch.device`
    istrc : Alias for `istensor`
    '''
    return istensor(x)



# %% ../nbs/63_trch.ipynb 10
def istorchtype(x) -> TypeGuard[pttype]:
    '''Check if `x` is subclass of `torch.dtype`
    
     See Also
    --------
    isdtype : Alias for `istorchtype`
    isttype : Alias for `istorchtype`
    istrctype : Alias for `istorchtype`
    '''
    return not isnone(x) and isinstance(x, dtype)

def isdtype(x) -> TypeGuard[pttype]:
    '''Check if `x` is subclass of `torch.dtype`
    
     See Also
    --------
    isttype : Alias for `isdtype`
    istrctype : Alias for `isdtype`
    istorchtype : Alias for `isdtype`
    '''
    return istorchtype(x)


def isttype(x) -> TypeGuard[pttype]:
    '''Check if `x` is subclass of `torch.dtype`
    
     See Also
    --------
    isdtype : Alias for `isttype`
    istrctype : Alias for `isttype`
    istorchtype : Alias for `isttype`
    '''
    return istorchtype(x)

def istrctype(x) -> TypeGuard[pttype]:
    '''Check if `x` is subclass of `torch.dtype`
    
     See Also
    --------
    isdtype : Alias for `istrctype`
    isttype : Alias for `istrctype`
    istorchtype : Alias for `istrctype`
    '''
    return istorchtype(x)


# %% ../nbs/63_trch.ipynb 12
def iscpu(s: str) -> TypeGuard[Literal['cpu']]:
    '''Check if `s` is `'cpu'`'''
    return s == 'cpu'

def ismps(s: str) -> TypeGuard[Literal['mps']]:
    '''Check if `s` is `'mps'`'''
    return s == 'mps'

def iscuda(s: str) -> TypeGuard[Literal['cuda']]:
    '''Check if `s` is `'cuda'`'''
    return s == 'cuda'

# %% ../nbs/63_trch.ipynb 14
def iscputensor(x) -> TypeGuard[pttensor]:
    '''Check if `x` is a `torch.Tensor` on cpu'''
    return istensor(x) and iscpu(x.device.type)

def ismpstensor(x) -> TypeGuard[pttensor]:
    '''Check if `x` is a `torch.Tensor` on mps'''
    return istensor(x) and ismps(x.device.type)

def ismpstensor(x) -> TypeGuard[pttensor]:
    '''Check if `x` is a `torch.Tensor` on cuda'''
    return istensor(x) and iscuda(x.device.type)

# %% ../nbs/63_trch.ipynb 16
def isdataset(x) -> TypeGuard[ptdataset]:
    '''Check if `x` is a `torch.utils.data.Dataset`
    
    See Also
    --------
    isds : Alias for `isdataset`    
    '''
    return not isnone(x) and isinstance(x, Dataset)

def isds(x) -> TypeGuard[ptdataset]:
    '''Check if `x` is a `torch.utils.data.Dataset`
    
    See Also
    --------
    isdataset : Alias for `isds`
    '''
    return isdataset(x)

def isdataloader(x) -> TypeGuard[ptdataloader]:
    '''Check if `x` is a `torch.utils.data.DataLoader`
    
    See Also
    --------
    isds : Alias for `isdataloader`    
    '''
    return not isnone(x) and isinstance(x, DataLoader)

def isdl(x) -> TypeGuard[ptdataloader]:
    '''Check if `x` is a `torch.utils.data.DataLoader`
    
    See Also
    --------
    isdataloader : Alias for `isdl`
    '''
    return isdataloader(x)

def isdatamodule(x) -> TypeGuard[pldatamodule]:
    '''Check if `x` is a `pytorch_lightning.LightningDataModule`
    
    See Also
    --------
    isdm : Alias for `isdatamodule`    
    '''
    return not isnone(x) and isinstance(x, DataModule)

def isdm(x) -> TypeGuard[pldatamodule]:
    '''Check if `x` is a `torch.utils.data.DataLoader`
    
    See Also
    --------
    isdatamodule : Alias for `isdm`
    '''
    return isdatamodule(x)


def isdfds(x) -> TypeGuard[ptdataset]:
    '''Check if `x` is a "DataFrame Dataset" i.e. a `torch.utils.data.Dataset` 
    with a `pd.DataFrame` as `df` attribute.

    See Also
    --------
    isdfdataset : Alias for `isdfds`
    isdataframedataset : Alias for `isdfds`
    '''
    return isds(x) and hasattr(x, 'df') and isdf(getattr(x, 'df', None))


def isdfdataset(x) -> TypeGuard[ptdataset]:
    '''Check if `x` is a "DataFrame Dataset" i.e. a `torch.utils.data.Dataset` 
    with a `pd.DataFrame` as `df` attribute.

    See Also
    --------
    isdfds : Alias for `isdfdataset`
    isdataframedataset : Alias for `isdfdataset`
    '''
    return isdfds(x)

def isdataframedataset(x) -> TypeGuard[ptdataset]:
    '''Check if `x` is a "DataFrame Dataset" i.e. a `torch.utils.data.Dataset` 
    with a `pd.DataFrame` as `df` attribute.

    See Also
    --------
    isdfdataset : Alias for `isdataframedataset`
    isdfds : Alias for `isdataframedataset`
    '''
    return isdfds(x)

# %% ../nbs/63_trch.ipynb 19
#| export
