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.

'Variable type field must be a TensorType.' error when running cell2location

See original GitHub issue

Hi,

I’ve followed the tutorial, however I’m stack during the last part of 2/3. When I run the code above, I get an error.

os.environ["THEANO_FLAGS"] = 'device=cpu,floatX=float32,openmp=True,force_device=True'
r = cell2location.run_cell2location(

      # Single cell reference signatures as pd.DataFrame
      # (could also be data as anndata object for estimating signatures
      #  as cluster average expression - `sc_data=adata_snrna_raw`)
      sc_data=inf_aver,
      # Spatial data as anndata object
      sp_data=adata_vis,

      # the column in sc_data.obs that gives cluster idenitity of each cell
      summ_sc_data_args={'cluster_col': "TaxonomyRank4",
                         # select marker genes of cell types by specificity of their expression signatures
                         'selection': "cluster_specificity",
                         # specificity cutoff (1 = max, 0 = min)
                         'selection_specificity': 0.5
                        },

      train_args={'use_raw': True, # By default uses raw slots in both of the input datasets.
                  'n_iter': 40000, # Increase the number of iterations if needed (see QC below)

                  # Whe analysing the data that contains multiple experiments,
                  # cell2location automatically enters the mode which pools information across experiments
                  'sample_name_col': 'sample'}, # Column in sp_data.obs with experiment ID (see above)


      export_args={'path': data_dir + 'cell2location_model/'
                  }

)

And the error what I get is:

### Summarising single cell clusters ###
### Creating model ### - time 0.0 min
Traceback (most recent call last):
  File "run_cell2location.py", line 191, in <module>
    export_args={'path': data_dir + 'cell2location_model/'
  File "/home/tsztank/miniconda3/envs/cellpymc/lib/python3.7/site-packages/cell2location/run_c2l.py", line 345, in run_cell2location
    **model_kwargs)
  File "/home/tsztank/miniconda3/envs/cellpymc/lib/python3.7/site-packages/cell2location/models/LocationModelLinearDependentWMultiExperiment.py", line 278, in __init__
    total_size=self.X_data.shape)
  File "/home/tsztank/miniconda3/envs/cellpymc/lib/python3.7/site-packages/pymc3/distributions/distribution.py", line 83, in __new__
    return model.Var(name, dist, data, total_size, dims=dims)
  File "/home/tsztank/miniconda3/envs/cellpymc/lib/python3.7/site-packages/pymc3/model.py", line 1117, in Var
    model=self,
  File "/home/tsztank/miniconda3/envs/cellpymc/lib/python3.7/site-packages/pymc3/model.py", line 1737, in __init__
    data = as_tensor(data, name, model, distribution)
  File "/home/tsztank/miniconda3/envs/cellpymc/lib/python3.7/site-packages/pymc3/model.py", line 1691, in as_tensor
    data = tt.as_tensor_variable(data, name=name)
  File "/home/tsztank/miniconda3/envs/cellpymc/lib/python3.7/site-packages/theano/tensor/basic.py", line 158, in as_tensor_variable
    "Variable type field must be a TensorType.", x, x.type)
theano.tensor.var.AsTensorError: ('Variable type field must be a TensorType.', SparseVariable{csr,int16}, Sparse[int16, csr])

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
lopollarcommented, Mar 3, 2021

Hi, I am trying to run through th whole tutorial of cell2location on the data provided by the tutorial before trying it on my own data. I however, encounter problems when arriving at running the actual model. when running:

sc.settings.set_figure_params(dpi = 100, color_map = 'viridis', dpi_save = 100,
                              vector_friendly = True, format = 'pdf',
                              facecolor='white')

r = cell2location.run_cell2location(

      # Single cell reference signatures as pd.DataFrame
      # (could also be data as anndata object for estimating signatures
      #  as cluster average expression - `sc_data=adata_snrna_raw`)
      sc_data=inf_aver,
      # Spatial data as anndata object
      sp_data=adata_vis,

      # the column in sc_data.obs that gives cluster idenitity of each cell
      summ_sc_data_args={'cluster_col': "annotation_1",
                         # select marker genes of cell types by specificity of their expression signatures
                         'selection': "cluster_specificity",
                         # specificity cutoff (1 = max, 0 = min)
                         'selection_specificity': 0.07
                        },

      train_args={'use_raw': True, # By default uses raw slots in both of the input datasets.
                  'n_iter': 40000, # Increase the number of iterations if needed (see QC below)

                  # Whe analysing the data that contains multiple experiments,
                  # cell2location automatically enters the mode which pools information across experiments
                  'sample_name_col': 'sample'}, # Column in sp_data.obs with experiment ID (see above)


      export_args={'path': results_folder, # path where to save results
                   'run_name_suffix': '' # optinal suffix to modify the name the run
                  },

      model_kwargs={ # Prior on the number of cells, cell types and co-located groups

                    'cell_number_prior': {
                        # - N - the expected number of cells per location:
                        'cells_per_spot': 8,
                        # - A - the expected number of cell types per location:
                        'factors_per_spot': 9,
                        # - Y - the expected number of co-located cell type groups per location
                        'combs_per_spot': 5
                    },

                     # Prior beliefs on the sensitivity of spatial technology:
                    'gene_level_prior':{
                        # Prior on the mean
                        'mean': 1/2,
                        # Prior on standard deviation,
                        # a good choice of this value should be at least 2 times lower that the mean
                        'sd': 1/4
                    }
      }
)

