# 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
        else:
            opt = np.argmax

        min_dpt_cell[opt(diffmap_min_dpt)] += 1

    # root cell is cell with max vote
    return min_dpt[np.argmax(min_dpt_cell)], adata_post_ti


def trajectory_conservation(
        adata_pre,
        adata_post,
        label_key,
        pseudotime_key="dpt_pseudotime",
        batch_key=None
):
    """
    :param adata_pre: unintegrated adata
    :param adata_post: integrated adata
    :param label_key: column in `adata_pre.obs` of the groups used to precompute the trajectory
    :param pseudotime_key: column in `adata_pre.obs` in which the pseudotime is saved in.
        Column can contain empty entries, the dataset will be subset to the cells with scores.
    :param batch_key: set to batch key if if you want to compute the trajectory metric by batch
    """
    # subset to cells for which pseudotime has been computed
    cell_subset = adata_pre.obs.index[adata_pre.obs[pseudotime_key].notnull()]
    adata_pre_ti = adata_pre[cell_subset]
    adata_post_ti = adata_post[cell_subset]
    try:
        iroot, adata_post_ti2 = get_root(adata_pre_ti, adata_post_ti, label_key, pseudotime_key)

# 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

        else:
            opt = np.argmax

        min_dpt_cell[opt(diffmap_min_dpt)] += 1

    # root cell is cell with max vote
    return min_dpt[np.argmax(min_dpt_cell)], adata_post_ti


def trajectory_conservation(
        adata_pre,
        adata_post,
        label_key,
        pseudotime_key="dpt_pseudotime",
        batch_key=None
):
    """
    :param adata_pre: unintegrated adata
    :param adata_post: integrated adata
    :param label_key: column in `adata_pre.obs` of the groups used to precompute the trajectory
    :param pseudotime_key: column in `adata_pre.obs` in which the pseudotime is saved in.
        Column can contain empty entries, the dataset will be subset to the cells with scores.
    :param batch_key: set to batch key if if you want to compute the trajectory metric by batch
    """
    # subset to cells for which pseudotime has been computed
    cell_subset = adata_pre.obs.index[adata_pre.obs[pseudotime_key].notnull()]
    adata_pre_ti = adata_pre[cell_subset]
    adata_post_ti = adata_post[cell_subset]
    try:
        iroot, adata_post_ti2 = get_root(adata_pre_ti, adata_post_ti, label_key, pseudotime_key)

    except RootCellError: