# Path: scib/utils.py
# def check_batch(batch, obs, verbose=False):
#     if batch not in obs:
#         raise ValueError(f'column {batch} is not in obs')
#     elif verbose:
#         print(f'Object contains {obs[batch].nunique()} batches.')
# 
# Path: scib/metrics/utils.py
# class RootCellError(Exception):
#     def __init__(self, message):
#         self.message = message


import numpy as np
import pandas as pd
import scanpy as sc
from scipy.sparse.csgraph import connected_components
from ..utils import check_batch
from .utils import RootCellError
        Column can contain empty entries, the dataset will be subset to the cells with scores.
    :param dpt_dim: number of diffmap dimensions used to determine root
    """
    n_components, adata_post.obs['neighborhood'] = connected_components(
        csgraph=adata_post.obsp['connectivities'],
        directed=False,
        return_labels=True
    )

    start_clust = adata_pre.obs.groupby([ct_key]).mean()[pseudotime_key].idxmin()
    min_dpt = adata_pre.obs[adata_pre.obs[ct_key] == start_clust].index
    which_max_neigh = adata_post.obs['neighborhood'] == adata_post.obs['neighborhood'].value_counts().idxmax()
    min_dpt = [value for value in min_dpt if value in adata_post.obs[which_max_neigh].index]

    adata_post_ti = adata_post[which_max_neigh]

    min_dpt = [adata_post_ti.obs_names.get_loc(i) for i in min_dpt]

    # compute Diffmap for adata_post
    sc.tl.diffmap(adata_post_ti)

    # determine most extreme cell in adata_post Diffmap
    min_dpt_cell = np.zeros(len(min_dpt))
    for dim in np.arange(dpt_dim):

        diffmap_mean = adata_post_ti.obsm["X_diffmap"][:, dim].mean()
        diffmap_min_dpt = adata_post_ti.obsm["X_diffmap"][min_dpt, dim]

        # count opt cell
        if len(diffmap_min_dpt) == 0:

# Path: scib/utils.py
# def check_batch(batch, obs, verbose=False):
#     if batch not in obs:
#         raise ValueError(f'column {batch} is not in obs')
#     elif verbose:
#         print(f'Object contains {obs[batch].nunique()} batches.')
# 
# Path: scib/metrics/utils.py
# class RootCellError(Exception):
#     def __init__(self, message):
#         self.message = message


# Path: scib/metrics/trajectory.py
import numpy as np
import pandas as pd
import scanpy as sc
from scipy.sparse.csgraph import connected_components
from ..utils import check_batch
from .utils import RootCellError

        Column can contain empty entries, the dataset will be subset to the cells with scores.
    :param dpt_dim: number of diffmap dimensions used to determine root
    """
    n_components, adata_post.obs['neighborhood'] = connected_components(
        csgraph=adata_post.obsp['connectivities'],
        directed=False,
        return_labels=True
    )

    start_clust = adata_pre.obs.groupby([ct_key]).mean()[pseudotime_key].idxmin()
    min_dpt = adata_pre.obs[adata_pre.obs[ct_key] == start_clust].index
    which_max_neigh = adata_post.obs['neighborhood'] == adata_post.obs['neighborhood'].value_counts().idxmax()
    min_dpt = [value for value in min_dpt if value in adata_post.obs[which_max_neigh].index]

    adata_post_ti = adata_post[which_max_neigh]

    min_dpt = [adata_post_ti.obs_names.get_loc(i) for i in min_dpt]

    # compute Diffmap for adata_post
    sc.tl.diffmap(adata_post_ti)

    # determine most extreme cell in adata_post Diffmap
    min_dpt_cell = np.zeros(len(min_dpt))
    for dim in np.arange(dpt_dim):

        diffmap_mean = adata_post_ti.obsm["X_diffmap"][:, dim].mean()
        diffmap_min_dpt = adata_post_ti.obsm["X_diffmap"][min_dpt, dim]

        # count opt cell
        if len(diffmap_min_dpt) == 0:

            raise RootCellError('No root cell in largest component')