Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: can't set attribute 'categories' #58

Closed
BillyChen123 opened this issue Oct 29, 2023 · 1 comment
Closed

AttributeError: can't set attribute 'categories' #58

BillyChen123 opened this issue Oct 29, 2023 · 1 comment

Comments

@BillyChen123
Copy link

I have not met this issue before. Why can this method have attribute error? Can you help me to fit it?

datasets = sc.read_h5ad(filepath)
datasets.obs['Batch'] = pd.CategoricalIndex(datasets.obs['Batch'])
datasets.obs['Batch2'] = pd.CategoricalIndex(datasets.obs['Batch2'])
datasets.obs['Group'] = pd.CategoricalIndex(datasets.obs['Group'])
sc.pp.normalize_total(datasets,target_sum= 1e4)
sc.pp.log1p(datasets)
sc.pp.highly_variable_genes(datasets)
#sc.pl.highly_variable_genes(datasets)
adata=desc.train(datasets,
        dims=[datasets.shape[1],64,32],
        tol=0.005,
        n_neighbors=10,
        batch_size=256,
        louvain_resolution=[0.8,1.0],# not necessarily a list, you can only set one value, like, louvain_resolution=1.0
        # save_dir=str(save_dir),
        do_tsne=True,
        learning_rate=200, # the parameter of tsne
        use_GPU=False,
        num_Cores=1, #for reproducible, only use 1 cpu
        num_Cores_tsne=4,
        save_encoder_weights=False,
        save_encoder_step=3,# save_encoder_weights is False, this parameter is not used
        use_ae_weights=False,
        do_umap=False) #if do_uamp is False, it will don't compute umap coordiate```

Here is the report!
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb 单元格 2 line 1
      [8](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7) sc.pp.highly_variable_genes(datasets)
      [9](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8) #sc.pl.highly_variable_genes(datasets)
