-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_generator.py
146 lines (123 loc) · 5.8 KB
/
test_generator.py
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
142
143
144
145
'''
Author: Lennon Seiders
'''
import h5py
import random
import numpy as np
from scipy import ndimage
from scipy.stats import gaussian_kde
import imaging
import os
import cv2
import skimage
import background_generator
import math
'''
Functions for generating and testing synthetic image data. Images are saved in .h5 files
along with a list of primary and secondary peak coordinates.
Set parameter values in main() and run this script in order to generate synthetic images.
'''
# Load peak value data from lab dataset
pkvals = np.loadtxt('pkvals.csv', dtype=np.int16, converters=float)
ints_kde = gaussian_kde(pkvals)
def sample(kde):
x = kde.resample(1)[0]
while x < np.min(pkvals):
x = kde.resample(1)[0]
return x[0]
# Generate synthetic based on input parameters
def generate_radial_peaks_image(peaks_per_level, newImageFile, p_second, p_tail, size_mult):
size = (4096, 4096)
image = np.zeros(size, dtype=np.int16)
center = (2063, 2059)
radii_first = [1068, 1160, 1220, 1599, 1904]
radii_second = [1083, 1171, 1232, 1623, 1932]
# Create a 2d peak array. Higher peak intensity correlated with larger size.
def create_peak_structure(coord, size, peakIntensity):
mdl = int(size/2)
chunk = np.zeros((9,9))
peak = np.random.randint(0, peakIntensity, (3,3), dtype=np.int16)
peak[1][1] = peakIntensity
if size > 4: peak = np.pad(peak, (mdl-1,), mode='linear_ramp', end_values=np.random.randint(0, np.min(peak)+1, dtype=np.int16))
peak = peak * cv2.getStructuringElement(cv2.MORPH_ELLIPSE, ksize=(size,size))
chunk[4-(mdl):5+(mdl),4-(mdl):5+(mdl)] = peak
image[coord[0]-4:coord[0]+5, coord[1]-4:coord[1]+5] = chunk
# Place peaks on each radius. Peaks_per_level primary peaks + secondary peaks generated with probability p_second.
primary_peaks = []
secondary_peaks = []
for i, radius in enumerate(radii_first):
for _ in range(peaks_per_level):
angle = np.random.uniform(0, 2 * np.pi)
x = int(center[0] + radius * np.cos(angle))
y = int(center[1] + radius * np.sin(angle))
intensity = sample(ints_kde)
size = 3 + int(ints_kde.integrate_box_1d(0, intensity) * 2 * size_mult) * 2
create_peak_structure([x,y], size, intensity)
primary_peaks.append((x, y))
if random.random() < p_second:
dx = int(center[0] + radii_second[i] * np.cos(angle))
dy = int(center[1] + radii_second[i] * np.sin(angle))
create_peak_structure([dx,dy], size, intensity)
secondary_peaks.append((dx, dy))
if (random.random() < p_tail) and math.dist([x,y],[dx,dy]) < 13:
rr, cc = skimage.draw.line(x,y,dx,dy)
mid_x = int((rr[0] - rr[-1]) / 2)
mid_y = int((cc[0] - cc[-1]) / 2)
image[mid_x, mid_y] = intensity/(math.dist([x,y],[dx,dy])+(7-size))
a = np.geomspace(image[x,y], image[mid_x, mid_y], num=int((len(rr)/2) + 1))
b = np.geomspace(image[mid_x, mid_y], image[dx, dy], num=int(len(rr)/2))
tailvalues = np.hstack((a, b))
for j in range(0, len(rr)):
image[rr[j], [cc[j]]] = tailvalues[j]
# Gaussian smoothing and adding background noise
image = np.maximum(ndimage.gaussian_filter(image, sigma=0.7), image)
try: bg_f = np.array(h5py.File('background.h5','r')['synthetic_bg'])
except:
background_generator.main()
bg_f = np.array(h5py.File('background.h5','r')['synthetic_bg'])
finally: image += bg_f
# Set synthetic image file
newImageFile.create_group("imageseries")
newImageFile['imageseries']['images'] = image
newImageFile['imageseries']['primaryPeaks'] = primary_peaks
newImageFile['imageseries']['secondaryPeaks'] = secondary_peaks
return
# Create synthetic image file, generate image and peak lists
def generate_images(num_images, peaks_per_level=4, p_second=0.5, p_tail=0.75, size_mult=1):
print("generating", num_images, "diffraction images...")
if not os.path.exists('synthetic_images'):
os.makedirs('synthetic_images')
for i in range(num_images):
filename = 'synthetic_image_' + format(i+1, '04') + '.h5'
if os.path.isfile('synthetic_images/' + filename):
os.remove('synthetic_images/' + filename)
newImageFile = h5py.File('synthetic_images/' + filename,'a')
generate_radial_peaks_image(peaks_per_level, newImageFile, p_second, p_tail, size_mult)
# Test to run different filtering methods on synthetic data. Keeps track of peaks missed and false positives.
def test_synthetic_images(thres, directory='synthetic_images'):
missed = 0
false_positive = 0
correct = 0
total_pks = 0
i = 0
for filename in os.listdir(directory):
i += 1
f = os.path.join(directory, filename)
x, numpks = imaging.find_peaks_2d_test(f, thres)
total_pks += numpks
if x > 0: false_positive += x
elif x < 0: missed -= x
else: correct += 1
print(total_pks, 'total peaks tested in', i, 'images')
print('correct images:', correct)
print('missed:', missed)
print('incorrectly classified:', false_positive)
def main():
num_images = 10 # number of synthetic diffraction images to be generated
peaks_per_level = 4 # peaks per radius level
p_second = 0.5 # probability of secondary peaks being generated for each primary peak
p_tail = 0.5 # probaility of a pair of peaks having additional "tail" noise
size_mult = 1 # size multiplier. higher value (ex. 1.3) results in a higher chance of generating large peaks
generate_images(num_images, peaks_per_level, p_second, p_tail, size_mult)
if __name__ == "__main__":
main()