Exemplo n.º 1
0
    def get_light_curves(self, class_nums=(1,), nprocesses=1):
        light_curves = {}

        for class_num in class_nums:
            lcs = get_data(self.get_data_func, class_num, self.data_dir, self.save_dir, self.passbands,
                           self.known_redshift, nprocesses, self.reread_data)
            light_curves.update(lcs)

        return light_curves
def main():
    nprocesses = 1
    class_num = 1
    extrapolate = True
    SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
    data_dir = os.path.join(SCRIPT_DIR, '', 'data/ZTF_20190512')
    save_dir = os.path.join(SCRIPT_DIR, '..', 'data/saved_light_curves')

    light_curves = get_data(
        get_data_func=astrorapid.get_training_data.get_data_from_snana_fits,
        class_num=class_num,
        data_dir=data_dir,
        save_dir=save_dir,
        nprocesses=nprocesses,
        redo=False,
        calculate_t0=False)
    saved_gp_fits = save_gps(light_curves,
                             save_dir=save_dir,
                             class_num=class_num,
                             passbands=('g', 'r'),
                             plot=True,
                             nprocesses=nprocesses,
                             redo=True,
                             extrapolate=extrapolate)
def plot_metrics(model,
                 model_name,
                 X_test,
                 y_test,
                 timesX_test,
                 yerr_test,
                 labels_test,
                 objids_test,
                 passbands,
                 fig_dir,
                 nsamples,
                 data_dir,
                 save_dir,
                 nprocesses,
                 plot_gp=False,
                 extrapolate_gp=True,
                 reframe=False,
                 plot_name='',
                 npred=49,
                 probabilistic=False,
                 known_redshift=False,
                 get_data_func=None,
                 normalise=False,
                 bayesian=False):
    print(model_name)
    nobjects, ntimesteps, nfeatures = X_test.shape
    npassbands = len(passbands)

    sampled_ypred = []
    sampled_ystd = []
    draws = []
    if probabilistic:
        X_test = np.asarray(X_test, np.float32)
        y_test = np.asarray(y_test, np.float32)
        yhat = model(X_test)
        y_pred = np.asarray(yhat.mean())
        y_pred_std = np.asarray(yhat.stddev())
        if bayesian:
            ns = 100
            for i in range(ns):
                sampled_yhat = model(X_test)
                sampled_ypred.append(np.asarray(sampled_yhat.mean()))
                sampled_ystd.append(np.asarray(sampled_yhat.stddev()))
                draws.append(
                    np.random.normal(sampled_yhat.mean(),
                                     sampled_yhat.stddev()))
            # plot_mean_ypred = np.mean(np.array(sampled_ypred), axis=0)
            # plot_sigma_ypred = np.std(np.array(sampled_ypred), axis=0)
            plot_mean_ypred = np.mean(np.array(draws), axis=0)
            plot_sigma_ypred = np.std(np.array(draws), axis=0)
        else:
            yhat = model(X_test)
            plot_mean_ypred = np.asarray(yhat.mean())
            plot_sigma_ypred = np.asarray(yhat.stddev())
    else:
        y_pred = model.predict(X_test)

    if not reframe:
        npred = ntimesteps

    # Get raw light curve data
    light_curves = {}
    gp_fits = {}
    for classnum in np.unique(labels_test):
        print(f"Getting lightcurves for class:{classnum}")
        light_curves[classnum] = get_data(get_data_func=get_data_func,
                                          class_num=classnum,
                                          data_dir=data_dir,
                                          save_dir=save_dir,
                                          passbands=passbands,
                                          known_redshift=known_redshift,
                                          nprocesses=nprocesses,
                                          redo=False,
                                          calculate_t0=False)
        if plot_gp is True and nsamples == 1:
            gp_fits[classnum] = save_gps(light_curves,
                                         save_dir,
                                         classnum,
                                         passbands,
                                         plot=False,
                                         nprocesses=nprocesses,
                                         redo=False,
                                         extrapolate=extrapolate_gp)

    # # Plot predictions vs time per class
    # font = {'family': 'normal',
    #         'size': 36}
    # matplotlib.rc('font', **font)

    for idx in np.arange(0, 10):
        sidx = idx * nsamples  # Assumes like samples are in order
        print("Plotting example vs time", idx, objids_test[sidx])
        argmax = None  # timesX_test[sidx].argmax()  # -1

        # Get raw light curve observations
        lc = light_curves[labels_test[sidx]][objids_test[sidx]]
        if plot_gp is True and nsamples == 1:
            gp_lc = gp_fits[labels_test[sidx]][objids_test[sidx]]

        fig, (ax1, ax2) = plt.subplots(nrows=2,
                                       ncols=1,
                                       figsize=(13, 15),
                                       sharex=True)

        for pbidx, pb in enumerate(passbands):
            pbmask = lc['passband'] == pb

            for s in range(1):  # nsamples):
                lw = 3 if s == 0 else 0.5
                alpha = 1 if s == 0 else 0.1
                plotlabeltest = "ytest:{}".format(pb) if s == 0 else ''
                plotlabelpred = "ypred:{}".format(pb) if s == 0 else ''
                marker = None  # MARKPB[pb] if s == 0 else None
                if reframe:
                    ax1.plot(timesX_test[sidx + s][:-1][:argmax],
                             X_test[sidx + s][:, pbidx][:-1][:argmax],
                             c=COLPB[pb],
                             lw=lw,
                             label=plotlabeltest,
                             marker=marker,
                             markersize=10,
                             alpha=alpha,
                             linestyle='-')
                ax1.plot(timesX_test[sidx + s][1:][-npred:][:argmax],
                         y_test[sidx + s][:, pbidx][:argmax],
                         c=COLPB[pb],
                         lw=lw,
                         label=plotlabeltest,
                         marker='o',
                         markersize=10,
                         alpha=alpha,
                         linestyle='-')
                if probabilistic:
                    if bayesian:
                        for sp in range(ns):
                            ax1.errorbar(
                                timesX_test[sidx + s][1:][-npred:][:argmax],
                                sampled_ypred[sp][sidx + s][:, pbidx][:argmax],
                                yerr=sampled_ystd[sp][sidx +
                                                      s][:, pbidx][:argmax],
                                color=COLPB[f'{pb}pred'],
                                lw=0.5,
                                marker='*',
                                markersize=10,
                                alpha=1 / 256,
                                linestyle=':')
                    ax1.errorbar(timesX_test[sidx + s][1:][-npred:][:argmax],
                                 plot_mean_ypred[sidx + s][:, pbidx][:argmax],
                                 yerr=plot_sigma_ypred[sidx +
                                                       s][:, pbidx][:argmax],
                                 color=COLPB[f'{pb}pred'],
                                 lw=lw,
                                 label=plotlabelpred,
                                 marker='x',
                                 markersize=20,
                                 alpha=1,
                                 linestyle=':')

                else:
                    ax1.plot(timesX_test[sidx + s][1:][-npred:][:argmax],
                             y_pred[sidx + s][:, pbidx][:argmax],
                             c=COLPB[f'{pb}pred'],
                             lw=lw,
                             label=plotlabelpred,
                             marker='*',
                             markersize=10,
                             alpha=alpha,
                             linestyle=':')

        if not normalise:
            ax1.errorbar(
                lc[pbmask]['time'].data,
                lc[pbmask]['flux'].data,
                yerr=lc[pbmask]['fluxErr'].data,
                fmt="x",
                capsize=0,
                color=COLPB[pb],
                label='_nolegend_',
                markersize=15,
            )

            if plot_gp is True and nsamples == 1:
                gp_lc[pb].compute(lc[pbmask]['time'].data,
                                  lc[pbmask]['fluxErr'].data)
                pred_mean, pred_var = gp_lc[pb].predict(
                    lc[pbmask]['flux'].data,
                    timesX_test[sidx + s][:argmax],
                    return_var=True)
                pred_std = np.sqrt(pred_var)
                ax1.fill_between(timesX_test[sidx + s][:argmax],
                                 pred_mean + pred_std,
                                 pred_mean - pred_std,
                                 color=COLPB[pb],
                                 alpha=0.05,
                                 edgecolor="none")
            # ax1.text(0.05, 0.95, f"$\chi^2 = {round(save_chi2[objids_test[idx]], 3)}$", horizontalalignment='left',
            #          verticalalignment='center', transform=ax1.transAxes)
            plt.xlim(-70, 80)

        # Plot anomaly scores
        chi2_samples = []
        for s in range(1):  # nsamples):
            chi2 = 0
            for pbidx in range(npassbands):
                m = yerr_test[
                    sidx + s, :,
                    pbidx][:argmax] != 0  # ignore zeros (where no data exists)
                yt = y_test[sidx + s, :, pbidx][:argmax][m]
                yp = y_pred[sidx + s, :, pbidx][:argmax][m]
                ye = yerr_test[sidx + s, :, pbidx][:argmax][m]
                try:
                    chi2 += ((yp - yt) / ye)**2
                except ValueError as e:
                    pbidx -= 1
                    m = yerr_test[sidx + s, :, pbidx][:argmax] != 0
                    print(f"Failed chi2 object {objids_test[sidx + s]}", e)
            chi2_samples.append(chi2 / npassbands)
        anomaly_score_samples = chi2_samples
        anomaly_score_mean = np.mean(anomaly_score_samples, axis=0)
        anomaly_score_std = np.std(anomaly_score_samples, axis=0)
        ax2.text(
            0.05,
            0.95,
            f"$\chi^2 = {round(np.sum(anomaly_score_mean) / len(yt), 3)}$",
            horizontalalignment='left',
            verticalalignment='center',
            transform=ax2.transAxes)

        ax2.plot(timesX_test[sidx][1:][-npred:][:argmax][m],
                 anomaly_score_mean,
                 lw=3,
                 marker='o')
        ax2.fill_between(timesX_test[sidx][1:][-npred:][:argmax][m],
                         anomaly_score_mean + anomaly_score_std,
                         anomaly_score_mean - anomaly_score_std,
                         alpha=0.3,
                         edgecolor="none")

        ax1.legend(frameon=True, fontsize=33)
        ax1.set_ylabel("Relative flux")
        ax2.set_ylabel("Anomaly score")
        ax2.set_xlabel("Time since trigger [days]")
        plt.tight_layout()
        fig.subplots_adjust(hspace=0)
        plt.savefig(
            os.path.join(fig_dir, model_name,
                         f"lc_{objids_test[sidx]}_{idx}{plot_name}.pdf"))
        plt.close()

    print(model_name)
Exemplo n.º 4
0
    def get_light_curves(self, class_num, nprocesses=1):
        light_curves = get_data(get_data_func=self.get_data_func, class_num=class_num, data_dir=self.data_dir,
                                save_dir=self.save_dir, passbands=self.passbands, known_redshift=self.known_redshift,
                                nprocesses=nprocesses, redo=self.redo, calculate_t0=False)

        return light_curves
Exemplo n.º 5
0
    def get_light_curves(self, class_num, nprocesses=1):
        light_curves = get_data(class_num, self.data_dir, self.save_dir, self.passbands, nprocesses, self.reread)

        return light_curves