---> [10](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=9) adata=desc.train(datasets,
     [11](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10)         dims=[datasets.shape[1],64,32],
     [12](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=11)         tol=0.005,
     [13](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=12)         n_neighbors=10,
     [14](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=13)         batch_size=256,
     [15](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14)         louvain_resolution=[0.8,1.0],# not necessarily a list, you can only set one value, like, louvain_resolution=1.0
     [16](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=15)         # save_dir=str(save_dir),
     [17](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16)         do_tsne=True,
     [18](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=17)         learning_rate=200, # the parameter of tsne
     [19](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=18)         use_GPU=False,
     [20](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=19)         num_Cores=1, #for reproducible, only use 1 cpu
     [21](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=20)         num_Cores_tsne=4,
     [22](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=21)         save_encoder_weights=False,
     [23](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=22)         save_encoder_step=3,# save_encoder_weights is False, this parameter is not used
     [24](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=23)         use_ae_weights=False,
     [25](vscode-notebook-cell://ssh-remote%2B10.181.6.84/home/chenyz/Matrix_Factorization/rank/Untitled-1.ipynb#W5sdnNjb2RlLXJlbW90ZQ%3D%3D?line=24)         do_umap=False)

File [~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/desc/models/desc.py:303](https://vscode-remote+ssh-002dremote-002b10-002e181-002e6-002e84.vscode-resource.vscode-cdn.net/home/chenyz/Matrix_Factorization/rank/~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/desc/models/desc.py:303), in train(data, dims, alpha, tol, init, louvain_resolution, n_neighbors, pretrain_epochs, batch_size, activation, actincenter, drop_rate_SAE, is_stacked, use_earlyStop, use_ae_weights, save_encoder_weights, save_encoder_step, save_dir, max_iter, epochs_fit, num_Cores, num_Cores_tsne, use_GPU, GPU_id, random_seed, verbose, do_tsne, learning_rate, perplexity, do_umap, kernel_clustering)
    301         print("Start to process resolution=",str(resolution))
    302         use_ae_weights=use_ae_weights if ith==0 else True
--> 303         res=train_single(data=data,
    304             dims=dims,
    305             alpha=alpha,
    306             tol=tol,
    307             init=init,
    308             louvain_resolution=resolution,
    309             n_neighbors=n_neighbors,
    310             pretrain_epochs=pretrain_epochs,
    311             epochs_fit=epochs_fit,
    312             batch_size=batch_size,
    313             activation=activation,
    314             actincenter=actincenter,
    315             drop_rate_SAE=drop_rate_SAE,
    316             is_stacked=is_stacked,
    317             use_earlyStop=use_earlyStop,
    318             use_ae_weights=use_ae_weights,
    319 	    save_encoder_weights=save_encoder_weights,
    320             save_encoder_step=save_encoder_step,
    321             save_dir=save_dir,
    322             max_iter=max_iter,
    323             num_Cores=num_Cores,
    324             num_Cores_tsne=num_Cores_tsne,
    325             use_GPU=use_GPU,
    326             GPU_id=GPU_id,
    327             random_seed=random_seed,
    328             verbose=verbose,
    329 	    do_tsne=do_tsne,
    330 	    learning_rate=learning_rate,
    331 	    perplexity=perplexity,
    332             do_umap=do_umap,
    333             kernel_clustering=kernel_clustering)
    334         #update adata
    335         data=res

File [~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/desc/models/desc.py:152](https://vscode-remote+ssh-002dremote-002b10-002e181-002e6-002e84.vscode-resource.vscode-cdn.net/home/chenyz/Matrix_Factorization/rank/~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/desc/models/desc.py:152), in train_single(data, dims, alpha, tol, init, louvain_resolution, n_neighbors, pretrain_epochs, batch_size, activation, actincenter, drop_rate_SAE, is_stacked, use_earlyStop, use_ae_weights, save_encoder_weights, save_encoder_step, save_dir, max_iter, epochs_fit, num_Cores, num_Cores_tsne, use_GPU, GPU_id, random_seed, verbose, do_tsne, learning_rate, perplexity, do_umap, kernel_clustering)
    129 desc=DescModel(dims=dims,
    130           x=adata.X,
    131           alpha=alpha,
   (...)
    149           kernel_clustering=kernel_clustering
    150 )
    151 desc.compile(optimizer=SGD(0.01,0.9),loss='kld')
--> 152 Embeded_z,q_pred=desc.fit(maxiter=max_iter)
    153 print("The desc has been trained successfully!!!!!!")
    154 if verbose:

File [~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/desc/models/network.py:371](https://vscode-remote+ssh-002dremote-002b10-002e181-002e6-002e84.vscode-resource.vscode-cdn.net/home/chenyz/Matrix_Factorization/rank/~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/desc/models/network.py:371), in DescModel.fit(self, maxiter)
    369 def fit(self,maxiter=1e4):
    370     if isinstance(self.epochs_fit,int):
--> 371         embedded_z,q=self.fit_on_all(maxiter=maxiter,epochs_fit=self.epochs_fit,save_encoder_step=self.save_encoder_step)
    372     else:
    373         import math

File [~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/desc/models/network.py:362](https://vscode-remote+ssh-002dremote-002b10-002e181-002e6-002e84.vscode-resource.vscode-cdn.net/home/chenyz/Matrix_Factorization/rank/~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/desc/models/network.py:362), in DescModel.fit_on_all(self, maxiter, epochs_fit, save_encoder_step)
    357 #load model
    358 #encoder=load_model("encoder.h5")
    359 #
    361 y0=pd.Series(y_pred,dtype='category')
--> 362 y0.cat.categories=range(0,len(y0.cat.categories))
    363 print("The final prediction cluster is:")
    364 x=y0.value_counts()

File [~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/pandas/core/base.py:178](https://vscode-remote+ssh-002dremote-002b10-002e181-002e6-002e84.vscode-resource.vscode-cdn.net/home/chenyz/Matrix_Factorization/rank/~/anaconda3/envs/MOBA_Billy/lib/python3.10/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 [~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/pandas/core/accessor.py:99](https://vscode-remote+ssh-002dremote-002b10-002e181-002e6-002e84.vscode-resource.vscode-cdn.net/home/chenyz/Matrix_Factorization/rank/~/anaconda3/envs/MOBA_Billy/lib/python3.10/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 [~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/pandas/core/arrays/categorical.py:2867](https://vscode-remote+ssh-002dremote-002b10-002e181-002e6-002e84.vscode-resource.vscode-cdn.net/home/chenyz/Matrix_Factorization/rank/~/anaconda3/envs/MOBA_Billy/lib/python3.10/site-packages/pandas/core/arrays/categorical.py:2867), in CategoricalAccessor._delegate_property_set(self, name, new_values)
   2866 def _delegate_property_set(self, name: str, new_values):  # type: ignore[override]
-> 2867     return setattr(self._parent, name, new_values)

AttributeError: can't set attribute 'categories'
@goepp
Copy link

goepp commented Nov 9, 2023

Hi,

I had the same issue while running the desc tutorial, with pandas=2.1.1 and desc=2.1.1.
I found a workaround inspired by theislab/scvelo#811 (comment).

  1. In desc/models/network.py, I replaced line 318:
    y0.cat.categories=range(0,len(y0.cat.categories)) with
temp=y0.cat.categories.astype(str)
y0=y0.cat.set_categories(temp, rename=True)
  1. In desc/models/desc.py I replaced line 159 :
    y_pred.cat.categories=list(range(len(y_pred.unique()))) with
temp=y_pred.cat.categories.astype(str)
y_pred=y_pred.cat.set_categories(temp, rename=True)

Then desc.train worked without problem.

@eleozzr eleozzr closed this as completed Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants