-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset_generator.py
More file actions
189 lines (153 loc) · 6.43 KB
/
Copy pathdataset_generator.py
File metadata and controls
189 lines (153 loc) · 6.43 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import torch
import numpy as np
import spectral_analysis as sa
from utilities import np_to_complex_pt, evolve_pt, evolve_np, shift_to_centre
import os
import test
from tqdm import tqdm
import shutil
from math import floor
class Generator():
def __init__(self, data_num, initial_intensity, FT_X, phase_len, device, dtype, max_order = 10, max_value = np.pi, target_type = "hermite", target_metadata = None):
self.data_num = data_num
self.initial_intensity = initial_intensity
self.FT_X = FT_X
self.intensity_len = len(initial_intensity)
self.phase_len = phase_len
self.max_order = max_order
self.max_value = max_value
self.device = device
self.dtype = dtype
self.target_type = target_type
self.target_metadata = target_metadata
def generate_and_save(self):
shutil.rmtree('data')
if not os.path.isdir("data"):
os.mkdir("data")
if not os.path.isdir("data/train_intensity"):
os.mkdir("data/train_intensity")
if not os.path.isdir("data/train_phase"):
os.mkdir("data/train_phase")
for example_num in tqdm(range(1, self.data_num + 1)):
intensity = self.pulse_gen()
np.savetxt("data/train_intensity/" + str(example_num) + ".csv", intensity)
def phase_gen(self):
def polynomial():
'''
Sum of regular polynomials.
'''
X = np.linspace(-1, 1, self.phase_len)
Y = np.zeros(self.phase_len)
max_order = 10
for order in range(max_order):
coef = np.random.uniform(low = -1, high = 1)
Y += coef*X**order
Y /= np.max(np.abs(Y))
return Y*np.random.uniform(1, 2*np.pi)
def absolute_like():
'''
Absolute value with random "middle".
'''
X = np.linspace(-1, 1, self.phase_len)
middle = 0#np.random.uniform(-0.5, 0.5)
X += middle
Y = np.abs(X)/np.max(np.abs(X))
return Y*np.random.uniform(1, 25*np.pi)
def absolute_like_multi():
'''
Sum of up to 5 absolute values.
'''
Y = absolute_like()
num = np.random.randint(5)
for i in range(num):
Y += absolute_like()
Y /= np.max(np.abs(Y))
return Y*np.random.uniform(1, 25*np.pi)
def step_like():
'''
Random constant value till some place, then another random constant value.
'''
a, b = np.random.uniform(low = 0, high = 2*np.pi, size = 2)
border = np.random.randint(low = floor(0.3*self.phase_len), high = floor(0.7*self.phase_len))
Y = np.concatenate([a*np.ones(border), b*np.ones(self.phase_len-border)])
Y /= np.max(np.abs(Y))
return Y
def step_like_multi():
'''
Sum of up to 5 step functions.
'''
Y = step_like()
num = np.random.randint(5)
for i in range(num):
Y += step_like()
Y /= np.max(np.abs(Y))
return Y
def hermite_like():
'''
Sum of the Hermite polynomials.
'''
max_order = 15
Y = np.zeros(self.phase_len)
for order in range(max_order):
coef = np.random.uniform(low = -1, high = 1)
Y += coef*sa.hermitian_pulse(pol_num = order,
bandwidth = [-1, 1],
centre = 0,
FWHM = 0.5,
num = self.phase_len,
broad = True).Y
Y /= np.max(np.abs(Y))
return Y*np.random.uniform(1, 5*np.pi)
# let's toss a coin, my friend
coin = np.random.choice(np.array([0,1,2,3,4,5]), size = 1, p = [1, 0.00, 0.00, 0.00, 0.00, 0.00])
if coin == 0:
phase = hermite_like()
elif coin == 1:
phase = polynomial()
elif coin == 2:
phase = step_like()
elif coin == 3:
phase = step_like_multi()
elif coin == 4:
phase = absolute_like()
elif coin == 5:
phase = absolute_like_multi()
else:
raise Exception("Your multidimensional coin has more dimensions that one could expect.")
'''
shift = np.random.uniform(-1, 1) # just random shift from -1 up to 1 THz
phase += 2*np.pi*shift*self.FT_X
'''
return phase
def pulse_gen(self):
'''
Returns tuple (intensity, phase), where phase is NONZERO part of phase used to evolve initial_intensity into intensity.
'''
intensity = self.initial_intensity.copy()
intensity = np.array([complex(intensity[i], 0) for i in range(len(intensity))])
probability = np.random.uniform(0, 1)
if probability < 0: # phases from the phase generator
phase_significant = self.phase_gen()
intensity = evolve_np(intensity, phase_significant, dtype = self.dtype)
elif self.target_type == "exponential":
intensity = np.flip(np.exp(np.linspace(-3, 3, self.intensity_len)) - np.exp(-3))
for i in range(0, floor(len(intensity)*1/3)):
intensity[i] = 0
elif self.target_type == "hermite":
order = np.random.randint(1, 2)
correction = 0#np.random.uniform(-0.4, 0.4)
intensity = sa.hermitian_pulse(pol_num = 1,
bandwidth = [190, 196],
centre = 193,
FWHM = 1 + correction,
num = len(intensity)).Y
elif self.target_type == "gauss":
intensity = sa.hermitian_pulse(pol_num = 0,
bandwidth = [self.target_metadata[2], self.target_metadata[3]],
centre = self.target_metadata[0],
FWHM = self.target_metadata[1],
num = len(intensity)).Y
intensity = shift_to_centre(intensity_to_shift = intensity,
intensity_ref = self.initial_intensity)
intensity = intensity/np.sum(intensity)
return np.sqrt(intensity)