예제 #1
0
def plot_fits(ids, posdir=None, outdir='fit'):

    if not os.path.exists(outdir):
        os.makedirs(outdir)

    for id in ids:
        obsid = id['obsid']
        data_id = id['data_id']
        ax = id['ax']
        pf = open(os.path.join(posdir, 'pos.pkl'), 'r')
        pos = cPickle.load(pf)
        pf.close()

        pos_data = pos[ax]

        fig = plt.figure(figsize=(6,4))
        axplot = ui.get_model_plot(data_id)
        plt.plot( axplot.x/1000., axplot.y, 'b.')
        [ts, ds, dsminus, dsplus] = binned_mean(
            pos_data-np.mean(pos_data),
            pos['time']-pos['time'][0] )
        if not len(ts):
            continue
        plt.errorbar((ts-ts[0])/1000., ds,
                     yerr=dsminus, color='k', marker='.', linestyle='')
        plt.xlabel('Time (ksec)')
        plt.ylabel('%s' % ax)
        plt.subplots_adjust(left=.15)
        plt.title('Model(Blue), BinMean Pos(Black) (obs=%s)' % (obsid))
        plt.ylim(-.3,.3)
        plt.grid()
        plt.savefig('%s/%s_%s_sfit.png' % (outdir, obsid, ax))
        plt.close()
예제 #2
0
def _fit_poly(fit_data, evt_times, degree, data_id=0):
    """
    Given event data transformed into Y or Z angle positions, and a degree of the desired
    fit polynomial, fit a polynomial to the data.

    :param fit_data: event y or z angle position data
    :param evt_times: times of event/fit_data
    :param degree: degree of polynomial to use for the fit model
    :param data_id: sherpa dataset id to use for the fit

    :returns: (sherpa model plot, sherpa model)
    """
    # Set initial value for fit data position error
    init_error = 1

    ui.clean()
    ui.load_arrays(data_id, evt_times - evt_times[0], fit_data,
                   np.zeros_like(fit_data) + init_error)
    v2("Fitting a line to the data to get reduced stat errors")
    # First just fit a line to get reduced errors on this set
    ui.polynom1d.line
    ui.set_model(data_id, 'line')
    ui.thaw('line.c1')
    ui.fit(data_id)
    fit = ui.get_fit_results()
    calc_error = init_error * np.sqrt(fit.rstat)
    ui.set_staterror(data_id, calc_error)
    # Then fit the specified model
    v2("Fitting a polynomial of degree {} to the data".format(degree))
    ui.polynom1d.fitpoly
    ui.freeze('fitpoly')
    # Thaw the coefficients requested by the degree of the desired polynomial
    ui.thaw('fitpoly.c0')
    fitpoly.c0.val = 0
    for deg in range(1, 1 + degree):
        ui.thaw("fitpoly.c{}".format(deg))
    ui.set_model(data_id, 'fitpoly')
    ui.fit(data_id)
    # Let's screw up Y on purpose
    if data_id == 0:
        fitpoly.c0.val = 0
        fitpoly.c1.val = 7.5e-05
        fitpoly.c2.val = -1.0e-09
        fitpoly.c3.val = 0
        fitpoly.c4.val = 0
    mp = ui.get_model_plot(data_id)
    model = ui.get_model(data_id)
    return mp, model
def _fit_poly(fit_data, evt_times, degree, data_id=0):
    """
    Given event data transformed into Y or Z angle positions, and a degree of the desired
    fit polynomial, fit a polynomial to the data.

    :param fit_data: event y or z angle position data
    :param evt_times: times of event/fit_data
    :param degree: degree of polynomial to use for the fit model
    :param data_id: sherpa dataset id to use for the fit

    :returns: (sherpa model plot, sherpa model)
    """
    # Set initial value for fit data position error
    init_error = 1

    ui.clean()
    ui.load_arrays(data_id, evt_times - evt_times[0], fit_data,
                   np.zeros_like(fit_data) + init_error)
    v2("Fitting a line to the data to get reduced stat errors")
    # First just fit a line to get reduced errors on this set
    ui.polynom1d.line
    ui.set_model(data_id, 'line')
    ui.thaw('line.c1')
    ui.fit(data_id)
    fit = ui.get_fit_results()
    calc_error = init_error * np.sqrt(fit.rstat)
    ui.set_staterror(data_id, calc_error)
    # Then fit the specified model
    v2("Fitting a polynomial of degree {} to the data".format(degree))
    ui.polynom1d.fitpoly
    ui.freeze('fitpoly')
    # Thaw the coefficients requested by the degree of the desired polynomial
    ui.thaw('fitpoly.c0')
    fitpoly.c0.val = 0
    for deg in range(1, 1 + degree):
        ui.thaw("fitpoly.c{}".format(deg))
    ui.set_model(data_id, 'fitpoly')
    ui.fit(data_id)
    mp = ui.get_model_plot(data_id)
    model = ui.get_model(data_id)
    return mp, model
