question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`IndexError` in `spg.find_meta_gene`

See original GitHub issue

Hello,

I’m following along the tutorial but using my own AnnData and histology image. When calling spg.find_meta_gene as in the tutorial, I get the following error:

Trying to set attribute `.obs` of view, copying.
WARNING: It seems you use rank_genes_groups on the raw count data. Please logarithmize your data before calling rank_genes_groups.
Add gene:  ENSG00000197971
Minus gene:  ENSG00000111341
Absolute mean change: 4.6624002
Number of non-target spots reduced to: 77
===========================================================================
Meta gene is:  ENSG00000131095+ENSG00000197971-ENSG00000111341
===========================================================================
Trying to set attribute `.obs` of view, copying.
WARNING: It seems you use rank_genes_groups on the raw count data. Please logarithmize your data before calling rank_genes_groups.
Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "/jhpce/shared/jhpce/libd/spagcn/1.2.0/spagcn_venv/lib/python3.7/site-packages/SpaGCN/util.py", line 304, in find_meta_gene
    add_g=tmp.uns['rank_genes_groups']["names"][0][1]
  File "/jhpce/shared/jhpce/libd/spagcn/1.2.0/spagcn_venv/lib/python3.7/site-packages/numpy/core/records.py", line 292, in __getitem__
    obj = nt.void.__getitem__(self, indx)
IndexError: invalid index (1)

To be more precise, I ran the following for my AnnData:

#  Read in raw data and subset to current sample (my AnnData has several samples)
raw = sc.read_h5ad(input_adata_path)
raw.var_names_make_unique()
raw = raw[raw.obs.sample_id == sample_name, :]

# adata.obs["pred"] was computed earlier using code provided in your tutorial
raw.obs["pred"] = adata.obs["pred"].astype('category')

# Set names to match tutorial, given names present in my AnnData
raw.obs["x_array"]=raw.obs["array_row"]
raw.obs["y_array"]=raw.obs["array_col"]
raw.obs["x_pixel"]= raw.obs["pxl_col_in_fullres"]
raw.obs["y_pixel"]= raw.obs["pxl_row_in_fullres"]

#Convert sparse matrix to non-sparse
raw.X=(raw.X.A if issparse(raw.X) else raw.X)
raw.raw=raw
sc.pp.log1p(raw)

# In my case 8 spatial domains were found, and my AnnData contains gene IDs
# as Ensembl IDs rather than gene symbols. The start gene is still GFAP, and I've verified
# the gene is present in my AnnData
meta_name, meta_exp=spg.find_meta_gene(input_adata=raw,
                    pred=raw.obs["pred"].tolist(),
                    target_domain=7,
                    start_gene="ENSG00000131095",
                    mean_diff=0,
                    early_stop=True,
                    max_iter=3,
                    use_raw=False)

As the error message suggests, this line triggers the error. I manually ran code from spg.find_meta_gene line-by-line, discovering that the error occurred on i=1 here, and the value of tmp.uns['rank_genes_groups']["names"][0] is ('ENSG00000268674',), which of course has no [1] index, leading to the IndexError I see.

I apologize for not providing a small directly reproducible example, as I couldn’t produce the error with your example data, and I didn’t think it was practical to share my large AnnData file directly. Please let me know how I could provide more useful information.

Thanks in advance for any help.

Best,

-Nick

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jianhuupenncommented, Nov 2, 2021

Yes., that makes sense. You can use the previous round results as the meta gene, that is, “ENSG00000131095+ENSG00000197971-ENSG00000111341”. I’ll modify the function to make it stop when the non-target set is empty.

Thanks!

0reactions
jianhuupenncommented, Nov 2, 2021

Fixed, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix IndexError in Python - Rollbar
The IndexError in Python occurs when an item from a list is attempted to be accessed that is outside the index range of...
Read more >
Does "IndexError: list index out of range" when trying to ...
The IndexError is raised when you attempt to retrieve an index from a sequence, like a list or a tuple , and the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found