export
lacuna.io.export
¶
Export utilities for analysis results.
Provides convenient functions for exporting SubjectData analysis results to various formats (CSV, TSV, JSON) for downstream analysis and visualization.
batch_export_to_csv(mask_data_list, output_path, analysis_name=None, include_metadata=True)
¶
Export results from multiple SubjectData objects to a single CSV.
Combines results from multiple subjects into one CSV file with each row representing one subject. Ideal for group-level statistical analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_data_list
|
list[SubjectData]
|
List of SubjectData objects (typically from batch processing) |
required |
output_path
|
str or Path
|
Output CSV file path |
required |
analysis_name
|
str
|
Specific analysis to export. If None, exports all results. |
None
|
include_metadata
|
bool
|
Include subject metadata as columns |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to created CSV file |
Raises:
| Type | Description |
|---|---|
ValueError
|
If list is empty or subjects have no results |
Examples:
>>> from lacuna.io import load_bids_dataset, batch_export_to_csv
>>> from lacuna.analysis import RegionalDamage
>>>
>>> # Load multiple subjects
>>> dataset = load_bids_dataset("bids_dir")
>>> analysis = RegionalDamage()
>>>
>>> # Run analysis on all subjects
>>> results = [analysis.run(lesion) for lesion in dataset.values()]
>>>
>>> # Export to single CSV for group analysis
>>> batch_export_to_csv(results, "group_results.csv")
Notes
- All subjects must have the same analysis results structure
- Missing values are filled with NaN
- Each row represents one subject
- Columns are shared across all subjects
Source code in src/lacuna/io/export.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
batch_export_to_tsv(mask_data_list, output_path, analysis_name=None, include_metadata=True)
¶
Export results from multiple SubjectData objects to a single TSV.
Identical to batch_export_to_csv but uses tab delimiter. TSV is preferred in neuroimaging for BIDS compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_data_list
|
list[SubjectData]
|
List of SubjectData objects |
required |
output_path
|
str or Path
|
Output TSV file path |
required |
analysis_name
|
str
|
Specific analysis to export |
None
|
include_metadata
|
bool
|
Include subject metadata as columns |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to created TSV file |
Raises:
| Type | Description |
|---|---|
ValueError
|
If list is empty or subjects have no results |
Examples:
>>> from lacuna.io import batch_export_to_tsv
>>>
>>> # Export group results to BIDS-compatible TSV
>>> batch_export_to_tsv(results, "group_results.tsv")
See Also
batch_export_to_csv : CSV batch export
Source code in src/lacuna/io/export.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
export_provenance_to_json(mask_data, output_path, indent=2)
¶
Export provenance data to JSON format.
Saves the complete processing history and metadata as a standalone JSON file for reproducibility and audit trails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_data
|
SubjectData
|
SubjectData object with provenance data |
required |
output_path
|
str or Path
|
Output JSON file path |
required |
indent
|
int
|
JSON indentation for readability (0 for compact) |
2
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to created JSON file |
Raises:
| Type | Description |
|---|---|
ValueError
|
If mask_data has no provenance data |
Examples:
>>> from lacuna.io import export_provenance_to_json
>>>
>>> # Export provenance history
>>> export_provenance_to_json(result, "provenance.json")
>>>
>>> # Export compact JSON
>>> export_provenance_to_json(result, "prov.json", indent=0)
Notes
Provenance includes: - Source file paths - Processing steps (transformations, analyses) - Software versions - Timestamps - Parameters used for each operation
Source code in src/lacuna/io/export.py
export_results_to_csv(mask_data, output_path, analysis_name=None, include_metadata=True)
¶
Export analysis results to CSV format.
Converts nested results dictionary to a flat CSV structure suitable for statistical analysis or visualization in external tools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_data
|
SubjectData
|
SubjectData object with analysis results |
required |
output_path
|
str or Path
|
Output CSV file path |
required |
analysis_name
|
str
|
Specific analysis to export. If None, exports all results. Example: "RegionalDamage", "ParcelAggregation" |
None
|
include_metadata
|
bool
|
Include subject metadata (subject_id, session_id, etc.) as columns |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to created CSV file |
Raises:
| Type | Description |
|---|---|
ValueError
|
If mask_data has no results or specified analysis not found |
Examples:
>>> from lacuna import SubjectData
>>> from lacuna.analysis import RegionalDamage
>>> from lacuna.io import export_results_to_csv
>>>
>>> lesion = SubjectData.from_nifti("lesion.nii.gz")
>>> analysis = RegionalDamage()
>>> result = analysis.run(lesion)
>>>
>>> # Export all results
>>> export_results_to_csv(result, "results.csv")
>>>
>>> # Export specific analysis
>>> export_results_to_csv(result, "damage.csv", analysis_name="RegionalDamage")
Notes
- Results are flattened: nested dicts become columns with dot notation
- Example: {"ParcelAggregation": {"region1": 0.5}} becomes columns "ParcelAggregation.region1" with value 0.5
- Multiple analyses create multiple columns
- Metadata columns (if included): subject_id, session_id, coordinate_space
Source code in src/lacuna/io/export.py
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 | |
export_results_to_json(mask_data, output_path, analysis_name=None, include_metadata=True, include_provenance=False, indent=2)
¶
Export analysis results to JSON format.
Creates a JSON file with analysis results, optionally including metadata and provenance. Useful for web applications or further programmatic processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_data
|
SubjectData
|
SubjectData object with analysis results |
required |
output_path
|
str or Path
|
Output JSON file path |
required |
analysis_name
|
str
|
Specific analysis to export. If None, exports all results. |
None
|
include_metadata
|
bool
|
Include subject metadata in JSON |
True
|
include_provenance
|
bool
|
Include provenance data in JSON |
False
|
indent
|
int
|
JSON indentation for readability (0 for compact) |
2
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to created JSON file |
Raises:
| Type | Description |
|---|---|
ValueError
|
If mask_data has no results or specified analysis not found |
Examples:
>>> from lacuna.io import export_results_to_json
>>>
>>> # Export all results with metadata
>>> export_results_to_json(result, "results.json")
>>>
>>> # Export specific analysis with full provenance
>>> export_results_to_json(
... result,
... "damage_full.json",
... analysis_name="RegionalDamage",
... include_provenance=True
... )
>>>
>>> # Compact JSON for web APIs
>>> export_results_to_json(result, "api_response.json", indent=0)
Notes
JSON structure: { "metadata": {...}, # If include_metadata=True "results": {...}, # Analysis results "provenance": {...} # If include_provenance=True }
Source code in src/lacuna/io/export.py
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 384 385 386 387 | |
export_results_to_tsv(mask_data, output_path, analysis_name=None, include_metadata=True)
¶
Export analysis results to TSV (tab-separated values) format.
Identical to export_results_to_csv but uses tab delimiter. TSV is preferred in neuroimaging for BIDS compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_data
|
SubjectData
|
SubjectData object with analysis results |
required |
output_path
|
str or Path
|
Output TSV file path |
required |
analysis_name
|
str
|
Specific analysis to export. If None, exports all results. |
None
|
include_metadata
|
bool
|
Include subject metadata as columns |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to created TSV file |
Raises:
| Type | Description |
|---|---|
ValueError
|
If mask_data has no results or specified analysis not found |
Examples:
>>> from lacuna.io import export_results_to_tsv
>>>
>>> # Export to TSV (BIDS-compatible format)
>>> export_results_to_tsv(result, "results.tsv")
>>>
>>> # Export specific analysis without metadata
>>> export_results_to_tsv(
... result,
... "atlas_only.tsv",
... analysis_name="ParcelAggregation",
... include_metadata=False
... )
See Also
export_results_to_csv : CSV export (identical but comma-delimited)
Source code in src/lacuna/io/export.py
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 | |