Showing
1 changed file
with
94 additions
and
0 deletions
src/common/data_utils.py
0 → 100644
1 | +import numpy | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | +def find_closest(value, array): | ||
6 | + array = numpy.asarray(array) | ||
7 | + idx = (numpy.abs(array - value)).argmin() | ||
8 | + return array[idx] | ||
9 | + | ||
10 | + | ||
11 | +def frame_drop(scan, frame_keep_style="random", frame_keep_fraction=1, frame_dim=1, impute="drop"): | ||
12 | + if frame_keep_fraction >= 1: | ||
13 | + return scan | ||
14 | + | ||
15 | + n = scan.shape[frame_dim] | ||
16 | + if frame_keep_style == "random": | ||
17 | + frames_to_keep = int(numpy.floor(n * frame_keep_fraction)) | ||
18 | + indices = numpy.random.permutation(numpy.arange(n))[:frames_to_keep] | ||
19 | + elif frame_keep_style == "ordered": | ||
20 | + k = int(numpy.ceil(1 / frame_keep_fraction)) | ||
21 | + indices = numpy.arange(0, n, k) | ||
22 | + # pick every k'th frame | ||
23 | + else: | ||
24 | + raise Exception("Wrong frame drop style") | ||
25 | + | ||
26 | + if impute == "zeros": | ||
27 | + if frame_dim == 1: | ||
28 | + t = numpy.zeros((1, n, 1, 1)) | ||
29 | + t[:, indices] = 1 | ||
30 | + return scan * t | ||
31 | + if frame_dim == 2: | ||
32 | + t = numpy.zeros((1, 1, n, 1)) | ||
33 | + t[:, :, indices] = 1 | ||
34 | + return scan * t | ||
35 | + if frame_dim == 3: | ||
36 | + t = numpy.zeros((1, 1, 1, n)) | ||
37 | + t[:, :, :, indices] = 1 | ||
38 | + return scan * t | ||
39 | + elif impute == "fill": | ||
40 | + # fill with nearest available frame | ||
41 | + if frame_dim == 1: | ||
42 | + for i in range(scan.shape[1]): | ||
43 | + if i in indices: | ||
44 | + pass | ||
45 | + scan[:, i, :, :] = scan[:, find_closest(i, indices), :, :] | ||
46 | + return scan | ||
47 | + | ||
48 | + if frame_dim == 2: | ||
49 | + for i in range(scan.shape[1]): | ||
50 | + if i in indices: | ||
51 | + pass | ||
52 | + scan[:, :, i, :] = scan[:, :, find_closest(i, indices), :] | ||
53 | + return scan | ||
54 | + | ||
55 | + if frame_dim == 3: | ||
56 | + for i in range(scan.shape[1]): | ||
57 | + if i in indices: | ||
58 | + pass | ||
59 | + scan[:, :, :, i] = scan[:, :, :, find_closest(i, indices)] | ||
60 | + return scan | ||
61 | + elif impute == "noise": | ||
62 | + noise = numpy.random.uniform(high=scan.max(), low=scan.min(), size=scan.shape) | ||
63 | + | ||
64 | + if frame_dim == 1: | ||
65 | + t = numpy.zeros((1, n, 1, 1)) | ||
66 | + t[:, indices] = 1 | ||
67 | + return scan + noise * (1 - t) | ||
68 | + if frame_dim == 2: | ||
69 | + t = numpy.zeros((1, 1, n, 1)) | ||
70 | + t[:, :, indices] = 1 | ||
71 | + return scan + noise * (1 - t) | ||
72 | + if frame_dim == 3: | ||
73 | + t = numpy.zeros((1, 1, 1, n)) | ||
74 | + t[:, :, :, indices] = 1 | ||
75 | + return scan + noise * (1 - t) | ||
76 | + else: # drop | ||
77 | + if frame_dim == 1: | ||
78 | + return scan[:, indices, :, :] | ||
79 | + if frame_dim == 2: | ||
80 | + return scan[:, :, indices, :] | ||
81 | + if frame_dim == 3: | ||
82 | + return scan[:, :, :, indices] | ||
83 | + return scan | ||
84 | + | ||
85 | + | ||
86 | + | ||
87 | + | ||
88 | +def gaussian_noise(scan, sigma=0.0): | ||
89 | + return scan + sigma * numpy.random.randn(*scan.shape) | ||
90 | + | ||
91 | + | ||
92 | +def intensity_scaling(scan): | ||
93 | + scale = 2 ** (2 * numpy.random.rand() - 1) # 2**(-1,1) | ||
94 | + return scan * scale |
-
Please register or login to post a comment