theislab / scib

Benchmarking analysis of data integration tools
MIT License
283 stars 62 forks source link

scib import failure with scanpy 1.10.0 #401

Closed mmdanziger closed 4 months ago

mmdanziger commented 4 months ago

scib is not compatible with scanpy 1.10.0 because the symbol deprecated_arg_names has been removed from scanpy (in PR https://github.com/scverse/scanpy/pull/2702 )

This leads to build failures such as

    import scib
../../../virtualenv/python3.10.13/lib/python3.10/site-packages/scib/__init__.py:8: in <module>
    from . import integration, metrics, preprocessing, utils
../../../virtualenv/python3.10.13/lib/python3.10/site-packages/scib/metrics/__init__.py:2: in <module>
    from .ari import ari
../../../virtualenv/python3.10.13/lib/python3.10/site-packages/scib/metrics/ari.py:4: in <module>
    from scanpy._utils import deprecated_arg_names
E   ImportError: cannot import name 'deprecated_arg_names' from 'scanpy._utils' (/home/travis/virtualenv/python3.10.13/lib/python3.10/site-packages/scanpy/_utils/__init__.py)

The latest scanpy release 1.10.0 no longer uses the function https://github.com/scverse/scanpy/blob/6da39f128ecf78cf572f453ee2865d1b901715f3/scanpy/_utils/__init__.py#L89

def deprecated_arg_names(arg_mapping: Mapping[str, str]):
    """
    Decorator which marks a functions keyword arguments as deprecated. It will
    result in a warning being emitted when the deprecated keyword argument is
    used, and the function being called with the new argument.

    Parameters
    ----------
    arg_mapping
        Mapping from deprecated argument name to current argument name.
    """

    def decorator(func):
        @wraps(func)
        def func_wrapper(*args, **kwargs):
            warnings.simplefilter('always', DeprecationWarning)  # turn off filter
            for old, new in arg_mapping.items():
                if old in kwargs:
                    warnings.warn(
                        f"Keyword argument '{old}' has been "
                        f"deprecated in favour of '{new}'. "
                        f"'{old}' will be removed in a future version.",
                        category=DeprecationWarning,
                        stacklevel=2,
                    )
                    val = kwargs.pop(old)
                    kwargs[new] = val
            # reset filter
            warnings.simplefilter('default', DeprecationWarning)
            return func(*args, **kwargs)

        return func_wrapper

    return decorator

It has been replaced with https://github.com/scverse/scanpy/blob/214e05bdc54df61c520dc563ab39b7780e6d3358/scanpy/_utils/__init__.py#L130C1-L157C21 which has a slightly different API:

def renamed_arg(old_name, new_name, *, pos_0: bool = False):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            if old_name in kwargs:
                f_name = func.__name__
                pos_str = (
                    (
                        f" at first position. Call it as `{f_name}(val, ...)` "
                        f"instead of `{f_name}({old_name}=val, ...)`"
                    )
                    if pos_0
                    else ""
                )
                msg = (
                    f"In function `{f_name}`, argument `{old_name}` "
                    f"was renamed to `{new_name}`{pos_str}."
                )
                warnings.warn(msg, FutureWarning, stacklevel=3)
                if pos_0:
                    args = (kwargs.pop(old_name), *args)
                else:
                    kwargs[new_name] = kwargs.pop(old_name)
            return func(*args, **kwargs)

        return wrapper

    return decorator

This can be easily fixed by just porting the old code into scib

I can open a PR if there is interest.

KaiWaldrant commented 4 months ago

This is blocking our PR to update scvi tools which has been updated to have better performance. It would be great if this can be fixed. openproblems-bio/openproblems-v2#416

mumichae commented 3 months ago

Sorry, this was on my TODO list for a while. The import issue should be now fixed with version 1.1.5. Please let me know if there are still any remaining issues.