import%20marimo%0A%0A__generated_with%20%3D%20%220.14.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20from%20slaf%20import%20SLAFArray%0A%20%20%20%20from%20slaf.integrations%20import%20scanpy%20as%20slaf_scanpy%0A%20%20%20%20from%20slaf.integrations.anndata%20import%20read_slaf%0A%0A%20%20%20%20return%20SLAFArray%2C%20mo%2C%20read_slaf%2C%20slaf_scanpy%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%20SLAF%20Getting%20Started%20Guide%0A%0A%20%20%20%20This%20notebook%20introduces%20SLAF%20(Sparse%20Lazy%20Array%20Format)%20-%20a%20high-performance%20format%20for%20single-cell%20data%20that%20combines%20the%20power%20of%20SQL%20with%20lazy%20evaluation.%0A%0A%20%20%20%20**Key%20Benefits%3A**%0A%0A%20%20%20%20-%20%F0%9F%9A%80%20**Fast**%3A%20SQL-level%20performance%20for%20data%20operations%0A%0A%20%20%20%20-%20%F0%9F%92%BE%20**Memory%20Efficient**%3A%20Lazy%20evaluation%2C%20only%20load%20what%20you%20need%0A%0A%20%20%20%20-%20%F0%9F%94%8D%20**SQL%20Native**%3A%20Direct%20SQL%20queries%20on%20your%20data%0A%0A%20%20%20%20-%20%F0%9F%A7%AC%20**Scanpy%20Compatible**%3A%20Drop-in%20replacement%20for%20AnnData%20workflows%0A%0A%20%20%20%20-%20%E2%9A%A1%20**Production%20Ready**%3A%20Built%20for%20large-scale%20single-cell%20analysis%0A%0A%20%20%20%20-%20%E2%9A%99%EF%B8%8F%20**ML%20Ready**%3A%20Ready%20for%20ML%20training%20with%20efficient%20tokenization%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(SLAFArray)%3A%0A%20%20%20%20%23%20Load%20SLAF%20dataset%20using%20the%20low-level%20interface%0A%20%20%20%20slaf%20%3D%20SLAFArray(%22..%2Fslaf-datasets%2Fpbmc3k_processed.slaf%22)%0A%20%20%20%20print(f%22%E2%9C%85%20Loaded%20SLAF%20dataset%3A%20%7Bslaf.shape%5B0%5D%3A%2C%7D%20cells%20%C3%97%20%7Bslaf.shape%5B1%5D%3A%2C%7D%20genes%22)%0A%0A%20%20%20%20%23%20Show%20dataset%20information%0A%20%20%20%20slaf.info()%0A%20%20%20%20return%20(slaf%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%23%201.%20Understanding%20the%20SLAF%20Database%20Schema%0A%0A%20%20%20%20SLAF%20stores%20data%20in%20three%20main%20tables%20that%20you%20can%20query%20directly%20with%20SQL%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20def%20show_database_schema()%3A%0A%20%20%20%20%20%20%20%20%23%20Show%20the%20database%20schema%0A%20%20%20%20%20%20%20%20print(%22%F0%9F%93%8A%20SLAF%20Database%20Schema%22)%0A%20%20%20%20%20%20%20%20print(%22%3D%22%20*%2050)%0A%0A%20%20%20%20%20%20%20%20%23%20Get%20table%20information%0A%20%20%20%20%20%20%20%20tables%20%3D%20%5B%22cells%22%2C%20%22genes%22%2C%20%22expression%22%5D%0A%0A%20%20%20%20%20%20%20%20for%20table%20in%20tables%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%5Cn%F0%9F%94%8D%20Table%3A%20%7Btable%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20table%20%3D%3D%20%22cells%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20Purpose%3A%20Cell%20metadata%20and%20QC%20metrics%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20Key%20columns%3A%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20cell_id%3A%20Unique%20cell%20identifier%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20cell_integer_id%3A%20Integer%20ID%20for%20efficient%20queries%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20cell_type%3A%20Cell%20type%20annotation%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20batch%3A%20Batch%20information%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20total_counts%3A%20Total%20UMI%20counts%20per%20cell%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20n_genes_by_counts%3A%20Number%20of%20genes%20expressed%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20high_mito%3A%20Boolean%20flag%20for%20high%20mitochondrial%20content%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20table%20%3D%3D%20%22genes%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20Purpose%3A%20Gene%20metadata%20and%20annotations%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20Key%20columns%3A%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20gene_id%3A%20Unique%20gene%20identifier%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20gene_integer_id%3A%20Integer%20ID%20for%20efficient%20queries%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20highly_variable%3A%20Boolean%20flag%20for%20highly%20variable%20genes%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20table%20%3D%3D%20%22expression%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20Purpose%3A%20Sparse%20expression%20matrix%20data%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20Key%20columns%3A%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20cell_integer_id%3A%20Integer%20cell%20ID%20for%20efficient%20queries%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20gene_integer_id%3A%20Integer%20gene%20ID%20for%20efficient%20queries%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%20%20-%20value%3A%20Expression%20value%20(UMI%20counts)%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20Note%3A%20String%20IDs%20(cell_id%2C%20gene_id)%20are%20in%20metadata%20tables%22)%0A%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20show_database_schema()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(slaf)%3A%0A%20%20%20%20def%20show_sample_data()%3A%0A%20%20%20%20%20%20%20%20%23%20Show%20sample%20data%20from%20each%20table%0A%20%20%20%20%20%20%20%20print(%22%F0%9F%93%8B%20Sample%20Data%20from%20Each%20Table%22)%0A%20%20%20%20%20%20%20%20print(%22%3D%22%20*%2050)%0A%0A%20%20%20%20%20%20%20%20%23%20Sample%20from%20cells%20table%0A%20%20%20%20%20%20%20%20print(%22%5Cn%F0%9F%94%AC%20Sample%20cells%3A%22)%0A%20%20%20%20%20%20%20%20cells_sample%20%3D%20slaf.query(%22SELECT%20*%20FROM%20cells%20LIMIT%203%22)%0A%20%20%20%20%20%20%20%20print(cells_sample)%0A%0A%20%20%20%20%20%20%20%20%23%20Sample%20from%20genes%20table%0A%20%20%20%20%20%20%20%20print(%22%5Cn%F0%9F%A7%AC%20Sample%20genes%3A%22)%0A%20%20%20%20%20%20%20%20genes_sample%20%3D%20slaf.query(%22SELECT%20*%20FROM%20genes%20LIMIT%203%22)%0A%20%20%20%20%20%20%20%20print(genes_sample)%0A%0A%20%20%20%20%20%20%20%20%23%20Sample%20from%20expression%20table%0A%20%20%20%20%20%20%20%20print(%22%5Cn%F0%9F%93%88%20Sample%20expression%20data%3A%22)%0A%20%20%20%20%20%20%20%20expr_sample%20%3D%20slaf.query(%22SELECT%20*%20FROM%20expression%20LIMIT%205%22)%0A%20%20%20%20%20%20%20%20print(expr_sample)%0A%0A%20%20%20%20show_sample_data()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%23%202.%20SQL%20Queries%20-%20Your%20Data%2C%20Your%20Way%0A%0A%20%20%20%20SLAF%20gives%20you%20direct%20SQL%20access%20to%20your%20data.%20Here%20are%20some%20practical%20examples%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(slaf)%3A%0A%20%20%20%20def%20run_basic_sql_queries()%3A%0A%20%20%20%20%20%20%20%20%23%20Basic%20SQL%20queries%0A%20%20%20%20%20%20%20%20print(%22%F0%9F%94%8D%20Basic%20SQL%20Queries%22)%0A%20%20%20%20%20%20%20%20print(%22%3D%22%20*%2040)%0A%0A%20%20%20%20%20%20%20%20%23%20Count%20records%0A%20%20%20%20%20%20%20%20print(%22%5Cn1.%20Count%20records%20in%20each%20table%3A%22)%0A%20%20%20%20%20%20%20%20for%20table%20in%20%5B%22cells%22%2C%20%22genes%22%2C%20%22expression%22%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%3D%20slaf.query(f%22SELECT%20COUNT(*)%20as%20count%20FROM%20%7Btable%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%20%20%20%7Btable%7D%3A%20%7Bcount.item(0%2C%200)%3A%2C%7D%20records%22)%0A%0A%20%20%20%20%20%20%20%20%23%20Cell%20type%20distribution%0A%20%20%20%20%20%20%20%20print(%22%5Cn2.%20Batch%20distribution%3A%22)%0A%20%20%20%20%20%20%20%20cell_types%20%3D%20slaf.query(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20batch%2C%20COUNT(*)%20as%20count%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20cells%0A%20%20%20%20%20%20%20%20%20%20%20%20GROUP%20BY%20batch%0A%20%20%20%20%20%20%20%20%20%20%20%20ORDER%20BY%20count%20DESC%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(cell_types)%0A%0A%20%20%20%20%20%20%20%20%23%20Expression%20statistics%0A%20%20%20%20%20%20%20%20print(%22%5Cn3.%20Expression%20value%20statistics%3A%22)%0A%20%20%20%20%20%20%20%20expr_stats%20%3D%20slaf.query(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20MIN(value)%20as%20min_expr%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20MAX(value)%20as%20max_expr%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20AVG(value)%20as%20avg_expr%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20COUNT(*)%20as%20total_records%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20COUNT(CASE%20WHEN%20value%20%3E%200%20THEN%201%20END)%20as%20non_zero_records%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20expression%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(expr_stats)%0A%0A%20%20%20%20run_basic_sql_queries()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(slaf)%3A%0A%20%20%20%20def%20run_advanced_sql_queries()%3A%0A%20%20%20%20%20%20%20%20%23%20Advanced%20SQL%20queries%20with%20joins%0A%20%20%20%20%20%20%20%20print(%22%F0%9F%94%97%20Advanced%20SQL%20Queries%20with%20Joins%22)%0A%20%20%20%20%20%20%20%20print(%22%3D%22%20*%2045)%0A%0A%20%20%20%20%20%20%20%20%23%20Cells%20with%20their%20expression%20summary%0A%20%20%20%20%20%20%20%20print(%22%5Cn1.%20Top%205%20cells%20by%20total%20expression%3A%22)%0A%20%20%20%20%20%20%20%20top_cells%20%3D%20slaf.query(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20c.cell_id%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20c.total_counts%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20COUNT(e.value)%20as%20expressed_genes%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20AVG(e.value)%20as%20avg_expression%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20cells%20c%0A%20%20%20%20%20%20%20%20%20%20%20%20LEFT%20JOIN%20expression%20e%20ON%20c.cell_integer_id%20%3D%20e.cell_integer_id%0A%20%20%20%20%20%20%20%20%20%20%20%20GROUP%20BY%20c.cell_id%2C%20c.total_counts%0A%20%20%20%20%20%20%20%20%20%20%20%20ORDER%20BY%20c.total_counts%20DESC%0A%20%20%20%20%20%20%20%20%20%20%20%20LIMIT%205%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(top_cells)%0A%0A%20%20%20%20%20%20%20%20%23%20Highly%20variable%20genes%20with%20expression%20stats%0A%20%20%20%20%20%20%20%20print(%22%5Cn2.%20Highly%20variable%20genes%20with%20expression%20stats%3A%22)%0A%20%20%20%20%20%20%20%20hvg_stats%20%3D%20slaf.query(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20g.gene_id%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20g.highly_variable%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20COUNT(e.value)%20as%20cells_expressed%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20AVG(e.value)%20as%20avg_expression%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20MAX(e.value)%20as%20max_expression%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20genes%20g%0A%20%20%20%20%20%20%20%20%20%20%20%20LEFT%20JOIN%20expression%20e%20ON%20g.gene_integer_id%20%3D%20e.gene_integer_id%0A%20%20%20%20%20%20%20%20%20%20%20%20WHERE%20g.highly_variable%20%3D%20true%0A%20%20%20%20%20%20%20%20%20%20%20%20GROUP%20BY%20g.gene_id%2C%20g.highly_variable%0A%20%20%20%20%20%20%20%20%20%20%20%20ORDER%20BY%20avg_expression%20DESC%0A%20%20%20%20%20%20%20%20%20%20%20%20LIMIT%205%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(hvg_stats)%0A%0A%20%20%20%20run_advanced_sql_queries()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%23%203.%20Convenience%20Methods%20-%20Easy%20Filtering%0A%0A%20%20%20%20SLAF%20provides%20convenient%20methods%20for%20common%20operations%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(slaf)%3A%0A%20%20%20%20%23%20Demonstrate%20convenience%20methods%0A%20%20%20%20print(%22%F0%9F%8E%AF%20Convenience%20Methods%22)%0A%20%20%20%20print(%22%3D%22%20*%2030)%0A%0A%20%20%20%20%23%20Filter%20cells%0A%20%20%20%20print(%22%5Cn1.%20Filter%20cells%20by%20criteria%3A%22)%0A%0A%20%20%20%20%23%20High%20quality%20cells%0A%20%20%20%20high_quality%20%3D%20slaf.filter_cells(n_genes_by_counts%3D%22%3E%3D1000%22%2C%20total_counts%3D%22%3E%3D2000%22)%0A%20%20%20%20print(f%22%20%20%20High%20quality%20cells%20(%E2%89%A51000%20genes%2C%20%E2%89%A52000%20counts)%3A%20%7Blen(high_quality)%3A%2C%7D%22)%0A%0A%20%20%20%20%23%20Filter%20genes%0A%20%20%20%20print(%22%5Cn2.%20Filter%20genes%20by%20criteria%3A%22)%0A%0A%20%20%20%20%23%20Highly%20variable%20genes%0A%20%20%20%20hvg_genes%20%3D%20slaf.filter_genes(highly_variable%3DTrue)%0A%20%20%20%20print(f%22%20%20%20Highly%20variable%20genes%3A%20%7Blen(hvg_genes)%3A%2C%7D%22)%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%23%204.%20Lazy%20AnnData%20Interface%20-%20Scanpy%20Compatible%0A%0A%20%20%20%20SLAF%20provides%20a%20lazy%20AnnData%20interface%20that's%20compatible%20with%20scanpy%20workflows%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(read_slaf)%3A%0A%20%20%20%20%23%20Load%20as%20lazy%20AnnData%0A%20%20%20%20adata%20%3D%20read_slaf(%22..%2Fslaf-datasets%2Fpbmc3k_processed.slaf%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22%E2%9C%85%20Loaded%20as%20LazyAnnData%3A%20%7Badata.shape%5B0%5D%3A%2C%7D%20cells%20%C3%97%20%7Badata.shape%5B1%5D%3A%2C%7D%20genes%22%0A%20%20%20%20)%0A%20%20%20%20print(f%22%20%20%20Type%3A%20%7Btype(adata)%7D%22)%0A%20%20%20%20print(f%22%20%20%20Expression%20matrix%20type%3A%20%7Btype(adata.X)%7D%22)%0A%20%20%20%20return%20(adata%2C)%0A%0A%0A%40app.cell%0Adef%20_(adata)%3A%0A%20%20%20%20%23%20Basic%20AnnData%20operations%0A%20%20%20%20print(%22%F0%9F%94%AC%20Basic%20AnnData%20Operations%22)%0A%20%20%20%20print(%22%3D%22%20*%2035)%0A%0A%20%20%20%20print(f%22Dataset%20shape%3A%20%7Badata.shape%7D%22)%0A%20%20%20%20print(f%22Number%20of%20cells%3A%20%7Badata.n_obs%7D%22)%0A%20%20%20%20print(f%22Number%20of%20genes%3A%20%7Badata.n_vars%7D%22)%0A%0A%20%20%20%20%23%20Show%20available%20metadata%0A%20%20%20%20if%20hasattr(adata%2C%20%22obs%22)%20and%20adata.obs%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20print(f%22%5CnCell%20metadata%20columns%3A%20%7Blist(adata.obs.columns)%7D%22)%0A%0A%20%20%20%20if%20hasattr(adata%2C%20%22var%22)%20and%20adata.var%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20print(f%22Gene%20metadata%20columns%3A%20%7Blist(adata.var.columns)%7D%22)%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(adata)%3A%0A%20%20%20%20%23%20Lazy%20slicing%20operations%0A%20%20%20%20print(%22%E2%9C%82%EF%B8%8F%20Lazy%20Slicing%20Operations%22)%0A%20%20%20%20print(%22%3D%22%20*%2030)%0A%0A%20%20%20%20%23%20Slice%20cells%0A%20%20%20%20subset_cells%20%3D%20adata%5B%3A100%2C%20%3A%5D%0A%20%20%20%20print(f%22First%20100%20cells%20subset%3A%20%7Bsubset_cells.shape%7D%22)%0A%0A%20%20%20%20%23%20Slice%20genes%0A%20%20%20%20subset_genes%20%3D%20adata%5B%3A%2C%20%3A50%5D%0A%20%20%20%20print(f%22First%2050%20genes%20subset%3A%20%7Bsubset_genes.shape%7D%22)%0A%0A%20%20%20%20%23%20Combined%20slice%0A%20%20%20%20subset_both%20%3D%20adata%5B%3A50%2C%20%3A25%5D%0A%20%20%20%20print(f%2250%20cells%20%C3%97%2025%20genes%20subset%3A%20%7Bsubset_both.shape%7D%22)%0A%0A%20%20%20%20print(%22%5CnNote%3A%20These%20operations%20are%20lazy%20-%20no%20data%20is%20loaded%20until%20needed!%22)%0A%0A%20%20%20%20return%20(subset_cells%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%23%205.%20Lazy%20Scanpy%20Preprocessing%0A%0A%20%20%20%20SLAF%20provides%20lazy%20versions%20of%20scanpy%20preprocessing%20functions%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(adata%2C%20slaf_scanpy)%3A%0A%20%20%20%20%23%20Lazy%20scanpy%20preprocessing%0A%20%20%20%20print(%22%F0%9F%A7%AC%20Lazy%20Scanpy%20Preprocessing%22)%0A%20%20%20%20print(%22%3D%22%20*%2035)%0A%0A%20%20%20%20%23%20Ensure%20metadata%20is%20loaded%20before%20preprocessing%0A%20%20%20%20print(%221.%20Loading%20metadata...%22)%0A%20%20%20%20_%20%3D%20adata.obs%20%20%23%20Trigger%20metadata%20loading%0A%20%20%20%20_%20%3D%20adata.var%20%20%23%20Trigger%20metadata%20loading%0A%20%20%20%20print(%22%20%20%20%E2%9C%85%20Metadata%20loaded%22)%0A%0A%20%20%20%20%23%20Calculate%20QC%20metrics%20(lazy)%0A%20%20%20%20print(%22%5Cn2.%20Calculating%20QC%20metrics...%22)%0A%20%20%20%20slaf_scanpy.pp.calculate_qc_metrics(adata%2C%20inplace%3DTrue)%0A%20%20%20%20print(%22%20%20%20%E2%9C%85%20QC%20metrics%20calculated%20(lazily)%22)%0A%0A%20%20%20%20%23%20Filter%20cells%20(lazy)%20-%20use%20more%20conservative%20filtering%0A%20%20%20%20print(%22%5Cn3.%20Filtering%20cells...%22)%0A%20%20%20%20slaf_scanpy.pp.filter_cells(adata%2C%20min_genes%3D200%2C%20inplace%3DTrue)%0A%20%20%20%20print(%22%20%20%20%E2%9C%85%20Cells%20filtered%20(lazily)%22)%0A%0A%20%20%20%20%23%20Filter%20genes%20(lazy)%20-%20use%20more%20conservative%20filtering%0A%20%20%20%20print(%22%5Cn4.%20Filtering%20genes...%22)%0A%20%20%20%20slaf_scanpy.pp.filter_genes(adata%2C%20min_cells%3D30%2C%20inplace%3DTrue)%0A%20%20%20%20print(%22%20%20%20%E2%9C%85%20Genes%20filtered%20(lazily)%22)%0A%0A%20%20%20%20print(f%22%5CnFinal%20dataset%20shape%3A%20%7Badata.shape%5B0%5D%3A%2C%7D%20cells%20%C3%97%20%7Badata.shape%5B1%5D%3A%2C%7D%20genes%22)%0A%20%20%20%20print(%22Note%3A%20All%20operations%20are%20lazy%20-%20data%20is%20only%20processed%20when%20accessed!%22)%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(adata%2C%20subset_cells)%3A%0A%20%20%20%20%23%20Demonstrate%20.compute()%20method%0A%20%20%20%20print(%22%E2%9A%A1%20Explicit%20Computation%20with%20.compute()%22)%0A%20%20%20%20print(%22%3D%22%20*%2040)%0A%0A%20%20%20%20print(%221.%20Computing%20expression%20matrix%20only%3A%22)%0A%20%20%20%20print(f%22%20%20%20Before%3A%20%7Btype(adata.X)%7D%22)%0A%20%20%20%20sparse_matrix%20%3D%20adata.X.compute()%0A%20%20%20%20print(f%22%20%20%20After%3A%20%7Btype(sparse_matrix)%7D%22)%0A%20%20%20%20print(f%22%20%20%20Shape%3A%20%7Bsparse_matrix.shape%7D%22)%0A%0A%20%20%20%20print(%22%5Cn2.%20Computing%20sliced%20data%3A%22)%0A%20%20%20%20print(f%22%20%20%20Slice%20type%3A%20%7Btype(subset_cells)%7D%22)%0A%20%20%20%20native_slice%20%3D%20subset_cells.compute()%0A%20%20%20%20print(f%22%20%20%20Computed%20slice%20type%3A%20%7Btype(native_slice)%7D%22)%0A%20%20%20%20print(f%22%20%20%20Shape%3A%20%7Bnative_slice.shape%7D%22)%0A%0A%20%20%20%20print(%22%5Cn3.%20Computing%20full%20dataset%20(with%20sync%20check)%3A%22)%0A%20%20%20%20print(f%22%20%20%20Before%3A%20%7Btype(adata)%7D%22)%0A%0A%20%20%20%20%23%20Check%20if%20metadata%20and%20expression%20matrix%20are%20in%20sync%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20obs_rows%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20len(adata.obs)%20if%20hasattr(adata%2C%20%22obs%22)%20and%20adata.obs%20is%20not%20None%20else%200%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20var_rows%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20len(adata.var)%20if%20hasattr(adata%2C%20%22var%22)%20and%20adata.var%20is%20not%20None%20else%200%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20x_rows%20%3D%20adata.X.shape%5B0%5D%20if%20hasattr(adata%2C%20%22X%22)%20and%20adata.X%20is%20not%20None%20else%200%0A%0A%20%20%20%20%20%20%20%20print(f%22%20%20%20Metadata%20rows%3A%20obs%3D%7Bobs_rows%7D%2C%20var%3D%7Bvar_rows%7D%22)%0A%20%20%20%20%20%20%20%20print(f%22%20%20%20Expression%20matrix%20rows%3A%20%7Bx_rows%7D%22)%0A%0A%20%20%20%20%20%20%20%20if%20obs_rows%20!%3D%20x_rows%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%E2%9A%A0%EF%B8%8F%20Metadata%20and%20expression%20matrix%20are%20out%20of%20sync%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20This%20is%20expected%20after%20lazy%20filtering%20operations.%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20Recommendation%3A%20Use%20.X.compute()%20for%20expression%20data%20only.%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%E2%9C%85%20Example%20continues%20successfully%20despite%20sync%20issue%22)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Try%20to%20compute%20the%20full%20dataset%0A%20%20%20%20%20%20%20%20%20%20%20%20native_adata%20%3D%20adata.compute()%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%20%20%20After%3A%20%7Btype(native_adata)%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%20%20%20Shape%3A%20%7Bnative_adata.shape%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22%20%20%20%E2%9C%85%20Successfully%20computed%20full%20dataset%22)%0A%0A%20%20%20%20except%20Exception%20as%20e%3A%0A%20%20%20%20%20%20%20%20print(f%22%20%20%20%E2%9A%A0%EF%B8%8F%20Sync%20issue%20detected%3A%20%7Bstr(e)%5B%3A100%5D%7D...%22)%0A%20%20%20%20%20%20%20%20print(%22%20%20%20This%20is%20expected%20after%20lazy%20filtering%20operations.%22)%0A%20%20%20%20%20%20%20%20print(%22%20%20%20Recommendation%3A%20Use%20.X.compute()%20for%20expression%20data%20only.%22)%0A%20%20%20%20%20%20%20%20print(%22%20%20%20%E2%9C%85%20Example%20continues%20successfully%20despite%20sync%20issue%22)%0A%0A%20%20%20%20print(%22%5CnKey%20insight%3A%20.compute()%20converts%20lazy%20objects%20to%20native%20scanpy%20objects!%22)%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20%23%20Demonstrate%20workaround%20for%20metadata%20sync%20issues%0A%20%20%20%20print(%22%F0%9F%94%84%20Workaround%20for%20Metadata%20Sync%20Issues%22)%0A%20%20%20%20print(%22%3D%22%20*%2045)%0A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20**Note**%3A%20After%20lazy%20filtering%20operations%2C%20metadata%20and%20expression%20matrix%20can%20get%20out%20of%20sync.%0A%20%20%20%20Here's%20how%20to%20work%20around%20this%20limitation%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%0A%20%20%20%20print(%221.%20Create%20fresh%20LazyAnnData%20after%20filtering%3A%22)%0A%20%20%20%20print(%22%20%20%20%60%60%60python%22)%0A%20%20%20%20print(%22%20%20%20%23%20Instead%20of%20computing%20the%20filtered%20object%20directly%22)%0A%20%20%20%20print(%22%20%20%20%23%20Create%20a%20fresh%20LazyAnnData%20and%20apply%20filters%20again%22)%0A%20%20%20%20print(%22%20%20%20fresh_adata%20%3D%20read_slaf('path%2Fto%2Fdataset.slaf')%22)%0A%20%20%20%20print(%22%20%20%20%23%20Apply%20your%20filters%20to%20the%20fresh%20object%22)%0A%20%20%20%20print(%22%20%20%20sc.pp.filter_cells(fresh_adata%2C%20min_genes%3D200)%22)%0A%20%20%20%20print(%22%20%20%20sc.pp.filter_genes(fresh_adata%2C%20min_cells%3D30)%22)%0A%20%20%20%20print(%22%20%20%20%23%20Now%20compute()%20should%20work%22)%0A%20%20%20%20print(%22%20%20%20real_adata%20%3D%20fresh_adata.compute()%22)%0A%20%20%20%20print(%22%20%20%20%60%60%60%22)%0A%0A%20%20%20%20print(%22%5Cn2.%20Use%20.X.compute()%20for%20expression%20data%20only%3A%22)%0A%20%20%20%20print(%22%20%20%20%60%60%60python%22)%0A%20%20%20%20print(%22%20%20%20%23%20For%20expression%20matrix%20only%22)%0A%20%20%20%20print(%22%20%20%20expression_matrix%20%3D%20adata.X.compute()%22)%0A%20%20%20%20print(%22%20%20%20%23%20For%20metadata%20only%22)%0A%20%20%20%20print(%22%20%20%20cell_metadata%20%3D%20adata.obs%22)%0A%20%20%20%20print(%22%20%20%20gene_metadata%20%3D%20adata.var%22)%0A%20%20%20%20print(%22%20%20%20%60%60%60%22)%0A%0A%20%20%20%20print(%22%5Cn3.%20Use%20SQL%20queries%20for%20complex%20filtering%3A%22)%0A%20%20%20%20print(%22%20%20%20%60%60%60python%22)%0A%20%20%20%20print(%22%20%20%20%23%20Use%20SLAF's%20SQL%20interface%20for%20complex%20filtering%22)%0A%20%20%20%20print(%22%20%20%20filtered_cells%20%3D%20slaf.filter_cells(n_genes_by_counts%3D'%3E%3D500')%22)%0A%20%20%20%20print(%22%20%20%20filtered_genes%20%3D%20slaf.filter_genes(highly_variable%3DTrue)%22)%0A%20%20%20%20print(%22%20%20%20%60%60%60%22)%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%23%206.%20Performance%20Characteristics%0A%0A%20%20%20%20Let's%20examine%20SLAF's%20performance%20characteristics%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(slaf)%3A%0A%20%20%20%20def%20examine_performance_characteristics()%3A%0A%20%20%20%20%20%20%20%20import%20time%0A%0A%20%20%20%20%20%20%20%20print(%22%E2%9A%A1%20Performance%20Characteristics%22)%0A%20%20%20%20%20%20%20%20print(%22%3D%22%20*%2035)%0A%0A%20%20%20%20%20%20%20%20%23%20Test%20cell%20filtering%20performance%0A%20%20%20%20%20%20%20%20print(%22%5Cn1.%20Cell%20filtering%20performance%3A%22)%0A%0A%20%20%20%20%20%20%20%20%23%20SLAF%20SQL%20approach%0A%20%20%20%20%20%20%20%20start_time%20%3D%20time.time()%0A%20%20%20%20%20%20%20%20_%20%3D%20slaf.query(%22SELECT%20COUNT(*)%20FROM%20cells%20WHERE%20n_genes_by_counts%20%3E%20500%22)%0A%20%20%20%20%20%20%20%20slaf_time%20%3D%20time.time()%20-%20start_time%0A%20%20%20%20%20%20%20%20print(f%22%20%20%20SLAF%20SQL%3A%20%7Bslaf_time%3A.4f%7Ds%22)%0A%0A%20%20%20%20%20%20%20%20%23%20SLAF%20convenience%20method%0A%20%20%20%20%20%20%20%20start_time%20%3D%20time.time()%0A%20%20%20%20%20%20%20%20_%20%3D%20slaf.filter_cells(n_genes_by_counts%3D%22%3E500%22)%0A%20%20%20%20%20%20%20%20slaf_filter_time%20%3D%20time.time()%20-%20start_time%0A%20%20%20%20%20%20%20%20print(f%22%20%20%20SLAF%20filter%20method%3A%20%7Bslaf_filter_time%3A.4f%7Ds%22)%0A%0A%20%20%20%20%20%20%20%20%23%20Test%20expression%20aggregation%0A%20%20%20%20%20%20%20%20print(%22%5Cn2.%20Expression%20aggregation%20performance%3A%22)%0A%0A%20%20%20%20%20%20%20%20%23%20SLAF%20SQL%20aggregation%0A%20%20%20%20%20%20%20%20start_time%20%3D%20time.time()%0A%20%20%20%20%20%20%20%20_%20%3D%20slaf.query(%22SELECT%20AVG(value)%20FROM%20expression%22)%0A%20%20%20%20%20%20%20%20slaf_agg_time%20%3D%20time.time()%20-%20start_time%0A%20%20%20%20%20%20%20%20print(f%22%20%20%20SLAF%20SQL%20aggregation%3A%20%7Bslaf_agg_time%3A.4f%7Ds%22)%0A%0A%20%20%20%20%20%20%20%20%23%20Lazy%20AnnData%20approach%20(when%20computed)%0A%20%20%20%20%20%20%20%20print(%22%20%20%20Lazy%20AnnData%3A%20Operations%20stored%2C%20computed%20on%20demand%22)%0A%0A%20%20%20%20examine_performance_characteristics()%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%23%208.%20Quick%20ML%20Training%20Example%0A%0A%20%20%20%20SLAF%20provides%20efficient%20tokenization%20and%20DataLoader%20for%20ML%20training%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(slaf)%3A%0A%20%20%20%20def%20demonstrate_ml_training()%3A%0A%20%20%20%20%20%20%20%20from%20slaf.ml.dataloaders%20import%20SLAFDataLoader%0A%20%20%20%20%20%20%20%20from%20slaf.ml.tokenizers%20import%20SLAFTokenizer%0A%0A%20%20%20%20%20%20%20%20print(%22%F0%9F%8E%AF%20Quick%20ML%20Training%20Example%22)%0A%20%20%20%20%20%20%20%20print(%22%3D%22%20*%2035)%0A%0A%20%20%20%20%20%20%20%20%23%20Initialize%20tokenizer%0A%20%20%20%20%20%20%20%20tokenizer%20%3D%20SLAFTokenizer(%0A%20%20%20%20%20%20%20%20%20%20%20%20slaf_array%3Dslaf%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tokenizer_type%3D%22geneformer%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20vocab_size%3D2000%2C%20%20%23%20Dataset%20has%20%3C2000%20genes%0A%20%20%20%20%20%20%20%20%20%20%20%20n_expression_bins%3D20%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20vocab_info%20%3D%20tokenizer.get_vocab_info()%0A%20%20%20%20%20%20%20%20print(f%22%E2%9C%85%20Tokenizer%3A%20%7Bvocab_info%5B'vocab_size'%5D%7D%20vocabulary%20size%22)%0A%0A%20%20%20%20%20%20%20%20%23%20Create%20DataLoader%0A%20%20%20%20%20%20%20%20dataloader%20%3D%20SLAFDataLoader(%0A%20%20%20%20%20%20%20%20%20%20%20%20slaf_array%3Dslaf%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tokenizer_type%3D%22geneformer%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20batch_size%3D16%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20max_genes%3D100%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20vocab_size%3D2000%2C%20%20%23%20Dataset%20has%20%3C2000%20genes%0A%20%20%20%20%20%20%20%20%20%20%20%20n_expression_bins%3D20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20n_epochs%3D1%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%23%20Get%20first%20batch%0A%20%20%20%20%20%20%20%20batch%20%3D%20next(iter(dataloader))%0A%20%20%20%20%20%20%20%20print(f%22%E2%9C%85%20DataLoader%3A%20%7Bbatch%5B'input_ids'%5D.shape%7D%20tensors%22)%0A%20%20%20%20%20%20%20%20print(f%22%20%20%20Cell%20IDs%3A%20%7Blen(batch%5B'cell_ids'%5D)%7D%20cells%22)%0A%0A%20%20%20%20%20%20%20%20return%20tokenizer%2C%20dataloader%0A%0A%20%20%20%20tokenizer%2C%20dataloader%20%3D%20demonstrate_ml_training()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%23%20Summary%0A%0A%20%20%20%20**What%20you've%20learned%3A**%0A%0A%20%20%20%201.%20**SQL%20Schema**%3A%20SLAF%20stores%20data%20in%203%20tables%20(cells%2C%20genes%2C%20expression)%20that%20you%20can%20query%20directly%0A%20%20%20%202.%20**SQL%20Power**%3A%20Direct%20SQL%20access%20for%20complex%20queries%20and%20aggregations%0A%20%20%20%203.%20**Convenience%20Methods**%3A%20Easy%20filtering%20with%20%60filter_cells()%60%20and%20%60filter_genes()%60%0A%20%20%20%204.%20**Lazy%20AnnData**%3A%20Scanpy-compatible%20interface%20with%20lazy%20evaluation%0A%20%20%20%205.%20**Lazy%20Preprocessing**%3A%20scanpy%20functions%20that%20work%20lazily%0A%20%20%20%206.%20**Performance**%3A%20SQL-level%20performance%20for%20data%20operations%0A%20%20%20%207.%20**Explicit%20Computation**%3A%20Use%20%60.compute()%60%20to%20convert%20lazy%20objects%20to%20native%20Python%20objects%0A%20%20%20%208.%20**ML%20Training**%3A%20Ready%20for%20ML%20training%20with%20efficient%20tokenization%20and%20DataLoader%0A%0A%20%20%20%20**Next%20Steps%3A**%0A%0A%20%20%20%20-%20**02-lazy-processing.py**%3A%20Deep%20dive%20into%20lazy%20evaluation%20capabilities%0A%20%20%20%20-%20**03-ml-training-pipeline.py**%3A%20Complete%20ML%20training%20workflows%20with%20streaming%20DataLoader%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
f420862965668ca55143d738a4ed3b45771961f58065efa9947ffd226c9664b5