forked from TangneyT/RepresentationLearning_FRBs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerate_FRB.py
More file actions
65 lines (54 loc) · 1.7 KB
/
Generate_FRB.py
File metadata and controls
65 lines (54 loc) · 1.7 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 17 18:18:28 2023
@author: tesstangney
"""
import matplotlib as plt
import numpy
import torch
def Generate_FRB(VAE, SCALER, LV, num, save = False):
'''
Generates fake data using the decoder of a trained VAE, need to change the
path of the saved images depending where you want them saved and also the
length of the noise to be added
Parameters
------
VAE : Objest
The trained model
SCALER : sklearn.preprocessing StandardScaler object
The scaler that was used to normalise the training data
LV : int
The number of Latent Variables
num : int
The number of fake FRBs to generate
save : Boolean
if True save the plots to Fake_FRB folder
Returns
-------
Fake_LC : list
List of generated timeseries data
'''
VAE.eval()
Fake_LC = []
for i in range(num):
draw = torch.randn(1, LV)
with torch.no_grad():
gen_3d = VAE.decoder(draw)
gen_2d = gen_3d.squeeze(1).detach().numpy()
FRB = SCALER.inverse_transform(gen_2d)
FRB = FRB.squeeze()
'''Add Gaussian noise'''
noise = numpy.random.normal(0,1,127)
frb_noise = FRB+noise
Fake_LC.append(FRB)
plt.figure()
plt.ylabel('S/N')
plt.xlabel('Time[ms]')
plt.plot(frb_noise, label='With Gaussian noise', c='orange')
plt.plot(FRB, c='black')
plt.legend()
if save == True:
plt.savefig('Fake_FRB'+str(i),
dpi = 250)
return Fake_LC