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

Binning problem #20

Closed
alexanderchang1 opened this issue Sep 17, 2024 · 2 comments
Closed

Binning problem #20

alexanderchang1 opened this issue Sep 17, 2024 · 2 comments

Comments

@alexanderchang1
Copy link

alexanderchang1 commented Sep 17, 2024

There's a problem in the spatial binning function in that creates the wrong matrix type that then triggers an error downstream.

from popari._dataset_utils import _spatial_binning

binned_datasets = []
for dataset in transformed_datasets:
    binned_dataset = _spatial_binning(dataset, chunks=8, downsample_rate=0.2)
    print(f"Dataset downsampled from {len(dataset)} spots to {len(binned_dataset)} spots.")
    binned_datasets.append(binned_dataset)
/bgfs/alee/LO_LAB/Personal/Alexander_Chang/alc376/envs/popari_env/lib/python3.12/site-packages/scipy/sparse/_index.py:151: SparseEfficiencyWarning: Changing the sparsity structure of a csr_array is expensive. lil and dok are more efficient.
  self._set_arrayXarray(i, j, x)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[23], line 8
      5 if isinstance(dataset.X, csr_array):
      6     # Convert dataset.X to csr_matrix
      7     dataset.X = dataset.X.tocsr()
----> 8 binned_dataset = _spatial_binning(dataset, chunks=8, downsample_rate=0.2)
      9 print(f"Dataset downsampled from {len(dataset)} spots to {len(binned_dataset)} spots.")
     10 binned_datasets.append(binned_dataset)

File /bgfs/alee/LO_LAB/Personal/Alexander_Chang/alc376/envs/popari_env/lib/python3.12/site-packages/popari/_dataset_utils.py:797, in _spatial_binning(dataset, level, chunks, downsample_rate, chunk_size, chunk_1d_density, num_jobs)
    795 binned_dataset.obsm["spatial"] = filtered_bin_coordinates
    796 binned_dataset.obs["total_transformed_counts"] = binned_dataset.X.sum(axis=1)
--> 797 binned_dataset.obsm[f"bin_assignments_{binned_dataset_name}"] = bin_assignments
    799 binned_dataset.uns["chunk_size"] = chunk_size
    800 binned_dataset.uns["chunk_1d_density"] = chunk_1d_density

File /bgfs/alee/LO_LAB/Personal/Alexander_Chang/alc376/envs/popari_env/lib/python3.12/site-packages/anndata/_core/aligned_mapping.py:216, in AlignedActual.__setitem__(self, key, value)
    215 def __setitem__(self, key: str, value: Value):
--> 216     value = self._validate_value(value, key)
    217     self._data[key] = value

File /bgfs/alee/LO_LAB/Personal/Alexander_Chang/alc376/envs/popari_env/lib/python3.12/site-packages/anndata/_core/aligned_mapping.py:279, in AxisArraysBase._validate_value(self, val, key)
    277             msg = "Index.equals and pd.testing.assert_index_equal disagree"
    278             raise AssertionError(msg)
--> 279 return super()._validate_value(val, key)

File /bgfs/alee/LO_LAB/Personal/Alexander_Chang/alc376/envs/popari_env/lib/python3.12/site-packages/anndata/_core/aligned_mapping.py:100, in AlignedMappingBase._validate_value(self, val, key)
     97     raise ValueError(msg)
     99 name = f"{self.attrname.title().rstrip('s')} {key!r}"
--> 100 return coerce_array(val, name=name, allow_df=self._allow_df)

File /bgfs/alee/LO_LAB/Personal/Alexander_Chang/alc376/envs/popari_env/lib/python3.12/site-packages/anndata/_core/storage.py:104, in coerce_array(value, name, allow_df, allow_array_like)
    102 if e is not None:
    103     msg += " (Failed to convert it to an array, see above for details.)"
--> 104 raise ValueError(msg) from e

ValueError: Obsm 'bin_assignments_MWS22-14789_level_0' needs to be of one of np.ndarray, numpy.ma.core.MaskedArray, scipy.sparse.spmatrix, awkward.Array, h5py.Dataset, zarr.Array, zappy.base.ZappyArray, anndata.experimental.[CSC,CSR]Dataset, dask.array.Array, cupy.ndarray, or cupyx.scipy.sparse.spmatrix, not <class 'scipy.sparse._csr.csr_array'>.
@alexanderchang1
Copy link
Author

See #21 which fixes this problem

@alam-shahul
Copy link
Owner

Closing this, as the dependencies have been updated so that this is no longer an issue.

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

2 participants