コード例 #1
0
def gain_variance(nu, path):
    # Step 1 Load the MWA tile positions
    # Step 2 Calculate the baselines
    # Step 3 Pick a tile (core, redundant, outrigger)
    # Step 4 Calculate the Covariances for those baselines at all frequencies
    # Step 4 Calculate the mean signals for those baselines
    # Step 5 Calculate the gain variance by summing by the rations per frequency
    # DFT that gain covariance matrix (off-diagonals == 0)
    # What is the frequency structure
    tile_id = [31, 1036, 81]  # 81 #1036

    mwa_telescope = RadioTelescope(load=True, path=path, frequency_channels=nu)
    average_sky_brightness = moment_returner(n_order=1,
                                             S_low=1,
                                             S_mid=1,
                                             S_high=5)
    print(average_sky_brightness)
    #print("brightness", average_sky_brightness)

    ratios = numpy.zeros(
        (3, len(mwa_telescope.antenna_positions.antenna_ids), len(nu)))
    variance = numpy.zeros((3, len(nu)))

    for k in range(len(tile_id)):
        # select baseline table indices in which tile_id is present
        baseline_indices = numpy.where(
            (mwa_telescope.baseline_table.antenna_id1 == tile_id[k])
            | (mwa_telescope.baseline_table.antenna_id2 == tile_id[k]))[0]

        baseline_table_selection = mwa_telescope.baseline_table.sub_table(
            (baseline_indices))

        for i in range(len(baseline_indices)):

            u = baseline_table_selection.u(frequency=nu[0])[i]
            v = baseline_table_selection.v(frequency=nu[0])[i]

            data_covariance = sky_covariance(u, v, nu) + beam_covariance(
                u, v, nu)
            sigma = beam_width(frequency=nu) / numpy.sqrt(2)
            #pyplot.loglog(nu / 1e6, numpy.diag(data_covariance))
            #pyplot.show()

            signal = 2 * numpy.pi * sigma**2 * average_sky_brightness
            #print("signal", average_sky_brightness)
            #print("noise", numpy.diag(data_covariance))

        variance[k, :] = numpy.sqrt(
            1 / (2 * numpy.real(numpy.sum(ratios[k, ...], axis=0))))

    alt_variance = numpy.diag(data_covariance) / (2 * signal**2 * 127 *
                                                  (nu / nu[0])**(-2 * 0.5))
    #pyplot.plot(nu/1e6, alt_variance, label = "analytic")
    #pyplot.legend()
    #pyplot.show()
    #print(variance[0,...] - alt_variance)
    return alt_variance
コード例 #2
0
def main():
    verbose = True
    path = "./hex_pos.txt"
    data_path = "/data/rjoseph/Hybrid_Calibration/Tile_Pertubation/Simulation_Output/" + "gaussian_tile1036_dipole1_corrected_True/"
    frequency_range = numpy.linspace(135, 165, 100) * 1e6
    telescope = RadioTelescope(load=True, path=path, verbose=verbose)

    ideal_data = numpy.load(data_path + "ideal_simulated_data.npy")
    broken_data = numpy.load(data_path + "broken_simulated_data.npy")

    get_power_spectrum(frequency_range, telescope, ideal_data, broken_data, faulty_tile=1036,
                       plot_file_name=data_path + "1036_1_repr_data.pdf", verbose=verbose)

    return
