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

KeyError: 'base' when using bc.tl.dge.get_de() or bc.st.additional_labeling() #271

Open
llumdi opened this issue Dec 14, 2022 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@llumdi
Copy link
Collaborator

llumdi commented Dec 14, 2022

By using besca25 I get the following error in the celltype annotation notebook:

DEgenes = bc.tl.dge.get_de(adata, clusters, demethod="wilcoxon", topnr=5000, logfc=1, padj=0.05)

 ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In [17], line 1
----> 1 DEgenes = bc.tl.dge.get_de(adata, clusters, demethod="wilcoxon", topnr=5000, logfc=1, padj=0.05)

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/besca/tl/dge/_dge.py:698, in get_de(adata, mygroup, demethod, topnr, logfc, padj)
    696 mygroups = list(sort(list(set(adata.obs[mygroup]))))
    697 delist = {}
--> 698 rank_genes_groups(
    699     adata,
    700     groupby=mygroup,
    701     use_raw=True,
    702     n_genes=adata.raw.X.shape[1],
    703     method=demethod,
    704 )
    705 for i in mygroups:
    706     df = DataFrame(adata.uns["rank_genes_groups"]["names"]).head(topnr)[i]

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/scanpy/tools/_rank_genes_groups.py:590, in rank_genes_groups(adata, groupby, use_raw, groups, reference, n_genes, rankby_abs, pts, key_added, copy, method, corr_method, tie_correct, layer, **kwds)
    580 adata.uns[key_added] = {}
    581 adata.uns[key_added]['params'] = dict(
    582     groupby=groupby,
    583     reference=reference,
   (...)
    587     corr_method=corr_method,
    588 )
--> 590 test_obj = _RankGenes(adata, groups_order, groupby, reference, use_raw, layer, pts)
    592 if check_nonnegative_integers(test_obj.X) and method != 'logreg':
    593     logg.warning(
    594         "It seems you use rank_genes_groups on the raw count data. "
    595         "Please logarithmize your data before calling rank_genes_groups."
    596     )

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/scanpy/tools/_rank_genes_groups.py:93, in _RankGenes.__init__(self, adata, groups, groupby, reference, use_raw, layer, comp_pts)
     82 def __init__(
     83     self,
     84     adata,
   (...)
     90     comp_pts=False,
     91 ):
---> 93     if 'log1p' in adata.uns_keys() and adata.uns['log1p']['base'] is not None:
     94         self.expm1_func = lambda x: np.expm1(x * np.log(adata.uns['log1p']['base']))
     95     else:

KeyError: 'base'

This is a blocking error for my analysis. I hope you can help.
Best,
Llucia

@llumdi llumdi added the bug Something isn't working label Dec 14, 2022
@llumdi llumdi changed the title Error when using bc.tl.dge.get_de() KeyError: 'base' when using bc.tl.dge.get_de() or bc.st.additional_labeling() Dec 14, 2022
@llumdi
Copy link
Collaborator Author

llumdi commented Dec 14, 2022

Same error in function bc.st.additional_labeling()

adata = bc.st.additional_labeling(adata, "celltype1", "celltype1", "Major cell types, short names", annot_author, results_folder)

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In [93], line 2
      1 ### Save labelling celltype1
----> 2 adata = bc.st.additional_labeling(adata, "celltype0", "celltype0", "Broad annotation, short names", annot_author, results_folder)
      3 adata = bc.st.additional_labeling(adata, "celltype1", "celltype1", "Major cell types, short names", annot_author, results_folder)
      4 adata = bc.st.additional_labeling(adata, "celltype2", "celltype2", "Intermediate cell types, short names", annot_author, results_folder)

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/besca/st/_wrapper_funcs.py:706, in additional_labeling(adata, labeling_to_use, labeling_name, labeling_description, labeling_author, results_folder, cluster_method, is_celltype_labeling, filename)
    703 if len(set(adata.obs.get(labeling_to_use))) != 1:
    704     start1 = time()
--> 706     rank_genes_groups(
    707         adata,
    708         labeling_to_use,
    709         method="wilcoxon",
    710         use_raw=True,
    711         n_genes=adata.raw.X.shape[1],
    712     )
    713     print("rank genes per label calculated using method wilcoxon.")
    714     logging.info(
    715         "Marker gene detection performed on a per-label basis using the method wilcoxon."
    716         + "\n\tTime for marker gene detection: "
    717         + str(round(time() - start1, 3))
    718         + "s"
    719     )

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/scanpy/tools/_rank_genes_groups.py:590, in rank_genes_groups(adata, groupby, use_raw, groups, reference, n_genes, rankby_abs, pts, key_added, copy, method, corr_method, tie_correct, layer, **kwds)
    580 adata.uns[key_added] = {}
    581 adata.uns[key_added]['params'] = dict(
    582     groupby=groupby,
    583     reference=reference,
   (...)
    587     corr_method=corr_method,
    588 )
--> 590 test_obj = _RankGenes(adata, groups_order, groupby, reference, use_raw, layer, pts)
    592 if check_nonnegative_integers(test_obj.X) and method != 'logreg':
    593     logg.warning(
    594         "It seems you use rank_genes_groups on the raw count data. "
    595         "Please logarithmize your data before calling rank_genes_groups."
    596     )

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/scanpy/tools/_rank_genes_groups.py:93, in _RankGenes.__init__(self, adata, groups, groupby, reference, use_raw, layer, comp_pts)
     82 def __init__(
     83     self,
     84     adata,
   (...)
     90     comp_pts=False,
     91 ):
---> 93     if 'log1p' in adata.uns_keys() and adata.uns['log1p']['base'] is not None:
     94         self.expm1_func = lambda x: np.expm1(x * np.log(adata.uns['log1p']['base']))
     95     else:

KeyError: 'base'

@kohleman kohleman self-assigned this Dec 15, 2022
@kohleman
Copy link
Collaborator

kohleman commented Dec 15, 2022

Looks like this scanpy bug: scverse/scanpy#2239
The workaround as described in the above issue would be:

adata.uns['log1p']["base"] = None

Every time after reading a h5ad file.

@llumdi
Copy link
Collaborator Author

llumdi commented Dec 15, 2022

Thanks, it works now. Would be good to add it in the celltype notebook until is fixed

kohleman added a commit that referenced this issue Feb 7, 2023
Small fix in cell annotation workbook as per #271
idavydov pushed a commit to idavydov/besca that referenced this issue Nov 9, 2023
idavydov pushed a commit to idavydov/besca that referenced this issue Nov 9, 2023
Small fix in cell annotation workbook as per bedapub#271
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants