diff --git a/fslstats.cc b/fslstats.cc index 8e90fd22080a2e5a0917f1cccbf5eba447e2b46e..b29d9f934a274dd6ea98fb081eb499897d1c0c4b 100644 --- a/fslstats.cc +++ b/fslstats.cc @@ -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") { diff --git a/tests/fslstats/feedsRun b/tests/fslstats/feedsRun index a007f1dd74c02c6dcb61231246aa462a4af03f61..8e722db837e9a841e625dec2988cdc53e6b8505a 100755 --- a/tests/fslstats/feedsRun +++ b/tests/fslstats/feedsRun @@ -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() diff --git a/tests/fslstats/test_cogMM_c.json b/tests/fslstats/test_cogMM_c.json new file mode 100644 index 0000000000000000000000000000000000000000..758ec3c4ea87958f2b46bc64f2c2c4b4cc53a91a --- /dev/null +++ b/tests/fslstats/test_cogMM_c.json @@ -0,0 +1,15 @@ +{ + "volumes": [ + { + "indices": [ + { + "-c": [ + 4.505939956803816, + 4.486097425091172, + 4.488288600081377 + ] + } + ] + } + ] +} diff --git a/tests/fslstats/test_cogVox_C.json b/tests/fslstats/test_cogVox_C.json new file mode 100644 index 0000000000000000000000000000000000000000..1b6b7b0042df12032ef3fba38c5279efd005e671 --- /dev/null +++ b/tests/fslstats/test_cogVox_C.json @@ -0,0 +1,15 @@ +{ + "volumes": [ + { + "indices": [ + { + "-C": [ + 4.505939956803816, + 4.486097425091172, + 4.488288600081377 + ] + } + ] + } + ] +} diff --git a/tests/fslstats/test_diff_d_imageForDiff.nii.gz_m.json b/tests/fslstats/test_diff_d_imageForDiff.nii.gz_m.json new file mode 100644 index 0000000000000000000000000000000000000000..76d250a99ba24c780ad8177fbd3f31c5cbc82767 --- /dev/null +++ b/tests/fslstats/test_diff_d_imageForDiff.nii.gz_m.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-m": 0.0077142266542650755 + } + ] + } + ] +} diff --git a/tests/fslstats/test_entropyNonzero_E.json b/tests/fslstats/test_entropyNonzero_E.json new file mode 100644 index 0000000000000000000000000000000000000000..9573a6e2e3ea79b23560e254157c2d15a12dfff5 --- /dev/null +++ b/tests/fslstats/test_entropyNonzero_E.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-E": 0.9468094676591793 + } + ] + } + ] +} diff --git a/tests/fslstats/test_entropy_e.json b/tests/fslstats/test_entropy_e.json new file mode 100644 index 0000000000000000000000000000000000000000..407d1864d013c94088173ee18a6cca522b736898 --- /dev/null +++ b/tests/fslstats/test_entropy_e.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-e": 0.9468094676591793 + } + ] + } + ] +} diff --git a/tests/fslstats/test_histLimits_H_10_0_1.json b/tests/fslstats/test_histLimits_H_10_0_1.json new file mode 100644 index 0000000000000000000000000000000000000000..4ce81ec78aa6c653dcdec7d31c72c7d604b95fd4 --- /dev/null +++ b/tests/fslstats/test_histLimits_H_10_0_1.json @@ -0,0 +1,22 @@ +{ + "volumes": [ + { + "indices": [ + { + "-H": [ + 193.0, + 220.0, + 194.0, + 201.0, + 190.0, + 184.0, + 191.0, + 199.0, + 200.0, + 228.0 + ] + } + ] + } + ] +} diff --git a/tests/fslstats/test_hist_h_10.json b/tests/fslstats/test_hist_h_10.json new file mode 100644 index 0000000000000000000000000000000000000000..2e210d2ba04254ede95cb789bb3e6f50b807a412 --- /dev/null +++ b/tests/fslstats/test_hist_h_10.json @@ -0,0 +1,22 @@ +{ + "volumes": [ + { + "indices": [ + { + "-h": [ + 198.0, + 216.0, + 194.0, + 200.0, + 190.0, + 184.0, + 191.0, + 199.0, + 199.0, + 229.0 + ] + } + ] + } + ] +} diff --git a/tests/fslstats/test_maxCoord_x.json b/tests/fslstats/test_maxCoord_x.json new file mode 100644 index 0000000000000000000000000000000000000000..1a2017282d339384c75ffb8cba333700341fdad7 --- /dev/null +++ b/tests/fslstats/test_maxCoord_x.json @@ -0,0 +1,15 @@ +{ + "volumes": [ + { + "indices": [ + { + "-x": [ + 4, + 8, + 7 + ] + } + ] + } + ] +} diff --git a/tests/fslstats/test_meanHistCog_m_h_10_c.json b/tests/fslstats/test_meanHistCog_m_h_10_c.json new file mode 100644 index 0000000000000000000000000000000000000000..58380fdb02573fc29b8dded4ae7edd8f7a3c8701 --- /dev/null +++ b/tests/fslstats/test_meanHistCog_m_h_10_c.json @@ -0,0 +1,28 @@ +{ + "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 + } + ] + } + ] +} diff --git a/tests/fslstats/test_meanHist_m_h_10.json b/tests/fslstats/test_meanHist_m_h_10.json new file mode 100644 index 0000000000000000000000000000000000000000..c8316e5e320510f9a8d269ea71760ffa426277c4 --- /dev/null +++ b/tests/fslstats/test_meanHist_m_h_10.json @@ -0,0 +1,23 @@ +{ + "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 + } + ] + } + ] +} diff --git a/tests/fslstats/test_meanNonzero_M.json b/tests/fslstats/test_meanNonzero_M.json new file mode 100644 index 0000000000000000000000000000000000000000..e7892ba4cd9fdd18868824b58ea99ed18f3cc330 --- /dev/null +++ b/tests/fslstats/test_meanNonzero_M.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-M": 0.5036357601464115 + } + ] + } + ] +} diff --git a/tests/fslstats/test_mean_m.json b/tests/fslstats/test_mean_m.json new file mode 100644 index 0000000000000000000000000000000000000000..3763ba8a708cddda21719ed8afdd6df152158fdd --- /dev/null +++ b/tests/fslstats/test_mean_m.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-m": 0.5036357601464115 + } + ] + } + ] +} diff --git a/tests/fslstats/test_mean_m_a.json b/tests/fslstats/test_mean_m_a.json new file mode 100644 index 0000000000000000000000000000000000000000..3763ba8a708cddda21719ed8afdd6df152158fdd --- /dev/null +++ b/tests/fslstats/test_mean_m_a.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-m": 0.5036357601464115 + } + ] + } + ] +} diff --git a/tests/fslstats/test_mean_m_a_n.json b/tests/fslstats/test_mean_m_a_n.json new file mode 100644 index 0000000000000000000000000000000000000000..3763ba8a708cddda21719ed8afdd6df152158fdd --- /dev/null +++ b/tests/fslstats/test_mean_m_a_n.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-m": 0.5036357601464115 + } + ] + } + ] +} diff --git a/tests/fslstats/test_minCoord_X.json b/tests/fslstats/test_minCoord_X.json new file mode 100644 index 0000000000000000000000000000000000000000..aa72b2ef8e9255249a9e19a6ffa9fc1c0ed2870d --- /dev/null +++ b/tests/fslstats/test_minCoord_X.json @@ -0,0 +1,15 @@ +{ + "volumes": [ + { + "indices": [ + { + "-X": [ + 8, + 1, + 6 + ] + } + ] + } + ] +} diff --git a/tests/fslstats/test_notRobust_R.json b/tests/fslstats/test_notRobust_R.json new file mode 100644 index 0000000000000000000000000000000000000000..b5b7acd3e86b3f8de0302bcdc47f6ca7453a63dd --- /dev/null +++ b/tests/fslstats/test_notRobust_R.json @@ -0,0 +1,14 @@ +{ + "volumes": [ + { + "indices": [ + { + "-R": [ + 0.00036734374589286745, + 0.9998085498809814 + ] + } + ] + } + ] +} diff --git a/tests/fslstats/test_percentileNonzero_P_50.json b/tests/fslstats/test_percentileNonzero_P_50.json new file mode 100644 index 0000000000000000000000000000000000000000..47aeceb4aa12b07fd9f9d94fc334e32df0da3c83 --- /dev/null +++ b/tests/fslstats/test_percentileNonzero_P_50.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-P": 0.5013243556022644 + } + ] + } + ] +} diff --git a/tests/fslstats/test_percentile_p_50.json b/tests/fslstats/test_percentile_p_50.json new file mode 100644 index 0000000000000000000000000000000000000000..a105e7c94c23eb9c88bce4dd7f1c4be225ab2924 --- /dev/null +++ b/tests/fslstats/test_percentile_p_50.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-p": 0.5013243556022644 + } + ] + } + ] +} diff --git a/tests/fslstats/test_preopIndexMaskMeanTimeSeries_t_K_m.json b/tests/fslstats/test_preopIndexMaskMeanTimeSeries_t_K_m.json new file mode 100644 index 0000000000000000000000000000000000000000..9f0e754f72c2f899b2d531512c8dd4294a6b4822 --- /dev/null +++ b/tests/fslstats/test_preopIndexMaskMeanTimeSeries_t_K_m.json @@ -0,0 +1,36 @@ +{ + "volumes": [ + { + "indices": [ + { + "-m": 0.5266819608311916 + }, + { + "-m": 0.5156517274568186 + }, + { + "-m": 0.49233699466459285 + }, + { + "-m": 0.5116614613561304 + } + ] + }, + { + "indices": [ + { + "-m": 0.5513619920395724 + }, + { + "-m": 0.5131755568141885 + }, + { + "-m": 0.49409089087108476 + }, + { + "-m": 0.5092079986783641 + } + ] + } + ] +} diff --git a/tests/fslstats/test_preopIndexMaskMean_K_m.json b/tests/fslstats/test_preopIndexMaskMean_K_m.json new file mode 100644 index 0000000000000000000000000000000000000000..fd62a2f93b208692544c6a564450f7913aa16133 --- /dev/null +++ b/tests/fslstats/test_preopIndexMaskMean_K_m.json @@ -0,0 +1,20 @@ +{ + "volumes": [ + { + "indices": [ + { + "-m": 1.078043952870764 + }, + { + "-m": 1.0288272842710071 + }, + { + "-m": 0.9864278855356776 + }, + { + "-m": 1.0208694600344945 + } + ] + } + ] +} diff --git a/tests/fslstats/test_preopMeanTimeSeries_t_m.json b/tests/fslstats/test_preopMeanTimeSeries_t_m.json new file mode 100644 index 0000000000000000000000000000000000000000..b2b481f0415c2d2ae1060aaa139593ce3b1db9fe --- /dev/null +++ b/tests/fslstats/test_preopMeanTimeSeries_t_m.json @@ -0,0 +1,18 @@ +{ + "volumes": [ + { + "indices": [ + { + "-m": 0.503236498022452 + } + ] + }, + { + "indices": [ + { + "-m": 0.504035022270371 + } + ] + } + ] +} diff --git a/tests/fslstats/test_robust_r.json b/tests/fslstats/test_robust_r.json new file mode 100644 index 0000000000000000000000000000000000000000..9810ac1677f1508fa2a4fb09a4e53c2da26217c6 --- /dev/null +++ b/tests/fslstats/test_robust_r.json @@ -0,0 +1,14 @@ +{ + "volumes": [ + { + "indices": [ + { + "-r": [ + 0.018357284367084503, + 0.982818067073822 + ] + } + ] + } + ] +} diff --git a/tests/fslstats/test_roi_w.json b/tests/fslstats/test_roi_w.json new file mode 100644 index 0000000000000000000000000000000000000000..d023769141f6bc830c86436e7d26d730e69e1074 --- /dev/null +++ b/tests/fslstats/test_roi_w.json @@ -0,0 +1,20 @@ +{ + "volumes": [ + { + "indices": [ + { + "-w": [ + 0, + 10, + 0, + 10, + 0, + 10, + 0, + 2 + ] + } + ] + } + ] +} diff --git a/tests/fslstats/test_stdevNonzero_S.json b/tests/fslstats/test_stdevNonzero_S.json new file mode 100644 index 0000000000000000000000000000000000000000..cdb0547b2196d2c346452c752aca43fd0e581c37 --- /dev/null +++ b/tests/fslstats/test_stdevNonzero_S.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-S": 0.2949828225878523 + } + ] + } + ] +} diff --git a/tests/fslstats/test_stdev_s.json b/tests/fslstats/test_stdev_s.json new file mode 100644 index 0000000000000000000000000000000000000000..b473581a8be07219141475c501720b8a2f605684 --- /dev/null +++ b/tests/fslstats/test_stdev_s.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-s": 0.29498282199940035 + } + ] + } + ] +} diff --git a/tests/fslstats/test_threshold_l_0.25_u_0.75_m.json b/tests/fslstats/test_threshold_l_0.25_u_0.75_m.json new file mode 100644 index 0000000000000000000000000000000000000000..6712e13e07ae3d558abd26e1e223374c7fdf754c --- /dev/null +++ b/tests/fslstats/test_threshold_l_0.25_u_0.75_m.json @@ -0,0 +1,11 @@ +{ + "volumes": [ + { + "indices": [ + { + "-m": 0.49646345829352356 + } + ] + } + ] +} diff --git a/tests/fslstats/test_volumeNonzero_V.json b/tests/fslstats/test_volumeNonzero_V.json new file mode 100644 index 0000000000000000000000000000000000000000..9a1ed332a56087d76f6d4a1d414828c2b64dc003 --- /dev/null +++ b/tests/fslstats/test_volumeNonzero_V.json @@ -0,0 +1,14 @@ +{ + "volumes": [ + { + "indices": [ + { + "-V": [ + 2000.0, + 2000.0 + ] + } + ] + } + ] +} diff --git a/tests/fslstats/test_volume_v.json b/tests/fslstats/test_volume_v.json new file mode 100644 index 0000000000000000000000000000000000000000..5e4fb299ead33a17ff8540d8d425fbaca09972cc --- /dev/null +++ b/tests/fslstats/test_volume_v.json @@ -0,0 +1,14 @@ +{ + "volumes": [ + { + "indices": [ + { + "-v": [ + 2000.0, + 2000.0 + ] + } + ] + } + ] +}