コード例 #3
0
def main(ssh=False, labelfontsize=13, tickfontsize=11, plot_name="Baseline_Distribution_MWA.pdf"):
    plot_path = "../../Plots/Analytic_Covariance/"
    u_range = numpy.logspace(0, numpy.log10(500), 100)
    frequency_range = numpy.linspace(135, 165, 251) * 1e6


    k_perp = from_u_to_k_perp(u_range, frequency_range[int(len(frequency_range) / 2)])
    x_label = r"$k_{\perp}$ [Mpc$^{-1}$]"

    mwa_position_path = "./Data/MWA_Compact_Coordinates.txt"
    mwa_telescope = RadioTelescope(load=True, path=mwa_position_path)

    log_steps = numpy.diff(numpy.log10(u_range))
    u_bin_edges = numpy.zeros(len(u_range) + 1)
    u_bin_edges[1:] = 10**(numpy.log10(u_range) + 0.5*log_steps[0])
    u_bin_edges[0] = 10**(numpy.log10(u_range[0] - 0.5*log_steps[0]))
    baseline_lengths = numpy.sqrt(mwa_telescope.baseline_table.u_coordinates ** 2 +
                                  mwa_telescope.baseline_table.u_coordinates ** 2)

    counts, edges = numpy.histogram(baseline_lengths, bins=u_bin_edges)
    figure, axes = pyplot.subplots(1, 1, figsize=(5, 5))
    axes.plot(k_perp, counts/len(baseline_lengths))
    axes.set_xscale('log')
    axes.set_xlabel(x_label, fontsize=labelfontsize)
    axes.set_ylabel('Fraction of Baselines', fontsize=labelfontsize)

    axes.tick_params(axis='both', which='major', labelsize=tickfontsize)

    figure.tight_layout()

    figure.savefig(plot_path + plot_name)

    if not ssh:
        print("I am plotting")

        pyplot.show()

    return
コード例 #4
0
def simulate_gain_variances(create_signal=True, compute_FIM=True, plot_variance=True):
    frequency_range = numpy.linspace(135, 165, 50) * 1e6
    path = "Data/MWA_All_Coordinates_Cath.txt"
    output_path = "/data/rjoseph/Hybrid_Calibration/Tile_Pertubation/Simulation_Output/"
    project_path = "Numba_Gain_Variance_Simulation"

    sky_param = "random"
    n_realisations = 100

    tile_id1 = 36
    tile_id2 = 1036
    tile_id3 = 81

    tile_name1 = "Core"
    tile_name2 = "Hex"
    tile_name3 = "Outer"

    telescope = RadioTelescope(load=True, path=path)
    baseline_table = telescope.baseline_table

    if not os.path.exists(output_path + project_path + "/"):
        print
        ""
        print
        "!!!Warning: Creating output folder at output destination!"
        os.makedirs(output_path + project_path + "/" + "Simulated_Visibilities")
        os.makedirs(output_path + project_path + "/" + "FIM_realisations")

    if create_signal:
        print("Creating Signal Realisations")
        for i in range(n_realisations):
            print(f"Realisation {i}")
            signal_time0 = time.perf_counter()
            source_population = SkyRealisation(sky_type=sky_param, seed=i)
            signal = get_observations(source_population, baseline_table, frequency_range, interpolation='numba')
            numpy.save(output_path + project_path + "/" + "Simulated_Visibilities/" + f"visibility_realisation_{i}",
                       signal)
            signal_time1 = time.perf_counter()
            print(f"Realisation {i} Time = {signal_time1 - signal_time0} \n")

    if compute_FIM:
        covariance_matrices = compute_frequency_covariance(baseline_table, frequency_range)

        for i in range(n_realisations):
            print(f"Realisation {i}")

            signal = numpy.load(
                output_path + project_path + "/" + "Simulated_Visibilities/" + f"visibility_realisation_{i}.npy")
            FIM = get_FIM(signal, telescope, baseline_table, frequency_range, covariance_matrices)
            numpy.save(output_path + project_path + "/" + "FIM_realisations/" + f"fim_realisation_{i}", FIM)

    if plot_variance:
        print("Plotting variances")

        theoretical_variance = gain_variance(frequency_range, path=path)

        antenna_id = telescope.antenna_positions.antenna_ids
        n_antennas = len(antenna_id)

        tile1_index = numpy.where(antenna_id == tile_id1)[0]
        tile2_index = numpy.where(antenna_id == tile_id2)[0]
        tile3_index = numpy.where(antenna_id == tile_id3)[0]

        #figure = pyplot.figure(figsize=(18, 5))
        #tile1_plot = figure.add_subplot(131)
        #tile2_plot = figure.add_subplot(132)
        #tile3_plot = figure.add_subplot(133)

        figure, axes = pyplot.subplots(3, 3, figsize=(18, 12))
        tile_indices = numpy.array([tile1_index, tile2_index, tile3_index])
        tile_names = [tile_name1, tile_name2, tile_name3]


        for i in range(100):
            FIM = numpy.load(output_path + project_path + "/" + "FIM_realisations/" + f"fim_realisation_{i}.npy")

            covariance = numpy.zeros((n_antennas, n_antennas, len(frequency_range)))
            for i in range(len(frequency_range)):
                covariance[..., i] = numpy.linalg.pinv(FIM[..., i])


            for k in range(3):
                for l in range(3):

                    #print(covariance[tile_indices[k], tile_indices[l], :].flatten())
                    axes[k, l].plot(frequency_range / 1e6, covariance[tile_indices[k], tile_indices[l], :].flatten(),
                                    'k', alpha=0.1)
                    axes[k, l].set_title(tile_names[k] + ", " + tile_names[l])

                    if l == k:
                        axes[k,l].plot(frequency_range/1e6, theoretical_variance)
                        axes[k,l].plot(frequency_range/1e6, theoretical_variance*10**(2.8))

                        #axes[k, l].set_ylim(0.002,0.005)
                        #axes[k, l].set_yscale('symlog')


                    if k == 2:
                        axes[k, l].set_xlabel("Frequency [MHz]")
            #tile1_plot.plot(frequency_range / 1e6, covariance[tile1_index, tile1_index, :].flatten(), 'k', alpha=0.1)
            #tile2_plot.plot(frequency_range / 1e6, covariance[tile2_index, tile2_index, :].flatten(), 'k', alpha=0.1)
            #tile3_plot.plot(frequency_range / 1e6, covariance[tile3_index, tile3_index, :].flatten(), 'k', alpha=0.1)

        #tile1_plot.set_title(tile_name1)
        #tile2_plot.set_title(tile_name2)
        #tile3_plot.set_title(tile_name3)

        pyplot.show()


    print("Finished")
    return