예제 #4
0
def plot_fits(ids, posdir=None, outdir='fit'):

    if not os.path.exists(outdir):
        os.makedirs(outdir)

    for id in ids:
        obsid = id['obsid']
        data_id = id['data_id']
        ax = id['ax']
        pf = open(os.path.join(posdir, 'pos.pkl'), 'r')
        pos = cPickle.load(pf)
        pf.close()

        pos_data = pos[ax]

        fig = plt.figure(figsize=(6, 4))
        axplot = ui.get_model_plot(data_id)
        plt.plot(axplot.x / 1000., axplot.y, 'b.')
        [ts, ds, dsminus, dsplus] = binned_mean(pos_data - np.mean(pos_data),
                                                pos['time'] - pos['time'][0])
        if not len(ts):
            continue
        plt.errorbar((ts - ts[0]) / 1000.,
                     ds,
                     yerr=dsminus,
                     color='k',
                     marker='.',
                     linestyle='')
        plt.xlabel('Time (ksec)')
        plt.ylabel('%s' % ax)
        plt.subplots_adjust(left=.15)
        plt.title('Model(Blue), BinMean Pos(Black) (obs=%s)' % (obsid))
        plt.ylim(-.3, .3)
        plt.grid()
        plt.savefig('%s/%s_%s_sfit.png' % (outdir, obsid, ax))
        plt.close()
예제 #5
0
     ax.relim()
     ax.autoscale_view()
 else:
     ax.plot(x, y)
 ax.texts = []
 ax.annotate("{}".format(y.name),
             xy=(0.5, 0.5),
             xycoords="axes fraction",
             ha='center',
             va='center',
             color='grey')
 t_ccd = dat['TEMPCD']
 try:
     fit, modpars = fit_pix_values(t_ccd, y, id=i_col)
     fits[y.name] = {'fit': fit, 'modpars': modpars}
     fitmod = ui.get_model_plot(i_col)
 except Exception as exception:
     pix_log.warn('Sherpa fit failed on {}'.format(y.name))
     pix_log.warn(exception)
     pix_log.warn('Continuing')
     fits[y.name] = None
     continue
 if len(ax.lines) > 1:
     l1 = ax.lines[1]
     l1.set_data(x, fitmod.y)
     ax.relim()
     ax.autoscale_view()
 else:
     ax.plot(x, fitmod.y, color='red')
 if opt.plot_fit_curves:
     fitax = fitaxes[r][c]
예제 #6
0
    ui.set_method('simplex')
    ui.load_arrays(data_id,
                   rates['time'],
                   rates['rate'])
    ui.set_staterror(data_id,
                     rates['err'])

    ftype_poly = ui.polynom1d(ftype)
    ui.set_model(data_id, ftype_poly)
    ui.thaw(ftype_poly.c0)
    ui.thaw(ftype_poly.c1)
    ui.notice(DateTime(trend_date_start).frac_year)
    ui.fit(data_id)
    ui.notice()
    myfit = ui.get_fit_results()
    axplot = ui.get_model_plot(data_id)
    if myfit.succeeded:
        b = ftype_poly.c1.val * DateTime(trend_date_start).frac_year + ftype_poly.c0.val
        m = ftype_poly.c1.val
        rep_file = open('%s_fitfile.json' % ftype, 'w')
        rep_file.write(json.dumps(dict(time0=DateTime(trend_date_start).frac_year,
                                       datestart=trend_date_start,
                                       datestop=data_stop,
                                       bin=trend_type,
                                       m=m,
                                       b=b,
                                       comment="mx+b with b at time0 and m = (delta rate)/year"),
                                  sort_keys=True,
                                  indent=4))
        rep_file.close()
    else:
y = dat[best_pix] * GAIN / integ
# ax.yaxis.set_label_position("right")
# ax.yaxis.tick_right()
# ax.grid()
# plot_cxctime(dat['time'], y, 'k', ax=ax)
# ax.set_ylabel('Dark Current (e-/sec)')
# ccdax.set_ylabel('CCD Temp (DegC)')
# ax.texts = []
# ax.annotate("{}".format(y.name),
#            xy=(0.5, 0.5), xycoords="axes fraction",
#            ha='center', va='center',
#            color='lightgrey')
t_ccd = dat["TEMPCD"]
fit, modpars = fit_pix_values(t_ccd, y, id=i_col)
fits[y.name] = {"fit": fit, "modpars": modpars}
fitmod = ui.get_model_plot(i_col)
# plot_cxctime(DateTime(dat['time'][0]).secs + x, fitmod.y, color='red', ax=ax)
# plt.tight_layout()
plot2 = plot_two(fig_id="overplots", x2=dat["time"], y2=y, x=dat["time"], y=t_ccd, color="blue", color2="black")
plot_cxctime(
    DateTime(dat["time"][0]).secs + x,
    fitmod.y,
    color="red",
    ax=plot2["ax2"],
    linewidth=6,
    alpha=0.5,
    label="Dark current model",
)
plot2["ax2"].legend(loc="upper left", fontsize=10)
plot2["ax"].yaxis.label.set_color("blue")
plot2["ax"].tick_params(axis="y", colors="blue")