I encounter following error, the same type as mentioned above:

---------------------------------------------------------------------------
AsTensorError                             Traceback (most recent call last)
<ipython-input-28-5578f54fa95f> in <module>
     49                         # Prior on standard deviation,
     50                         # a good choice of this value should be at least 2 times lower that the mean
---> 51                         'sd': 1/4
     52                     }
     53       }

/srv/scratch/lottep/anaconda3/envs/cellpymc2/lib/python3.7/site-packages/cell2location/run_c2l.py in run_cell2location(sc_data, sp_data, model_name, verbose, show_locations, return_all, summ_sc_data_args, train_args, model_kwargs, posterior_args, export_args)
    343                 fact_names=fact_names,
    344                 sample_id=sp_data.obs[train_args['sample_name_col']],
--> 345                 **model_kwargs)
    346 
    347     ####### Print run name #######

/srv/scratch/lottep/anaconda3/envs/cellpymc2/lib/python3.7/site-packages/cell2location/models/LocationModelLinearDependentWMultiExperiment.py in __init__(self, cell_state_mat, X_data, n_comb, data_type, n_iter, learning_rate, total_grad_norm_constraint, verbose, var_names, var_names_read, obs_names, fact_names, sample_id, gene_level_prior, gene_level_var_prior, cell_number_prior, cell_number_var_prior, phi_hyp_prior, spot_fact_mean_var_ratio, exper_gene_level_mean_var_ratio)
    276                                                                      1 / tt.pow(self.gene_E, 2)),
    277                                                    observed=self.x_data,
--> 278                                                    total_size=self.X_data.shape)
    279 
    280             # =====================Compute nUMI from each factor in spots  ======================= #

/srv/scratch/lottep/anaconda3/envs/cellpymc2/lib/python3.7/site-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
     81         else:
     82             dist = cls.dist(*args, **kwargs)
---> 83         return model.Var(name, dist, data, total_size, dims=dims)
     84 
     85     def __getnewargs__(self):

/srv/scratch/lottep/anaconda3/envs/cellpymc2/lib/python3.7/site-packages/pymc3/model.py in Var(self, name, dist, data, total_size, dims)
   1115                     distribution=dist,
   1116                     total_size=total_size,
-> 1117                     model=self,
   1118                 )
   1119             self.observed_RVs.append(var)

/srv/scratch/lottep/anaconda3/envs/cellpymc2/lib/python3.7/site-packages/pymc3/model.py in __init__(self, type, owner, index, name, data, distribution, total_size, model)
   1735 
   1736         if distribution is not None:
-> 1737             data = as_tensor(data, name, model, distribution)
   1738 
   1739             self.missing_values = data.missing_values

/srv/scratch/lottep/anaconda3/envs/cellpymc2/lib/python3.7/site-packages/pymc3/model.py in as_tensor(data, name, model, distribution)
   1689         return data
   1690     else:
-> 1691         data = tt.as_tensor_variable(data, name=name)
   1692         data.missing_values = None
   1693         return data

/srv/scratch/lottep/anaconda3/envs/cellpymc2/lib/python3.7/site-packages/theano/tensor/basic.py in as_tensor_variable(x, name, ndim)
    156         if not isinstance(x.type, TensorType):
    157             raise AsTensorError(
--> 158                 "Variable type field must be a TensorType.", x, x.type)
    159 
    160         if ndim is None:

AsTensorError: ('Variable type field must be a TensorType.', SparseVariable{csr,int16}, Sparse[int16, csr])

I run the model on an ‘old’ titan X GPU, as newer GPU’s don’t support theano anymore. I installed the package yesterday, based on the environment.yml file provided, and then using the github as mentioned by you.

However, I encountered a problem with the arviz package. When loading the package in python, I received following error: module ‘arviz’ has no attribute ‘geweke’ For this reason, I installed arviz version 0.10.0 instead of 0.11.2. This made it possible to load the package into python. If necessary, I can send more information, but I just followed all provided steps.

1reaction
yozhikoffcommented, Mar 4, 2021

@onahman Setting GPU shouldn’t be too hard, we have a short guide about it here. The only potential issue is that it does require root access, is it a problem for you?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common errors — cell2location documentation - Read the Docs
Solution: use our singularity and docker images with CUDA 10.2. B. The single cell reference is a very poor match to the data...
Read more >
Cell2location overview and validation using simulated data a ...
Cell2location takes reference cell type signatures derived from scRNA-seq and spatial transcriptomics data as input. The model then decomposes spatially ...
Read more >
Comprehensive mapping of tissue architecture via ... - YouTube
Speaker: Omer Bayraktar, Wellcome Sanger InstituteVirtual seminar series for Spatial Omics, organized by Prof. Rong Fan and Prof.
Read more >
Comprehensive mapping of tissue cell architecture ... - bioRxiv
multi-cell spatial transcriptomic data into cell type abundance maps. ... When using cell2location to map these cell types to the.
Read more >
cell2location - PyPI
cell2location : High-throughput spatial mapping of cell types. ... type reference signatures and spatial transcriptomics data as input (2, ...
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