コード例 #5
0
def main(ssh=False, labelfontsize=12, ticksize=11):
    plot_path = "../../Plots/Analytic_Covariance/"
    mwa_position_path = "./Data/MWA_Compact_Coordinates.txt"
    mwa_telescope = RadioTelescope(load=True, path=mwa_position_path)

    u_range = numpy.logspace(1, numpy.log10(500), 100)

    # 100 frequency channels is fine for now, maybe later do a higher number to push up the k_par range
    frequency_range = numpy.linspace(135, 165, 251) * 1e6
    weights = compute_weights(u_range,
                              mwa_telescope.baseline_table.u_coordinates,
                              mwa_telescope.baseline_table.v_coordinates)
    eta, sky_only_raw, sky_only_cal = residual_ps_error(
        u_range,
        frequency_range,
        residuals='sky',
        broken_baselines_weight=0.3,
        weights=weights)
    eta, sky_and_beam_raw, sky_and_beam_cal = residual_ps_error(
        u_range,
        frequency_range,
        residuals='both',
        broken_baselines_weight=0.3,
        weights=weights)
    fiducial_ps = fiducial_eor(u_range, eta)

    difference_cal = sky_and_beam_cal - sky_only_cal

    figure, axes = pyplot.subplots(1, 3, figsize=(15, 5))

    ps_norm = colors.LogNorm(vmin=1e2, vmax=1e15)
    plot_power_spectrum(u_range,
                        eta,
                        frequency_range,
                        sky_and_beam_cal,
                        title=r"$\mathbf{C}_{r}$(sky + beam)",
                        axes=axes[0],
                        axes_label_font=labelfontsize,
                        tickfontsize=ticksize,
                        norm=ps_norm,
                        ylabel_show=True,
                        xlabel_show=True,
                        colorbar_show=True)

    diff_norm = colors.SymLogNorm(linthresh=1e2,
                                  linscale=1.5,
                                  vmin=-1e12,
                                  vmax=1e12)

    plot_power_spectrum(
        u_range,
        eta,
        frequency_range,
        difference_cal,
        axes=axes[1],
        axes_label_font=labelfontsize,
        tickfontsize=ticksize,
        norm=diff_norm,
        colorbar_show=True,
        xlabel_show=True,
        title=r"$\mathbf{C}_{r}$(sky + beam) - $\mathbf{C}_{r}$(sky) ",
        diff=True,
        colormap='coolwarm')

    ratio_norm = colors.SymLogNorm(linthresh=1e1,
                                   linscale=0.5,
                                   vmin=-1e1,
                                   vmax=1e3)

    plot_power_spectrum(
        u_range,
        eta,
        frequency_range,
        difference_cal / fiducial_ps,
        axes=axes[2],
        axes_label_font=labelfontsize,
        tickfontsize=ticksize,
        norm=ratio_norm,
        colorbar_show=True,
        colorbar_limits='neither',
        xlabel_show=True,
        z_label="Difference Ratio",
        title=
        r"$(\mathbf{C}_{r}$(sky + beam) - $\mathbf{C}_{r}$(sky))/$\mathbf{C}_{s}$ ",
        diff=True)

    figure.tight_layout()
    figure.savefig(plot_path +
                   "Comparing_Sky_and_Beam_Errors_Post_Calibration_MWA.pdf")
    if not ssh:
        pyplot.show()

    return
