def test_simulation_time(num_simulations=100): start_time = time() for i in trange(num_simulations): event = SimulatedFRB() event.simulateFRB() end_time = time() print( f"{num_simulations} sims completed in {end_time - start_time} seconds")
def plot_injectedFRB(SNR=10, seed=np.random.randint(0, 5000)): np.random.seed(seed) fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(10, 8)) event = SimulatedFRB() event.scintillate() # add .FRB attribute to event event.roll() event.fractional_bandwidth() signal = event.injectFRB(SNR=SNR, background=None) # collapse all frequencies by taking mean for each column profile_1D = np.mean(signal, axis=0) # plot background ax[0].imshow(event.background) ax[0].set_title("Background") # plot the signal im = ax[1].imshow(signal) ax[1].set_title(f"Noise and FRB (SNR: {SNR})") # plot the 1D profile ax[2].plot(profile_1D) ax[2].set_title(f"Profile") plt.colorbar(mappable=im) fig.tight_layout() return fig
def test_injectFRB(self): event = SimulatedFRB() event.scintillate() # add .FRB attribute to event event.roll() event.fractional_bandwidth() signal = event.injectFRB(SNR=10, background=None) assert not np.array_equal(event.background, signal) assert np.mean(np.mean(signal, axis=0)) / np.abs(np.mean(np.mean(event.background, axis=0))) > 9, \ "Signal power not increased properly"
def real_RFI_plot(RFI_array_file, seed=24, figsize=(12, 8), SNRmin=5, SNRmax=15): np.random.seed(seed) npz_file = np.load(RFI_array_file) real_RFI = npz_file['rfi_data'] frequencies = npz_file['freq'] weights = npz_file['weights'] print(f"Frequencies: {frequencies}") random_indexes = np.random.randint(low=0, high=len(real_RFI), size=8) sample_RFI, sample_weights = real_RFI[random_indexes], weights[ random_indexes] fig_RFI, ax_RFI = plt.subplots(nrows=4, ncols=2, figsize=figsize) for RFI_image, w, ax in zip(sample_RFI, sample_weights, ax_RFI.flatten()): event = SimulatedFRB(f_low=1850, f_high=2700, f_ref=np.median(frequencies), bandwidth=np.ptp(frequencies)) event.simulateFRB(background=RFI_image, weights=w, SNRmin=SNRmin, SNRmax=SNRmax) ax.imshow(event.simulatedFRB, aspect='auto', extent=[0, 256, np.min(frequencies), np.max(frequencies)]) ax.set_title(f'SNR: {np.round(event.SNR, 1)}') fig_RFI.tight_layout() return fig_RFI
def noise_and_FRB(nrows=4, ncols=2): fig, ax = plt.subplots(nrows=nrows, ncols=ncols) example_number = 1 flat_axes = ax.flatten() while example_number < len(flat_axes): event = SimulatedFRB() event.simulateFRB() frb = event.simulatedFRB # collapse all frequencies by taking mean for each column profile_1D = np.mean(frb, axis=0) # plot results for every 3 axes flat_axes[example_number - 1].imshow(frb) flat_axes[example_number - 1].set_title( f"Noise and FRB (SNR: {np.round(event.SNR, 2)})") flat_axes[example_number].plot(profile_1D) flat_axes[example_number].set_title(f"Profile") example_number += 2 fig.tight_layout() return fig
def jupyter_simulatedFRBs(nrows=3, ncols=3, seed=256): np.random.seed(seed) # plot the simulated events fig_simulated, ax_simulated = plt.subplots(nrows=nrows, ncols=ncols, figsize=(16, 12)) # create simulation objects and simulate an FRB for each of them simulated_events = [SimulatedFRB() for i in np.arange(nrows * ncols)] lowest_vmin, greatest_vmax = 0, 1 # track vmin and vmax for colorbar normalization for event in simulated_events: event.simulateFRB(SNRmin=6, SNRmax=20) if np.min(event.simulatedFRB) < lowest_vmin: lowest_vmin = np.min(event.simulatedFRB) if np.max(event.simulatedFRB) > greatest_vmax: greatest_vmax = np.max(event.simulatedFRB) for axis, event in zip(ax_simulated.flatten(), simulated_events): im = axis.imshow( event.simulatedFRB, extent=[0, event.nt, event.frequencies[0], event.frequencies[-1]], aspect='auto', vmin=lowest_vmin, vmax=greatest_vmax) axis.set(title=f"SNR: {np.round(event.SNR, 2)}", xlabel='time (ms)', ylabel='frequency (MHz)') axis.set_yticks( np.arange(event.frequencies[0], event.frequencies[-1], 350)) fig_simulated.tight_layout() fig_simulated.subplots_adjust(right=0.92) cbar_ax = fig_simulated.add_axes([0.94, 0.09, 0.02, 0.85]) fig_simulated.colorbar(im, cax=cbar_ax) return fig_simulated
def test_SNR(self): """Correctly multiplying the signal by the SNR""" event = SimulatedFRB() event.scintillate() # add .FRB attribute to event event.roll() event.fractional_bandwidth() # keep track of the of the original FRB original_FRB = np.copy(event.FRB) # get random SNR and multiply by signal event.sample_SNR() injectedFRB = event.injectFRB(event.SNR) assert not np.all(original_FRB == injectedFRB), "FRB didn't change!" assert np.max(injectedFRB) > np.max( original_FRB), "Not multiplied correctly"
import numpy as np import matplotlib.pyplot as plt from time import time from tqdm import trange from simulated_NN import SimulatedFRB, make_labels # make a SimulatedFRB object for testing event = SimulatedFRB() class TestSimulateFRB(object): def test_background(self): background = event.background assert background.shape == (64, 256), "Shape doesn't match" assert -1 < np.mean(background) < 1 def test_gaussian_profile(self): g = event.gaussian_profile() assert g.shape == ( 64, 256), "Gaussian profile doesn't have correct shape (64, 256)" assert not np.all(g == 1e-18), "No elements changed/are different" def test_scattering_profile(self): g = event.gaussian_profile() # used to match shapes scatter = event.scatter_profile() assert scatter.shape == g.shape, f"Needs shape {g.shape} to match Gaussian profile" assert not np.all( scatter == scatter[0][0]), "No elements changed/are different" def test_pulse(self): g = event.gaussian_profile() # used to match shapes
def full_FRB_plot(nrows=4, ncols=3): fig, ax = plt.subplots(nrows=nrows, ncols=ncols) example_number = 1 flat_axes = ax.flatten() while example_number < len(flat_axes): event = SimulatedFRB() event.scintillate() original_FRB = np.copy(event.FRB) event.roll() event.fractional_bandwidth() event.sample_SNR() full_signal = event.injectFRB(event.SNR) # collapse all frequencies by taking mean for each column profile_1D = np.mean(full_signal, axis=0) # plot results for every 3 axes flat_axes[example_number - 1].imshow(original_FRB) flat_axes[example_number - 1].set_title("Original FRB") flat_axes[example_number].imshow(full_signal) flat_axes[example_number].set_title( f"with SNR {np.round(event.SNR, 2)}") flat_axes[example_number + 1].plot(profile_1D) flat_axes[example_number + 1].set_title(f"Profile") example_number += 3 fig.tight_layout() return fig
import matplotlib as mpl import matplotlib.pyplot as plt from time import time from tqdm import trange from simulated_NN import SimulatedFRB, make_labels # setting matplotlib defaults plt.ion() font_info = {'family': 'sans-serif', 'sans-serif': 'Myriad Pro', 'size': 16} mpl.rc('font', **font_info) mpl.rcParams['pdf.fonttype'] = 42 # mpl_params = {'axes.labelsize': 16, 'xtick.labelsize': 8, 'ytick.labelsize': 8, 'pdf.fonttype': 42} # create SimulatedFRB object for testing event = SimulatedFRB(tau=0.1) def plot_pulse(): profiles = { 'gaussian': event.gaussian_profile(), 'scatter': event.scatter_profile(), 'pulse (no scintillation)': event.pulse_profile(), 'pulse (with scintillation)': event.scintillate() } fig, axes = plt.subplots(nrows=4, ncols=2) for ax, profile in zip(axes[:, 0].flatten(), profiles.keys()): ax.imshow(profiles[profile]) ax.set_title(profile)