-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_compat_layer.py
More file actions
executable file
·383 lines (272 loc) · 8.25 KB
/
demo_compat_layer.py
File metadata and controls
executable file
·383 lines (272 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""Demo: PyBIDS Compatibility Layer - Basic Usage"""
import marimo
__generated_with = "0.23.3"
app = marimo.App(width="medium")
@app.cell(hide_code=True)
def _(mo):
mo.md("""
# PyBIDS Compatibility Layer - Basic Demo
This notebook demonstrates the bids2table compatibility layer providing a
drop-in replacement for PyBIDS with 20x better performance.
## What You'll Learn
1. How to initialize a BIDSLayout (with automatic caching)
2. Query files by BIDS entities
3. Access metadata with BIDS inheritance
4. Use Query sentinels (OPTIONAL, NONE, ANY)
5. Get BIDSFile objects with entity parsing
6. Compare compat layer vs native b2t approaches
""")
return
@app.cell
def _():
import marimo as mo
from pathlib import Path
# Find the test dataset - using ds114 (multi-session, multiple tasks)
repo_root = Path.cwd().parent if Path.cwd().name == 'examples' else Path.cwd()
dataset_path = repo_root / 'datasets' / 'bids-examples' / 'ds114'
if not dataset_path.exists():
mo.md(f"⚠️ Dataset not found: {dataset_path}")
mo.stop()
mo.md(f"✅ Using dataset: `{dataset_path.name}` (multi-session, multi-task)")
return Path, dataset_path, mo
@app.cell(hide_code=True)
def _(mo):
mo.md("""
---
## 1. Initialize BIDSLayout
The compatibility layer provides the same API as PyBIDS but uses
bids2table under the hood for fast indexing.
""")
return
@app.cell
def _(dataset_path):
from bids2table_compat import BIDSLayout
# Initialize (automatically creates parquet cache)
layout = BIDSLayout(dataset_path, validate=False)
print(f"Indexed: {layout}")
print(f"Cache: {layout.cache_path}")
return (layout,)
@app.cell(hide_code=True)
def _(mo):
mo.md("""
---
## 2. Get Subjects and Sessions
Enumerate subjects and sessions in the dataset.
""")
return
@app.cell
def _(layout, mo):
subjects = layout.get_subjects()
sessions = layout.get_sessions()
tasks = sorted(layout.df['task'].dropna().unique().tolist())
mo.md(f"""
**Subjects**: `{subjects[:5]}...` ({len(subjects)} total)
**Sessions**: `{sessions if sessions else 'None (single-session dataset)'}`
**Tasks**: `{tasks}`
""")
return (subjects,)
@app.cell(hide_code=True)
def _(mo):
mo.md("""
---
## 3. Query Files by Entity
Use `.get()` to query files with BIDS entity filters.
""")
return
@app.cell
def _(layout, mo, subjects):
# Query files for first subject
subject = subjects[0]
files = layout.get(subject=subject, return_type='filename')
mo.md(f"""
**Query**: Files for `sub-{subject}`
**Found**: {len(files)} files
**Examples**:
""")
return files, subject
@app.cell
def _(Path, files, mo):
# Show first few files
mo.md("\n".join([f"- `{Path(f).name}`" for f in files[:3]]))
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""
---
## 4. Query with Multiple Filters
Combine multiple entity filters to narrow down results.
""")
return
@app.cell
def _(layout, mo, subject):
# Query anatomical files for subject
anat_files = layout.get(
subject=subject,
datatype='anat',
return_type='filename'
)
mo.md(f"""
**Query**: `sub-{subject}` + `datatype='anat'`
**Found**: {len(anat_files)} anatomical files
**Files**:
""")
return (anat_files,)
@app.cell
def _(Path, anat_files, mo):
mo.md("\n".join([f"- `{Path(f).name}`" for f in anat_files]))
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""
---
## 5. Query with Query.OPTIONAL
Handle datasets that may or may not have sessions.
""")
return
@app.cell
def _(layout, mo, subject):
from bids2table_compat import Query
# Query allowing any session (or no session)
files_optional = layout.get(
subject=subject,
session=Query.OPTIONAL,
return_type='filename'
)
mo.md(f"""
**Query**: `sub-{subject}` + `session=Query.OPTIONAL`
**Found**: {len(files_optional)} files (allows any/no session)
""")
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""
---
## 6. Get BIDSFile Objects with Entities
Use `return_type='file'` to get BIDSFile objects that can parse entities.
""")
return
@app.cell
def _(Path, layout, mo):
# Get BIDSFile objects
bids_files = layout.get(suffix='bold', return_type='file')
if bids_files:
example_file = bids_files[0]
entities = example_file.get_entities()
entity_text = f"""
**Query**: `suffix='bold'` + `return_type='file'`
**Found**: {len(bids_files)} BOLD files
**Example file**: `{Path(example_file.path).name}`
**Entities**: `{entities}`
"""
else:
example_file = None
entities = None
entity_text = "⚠️ No BOLD files found in dataset"
mo.md(entity_text)
return (bids_files,)
@app.cell(hide_code=True)
def _(mo):
mo.md("""
---
## 7. Get Metadata with BIDS Inheritance
Load JSON sidecar metadata following BIDS inheritance rules.
""")
return
@app.cell
def _(bids_files, layout, mo):
if bids_files and len(bids_files) > 0:
metadata = layout.get_metadata(bids_files[0].path)
metadata_keys = list(metadata.keys())
md_text = f"""
**Metadata keys**: `{metadata_keys[:5]}...`
"""
if 'RepetitionTime' in metadata:
md_text += f"**RepetitionTime**: `{metadata['RepetitionTime']}` seconds"
else:
md_text = "⚠️ No files to show metadata for"
metadata = {}
metadata_keys = []
mo.md(md_text)
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""
---
## 8. Cache Information
The compat layer uses parquet caching for fast reloading.
""")
return
@app.cell
def _(layout, mo):
cache_size_kb = layout.cache_path.stat().st_size / 1024 if layout.cache_path.exists() else 0
mo.md(f"""
**Cache path**: `{layout.cache_path}`
**Cache exists**: `{layout.cache_path.exists()}`
**Cache size**: `{cache_size_kb:.1f} KB`
💡 **Note**: Parquet cache is ~100x smaller than PyBIDS SQLite cache!
""")
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""
---
## Comparison: Compat Layer vs Native b2t
The compat layer provides a familiar API, but you can also use native
bids2table DataFrames for maximum flexibility.
""")
return
@app.cell
def _(mo):
mo.md("""
### Compat Layer (Drop-in Replacement)
```python
from bids2table_compat import BIDSLayout
layout = BIDSLayout('/data/dataset')
subjects = layout.get_subjects()
files = layout.get(subject='01', suffix='T1w')
```
✅ Familiar PyBIDS API
✅ Easy migration (change 1 line)
✅ Same query patterns
---
### Native b2t (Best Performance)
```python
import bids2table as b2t
import pandas as pd
tab = b2t.index_dataset('/data/dataset')
df = tab.to_pandas()
subjects = sorted(df['sub'].unique())
files = df[(df['sub'] == '01') & (df['suffix'] == 'T1w')]['path'].tolist()
```
✅ More flexible (full pandas)
✅ Slightly faster queries
✅ Direct DataFrame access
---
### Which to Use?
- **Migrating from PyBIDS?** → Use compat layer
- **New project?** → Consider native b2t
- **Need complex queries?** → Native b2t gives full pandas power
- **Want simplicity?** → Compat layer is cleaner
""")
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""
---
## Summary
This demo showed the basic features of the compatibility layer:
✅ **BIDSLayout initialization** with automatic caching
✅ **Subject/session enumeration**
✅ **File querying** with entity filters
✅ **Query sentinels** (OPTIONAL, NONE, ANY)
✅ **BIDSFile objects** with entity parsing
✅ **Metadata loading** with BIDS inheritance
✅ **Parquet caching** for performance
**Next**: See `demo_custom_entities.py` for advanced patterns including
custom entities (the templateflow pattern).
---
📚 **Documentation**: See `MIGRATION_GUIDE.md` for complete migration instructions.
""")
return
if __name__ == "__main__":
app.run()