コード例 #6
0
def main(beam_type='gaussian',
         faulty_dipole=1,
         faulty_tile=36,
         n_channels=100,
         calibrate=True,
         verbose=True):
    print(beam_type, faulty_dipole, faulty_dipole, n_channels, calibrate,
          verbose)
    output_path = "/data/rjoseph/Hybrid_Calibration/Tile_Pertubation/Simulation_Output/"
    prefix = "TEST"
    suffix = ""

    path = "Data/MWA_All_Coordinates_Cath.txt"
    frequency_range = numpy.linspace(135, 165, n_channels) * 1e6
    #faulty_dipole = 1 #6
    #faulty_tile = 36 #1036, 81, 36
    sky_param = "random"
    mode = "parallel"
    processes = 2
    #calibrate = True
    #beam_type = "gaussian"
    plot_file_name = "Compare_MWA_Beam_Core_Gain_Corrected_1.pdf"

    telescope = RadioTelescope(load=True, path=path, verbose=verbose)
    baseline_table = telescope.baseline_table
    source_population = SkyRealisation(sky_type=sky_param, verbose=verbose)

    ####################################################################################################################
    if verbose:
        print("Generating visibility measurements for each frequency")
    ideal_measured_visibilities, broken_measured_visibilities = get_observations(
        source_population,
        baseline_table,
        faulty_dipole,
        faulty_tile,
        frequency_range,
        beam_type,
        calibrate,
        compute_mode=mode,
        processes=processes)
    #############################################################################################################################

    #save simulated data:
    project_name = prefix + beam_type + "_tile" + str(faulty_tile) +"_dipole" + str(faulty_dipole) + "_corrected_" \
                   + str(calibrate) + suffix

    if not os.path.exists(output_path + project_name):
        print
        ""
        print
        "!!!Warning: Creating output folder at output destination!"
        os.makedirs(output_path + project_name)
        output_types = ["ideal", "broken"]
        numpy.save(
            output_path + project_name + "/" + "ideal" + "_simulated_data",
            ideal_measured_visibilities)
        numpy.save(
            output_path + project_name + "/" + "broken" + "_simulated_data",
            broken_measured_visibilities)

    file = open(output_path + project_name + "/" + "simulation_parameters.log",
                "w")
    file.write(
        f"Frequency range: {numpy.min(frequency_range)} - {numpy.max(frequency_range)} MHz \n"
    )
    file.write(f"Faulty Dipole: {faulty_dipole}\n")
    file.write(f"Faulty Tile: {faulty_tile}\n")
    file.write(f"Sky Parameters: {sky_param} \n")
    file.write(f"Calibrate: {calibrate}\n")
    file.write(f"Beam model:{beam_type} \n")
    file.write(f"Position File: {path}")
    file.close()

    get_power_spectrum(frequency_range, telescope, ideal_measured_visibilities,
                       broken_measured_visibilities, faulty_tile,
                       output_path + project_name + "/" + plot_file_name,
                       verbose)

    return
