|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import os |
| 5 | +import tempfile |
| 6 | + |
| 7 | +from tractolearn.tractoio.file_extensions import ( |
| 8 | + DictDataExtensions, |
| 9 | + TractogramExtensions, |
| 10 | + fname_sep, |
| 11 | +) |
| 12 | +from tractolearn.tractoio.utils import ( |
| 13 | + compose_filename, |
| 14 | + filter_filenames, |
| 15 | + identify_missing_bundle, |
| 16 | + identify_missing_tractogram, |
| 17 | + read_data_from_json_file, |
| 18 | + save_data_to_json_file, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def test_identify_missing_bundle(tmp_path): |
| 23 | + |
| 24 | + with tempfile.NamedTemporaryFile( |
| 25 | + suffix=fname_sep + DictDataExtensions.JSON.value, dir=tmp_path |
| 26 | + ) as f: |
| 27 | + |
| 28 | + # Target bundle names |
| 29 | + bundle_names = ["CC_Fr_1", "CST_L", "AC"] |
| 30 | + |
| 31 | + bundle_data = dict({"CC_Fr_1": 1.0, "CST_L": 2.0, "AC": 3.0}) |
| 32 | + expected = sorted(set(bundle_names).difference(bundle_data.keys())) |
| 33 | + |
| 34 | + save_data_to_json_file(bundle_data, f.name) |
| 35 | + |
| 36 | + data = read_data_from_json_file( |
| 37 | + os.path.join(tmp_path, os.listdir(tmp_path)[0]) |
| 38 | + ) |
| 39 | + |
| 40 | + obtained = identify_missing_bundle(data, bundle_names) |
| 41 | + |
| 42 | + assert obtained == expected |
| 43 | + |
| 44 | + with tempfile.NamedTemporaryFile( |
| 45 | + suffix=fname_sep + DictDataExtensions.JSON.value, dir=tmp_path |
| 46 | + ) as f: |
| 47 | + |
| 48 | + # Target bundle names |
| 49 | + bundle_names = ["Cu", "PrCu"] |
| 50 | + |
| 51 | + bundle_data = dict({"Cu": 2.0}) |
| 52 | + expected = sorted(set(bundle_names).difference(bundle_data.keys())) |
| 53 | + |
| 54 | + save_data_to_json_file(bundle_data, f.name) |
| 55 | + |
| 56 | + data = read_data_from_json_file( |
| 57 | + os.path.join(tmp_path, os.listdir(tmp_path)[0]) |
| 58 | + ) |
| 59 | + |
| 60 | + obtained = identify_missing_bundle(data, bundle_names) |
| 61 | + |
| 62 | + assert obtained == expected |
| 63 | + |
| 64 | + |
| 65 | +def test_identify_missing_tractogram(tmp_path): |
| 66 | + |
| 67 | + # Target bundle names |
| 68 | + bundle_names = ["CC_Fr_1", "CST_L", "AC"] |
| 69 | + |
| 70 | + # Create some files in the temporary path |
| 71 | + file_rootnames = ["CC_Fr_1", "CC_Fr_2", "AC"] |
| 72 | + fnames = [ |
| 73 | + compose_filename(tmp_path, val, TractogramExtensions.TRK.value) |
| 74 | + for val in file_rootnames |
| 75 | + ] |
| 76 | + [open(val, "w") for val in fnames] |
| 77 | + |
| 78 | + expected = sorted(set(bundle_names).difference(file_rootnames)) |
| 79 | + |
| 80 | + obtained = identify_missing_tractogram(tmp_path, bundle_names) |
| 81 | + |
| 82 | + assert obtained == expected |
| 83 | + |
| 84 | + # Target bundle names |
| 85 | + bundle_names = ["Cu"] |
| 86 | + expected = [ |
| 87 | + compose_filename(tmp_path, val, TractogramExtensions.TRK.value) |
| 88 | + for val in bundle_names |
| 89 | + ] |
| 90 | + |
| 91 | + # Create some files in the temporary path |
| 92 | + file_rootnames = ["Cu", "PrCu"] |
| 93 | + fnames = [ |
| 94 | + compose_filename(tmp_path, val, TractogramExtensions.TRK.value) |
| 95 | + for val in file_rootnames |
| 96 | + ] |
| 97 | + [open(val, "w") for val in fnames] |
| 98 | + |
| 99 | + expected = sorted(set(bundle_names).difference(file_rootnames)) |
| 100 | + |
| 101 | + obtained = identify_missing_tractogram(tmp_path, bundle_names) |
| 102 | + |
| 103 | + assert obtained == expected |
| 104 | + |
| 105 | + |
| 106 | +def test_filter_fnames(tmp_path): |
| 107 | + |
| 108 | + # Target bundle names |
| 109 | + bundle_names = ["CC_Fr_1", "CST_L", "AC"] |
| 110 | + |
| 111 | + # Create some files in the temporary path |
| 112 | + file_rootnames = ["CC_Fr_1", "CC_Fr_2", "AC"] |
| 113 | + fnames = [ |
| 114 | + compose_filename(tmp_path, val, TractogramExtensions.TRK.value) |
| 115 | + for val in file_rootnames |
| 116 | + ] |
| 117 | + [open(val, "w") for val in fnames] |
| 118 | + |
| 119 | + expected_rootnames = ["AC", "CC_Fr_1"] |
| 120 | + expected = [ |
| 121 | + compose_filename(tmp_path, val, TractogramExtensions.TRK.value) |
| 122 | + for val in expected_rootnames |
| 123 | + ] |
| 124 | + |
| 125 | + obtained = filter_filenames(tmp_path, bundle_names) |
| 126 | + |
| 127 | + assert obtained == expected |
| 128 | + |
| 129 | + # Target bundle names |
| 130 | + bundle_names = ["Cu"] |
| 131 | + expected = [ |
| 132 | + compose_filename(tmp_path, val, TractogramExtensions.TRK.value) |
| 133 | + for val in bundle_names |
| 134 | + ] |
| 135 | + |
| 136 | + # Create some files in the temporary path |
| 137 | + file_rootnames = ["Cu", "PrCu"] |
| 138 | + fnames = [ |
| 139 | + compose_filename(tmp_path, val, TractogramExtensions.TRK.value) |
| 140 | + for val in file_rootnames |
| 141 | + ] |
| 142 | + [open(val, "w") for val in fnames] |
| 143 | + |
| 144 | + expected_rootnames = ["Cu"] |
| 145 | + expected = [ |
| 146 | + compose_filename(tmp_path, val, TractogramExtensions.TRK.value) |
| 147 | + for val in expected_rootnames |
| 148 | + ] |
| 149 | + |
| 150 | + obtained = filter_filenames(tmp_path, bundle_names) |
| 151 | + |
| 152 | + assert obtained == expected |
0 commit comments