{"record":{"id":"f6dbda8ee33b819a","repo":"jax-ml/jax","slug":"bcsr-from-scipy-sparse-with-nonzero-n-dense-n-batc","errorCode":null,"errorMessage":"BCSR from_scipy_sparse with nonzero n_dense/n_batch.","messagePattern":"BCSR from_scipy_sparse with nonzero n_dense/n_batch\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcsr.py","lineNumber":992,"sourceCode":"    coo_indices = _bcsr_to_bcoo(self.indices, self.indptr, shape=self.shape)\n    return bcoo.BCOO((self.data, coo_indices), shape=self.shape)\n\n  @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 \"","sourceCodeStart":974,"sourceCodeEnd":1010,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcsr.py#L974-L1010","documentation":"BCSR.from_scipy_sparse only supports plain 2D CSR/COO/other scipy sparse matrices with no batch or dense (block) dimensions. Because scipy.sparse has no notion of n_batch or n_dense, passing nonzero values for these kwargs cannot be honored and raises NotImplementedError. For batched/dense-dim BCSR use BCSR.from_bcoo after constructing a BCOO.","triggerScenarios":"Calling BCSR.from_scipy_sparse(mat, n_batch=k) or with n_dense=k where k != 0. Signature allows the kwargs for API uniformity but only 0 is implemented.","commonSituations":"Copy-pasting kwargs from sparse.bcoo_from_scipy_sparse (which does support n_dense/n_batch) to from_scipy_sparse; writing format-generic loaders that pass the same options to every from_* constructor.","solutions":["Drop n_dense/n_batch (call with defaults 0) for plain 2D scipy matrices","For batched stacks, convert each scipy matrix separately and stack, or build a BCOO with the right layout and use BCSR.from_bcoo","Use bcoo.bcoo_from_scipy_sparse(mat, n_batch=..., n_dense=...) if BCOO output is acceptable"],"exampleFix":"# before\nm = BCSR.from_scipy_sparse(sp_mat, n_batch=1)  # NotImplementedError\n\n# after (stack manually)\nm = jax.tree.map(BCSR.from_scipy_sparse, stack_of_sp_mats)  # or per-matrix conversion","handlingStrategy":"validation","validationCode":"assert n_dense == 0 and n_batch == 0, 'from_scipy_sparse supports no dense/batch dims'","typeGuard":null,"tryCatchPattern":"try:\n    m = BCSR.from_scipy_sparse(mat, n_batch=n_batch)\nexcept NotImplementedError:\n    m = BCSR.from_bcoo(bcoo.bcoo_from_scipy_sparse(mat, n_batch=n_batch))","preventionTips":["Don't copy bcoo_from_scipy_sparse kwargs to from_scipy_sparse","Convert per-matrix and stack for batched scipy input"],"tags":["jax","sparse","bcsr","scipy","not-implemented"],"backgroundTag":"scipy-sparse-conversion-unsupported","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}