def main():
    path = "./Data/MWA_Compact_Coordinates.txt"
    plot_folder = "../../Plots/Analytic_Covariance/"
    plot_u_dist = False
    plot_array_matrix = False
    plot_inverse_matrix = False
    plot_weights = False
    grid_weights = True
    binned_weights = True
    telescope = RadioTelescope(load=True, path=path)
    baseline_lengths = numpy.sqrt(telescope.baseline_table.u_coordinates**2 +
                                  telescope.baseline_table.v_coordinates**2)

    if plot_u_dist:
        figure_u, axes_u = pyplot.subplots(1, 1)
        axes_u.hist(baseline_lengths,
                    density=True,
                    bins=100,
                    label="MWA Phase II Compact")
        axes_u.set_xlabel(r"$u\,[\lambda]$")
        axes_u.set_ylabel("Baseline PDF")
        axes_u.legend()
        figure_u.savefig(plot_folder + "MWA_Phase_II_Baseline_PDF.pdf")

    array_matrix = matrix_constructor_alternate(telescope)
    #
    # pyplot.rcParams['xtick.bottom'] = pyplot.rcParams['xtick.labelbottom'] = False
    # pyplot.rcParams['xtick.top'] = pyplot.rcParams['xtick.labeltop'] = True
    if plot_array_matrix:
        figure_amatrix = pyplot.figure(figsize=(250, 10))
        axes_amatrix = figure_amatrix.add_subplot(111)
        plot_amatrix = axes_amatrix.imshow(array_matrix.T, origin='lower')
        colorbar(plot_amatrix)
        axes_amatrix.set_xlabel("Baseline Number", fontsize=20)
        axes_amatrix.set_ylabel("Antenna Number", fontsize=20)
        figure_amatrix.savefig(plot_folder + "Array_Matrix_Double.pdf")

    inverse_array_matrix = numpy.linalg.pinv(array_matrix)
    if plot_inverse_matrix:
        figure_inverse = pyplot.figure(figsize=(110, 20))
        axes_inverse = figure_inverse.add_subplot(111)
        plot_inverse = axes_inverse.imshow(numpy.abs(inverse_array_matrix))
        colorbar(plot_inverse)

    baseline_weights = numpy.sqrt(
        (numpy.abs(inverse_array_matrix[::2, ::2])**2 +
         numpy.abs(inverse_array_matrix[1::2, 1::2])**2))
    print(
        f"Every Tile sees {len(baseline_weights[0,:][baseline_weights[0, :] > 1e-4])}"
    )

    # baseline_weights = numpy.sqrt(numpy.abs(inverse_array_matrix[:int(len(telescope.antenna_positions.antenna_ids) - 1), :int(len(baseline_lengths))])**2 + \
    #                    numpy.abs(inverse_array_matrix[int(len(telescope.antenna_positions.antenna_ids) -1 ):, :int(len(baseline_lengths)):])**2)
    if plot_weights:
        figure_weights, axes_weights = pyplot.subplots(1, 1)
        normalised_weights = axes_weights.imshow(baseline_weights)
        axes_weights.set_title("Antenna Baseline Weights")
        colorbar(normalised_weights)

        # blaah = numpy.unique(baseline_weights)
        # figblaah, axblaah = pyplot.subplots(1,1)
        # axblaah.hist(baseline_weights.flatten(), bins = 100)
        # axblaah.set_yscale('log')

    uu_weights = numpy.zeros((len(baseline_lengths), len(baseline_lengths)))
    baselines = telescope.baseline_table
    antennas = telescope.antenna_positions.antenna_ids
    for i in range(len(baseline_lengths)):
        index1 = numpy.where(antennas == baselines.antenna_id1[i])[0]
        index2 = numpy.where(antennas == baselines.antenna_id2[i])[0]

        if index1 == 0:
            baseline_weights1 = 0
        else:
            baseline_weights1 = baseline_weights[index1 - 1, :]

        if index2 == 0:
            baseline_weights2 = 0
        else:
            baseline_weights2 = baseline_weights[index2 - 1, :]
        uu_weights[i, :] = numpy.sqrt(
            (baseline_weights1**2 + baseline_weights2**2))

    u_bins = numpy.linspace(0, numpy.max(baseline_lengths), 101)
    bin_size = (u_bins.max() - u_bins.min()) / len(u_bins)
    sorted_indices = numpy.argsort(baseline_lengths)
    sorted_weights = uu_weights[sorted_indices, :][:, sorted_indices]

    bin_indices = numpy.digitize(baseline_lengths[sorted_indices], u_bins)
    print(
        f"A uncalibrated baseline sees  {len(uu_weights[:, 190][uu_weights[:, 190] > 1e-4])}"
    )
    print(
        f"A calibrated baseline sees {len(uu_weights[190, :][uu_weights[190, :] > 1e-4])}"
    )

    print(
        f"A sorted uncalibrated baseline sees  {len(sorted_weights[:, 2489][sorted_weights[:, 2489] > 1e-4])}"
    )
    print(
        f"A sorted calibrated baseline sees {len(sorted_weights[190, :][sorted_weights[190, :] > 1e-4])}"
    )

    if grid_weights:
        fig_cal, axes_cal = pyplot.subplots(1, 2, figsize=(100, 50))
        cal_plot = axes_cal[0].imshow(uu_weights,
                                      origin='lower',
                                      interpolation='none')
        axes_cal[0].set_xlabel("Uncalibrated Baseline Index")
        axes_cal[0].set_ylabel("Calibrated Baseline Index")
        axes_cal[0].set_title("Quadrature Added Real and Imaginary Weights")
        colorbar(cal_plot)

        sorted_plot = axes_cal[1].imshow(sorted_weights,
                                         interpolation='none',
                                         origin='lower')
        axes_cal[1].set_xlabel("Uncalibrated Baseline Index")
        axes_cal[1].set_title(" Baseline Length Sorted Weights")
        colorbar(sorted_plot)

        for i in range(len(bin_indices)):
            if i == 0:
                pass
            elif bin_indices[i] == bin_indices[i - 1]:
                pass
            else:
                axes_cal[1].axvline(i, linestyle="-", color='gray', alpha=0.4)
                axes_cal[1].axhline(i, linestyle="-", color='gray', alpha=0.4)

        # unique_values = numpy.unique(u_u_weights)
        # figs, axs = pyplot.subplots(1,1)
        # axs.hist(unique_values, bins = 1000)

    if binned_weights:
        bin_counter = numpy.zeros_like(uu_weights)
        bin_counter[uu_weights != 0] = 1

        uu1, uu2 = numpy.meshgrid(baseline_lengths, baseline_lengths)
        flattened_uu1 = uu1.flatten()
        flattened_uu2 = uu2.flatten()

        computed_weights = numpy.histogram2d(flattened_uu1,
                                             flattened_uu2,
                                             bins=u_bins,
                                             weights=uu_weights.flatten())
        computed_counts = numpy.histogram2d(flattened_uu1,
                                            flattened_uu2,
                                            bins=u_bins,
                                            weights=bin_counter.flatten())

        figure_binned, axes_binned = pyplot.subplots(
            3, 3, figsize=(12, 15), subplot_kw=dict(aspect='equal'))

        summed_norm = colors.LogNorm()
        counts_norm = colors.LogNorm()
        averaged_norm = colors.LogNorm()

        summed = axes_binned[0, 1].pcolor(u_bins,
                                          u_bins,
                                          computed_weights[0],
                                          norm=summed_norm)
        counts = axes_binned[0, 2].pcolor(u_bins,
                                          u_bins,
                                          computed_counts[0],
                                          norm=counts_norm)
        averaged = axes_binned[0, 0].pcolor(u_bins,
                                            u_bins,
                                            computed_weights[0] /
                                            computed_counts[0] / bin_size**2,
                                            norm=averaged_norm)

        averaged_cbar = colorbar(averaged)
        counts_cbar = colorbar(counts)
        summed_cbar = colorbar(summed)

        axes_binned[0, 1].set_title(r"Summed Weights")
        axes_binned[0, 2].set_title(r"Baseline Counts")
        axes_binned[0, 0].set_title(r"Averaged Weights")

        baseline_pdf = numpy.histogram(baseline_lengths,
                                       bins=u_bins,
                                       density=True)

        ww1, ww2 = numpy.meshgrid(baseline_pdf[0], baseline_pdf[0])
        summed_anorm = colors.LogNorm()
        counts_anorm = colors.LogNorm()
        averaged_anorm = colors.LogNorm()

        approx_sum = convolve2d(numpy.diag(baseline_pdf[0]), ww1, mode='same')

        #convolve2d(numpy.diag(baseline_pdf[0])*bin_size**2*len(baseline_lengths)**2, ww1*ww2*bin_size**2*len(baseline_lengths)**2, mode = 'same')*ww1*ww2*bin_size**2*len(baseline_lengths)**2
        # convolve2d(ww2, convolve2d(numpy.diag(baseline_pdf[0]), ww1, mode = 'same'), mode='same')# (ww1*ww2)*bin_size**2*len(baseline_lengths)
        approx_counts = (ww1 * ww2) * bin_size**2 * len(baseline_lengths)**2
        approx_averaged = approx_sum / approx_counts

        averaged_aplot = axes_binned[1, 0].pcolor(u_bins,
                                                  u_bins,
                                                  approx_averaged,
                                                  norm=averaged_anorm)
        sum_aplot = axes_binned[1, 1].pcolor(u_bins,
                                             u_bins,
                                             approx_sum,
                                             norm=summed_anorm)
        counts_aplot = axes_binned[1, 2].pcolor(u_bins,
                                                u_bins,
                                                approx_counts,
                                                norm=counts_anorm)

        acbar_sum = colorbar(sum_aplot)
        acbar_averaged = colorbar(averaged_aplot)
        acbar_counts = colorbar(counts_aplot)

        axes_binned[2, 0].set_title(r"Differences")

        blaah = axes_binned[2, 0].pcolor(
            u_bins, u_bins,
            computed_weights[0] / computed_counts[0] - approx_sum)

        axes_binned[2, 0].set_ylabel(r"$u^{\prime}\,[\lambda]$")
        colorbar(blaah)

        axes_binned[0, 0].set_ylabel(r"$u^{\prime}\,[\lambda]$")
        axes_binned[1, 0].set_ylabel(r"$u^{\prime}\,[\lambda]$")
        axes_binned[2, 0].set_ylabel(r"$u^{\prime}\,[\lambda]$")

        axes_binned[2, 0].set_xlabel(r"$u\,[\lambda]$")
        axes_binned[1, 1].set_xlabel(r"$u\,[\lambda]$")
        axes_binned[1, 2].set_xlabel(r"$u\,[\lambda]$")

        figure_binned.savefig(plot_folder + "Baseline_Weights_uu.pdf")

    pyplot.show()
    return
