forked from FiodarM/InvDesignNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
produce_data.py
61 lines (52 loc) · 1.65 KB
/
produce_data.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
import gc
import numpy as np
from grating import Grating
import os
from tqdm import tqdm
batch_size = 1000
n_grating_layers = 15
n_freqs = 200
a = 1.
freqs = np.linspace(0.15 / a, 0.25 / a, n_freqs)
epsilon_Si = 13.491
epsilon_SiO2 = 2.085136
def save_samples(fname, samples):
n_samples = len(samples['R'])
if os.path.exists(fname):
with np.load(fname) as existing:
for k in samples.keys():
samples[k] = np.vstack((existing[k], samples[k]))
try:
np.savez(fname, **samples)
except KeyboardInterrupt:
pass
print("Saved {0} samples to {1}. The dataset contains {2} samples."
.format(n_samples, fname, len(samples['R'])))
fname = 'dataset.npz'
if __name__ == '__main__':
samples = dict(D=[], R=[])
try:
i = 0
with tqdm(total=batch_size, leave=False) as pbar:
while True:
D = np.random.random_sample(n_grating_layers)
gr = Grating(epsilon_Si, epsilon_SiO2, D)
R = gr.transmittivity(freqs)
i += 1
pbar.update(1)
samples['D'].append(D)
samples['R'].append(R)
if i == batch_size:
save_samples(fname, samples)
samples = dict(D=[], R=[])
gc.collect()
pbar.reset()
i = 0
except KeyboardInterrupt:
print('Interrupting calculation...')
answer = input('Do you want to save calculated data? y/[n]: ')
if answer == 'y':
save_samples(fname, samples)
finally:
print('Exiting script')
exit(0)