Skip to content
Snippets Groups Projects
Commit 6ed1dff6 authored by Taylor Hanayik's avatar Taylor Hanayik
Browse files

add json tests; code cleanup;

parent 475ab8aa
No related branches found
No related tags found
1 merge request!22add --json option and associated logic
Pipeline #19048 skipped
This commit is part of merge request !22. Comments created here will be created in the context of that merge request.
Showing
with 356 additions and 27 deletions
......@@ -199,11 +199,9 @@ int fmrib_main_float(int argc, char *argv[], const bool timeseriesMode, const st
generateNonZeroMask(mask, masknz, vol);
}
int narg(2);
bool last = false;
while (narg < argc)
{
last = !(narg < argc - 1);
string sarg(argv[narg]);
if (sarg == "-n")
{
......
......@@ -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()
{
"volumes": [
{
"indices": [
{
"-c": [
4.505939956803816,
4.486097425091172,
4.488288600081377
]
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-C": [
4.505939956803816,
4.486097425091172,
4.488288600081377
]
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-m": 0.0077142266542650755
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-E": 0.9468094676591793
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-e": 0.9468094676591793
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-H": [
193.0,
220.0,
194.0,
201.0,
190.0,
184.0,
191.0,
199.0,
200.0,
228.0
]
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-h": [
198.0,
216.0,
194.0,
200.0,
190.0,
184.0,
191.0,
199.0,
199.0,
229.0
]
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-x": [
4,
8,
7
]
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-c": [
4.505939956803816,
4.486097425091172,
4.488288600081377
],
"-h": [
198.0,
216.0,
194.0,
200.0,
190.0,
184.0,
191.0,
199.0,
199.0,
229.0
],
"-m": 0.5036357601464115
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-h": [
198.0,
216.0,
194.0,
200.0,
190.0,
184.0,
191.0,
199.0,
199.0,
229.0
],
"-m": 0.5036357601464115
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-M": 0.5036357601464115
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-m": 0.5036357601464115
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-m": 0.5036357601464115
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-m": 0.5036357601464115
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-X": [
8,
1,
6
]
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-R": [
0.00036734374589286745,
0.9998085498809814
]
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-P": 0.5013243556022644
}
]
}
]
}
{
"volumes": [
{
"indices": [
{
"-p": 0.5013243556022644
}
]
}
]
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment