Repository navigation
Using Marimo with scverse mutable objects #9518
Replies: 2 comments
|
This combination works well once you follow one rule from marimo's docs (variable mutations are not tracked): don't mutate an object in a cell other than the one that defined it. marimo only re-runs cells when a variable is defined, so an in-place change to 1. Make each step return a new object. Many scanpy functions take # cell 1
adata = sc.read_h5ad("data.h5ad")# cell 2
adata_norm = sc.pp.normalize_total(adata, copy=True)
sc.pp.log1p(adata_norm) # fine: same cell that defines adata_norm# cell 3
adata_sub = adata_norm[adata_norm.obs["cell_type"] == "T cell"].copy()
adata_sub.obs["group"] = ...Downstream cells now depend on 2. Group the in-place steps into one cell when copies are too expensive (large adata = sc.read_h5ad("data.h5ad")
sc.pp.normalize_total(adata)
sc.pp.log1p(adata)
adata.obs["group"] = ...A reasonable split is consolidated in-place cells for heavy preprocessing, and copies / new variables for lighter exploratory steps (subsetting, adding If expensive cells re-running automatically gets in the way, set On cell change → lazy in the notebook settings (runtime configuration): dependents are marked stale instead of being run straight away. So it isn't more trouble than it's worth; it mostly means treating each cell as "defines one stage of the dataset". |
|
Thank you for the answer! Unfortunately none of the options is ideal... option 1 uses a lot of memory, especially with heavy spatialdata objects. Option 2 in theory works but in practice it makes the whole notebook collapse in just a few cells, outside of the plotting. I guess the ideal solution would be to make the spatialdata/anndata somewhat immutable. |
Uh oh!
There was an error while loading. Please reload this page.
Hi all,
I’ve been experimenting with Marimo recently and appreciate its reactive, immutable execution model. However, I’m running into some friction when trying to use it together with mutable objects from the scverse ecosystem like
spatialdata,anndataandmudata.A lot of typical workflows in scverse rely on in-place mutations (e.g., modifying
.obs,.var, or layers directly), which doesn’t seem to align well with Marimo’s approach. In particular, these in-place updates can interfere with change tracking and reactivity.I do not have much experience with data analysis and pyhton and I would appreciate some advice. For example:
I’d be interested to hear any experiences, recommendations, or design patterns that make this setup more ergonomic.
Thanks!
All reactions