# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/10_attr.ipynb.

# %% auto 0
__all__ = ['getshape', 'getnrow', 'getncol', 'getdf', 'getcols', 'getcolumns', 'iscatacc', 'getcat', 'getcodes', 'getcategories',
           'getidx', 'getindex', 'getloc', 'getiloc', 'geticat', 'getds', 'getdfds', 'getkernel', 'getkdegree',
           'getaffinity', 'getdiffop', 'getdata', 'getgraph', 'getclusters', 'getembedding', 'getcentroids',
           'getdiffpot']

# %% ../nbs/10_attr.ipynb 4
from typing import TypeGuard
from typing import Callable

# %% ../nbs/10_attr.ipynb 5
from itry import PHATE, earray
from nlit import (
    KERNEL, KERNEL_DEGREE, AFFINITY, DIFF_OP,  GRAPH, CLUSTERS, EMBEDDING,
    CENTROIDS, DIFF_POTENTIAL, CENTROIDS, DIFF_OP, AFFINITY, CLUSTERS, GRAPH,
    DATA, SHAPE, INDEX, COLUMNS, DF, LOC, ILOC, ICAT, IDX, COLS, DS, ROWS,
    CAT, CODES, CATEGORY
)

from chck import (
    isdf, isds, isseries, isdfds, isint, istup, iscatidx, isidxlike, 
    isdelegate, iscataccessor, anyguard, isidx
)
from hasr import has1, has2
from hasr import hascat, hascodes, hasastype, hascategories

# %% ../nbs/10_attr.ipynb 6
from .cons import empty
from .atyp import DataType, GraphType, Shaped
from .grds import isdata, isgraph, isshaped
from .core import make, getr, onto

# %% ../nbs/10_attr.ipynb 9
@make(SHAPE, guard=istup, otype=isshaped, default=tuple(), retself=True)
def getshape(x, default = tuple()) -> tuple | None: ...

# %% ../nbs/10_attr.ipynb 11
@onto(getshape, guard=isint, retself=True, default=0, otype=has1)
def getnrow(shape: tuple, default = 0) -> tuple | None: 
    print(shape)
    if len(shape) > 0 and isint(shape[0]): return shape[0]
    try: return shape[0]
    except: return default

@onto(getshape, guard=isint, retself=True, default=0, otype=has2)
def getncol(shape: tuple, default = 0) -> tuple | None:    
    if len(shape) > 1 and isint(shape[1]): return shape[1]
    try: return shape[1]
    except: return default

# %% ../nbs/10_attr.ipynb 14
@make(DF, guard=isdf, retself=True)
def getdf(x, default = None) -> DataType | None: ...

@make(COLS, otype=isshaped)
def getcols(x) -> DataType | None: ...

@make(COLUMNS, otype=isshaped)
def getcolumns(x) -> DataType | None: ...

# %% ../nbs/10_attr.ipynb 15
def iscatacc(x) -> bool:
    return anyguard(x, iscataccessor, isseries, isidxlike, isdelegate)

@make(CAT, otype=iscatacc, retself=True)
def getcat(x) -> DataType | None: ...

# %% ../nbs/10_attr.ipynb 16
@onto(getcat, guard=iscatacc, retself=True)
def getcodes(series: tuple, default = 0) -> tuple | None:
    if isseries(series) and hasastype(series): series = series.astype(CATEGORY)
    if hascat(series) and hascodes(series.cat): return series.cat.codes
    if iscatidx(series) and hascodes(series): return series.codes
    if iscataccessor(series) and hascodes(series): return series.codes
    return default

# %% ../nbs/10_attr.ipynb 17
@onto(getcat, guard=isidx, retself=True)
def getcategories(series: tuple, default = 0) -> tuple | None:
    if isseries(series) and hasastype(series): series = series.astype(CATEGORY)
    if hascat(series) and hascategories(series.cat): return series.cat.categories
    if iscatidx(series) and hascategories(series): return series.categories
    if iscataccessor(series) and hascategories(series): return series.codes
    return default

# %% ../nbs/10_attr.ipynb 19
@make(IDX)
def getidx(x, default = None) -> DataType | None: ...

@make(INDEX)
def getindex(x, default = None) -> DataType | None: ...

# %% ../nbs/10_attr.ipynb 20
@make(LOC, otype=isdf)
def getloc(x, default = None) -> DataType | None: ...

@make(ILOC, otype=isdf)
def getiloc(x, default = None) -> DataType | None: ...

@make(ICAT, otype=isdf)
def geticat(x, default = None) -> DataType | None: ...

# %% ../nbs/10_attr.ipynb 22
@make(DS, guard=isds, retself=True)
def getds(x, default = None) -> DataType | None: ...

# %% ../nbs/10_attr.ipynb 23
@make(DF, guard=isdf, otype=isdfds, retself=True) 
def getdfds(x, default = None) -> DataType | None: ...

# %% ../nbs/10_attr.ipynb 25
@make(KERNEL, guard=isdata)
def getkernel(x, default = None) -> DataType | None: ...

@make(KERNEL_DEGREE, guard=isdata)
def getkdegree(x, default =  None) -> DataType | None: ...

@make(AFFINITY, guard=isdata)
def getaffinity(x, default =  None) -> DataType | None: ...

@make(DIFF_OP, guard=isdata)
def getdiffop(x, default =  None) -> DataType | None: ...

@make(DATA, guard=isdata, default=earray)
def getdata(x, default =  None) -> DataType | None: ...

@make(GRAPH, guard=isgraph)
def getgraph(p: PHATE, default =  None) -> DataType | None: ...

@make(CLUSTERS, guard=isdata)
def getclusters(x, default =  None) -> DataType | None: ...

@make(EMBEDDING, guard=isdata)
def getembedding(x, default =  None) -> DataType | None: ...

@make(CENTROIDS,guard=isdata)
def getcentroids(x, default =  None) -> DataType | None: ...

@make(DIFF_POTENTIAL, guard=isdata)
def getdiffpot(p: PHATE, default =  None) -> DataType | None: ...
