SCCAF / sccaf

Single-Cell Clustering Assessment Framework
MIT License
85 stars 18 forks source link

Pandas cannot sent attribute #50

Open zsfrbkv opened 10 months ago

zsfrbkv commented 10 months ago

Hi!

I was trying to use your package on a pre-clustered anndata object as described in your manual.

from SCCAF import *

sc.tl.leiden(adata_subset, resolution=.5, key_added='L2_Round0')
SCCAF_optimize_all(ad=adata_subset, plot=False, min_acc=0.9, prefix = 'L2', use='pca')

and I get the following output with an error:

R1norm_cutoff: 0.500000
R2norm_cutoff: 0.050000
Accuracy: 0.000000
======================
Round1 ...
Mean CV accuracy: 0.9070
Accuracy on the training set: 0.9648
Accuracy on the hold-out set: 0.8084
Accuracy on the training set: 0.9820
Accuracy on the hold-out set: 0.8063
Accuracy on the training set: 0.9695
Accuracy on the hold-out set: 0.8161
Max R1mat: 0.772727
Max R2mat: 0.021877
min_acc: 0.806326
IGRAPH U-W- 14 4 --
+ attr: weight (e)
+ edges:
0--8 0--9 0--8 0--9
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[102], line 4
      1 import SCCAF
      3 sc.tl.leiden(adata_subset, resolution=.5, key_added='L2_Round0')
----> 4 SCCAF_optimize_all(ad=adata_subset, plot=False, min_acc=0.9, prefix = 'L2', use='pca')

File /zaira/miniconda3/envs/xenium/lib/python3.9/site-packages/SCCAF/__init__.py:648, in SCCAF_optimize_all(ad, min_acc, R1norm_cutoff, R2norm_cutoff, R1norm_step, R2norm_step, prefix, min_i, start, start_iter, *args, **kwargs)
    646 print("Accuracy: %f" % acc)
    647 print("======================")
--> 648 ad, m1, m2, acc, start_iter = SCCAF_optimize(ad=ad,
    649                                              R1norm_cutoff=R1norm_cutoff,
    650                                              R2norm_cutoff=R2norm_cutoff,
    651                                              start_iter=start_iter,
    652                                              min_acc=min_acc, 
    653                                              prefix=prefix,
    654                                              *args, **kwargs)
    655 print("m1: %f" % m1)
    656 print("m2: %f" % m2)

File /zaira/miniconda3/envs/xenium/lib/python3.9/site-packages/SCCAF/__init__.py:872, in SCCAF_optimize(ad, prefix, use, use_projection, R1norm_only, R2norm_only, dist_only, dist_not, plot, basis, plot_dist, plot_cmat, mod, low_res, c_iter, n_iter, n_jobs, start_iter, sparsity, n, fraction, R1norm_cutoff, R2norm_cutoff, dist_cutoff, classifier, mplotlib_backend, min_acc)
    869     print("Converge SCCAF_optimize no. cluster!")
    870     break
--> 872 merge_cluster(ad, old_id1, new_id, groups)
    874 if plot:
    875     sc.pl.scatter(ad, basis=basis, color=[new_id], color_map="RdYlBu_r", legend_loc='on data',
    876                   show=(mplotlib_backend is None))

File /zaira/miniconda3/envs/xenium/lib/python3.9/site-packages/SCCAF/__init__.py:553, in merge_cluster(ad, old_id, new_id, groups)
    551 ad.obs[new_id] = ad.obs[old_id]
    552 ad.obs[new_id] = ad.obs[new_id].astype('category')
--> 553 ad.obs[new_id].cat.categories = make_unique(groups.astype(str))
    554 ad.obs[new_id] = ad.obs[new_id].str.split('_').str[0]
    555 return ad

File /zaira/miniconda3/envs/xenium/lib/python3.9/site-packages/pandas/core/base.py:178, in NoNewAttributesMixin.__setattr__(self, key, value)
    172 if getattr(self, "__frozen", False) and not (
    173     key == "_cache"
    174     or key in type(self).__dict__
    175     or getattr(self, key, None) is not None
    176 ):
    177     raise AttributeError(f"You cannot add any new attribute '{key}'")
--> 178 object.__setattr__(self, key, value)

File /zaira/miniconda3/envs/xenium/lib/python3.9/site-packages/pandas/core/accessor.py:99, in PandasDelegate._add_delegate_accessors.<locals>._create_delegator_property.<locals>._setter(self, new_values)
     98 def _setter(self, new_values):
---> 99     return self._delegate_property_set(name, new_values)

File /zaira/miniconda3/envs/xenium/lib/python3.9/site-packages/pandas/core/arrays/categorical.py:2460, in CategoricalAccessor._delegate_property_set(self, name, new_values)
   2459 def _delegate_property_set(self, name, new_values):
-> 2460     return setattr(self._parent, name, new_values)

AttributeError: can't set attribute

Thank you for your help!

dicklim commented 1 month ago

Seems that something wrong with the version.

I changed the code in init.py line 553

ef merge_cluster(ad, old_id, new_id, groups):
    ad.obs[new_id] = ad.obs[old_id]
    ad.obs[new_id] = ad.obs[new_id].astype('category')
    print(ad.obs[new_id],make_unique(groups.astype(str)))

    ad.obs[new_id] = ad.obs[new_id].cat.rename_categories(make_unique(groups.astype(str))) # **my change**
    print(ad.obs[new_id],make_unique(groups.astype(str)))

    # ad.obs[new_id].cat.categories = make_unique(groups.astype(str)) # **the original code**
    ad.obs[new_id] = ad.obs[new_id].str.split('_').str[0]
    return ad

I am not sure about if it's true, but it works.

P.S There result is not good. From my aspect.