Exemplo n.º 1
0
def _apply_sig_clip(data,
                    e_data,
                    sig_thres=2,
                    ymin=0,
                    ymax=1.2,
                    var='V2',
                    display=False):
    """ Apply the sigma-clipping on the dataset and plot some diagnostic plots. """
    filtered_data = sigma_clip(data, sigma=sig_thres, axis=0)

    n_files = data.shape[0]
    n_pts = data.shape[1]  # baselines or bs number

    mn_data_clip, std_data_clip = [], []
    for i in range(n_pts):
        cond = filtered_data[:, i].mask
        data_clip = data[:, i][~cond]
        e_data_clip = e_data[:, i][~cond]
        cmn, std = wtmn(data_clip, weights=e_data_clip)
        mn_data_clip.append(cmn)
        std_data_clip.append(std)

    data_med = np.median(data, axis=0)

    if var == 'V2':
        ylabel = r'Raw V$^2$'
        xlabel = '# baselines'
    else:
        ylabel = r'Raw closure Phases $\Phi$ [deg]'
        xlabel = '# bispectrum'
        ymin = -ymax

    if display:
        plt.figure()
        plt.title('CALIBRATOR')
        if n_files != 1:
            plt.plot(data[0], color='grey', alpha=.2, label='Data vs. files')
            plt.plot(data[1:, :].T, color='grey', alpha=.2)
        plt.plot(data_med, 'g--', lw=1, label='Median value')
        for i in range(n_files):
            data_ind = data[i, :]
            cond = filtered_data[i, :].mask
            x = np.arange(len(data_ind))
            bad = data_ind[cond]
            x_bad = x[cond]
            if i == 0:
                plt.plot(x_bad,
                         bad,
                         'rx',
                         ms=3,
                         label=r'Rejected points (>%i$\sigma$)' % sig_thres)
            else:
                plt.plot(x_bad, bad, 'rx', ms=3)
        plt.legend(loc='best', fontsize=9)
        plt.ylim(ymin, ymax)
        plt.xlabel(xlabel)
        plt.ylabel(ylabel)
        plt.tight_layout()
    return np.array(mn_data_clip), np.array(std_data_clip)
Exemplo n.º 2
0
def average_calib_files(list_nrm, sig_thres=2, display=False):
    """ Average NRM data extracted from multiple calibrator files. Additionaly,
    perform sigma-clipping to reject suspicious dataset.

    Parameters:
    -----------
    `list_nrm` : {list}
        List of classes containing extracted NRM data (see bispect.py) of multiple calibrator files,\n
    `sig_thres` : {float}
        Threshold of the sigma clipping (default: 2-sigma around the median is used),\n    
     """
    nfiles = len(list_nrm)
    l_pa = np.zeros(nfiles)
    cp_vs_file, e_cp_vs_file = [], []
    vis2_vs_file, e_vis2_vs_file = [], []

    # Fill array containing each vis2 and cp across files.
    for n in range(nfiles):
        nrm = list_nrm[n]
        hdu = fits.open(nrm.infos.filename)
        hdr = hdu[0].header
        try:
            # todo: Check parallactic angle param of a real NIRISS header.
            l_pa[n] = hdr['PARANG']
        except KeyError:
            l_pa[n] = 0

        cp = nrm.cp
        e_cp = nrm.e_cp
        vis2 = nrm.vis2
        e_vis2 = nrm.e_vis2

        cp_vs_file.append(cp)
        e_cp_vs_file.append(e_cp)
        vis2_vs_file.append(vis2)
        e_vis2_vs_file.append(e_vis2)

    bl = list_nrm[0].bl

    cp_vs_file = np.array(cp_vs_file)
    e_cp_vs_file = np.array(e_cp_vs_file)
    vis2_vs_file = np.array(vis2_vs_file)
    e_vis2_vs_file = np.array(e_vis2_vs_file)

    zero_uncer = (e_vis2_vs_file == 0)
    e_vis2_vs_file[zero_uncer] = np.max(e_vis2_vs_file)

    cmn_vis2, std_vis2 = wtmn(vis2_vs_file, e_vis2_vs_file)
    cmn_cp, std_cp = wtmn(cp_vs_file, e_cp_vs_file)

    # Apply sigma clipping on the averages
    cmn_vis2_clip, std_vis2_clip = _apply_sig_clip(vis2_vs_file,
                                                   e_vis2_vs_file,
                                                   sig_thres=sig_thres,
                                                   var='V2',
                                                   display=display)
    cmn_cp_clip, std_cp_clip = _apply_sig_clip(cp_vs_file,
                                               e_cp_vs_file,
                                               sig_thres=sig_thres,
                                               ymax=10,
                                               var='CP',
                                               display=display)

    res = {
        'f_v2_clip': np.array(cmn_vis2_clip),
        'f_v2': np.array(cmn_vis2),
        'std_vis2_clip': np.array(std_vis2_clip),
        'std_vis2': np.array(std_vis2),
        'f_cp_clip': np.array(cmn_cp_clip),
        'f_cp': np.array(cmn_cp),
        'std_cp_clip': np.array(std_cp_clip),
        'std_cp': np.array(std_cp),
        'bl': bl,
        'pa': l_pa
    }

    return dict2class(res)
def _apply_sig_clip(data,
                    e_data,
                    sig_thres=2,
                    ymin=0,
                    ymax=1.2,
                    var="V2",
                    display=False):
    """Apply the sigma-clipping on the dataset and plot some diagnostic plots."""
    import matplotlib.pyplot as plt
    from astropy.stats import sigma_clip

    filtered_data = sigma_clip(data, sigma=sig_thres, axis=0)

    n_files = data.shape[0]
    n_pts = data.shape[1]  # baselines or bs number

    mn_data_clip, std_data_clip = [], []
    for i in range(n_pts):
        cond = filtered_data[:, i].mask
        data_clip = data[:, i][~cond]
        e_data_clip = e_data[:, i][~cond]
        cmn, std = wtmn(data_clip, weights=e_data_clip)
        mn_data_clip.append(cmn)
        std_data_clip.append(std)

    data_med = np.median(data, axis=0)

    if var == "V2":
        ylabel = r"Raw V$^2$"
        xlabel = "# baselines"
    else:
        ylabel = r"Raw closure Phases $\Phi$ [deg]"
        xlabel = "# bispectrum"
        ymin = -ymax

    if display:
        plt.figure()
        plt.title("CALIBRATOR")
        if n_files != 1:
            plt.plot(data[0], color="grey", alpha=0.2, label="Data vs. files")
            plt.plot(data[1:, :].T, color="grey", alpha=0.2)
        plt.plot(data_med, "g--", lw=1, label="Median value")
        for i in range(n_files):
            data_ind = data[i, :]
            cond = filtered_data[i, :].mask
            x = np.arange(len(data_ind))
            bad = data_ind[cond]
            x_bad = x[cond]
            if i == 0:
                plt.plot(
                    x_bad,
                    bad,
                    "rx",
                    ms=3,
                    label=r"Rejected points (>%i$\sigma$)" % sig_thres,
                )
            else:
                plt.plot(x_bad, bad, "rx", ms=3)
        plt.legend(loc="best", fontsize=9)
        plt.ylim(ymin, ymax)
        plt.xlabel(xlabel)
        plt.ylabel(ylabel)
        plt.tight_layout()
    return np.array(mn_data_clip), np.array(std_data_clip)