Skip to content

Commit 1ea2565

Browse files
committed
Add TileDB rerun test to reproduce bug.
1 parent f311120 commit 1ea2565

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

test/test_voxels_cli.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import nibabel as nb
66
import numpy as np
77
import pytest
8+
import tiledb
89

910
from modelarrayio.cli.main import main as modelarrayio_main
1011

@@ -281,3 +282,76 @@ def test_nifti_to_h5_scalar_columns_writes_prefixed_outputs(tmp_path, monkeypatc
281282
with h5py.File(beta_out, 'r') as h5:
282283
assert 'voxels' in h5
283284
assert sorted(h5['scalars'].keys()) == ['beta']
285+
286+
287+
def _build_nifti_cohort(tmp_path):
288+
"""Create a minimal NIfTI cohort (group mask + 2 subjects) and return CLI args."""
289+
shape = (4, 4, 4)
290+
true_coords = [(0, 0, 0), (1, 1, 1), (2, 2, 2), (3, 3, 3)]
291+
292+
group_mask = np.zeros(shape, dtype=np.uint8)
293+
for coord in true_coords:
294+
group_mask[coord] = 1
295+
group_mask_file = tmp_path / 'group_mask.nii.gz'
296+
_make_nifti(group_mask).to_filename(group_mask_file)
297+
298+
rows = []
299+
for sidx in range(2):
300+
scalar = np.zeros(shape, dtype=np.float32)
301+
for i, j, k in true_coords:
302+
scalar[i, j, k] = float(i + j + k + sidx)
303+
scalar_file = tmp_path / f'sub-{sidx + 1}_scalar.nii.gz'
304+
mask_file = tmp_path / f'sub-{sidx + 1}_mask.nii.gz'
305+
_make_nifti(scalar).to_filename(scalar_file)
306+
_make_nifti(group_mask).to_filename(mask_file)
307+
rows.append(
308+
{
309+
'scalar_name': 'FA',
310+
'source_file': scalar_file.name,
311+
'source_mask_file': mask_file.name,
312+
}
313+
)
314+
315+
cohort_csv = tmp_path / 'cohort.csv'
316+
with cohort_csv.open('w', newline='') as f:
317+
writer = csv.DictWriter(f, fieldnames=['scalar_name', 'source_file', 'source_mask_file'])
318+
writer.writeheader()
319+
writer.writerows(rows)
320+
321+
return group_mask_file, cohort_csv
322+
323+
324+
def test_nifti_tiledb_fails_when_output_already_exists(tmp_path, monkeypatch):
325+
"""Regression test for https://github.com/PennLINC/ModelArrayIO/issues/39.
326+
327+
The TileDB backend should raise an error when the output directory already
328+
contains arrays from a previous run (tiledb.Array.create fails if the URI
329+
already exists).
330+
"""
331+
group_mask_file, cohort_csv = _build_nifti_cohort(tmp_path)
332+
out_tdb = tmp_path / 'out.tdb'
333+
monkeypatch.chdir(tmp_path)
334+
335+
cli_args = [
336+
'nifti-to-h5',
337+
'--group-mask-file',
338+
str(group_mask_file),
339+
'--cohort-file',
340+
str(cohort_csv),
341+
'--output',
342+
str(out_tdb),
343+
'--backend',
344+
'tiledb',
345+
'--compression',
346+
'gzip',
347+
]
348+
349+
# First run should succeed
350+
assert modelarrayio_main(cli_args) == 0
351+
assert out_tdb.exists()
352+
assert tiledb.object_type(str(out_tdb / 'scalars' / 'FA' / 'values')) is not None
353+
354+
# Second run to the same output directory should fail because the TileDB
355+
# arrays already exist (this is the bug reported in issue #39).
356+
with pytest.raises(tiledb.TileDBError, match='already exists'):
357+
modelarrayio_main(cli_args)

0 commit comments

Comments
 (0)