|
| 1 | +import torch |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from torchkbnufft import (AdjKbNufft, AdjMriSenseNufft, KbInterpBack, |
| 5 | + KbInterpForw, KbNufft, MriSenseNufft) |
| 6 | + |
| 7 | +norm_tol = 1e-10 |
| 8 | + |
| 9 | + |
| 10 | +def test_kb_matching(): |
| 11 | + def check_tables(table1, table2): |
| 12 | + for ind, table in enumerate(table1): |
| 13 | + assert np.linalg.norm(table - table2[ind]) < norm_tol |
| 14 | + |
| 15 | + im_szs = [(256, 256), (10, 256, 256)] |
| 16 | + |
| 17 | + kbwidths = [2.34, 5] |
| 18 | + orders = [0, 2] |
| 19 | + |
| 20 | + for kbwidth in kbwidths: |
| 21 | + for order in orders: |
| 22 | + for im_sz in im_szs: |
| 23 | + smap = torch.randn(*((1,) + im_sz)) |
| 24 | + |
| 25 | + base_table = AdjKbNufft( |
| 26 | + im_sz, order=order, kbwidth=kbwidth).table |
| 27 | + |
| 28 | + cur_table = KbNufft(im_sz, order=order, kbwidth=kbwidth).table |
| 29 | + check_tables(base_table, cur_table) |
| 30 | + |
| 31 | + cur_table = KbInterpBack( |
| 32 | + im_sz, order=order, kbwidth=kbwidth).table |
| 33 | + check_tables(base_table, cur_table) |
| 34 | + |
| 35 | + cur_table = KbInterpForw( |
| 36 | + im_sz, order=order, kbwidth=kbwidth).table |
| 37 | + check_tables(base_table, cur_table) |
| 38 | + |
| 39 | + cur_table = MriSenseNufft( |
| 40 | + smap, im_sz, order=order, kbwidth=kbwidth).table |
| 41 | + check_tables(base_table, cur_table) |
| 42 | + |
| 43 | + cur_table = AdjMriSenseNufft( |
| 44 | + smap, im_sz, order=order, kbwidth=kbwidth).table |
| 45 | + check_tables(base_table, cur_table) |
| 46 | + |
| 47 | + |
| 48 | +def test_2d_init_inputs(): |
| 49 | + # all object initializations have assertions |
| 50 | + # this should result in an error if any dimensions don't match |
| 51 | + |
| 52 | + # test 2d scalar inputs |
| 53 | + im_sz = (256, 256) |
| 54 | + smap = torch.randn(*((1,) + im_sz)) |
| 55 | + grid_sz = (512, 512) |
| 56 | + n_shift = (128, 128) |
| 57 | + numpoints = 6 |
| 58 | + table_oversamp = 2**10 |
| 59 | + kbwidth = 2.34 |
| 60 | + order = 0 |
| 61 | + norm = 'None' |
| 62 | + |
| 63 | + ob = KbInterpForw( |
| 64 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 65 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order) |
| 66 | + ob = KbInterpBack( |
| 67 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 68 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order) |
| 69 | + |
| 70 | + ob = KbNufft( |
| 71 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 72 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 73 | + ob = AdjKbNufft( |
| 74 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 75 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 76 | + |
| 77 | + ob = MriSenseNufft( |
| 78 | + smap=smap, im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 79 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 80 | + ob = AdjMriSenseNufft( |
| 81 | + smap=smap, im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 82 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 83 | + |
| 84 | + # test 2d tuple inputs |
| 85 | + im_sz = (256, 256) |
| 86 | + smap = torch.randn(*((1,) + im_sz)) |
| 87 | + grid_sz = (512, 512) |
| 88 | + n_shift = (128, 128) |
| 89 | + numpoints = (6, 6) |
| 90 | + table_oversamp = (2**10, 2**10) |
| 91 | + kbwidth = (2.34, 2.34) |
| 92 | + order = (0, 0) |
| 93 | + norm = 'None' |
| 94 | + |
| 95 | + ob = KbInterpForw( |
| 96 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 97 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order) |
| 98 | + ob = KbInterpBack( |
| 99 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 100 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order) |
| 101 | + |
| 102 | + ob = KbNufft( |
| 103 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 104 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 105 | + ob = AdjKbNufft( |
| 106 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 107 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 108 | + |
| 109 | + ob = MriSenseNufft( |
| 110 | + smap=smap, im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 111 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 112 | + ob = AdjMriSenseNufft( |
| 113 | + smap=smap, im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 114 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 115 | + |
| 116 | + |
| 117 | +def test_3d_init_inputs(): |
| 118 | + # all object initializations have assertions |
| 119 | + # this should result in an error if any dimensions don't match |
| 120 | + |
| 121 | + # test 3d scalar inputs |
| 122 | + im_sz = (10, 256, 256) |
| 123 | + smap = torch.randn(*((1,) + im_sz)) |
| 124 | + grid_sz = (10, 512, 512) |
| 125 | + n_shift = (5, 128, 128) |
| 126 | + numpoints = 6 |
| 127 | + table_oversamp = 2**10 |
| 128 | + kbwidth = 2.34 |
| 129 | + order = 0 |
| 130 | + norm = 'None' |
| 131 | + |
| 132 | + ob = KbInterpForw( |
| 133 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 134 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order) |
| 135 | + ob = KbInterpBack( |
| 136 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 137 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order) |
| 138 | + |
| 139 | + ob = KbNufft( |
| 140 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 141 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 142 | + ob = AdjKbNufft( |
| 143 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 144 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 145 | + |
| 146 | + ob = MriSenseNufft( |
| 147 | + smap=smap, im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 148 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 149 | + ob = AdjMriSenseNufft( |
| 150 | + smap=smap, im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 151 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 152 | + |
| 153 | + # test 3d tuple inputs |
| 154 | + im_sz = (10, 256, 256) |
| 155 | + smap = torch.randn(*((1,) + im_sz)) |
| 156 | + grid_sz = (10, 512, 512) |
| 157 | + n_shift = (5, 128, 128) |
| 158 | + numpoints = (6, 6, 6) |
| 159 | + table_oversamp = (2**10, 2**10, 2**10) |
| 160 | + kbwidth = (2.34, 2.34, 2.34) |
| 161 | + order = (0, 0, 0) |
| 162 | + norm = 'None' |
| 163 | + |
| 164 | + ob = KbInterpForw( |
| 165 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 166 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order) |
| 167 | + ob = KbInterpBack( |
| 168 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 169 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order) |
| 170 | + |
| 171 | + ob = KbNufft( |
| 172 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 173 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 174 | + ob = AdjKbNufft( |
| 175 | + im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 176 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 177 | + |
| 178 | + ob = MriSenseNufft( |
| 179 | + smap=smap, im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 180 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
| 181 | + ob = AdjMriSenseNufft( |
| 182 | + smap=smap, im_size=im_sz, grid_size=grid_sz, n_shift=n_shift, numpoints=numpoints, |
| 183 | + table_oversamp=table_oversamp, kbwidth=kbwidth, order=order, norm=norm) |
0 commit comments