def main():
    output_path = "/data/rjoseph/Hybrid_Calibration/Tile_Pertubation/Simulation_Output/"
    project_path = "sky_and_residual_simulation"
    n_realisations = 10000
    frequency_range = numpy.linspace(135, 165, 100) * 1e6
    shape = ['linear', 400, 20, 'log']
    load = False
    create_signal = False
    compute_ratio = False
    compute_covariance = False
    serial = True
    plot_covariance = True
    plot_model_signal = False
    telescope = RadioTelescope(load=load, shape=shape)
    baseline_table = telescope.baseline_table

    if not os.path.exists(output_path + project_path + "/"):
        print("Creating Project folder at output destination!")
        os.makedirs(output_path + project_path)

    if create_signal:
        create_model_and_residuals(baseline_table, frequency_range,
                                   n_realisations, output_path + project_path)

    if plot_model_signal:
        pass

    if compute_ratio:

        ratio_full = numpy.load(output_path + project_path + "/" +
                                "Simulated_Visibilities/" +
                                f"residual_model_ratios_full.npy")

        maximum_baseline = numpy.min(baseline_table.u_coordinates)
        max_index = numpy.where(
            numpy.abs(baseline_table.u_coordinates -
                      maximum_baseline) == numpy.min(
                          numpy.abs(baseline_table.u_coordinates -
                                    maximum_baseline)))[0][0]
        half_index = numpy.where(
            numpy.abs(baseline_table.u_coordinates -
                      maximum_baseline / 2) == numpy.min(
                          numpy.abs(baseline_table.u_coordinates -
                                    maximum_baseline / 2)))[0][0]
        min_index = numpy.where(
            numpy.abs(baseline_table.u_coordinates + 7) == numpy.min(
                numpy.abs(baseline_table.u_coordinates + 7)))[0][0]

        indices = [min_index, half_index, max_index]

        ######### Compute variances as approximation ##########
        model_variance = sky_covariance(0,
                                        0,
                                        frequency_range,
                                        S_low=1,
                                        S_high=10)
        residual_variance = sky_covariance(0, 0, frequency_range, S_high=1)

        figure, axes = pyplot.subplots(2,
                                       3,
                                       figsize=(15, 5),
                                       subplot_kw=dict(xlabel=r"$\nu$ [MHz]"))

        for i in range(3):
            axes[0, i].plot(frequency_range / 1e6,
                            numpy.abs(1 - ratio_full[indices[i], :, ::1]),
                            color='k',
                            alpha=0.01)
            axes[0, i].plot(frequency_range / 1e6,
                            numpy.diag(residual_variance) /
                            numpy.diag(model_variance),
                            color='C0')

            axes[1, i].pcolormesh(
                frequency_range / 1e6, frequency_range / 1e6,
                numpy.log10(
                    numpy.abs(numpy.cov(1 - ratio_full[indices[i], :, :]))))
            axes[0, i].set_yscale("symlog")
            axes[0, i].set_title(
                f"u={int(numpy.abs(baseline_table.u_coordinates[indices[i]]))}"
            )

        axes[0, 0].set_ylabel(r"$\delta$g")
        axes[1, 0].set_ylabel(r"$\nu$ [MHz]")

        pyplot.show()

    if compute_covariance:
        if serial:
            compute_frequency_frequency_covariance_serial(
                baseline_table, frequency_range, output_path + project_path,
                n_realisations)
        else:
            pass

    if plot_covariance:
        #gain_covariance_impact(baseline_table, frequency_range, output_path + project_path)
        residual_PS_error(baseline_table, frequency_range,
                          output_path + project_path)

    return