[d6d24a]: / Segmentation / tests / data_tests / test_data_gen.py

Download this file

142 lines (113 with data), 5.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def test_random_data_gen_2D():
from Segmentation.data_gen.toy_image_gen import Toy_Image
n_reps, n_classes = 10, 20
test_ti = Toy_Image(n_classes, 40, 40, 3)
for rep in range(n_reps):
for i in range(n_classes):
test_ti.set_colour_to_random_xy(i)
import matplotlib.pyplot as plt
plt.imshow(test_ti.image, cmap='jet')
img_path = "./Data/Tests_data/random_image_2d.png"
plt.savefig(img_path)
import os
assert os.path.isfile(img_path), "file does not exist"
os.remove(img_path)
def test_random_shapes_gen_2D():
from Segmentation.data_gen.toy_image_gen import Toy_Image
from random import randint
n_reps, n_classes = 10, 6
width, height = 40, 40
colour_channels = 3
test_ti = Toy_Image(n_classes, width, height, colour_channels)
for rep in range(n_reps):
for colour_idx in range(n_classes):
x, y = test_ti.get_random_xy()
rand_width = randint(1, int(test_ti.width / 8))
rand_height = randint(1, int(test_ti.height / 8))
rnd_i = randint(0, 4)
if rnd_i == 0:
test_ti.set_square_to_xy(x, y, rand_width, colour_idx)
elif rnd_i == 1:
test_ti.set_circle_to_xy(x, y, rand_width, colour_idx)
elif rnd_i == 2:
test_ti.set_rect_to_xy(x, y, rand_width, rand_height, colour_idx)
elif rnd_i == 3:
test_ti.set_ellipse_to_xy(x, y, rand_width, rand_height, colour_idx)
elif rnd_i == 4:
test_ti.set_colour_to_random_xy(colour_idx)
else:
raise Exception(f"Invalid {rnd_i}")
import matplotlib.pyplot as plt
plt.imshow(test_ti.image, cmap='jet')
img_path = "./Data/Tests_data/random_shapes_image_2d.png"
plt.savefig(img_path)
import os
assert os.path.isfile(img_path), "file does not exist"
os.remove(img_path)
def test_get_test_images():
from Segmentation.data_gen.toy_image_gen import get_test_images
n_images, n_reps, n_classes = 10, 4, 5
width, height = 30, 30
colour_channels = 3
images, one_hots = get_test_images(n_images, n_reps, n_classes,
width, height, colour_channels)
assert len(images) == n_images
assert len(one_hots) == n_images
import matplotlib.pyplot as plt
import os
for image in images:
plt.imshow(image, cmap='jet')
img_path = "./Data/Tests_data/random_shapes_images_2d.png"
plt.savefig(img_path)
assert os.path.isfile(img_path), "file does not exist"
os.remove(img_path)
def test_random_volumes_gen():
from Segmentation.data_gen.toy_volume_gen import Toy_Volume
from random import randint
n_reps, n_classes = 10, 6
width, height, depth = 15, 15, 15
colour_channels = 3
test_tv = Toy_Volume(n_classes, width, height, depth, colour_channels)
for rep in range(n_reps):
for colour_idx in range(n_classes):
x, y, z = test_tv.get_random_xyz()
rand_x_len = randint(1, int(test_tv.width / 4))
rand_y_len = randint(1, int(test_tv.height / 4))
rand_z_len = randint(1, int(test_tv.depth / 4))
rnd_i = randint(0, 4)
if rnd_i == 0:
test_tv.set_rect_cuboid_to_xyz(x, y, z,
rand_x_len, rand_y_len, rand_z_len,
colour_idx)
elif rnd_i == 1:
test_tv.set_ellipsoid_to_xyz(x, y, z,
rand_x_len, rand_y_len, rand_z_len,
colour_idx)
elif rnd_i == 2:
test_tv.set_cube_to_xyz(x, y, z,
rand_x_len,
colour_idx)
elif rnd_i == 3:
test_tv.set_sphere_to_xyz(x, y, z,
rand_x_len,
colour_idx)
elif rnd_i == 4:
test_tv.set_colour_to_random_xyz(colour_idx)
else:
raise Exception(f"Invalid {rnd_i}")
from Segmentation.data_gen.toy_volume_gen import plot_volume
plot_volume(test_tv.volume, False)
def test_get_test_volumes():
from Segmentation.data_gen.toy_volume_gen import get_test_volumes
n_volumes, n_reps, n_classes = 10, 4, 5
width, height, depth = 15, 15, 20
colour_channels = 3
volumes, one_hots = get_test_volumes(n_volumes, n_reps, n_classes,
width, height, depth, colour_channels)
assert len(volumes) == n_volumes
assert len(one_hots) == n_volumes
import matplotlib.pyplot as plt
import os
from Segmentation.data_gen.toy_volume_gen import plot_volume
for volume in volumes:
plot_volume(volume, False)