{"record":{"id":"24d447413112695a","repo":"jax-ml/jax","slug":"bcsr-from-scipy-sparse-requires-2d-array-mat-ndi","errorCode":null,"errorMessage":"BCSR from_scipy_sparse requires 2D array; {mat.ndim}D is given.","messagePattern":"BCSR from_scipy_sparse requires 2D array; (.+?)D is given\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcsr.py","lineNumber":995,"sourceCode":"  @classmethod\n  def from_bcoo(cls, arr: bcoo.BCOO) -> BCSR:\n    if arr.n_sparse != 2:\n      raise NotImplementedError(f\"BSCR.from_bcoo requires n_sparse=2; got {arr.n_sparse=}\")\n    if not arr.indices_sorted:\n      arr = arr.sort_indices()\n    indices, indptr = _bcoo_to_bcsr(\n        arr.indices, shape=arr.shape, index_dtype=arr.indices.dtype\n    )\n    return cls((arr.data, indices, indptr), shape=arr.shape)\n\n  @classmethod\n  def from_scipy_sparse(cls, mat, *, index_dtype=None, n_dense=0, n_batch=0):\n    \"\"\"Create a BCSR array from a :mod:`scipy.sparse` array.\"\"\"\n    if n_dense != 0 or n_batch != 0:\n      raise NotImplementedError(\"BCSR from_scipy_sparse with nonzero n_dense/n_batch.\")\n\n    if mat.ndim != 2:\n      raise ValueError(f\"BCSR from_scipy_sparse requires 2D array; {mat.ndim}D is given.\")\n\n    mat = mat.tocsr()\n    data = jnp.asarray(mat.data)\n    indices = jnp.asarray(mat.indices).astype(index_dtype or jnp.int32)\n    indptr = jnp.asarray(mat.indptr).astype(index_dtype or jnp.int32)\n    return cls((data, indices, indptr), shape=mat.shape)\n\n#--------------------------------------------------------------------\n# vmappable handlers\ndef _bcsr_to_elt(cont, _, val, axis):\n  if axis is None:\n    return val\n  if axis >= val.n_batch:\n    raise ValueError(f\"Cannot map in_axis={axis} for BCSR array with n_batch=\"\n                     f\"{val.n_batch}. in_axes for batched BCSR operations must \"\n                     \"correspond to a batched dimension.\")\n  return BCSR((cont(val.data, axis),\n               cont(val.indices, axis),","sourceCodeStart":977,"sourceCodeEnd":1013,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcsr.py#L977-L1013","documentation":"scipy.sparse matrices are inherently 2D, and the BCSR format maps 1:1 onto a 2D (rows x cols) layout. from_scipy_sparse validates mat.ndim == 2 and raises ValueError otherwise. Non-2D inputs almost always indicate a wrong object was passed (e.g. a dense numpy array or a 1D vector).","triggerScenarios":"Calling BCSR.from_scipy_sparse(mat) where mat.ndim != 2 — most commonly passing a numpy ndarray, a 1D scipy-like vector, or a higher-dimensional array instead of a scipy.sparse matrix.","commonSituations":"Refactoring a pipeline that previously accepted dense arrays; passing np.asarray(sp_mat) (which yields a 2D sparse-backed ndarray in new scipy and may behave unexpectedly) or a plain numpy array by mistake.","solutions":["Verify the input is a scipy.sparse matrix (scipy.sparse.issparse) before calling","If you have a dense array, use BCSR.from_bcoo(bcoo.bcoo_fromdense(x)) or sparse.BCSR.fromdense-style paths instead","For 1D vectors, reshape to (1, N) or (N, 1) first if a matrix is semantically correct","Add an assert/issparse check in loaders that accept polymorphic input"],"exampleFix":"# before\nm = BCSR.from_scipy_sparse(dense_np_array)  # ValueError\n\n# after\nassert scipy.sparse.issparse(sp_mat) and sp_mat.ndim == 2\nm = BCSR.from_scipy_sparse(sp_mat)","handlingStrategy":"type-guard","validationCode":"import scipy.sparse\nassert scipy.sparse.issparse(mat), 'expected scipy.sparse matrix'\nassert mat.ndim == 2","typeGuard":"def is_valid_scipy_input(mat) -> bool:\n    import scipy.sparse\n    return scipy.sparse.issparse(mat) and mat.ndim == 2","tryCatchPattern":"try:\n    m = BCSR.from_scipy_sparse(mat)\nexcept ValueError as e:\n    m = BCSR.from_bcoo(bcoo.bcoo_fromdense(jnp.asarray(mat)))","preventionTips":["Gate loaders with scipy.sparse.issparse","Route dense arrays to fromdense/bcoo paths"],"tags":["jax","sparse","bcsr","scipy","input-validation"],"backgroundTag":"sparse-input-dimension-invalid","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}