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!
30 files
+ 525
27
Compare changes
  • Side-by-side
  • Inline
Files
30
+ 88
25
@@ -46,31 +46,31 @@ import nibabel as nib
np.random.seed(0)
options = [
{"option": "-r", "expected": "0.018533 0.978824"},
{"option": "-R", "expected": "0.000546 0.999809"},
{"option": "-e", "expected": "0.906103"},
{"option": "-E", "expected": "0.906103"},
{"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"},
{"option": "-w", "expected": "0 10 0 10 0 10 0 1"},
{"option": "-x", "expected": "9 7 4"},
{"option": "-X", "expected": "8 2 1"},
{"option": "-c", "expected": "4.498706 4.545873 4.613188"},
{"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"},
{"name": "robust", "option": "-r", "expected": "0.018533 0.978824"},
{"name": "notRobust", "option": "-R", "expected": "0.000546 0.999809"},
{"name": "entropy", "option": "-e", "expected": "0.906103"},
{"name": "entropyNonzero", "option": "-E", "expected": "0.906103"},
{"name": "volume", "option": "-v", "expected": "1000 1000.000000"},
{"name": "volumeNonzero", "option": "-V", "expected": "1000 1000.000000"},
{"name": "mean", "option": "-m", "expected": "0.495922"},
{"name": "mean", "option": "-m -a", "expected": "0.495922"},
{"name": "mean", "option": "-m -a -n", "expected": "0.495922"},
{"name": "meanNonzero", "option": "-M", "expected": "0.495922"},
{"name": "stdev", "option": "-s", "expected": "0.290744"},
{"name": "stdevNonzero", "option": "-S", "expected": "0.290744"},
{"name": "roi","option": "-w", "expected": "0 10 0 10 0 10 0 1"},
{"name": "maxCoord", "option": "-x", "expected": "9 7 4"},
{"name": "minCoord", "option": "-X", "expected": "8 2 1"},
{"name": "cogMM", "option": "-c", "expected": "4.498706 4.545873 4.613188"},
{"name": "cogVox","option": "-C", "expected": "4.498706 4.545873 4.613188"},
{"name": "percentile", "option": "-p 50", "expected": "0.482584"},
{"name": "percentileNonzero","option": "-P 50", "expected": "0.482584"},
{"name": "meanHistCog", "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"},
{"name": "meanHist", "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"},
{"name": "hist", "option": "-h 10", "expected": "101.000000 \n99.000000 \n108.000000 \n107.000000 \n102.000000 \n93.000000 \n99.000000 \n88.000000 \n95.000000 \n108.000000"},
{"name": "histLimits", "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"},
{"name": "threshold", "option": "-l 0.25 -u 0.75 -m", "expected": "0.495150"},
{"name": "diff", "option": "-d imageForDiff.nii.gz -m", "expected": "0.000000"},
]
# create a list of tests and expected results
@@ -82,6 +82,7 @@ for option in options:
"mask": "",
"options": option["option"],
"expected": option["expected"],
"name": option["name"],
}
)
@@ -91,21 +92,49 @@ tests_with_preoptions = [
"mask": "mask.nii.gz",
"options": "-m",
"expected": "1.078044 \n1.028827 \n0.986428 \n1.020869",
"name": "preopIndexMaskMean",
},
{
"preoptions": "-t -K",
"mask": "mask.nii.gz",
"options": "-m",
"expected": "0.526682 \n0.515652 \n0.492337 \n0.511661 \n0.551362 \n0.513176 \n0.494091 \n0.509208",
"name": "preopIndexMaskMeanTimeSeries",
},
{
"preoptions": "-t",
"mask": "",
"options": "-m",
"expected": "0.503236 \n0.504035",
"name": "preopMeanTimeSeries",
},
]
json_tests = []
for option in options:
file_string = f"test_{option['name']}_{option['option'].replace(' ', '_')}.json"
file_string = file_string.replace("-", "")
json_tests.append(
{
"preoptions": "",
"mask": "",
"options": option["option"],
"expected": file_string,
}
)
for option in tests_with_preoptions:
file_string = f"test_{option['name']}_{option['preoptions'].replace(' ', '_')}_{option['options'].replace(' ', '_')}.json"
file_string = file_string.replace("-", "")
json_tests.append(
{
"preoptions": option["preoptions"],
"mask": option["mask"],
"options": option["options"],
"expected": file_string,
}
)
def test_fslstats():
imgfile = 'test.nii.gz'
@@ -121,8 +150,11 @@ def test_fslstats():
try:
assert output == test["expected"]
except AssertionError:
print('Failed test')
print(cmd)
print('Output')
print(output)
print('Expected')
print(test["expected"])
raise
@@ -147,6 +179,37 @@ def test_fslstats():
print(test["expected"])
raise
# run the json tests
for test in json_tests:
cmd = f"fslstats -json {test['preoptions']} {test['mask']} {imgfile} {test['options']}"
# remove any double spaces from empty test options
cmd = " ".join(cmd.split())
print(cmd, flush=True)
output = run.runfsl(cmd)
# try to load the expected json file
# if it exists, compare the output to the expected
try:
print(test["expected"])
with open(test["expected"], "r") as json_file:
# read the file as a string
expected = json_file.read()
try:
assert output == expected
except AssertionError:
print('Failed test')
print(cmd)
print('Output')
print(output)
print('Expected')
print(expected)
raise
except FileNotFoundError:
# if the expected json file doesn't exist, create it from the output
# usefull for creating new tests or the first time a test is run
with open(test["expected"], "w") as json_file:
json_file.write(output)
if __name__ == "__main__":
test_fslstats()
Loading