emdann / milopy

Python implementation of Milo for differential abundance testing on KNN graph
MIT License
60 stars 7 forks source link

DA_nhoods() .iteritems deprecated #40

Closed james-cranley closed 1 year ago

james-cranley commented 1 year ago

Hi @emdann running milo.DA_nhoods(adata, design="~diagnosis") gave me an error which is due to the lack of .iteritems in pandas 2. It looks like it was deprecated since pandas 1.5.0 (https://pandas.pydata.org/pandas-docs/version/1.5/reference/api/pandas.DataFrame.iteritems.html). See traceback below

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_59903/2774797340.py in ?()
----> 1 milo.DA_nhoods(adata, design="~diagnosis")

/lustre/scratch126/cellgen/team205/jc48/miniconda3/envs/milopy_env/lib/python3.9/site-packages/milopy/core.py in ?(adata, design, model_contrasts, subset_samples, add_intercept)
    240 
    241     # Define model matrix
    242     if not add_intercept or model_contrasts is not None:
    243         design = design + ' + 0'
--> 244     model = stats.model_matrix(object=stats.formula(
    245         design), data=design_df)
    246 
    247     # Fit NB-GLM

/lustre/scratch126/cellgen/team205/jc48/miniconda3/envs/milopy_env/lib/python3.9/site-packages/rpy2/robjects/functions.py in ?(self, *args, **kwargs)
    194             r_k = prm_translate.get(k, None)
    195             if r_k is not None:
    196                 v = kwargs.pop(k)
    197                 kwargs[r_k] = v
--> 198         return (super(SignatureTranslatedFunction, self)
    199                 .__call__(*args, **kwargs))

/lustre/scratch126/cellgen/team205/jc48/miniconda3/envs/milopy_env/lib/python3.9/site-packages/rpy2/robjects/functions.py in ?(self, *args, **kwargs)
    120             # TODO: shouldn't this be handled by the conversion itself ?
    121             if isinstance(v, rinterface.Sexp):
    122                 new_kwargs[k] = v
    123             else:
--> 124                 new_kwargs[k] = conversion.py2rpy(v)
    125         res = super(Function, self).__call__(*new_args, **new_kwargs)
    126         res = conversion.rpy2py(res)
    127         return res

/lustre/scratch126/cellgen/team205/jc48/miniconda3/envs/milopy_env/lib/python3.9/functools.py in ?(*args, **kw)
    884         if not args:
    885             raise TypeError(f'{funcname} requires at least '
    886                             '1 positional argument')
    887 
--> 888         return dispatch(args[0].__class__)(*args, **kw)

/lustre/scratch126/cellgen/team205/jc48/miniconda3/envs/milopy_env/lib/python3.9/site-packages/rpy2/robjects/pandas2ri.py in ?(obj)
     52 @py2rpy.register(PandasDataFrame)
     53 def py2rpy_pandasdataframe(obj):
     54     od = OrderedDict()
---> 55     for name, values in obj.iteritems():
     56         try:
     57             od[name] = conversion.py2rpy(values)
     58         except Exception as e:

/lustre/scratch126/cellgen/team205/jc48/miniconda3/envs/milopy_env/lib/python3.9/site-packages/pandas/core/generic.py in ?(self, name)
   5985             and name not in self._accessors
   5986             and self._info_axis._can_hold_identifiers_and_holds_name(name)
   5987         ):
   5988             return self[name]
-> 5989         return object.__getattribute__(self, name)

AttributeError: 'DataFrame' object has no attribute 'iteritems'

in case others face this issue, what worked for me:

# with milopy-env enviroment active
# uninstall/reinstall older pandas
pip uninstall pandas
pip install pandas==1.3.5

then I got a different error running milo.DA_nhoods(adata, design="~diagnosis") saying the R package statmod was not found. Good old chatGPT suggested running this in the notebook, which worked 😁.

from rpy2.robjects.packages import importr
utils = importr('utils')
utils.install_packages('statmod')

Perhaps this could be avoided by specifying pandas 1.5.0 in the yaml file?

emdann commented 1 year ago

Hi @james-cranley, thanks for the report. This looks more like a problem with rpy2 using a deprecated pandas version. Can I ask you which version of rpy2 you have? I don't want to pin milopy to an old version of pandas since this will cause a lot of issues when trying to install milopy with other packages.

You are right that the statmod R package should be installed independently and I've updated the README with that.

Zethson commented 1 year ago

@emdann think there was another issue with Pandas 2.0 that I fixed in the pertpy version.

emdann commented 1 year ago

Fixed in rpy2 https://github.com/rpy2/rpy2/blob/7cf7bfb86dd768fc0133667e108dcc36bdb992c8/rpy2/robjects/functions.py#L130