Attribute Error: 'int' object has no attribute 'item' when running emb_mode="cell_and_gene" in InSilicoPerturber

#211
by junguyen - opened

Thank you for the quick responses thus far!

I am running InSilicoPerturber on the cell_type_train_data.dataset and would like to observe changes in gene embeddings after perturbation of a single gene. I've had previous success running InSilicoPerturber with the same parameters, but only changing emb_mode="cell". However, when running emb_mode="cell_and_gene", it raises the AttributeError below. I also have the latest version of Geneformer pulled. Could I get help on this error? Thanks!

isp = InSilicoPerturber(perturb_type="delete",
                        perturb_rank_shift=None,
                        genes_to_perturb=["ENSG00000021826"],
                        combos=0,
                        anchor_gene=None,
                        model_type="Pretrained",
                        num_classes=0,
                        emb_mode="cell_and_gene",
                        cell_emb_style="mean_pool",
                        filter_data={'organ_major':['liver']},
                        cell_states_to_model=None,
                        max_ncells=50,
                        emb_layer=-1,
                        forward_batch_size=50,
                        nproc=16,
                        token_dictionary_file = "/home/ubuntu/Geneformer/geneformer/token_dictionary.pkl")

isp.perturb_data("/home/ubuntu/Geneformer/",
                 "/data/cell/",
                 "/data/cell/delete_cell_and_gene/liver/",
                 "delete_cell_and_gene_CPS1")
Filter (num_proc=16): 100%|β–ˆ| 28376/28376 [00:13<00:00, 20
Map (num_proc=16): 100%|β–ˆ| 50/50 [00:13<00:00,  3.80 examp
Map (num_proc=16): 100%|β–ˆ| 50/50 [00:00<00:00, 217.76 exam
Map (num_proc=16): 100%|β–ˆ| 50/50 [00:00<00:00, 206.18 exam
Map (num_proc=16): 100%|β–ˆ| 50/50 [00:00<00:00, 197.06 exam
Traceback (most recent call last):                        
  File "<stdin>", line 1, in <module>                     
  File "/home/ubuntu/Geneformer/geneformer/in_silico_pertu
rber.py", line 975, in perturb_data                       
    self.in_silico_perturb(model,                         
  File "/home/ubuntu/Geneformer/geneformer/in_silico_pertu
rber.py", line 1091, in in_silico_perturb                 
    affected_gene = gene_list[k].item()                   
AttributeError: 'int' object has no attribute 'item'      

I believe I've managed to fix this issue by changing the following, starting at line 1094:

if self.emb_mode == "cell_and_gene":
    for k in range(nonpadding_cos_sims_data.shape[1]):
        cos_sim_value = nonpadding_cos_sims_data[k]
        gene_list_1 = np.array(gene_list)
        gene_list_1 = tuple(gene_list_1)
        affected_gene = gene_list_1[k].item()
        cos_sims_dict[(perturbed_genes, affected_gene)] += [cos_sim_value.item()]

This allowed me to run the functions without errors, but I was not able to get gene embeddings like I had hoped. New discussion posted on Discussion #256.

Thank you for your interest in Geneformer and for your patience! We pushed an update that should resolve this issue. If you continue to face errors after pulling the updated code, please let us know by either reopening this discussion if it's the same error or opening a new discussion if it's a new error. Thank you!

ctheodoris changed discussion status to closed

Sign up or log in to comment