Skip to content
Snippets Groups Projects

add --json option and associated logic

Merged Taylor Hanayik requested to merge FEAT/add-json-output-format into master
All threads resolved!
7 files
+ 268
365
Compare changes
  • Side-by-side
  • Inline
Files
7
+ 11
39
@@ -53,6 +53,8 @@ options = [
{"option": "-v", "expected": "1000 1000.000000"},
{"option": "-V", "expected": "1000 1000.000000"},
{"option": "-m", "expected": "0.495922"},
{"option": "-m -a", "expected": "0.495922"},
{"option": "-m -a -n", "expected": "0.495922"},
{"option": "-M", "expected": "0.495922"},
{"option": "-s", "expected": "0.290744"},
{"option": "-S", "expected": "0.290744"},
@@ -63,6 +65,12 @@ options = [
{"option": "-C", "expected": "4.498706 4.545873 4.613188"},
{"option": "-p 50", "expected": "0.482584"},
{"option": "-P 50", "expected": "0.482584"},
{"option": "-m -h 10 -c", "expected": "0.495922 101.000000 \n99.000000 \n108.000000 \n107.000000 \n102.000000 \n93.000000 \n99.000000 \n88.000000 \n95.000000 \n108.000000 \n 4.498706 4.545873 4.613188"},
{"option": "-m -h 10", "expected": "0.495922 101.000000 \n99.000000 \n108.000000 \n107.000000 \n102.000000 \n93.000000 \n99.000000 \n88.000000 \n95.000000 \n108.000000"},
{"option": "-h 10", "expected": "101.000000 \n99.000000 \n108.000000 \n107.000000 \n102.000000 \n93.000000 \n99.000000 \n88.000000 \n95.000000 \n108.000000"},
{"option": "-H 10 0 1", "expected": "98.000000 \n102.000000 \n107.000000 \n108.000000 \n102.000000 \n93.000000 \n99.000000 \n88.000000 \n95.000000 \n108.000000"},
{"option": "-l 0.25 -u 0.75 -m", "expected": "0.495150"},
{"option": "-d imageForDiff.nii.gz -m", "expected": "0.000000"},
]
# create a list of tests and expected results
@@ -82,55 +90,24 @@ tests_with_preoptions = [
"preoptions": "-K",
"mask": "mask.nii.gz",
"options": "-m",
"expected": "0.948930 \n0.947671 \n1.003258 \n1.010696",
"expected": "1.078044 \n1.028827 \n0.986428 \n1.020869",
},
{
"preoptions": "-t -K",
"mask": "mask.nii.gz",
"options": "-m",
"expected": "0.459736 \n0.476035 \n0.504080 \n0.549485 \n0.489194 \n0.471636 \n0.499178 \n0.461211",
"expected": "0.526682 \n0.515652 \n0.492337 \n0.511661 \n0.551362 \n0.513176 \n0.494091 \n0.509208",
},
{
"preoptions": "-t",
"mask": "",
"options": "-m",
"expected": "0.496675 \n0.487950",
"expected": "0.503236 \n0.504035",
},
]
# taken from fslchpixdim test
def create_image(shape, pixdim):
pixdim = list(pixdim)
data = np.random.random(shape).astype(np.float32)
hdr = nib.Nifti1Header()
hdr.set_data_dtype(np.float32)
hdr.set_data_shape(shape)
hdr.set_zooms(pixdim[:len(shape)])
return nib.Nifti1Image(data, np.eye(4), hdr)
def create_mask(input_img, n_rois=4):
'''
Create a mask image from the input image.
The mask image will have n_rois different values with random sizes randomly placed in the image.
'''
data = input_img.get_fdata()
mask = np.zeros_like(data)
for i in range(n_rois + 1):
roi_size = np.random.randint(1, data.size)
roi_value = i
roi = np.random.choice(data.size, roi_size, replace=False)
mask.flat[roi] = roi_value
return nib.Nifti1Image(mask, input_img.affine, input_img.header)
def test_fslstats():
imgfile = 'test.nii.gz'
shape = (10,10,10)
img = create_image(shape, [1] * len(shape))
img.to_filename(imgfile)
mask = create_mask(img)
mask.to_filename('mask.nii.gz')
# run the tests witoout preoptions
for test in tests:
@@ -151,9 +128,6 @@ def test_fslstats():
# run the tests with preoptions
imgfile = 'test_4d.nii.gz'
shape = (10,10,10, 2)
img = create_image(shape, [1] * len(shape))
img.to_filename(imgfile)
for test in tests_with_preoptions:
cmd = f"fslstats {test['preoptions']} {test['mask']} {imgfile} {test['options']}"
# remove any double spaces from empty test options
@@ -175,6 +149,4 @@ def test_fslstats():
if __name__ == "__main__":
if len(sys.argv) > 1:
os.chdir(sys.argv[1])
test_fslstats()
Loading