Commit db762d9f authored by Zolnierczuk, Piotr's avatar Zolnierczuk, Piotr
Browse files

further improvements to nseplot

parent 1ad0720b
Loading
Loading
Loading
Loading
+52 −37
Original line number Diff line number Diff line
@@ -13,6 +13,21 @@ from pysen.plotutil import get_nsubplots
from pysen.io import echo_to_hdf
from pysen.atarilib import fit_echo_current, Spectrum

def plot_single_echo(ax, data, fit, nptr, res=None, label=None):
    x, y, yerr = data
    xef, yef   = fit
    nph, nup   = nptr
    p = ax.errorbar(x[:nph], y[:nph], yerr=yerr[:nph], fmt='.', lw=2, ms=3)
    col = p[0].get_color()
    ax.errorbar(x[nph:nup], y[nph:nup], yerr=yerr[nph:nup], fmt='.', color=col, ms=3)
    ax.errorbar(x[nup:   ], y[nup:   ], yerr=yerr[nup:   ], fmt='.', color=col, ms=3)
    ax.plot(xef, yef, '-', label=label)
    p = ax.axvline(res['cur0'], lw=1, ls='--')
    col = p.get_color()
    ax.axhline(res['up'][0], color=col, lw=1, ls='-')
    ax.axhline(res['dn'][0], color=col, lw=1, ls='-')
    ax.axhline(res['average'][0], color=col, lw=1, ls='-')

MAX_CHI2_DEFAULT = 1e-2
def plot_field(ax,t,v,**kwargs):
    "plot mag field"
@@ -45,11 +60,14 @@ def plot_field(ax,t,v,**kwargs):
    ax.legend(loc='upper left')
    if chi2>max_chi2:
        ax.set_facecolor('yellow')
        return False, chi2
    return True, chi2

def magnetic_fields_plot(hdfile, iecho=0, **kwargs):
    "plot magnetic fields"
    #
    max_chi2 = kwargs.get('max_chi2', MAX_CHI2_DEFAULT)
    first_point = kwargs.get('first_point', 1) # skip 1st point
    #
    base    = os.path.splitext(os.path.basename(hdfile.filename))[0]

@@ -86,11 +104,14 @@ def magnetic_fields_plot(hdfile, iecho=0, **kwargs):
        fig.suptitle(r'%s | %s | $\lambda$=%.2g$\AA$ $Q$=%.3f$\AA^{-1}$ $\tau$=%.3fns' %
                     (sample, base, lam0/ANGSTROM, q0, tau0))

        log = logging.getLogger()
        for iplt, bsensor in enumerate(bfield):
            bmag  = bfield[bsensor]
            label = bsensor.split('.')[-1]
            ax    = axes[iplt//nxplot,iplt%nxplot]
            plot_field(ax, dj[:nph]/MICRO, bmag[:nph,:]*GAUSS/MICRO, label=label, max_chi2=max_chi2)
            res, chi2 = plot_field(ax, dj[first_point:nph]/MICRO, bmag[first_point:nph,:]*GAUSS/MICRO, label=label, max_chi2=max_chi2)
            if not res:
                log.warning('*** field variation detected %s %s', label,chi2)
        for iplt in range(iplt+1, nxplot*nyplot):
            ax  = axes[iplt//nxplot,iplt%nxplot]
            ax.axis('off')
@@ -193,13 +214,13 @@ def atari_plot(hdfile, iecho=0, **kwargs):
        res['dn'] = np.average(dn),np.sqrt(np.sum(dn)/len(dn))
        #for key in res:
        #    print(key, res[key])
        ax = plt.gca()

        p = plt.errorbar(cur[:nph], pha, yerr=epha, fmt='.', lw=1)
        p = ax.errorbar(cur[:nph], pha, yerr=epha, fmt='.', lw=1)
        dcol = p[0].get_color()
        plt.plot(xef, yef, '--')
        plt.axvline(phase0,   color='blue',  lw=1, ls='--', label='%.3fA' % phase0  )
        plt.axvline(phase_ef, color='red',   lw=2, ls='-',  label='%.3fA' % phase_ef)
        ax = plt.gca()

        if not only_echo:
            R = 2*res['amplitude'][0]/( res['up'][0] - res['dn'][0] )
@@ -214,6 +235,7 @@ def atari_plot(hdfile, iecho=0, **kwargs):
        plt.grid(True)
        plt.suptitle(r'%s    $Q$=%.3f$\AA^{-1}$ $\tau$=%.3gns' % (comment.decode("utf-8"), q0,tau0))


def echo_plot(hdfile, iecho=0, **kwargs):
    "echo plot"
    #
@@ -246,6 +268,7 @@ def echo_plot(hdfile, iecho=0, **kwargs):
    ny      = hdfile['/detector'].attrs['no_y_channels']
    nx      = hdfile['/detector'].attrs['no_x_channels']
    nph     = n_idx['dn']
    miny, maxy    = np.inf, 0

    if  center_only:
        ntaus = 0
@@ -306,26 +329,28 @@ def echo_plot(hdfile, iecho=0, **kwargs):
                                                     lam0=lam0, phase0=phase0, phasesens=phasesens)

        if center_only:
            ax = fig0.add_subplot(nxtau, nytau, itau)
            fig0.suptitle(r'%s | %s | $\lambda$=%.2g$\AA$ $Q$=%.3f$\AA^{-1}$ ' %
                          (sample, base, lam0/ANGSTROM, q0))
            ax.errorbar(cur[:nph], y[:nph], yerr=ey[:nph], fmt='k.', lw=2, ms=3)
            ax.plot(xef, yef, 'r-', label=r'$\tau$=%.3gns' % tau0)
            ax.axvline(phase_ef0, color='blue',   lw=1, ls='--')
            ax.errorbar(cur[nph:n_idx['up']], dn, fmt='k.', ms=3)
            ax.errorbar(cur[n_idx['up']:   ], up, fmt='k.', ms=3)
            upave = np.average(up)
            dnave = np.average(dn)
            udave = (upave+dnave)/2
            res['up'] = upave, np.sqrt(np.sum(up)/len(up))
            res['dn'] = dnave, np.sqrt(np.sum(dn)/len(dn))
            res['cur0'] = phase_ef0
            #
            ax = fig0.add_subplot(nxtau, nytau, itau)
            fig0.suptitle(r'%s | %s | $\lambda$=%.2g$\AA$ $Q$=%.3f$\AA^{-1}$ ' %
                          (sample, base, lam0/ANGSTROM, q0))
            plot_single_echo(ax, (cur, y, ey), (xef, yef), (nph,n_idx['up']),
                res=res, label=r'$\tau$=%.3gns' % tau0)

            R = 2*res['amplitude'][0]/( res['up'][0] - res['dn'][0] )
            resolution.append((tau0, R))
            ax.legend()
            ax.legend(loc='upper left', fontsize='small')
            ax.grid()
            #ax.set_xticklabels([])
            if itau%nytau!=1:
                ax.set_yticklabels([])
            if max(y)>maxy:
                maxy = max(y)
            if min(y)<miny:
                miny = min(y)
            continue

        npx, nx, ny, nt = det.shape
@@ -371,38 +396,29 @@ def echo_plot(hdfile, iecho=0, **kwargs):
                #
                if res['chi2']>max_chi2:
                    continue
                res['up'] = upave, np.sqrt(np.sum(up)/len(up))
                res['dn'] = dnave, np.sqrt(np.sum(dn)/len(dn))
                R = 2*res['amplitude'][0]/( res['up'][0] - res['dn'][0] )
                R = 2*res['amplitude'][0]/(upave - dnave)
                if abs(R)<min_amp:
                    continue

                ax.errorbar(cur[:nph], y[:nph], yerr=ey[:nph], fmt='k.',
                            lw=1, ms=1, label='(%s,%s)' % (i,j))
                res['up'] = upave, np.sqrt(np.sum(up)/len(up))
                res['dn'] = dnave, np.sqrt(np.sum(dn)/len(dn))
                res['cur0'] = phase_ef
                plot_single_echo(ax, (cur, y, ey), (xef, yef), (nph,n_idx['up']),
                                 res=res, label='(%s,%s)' % (i,j))
                ax.set_ylim(bottom=ymin, top=ymax)
                ax.plot(xef, yef, 'r-')
                ax.axvline(phase_ef, color='blue',   lw=1, ls='--')
                ax.errorbar(cur[nph:n_idx['up']], dn, fmt='k.', ms=1)
                ax.errorbar(cur[n_idx['up']:   ], up, fmt='k.', ms=1)
                if R<0:
                    ax.set_facecolor('yellow')
                #ax.legend()
                #    plt.text(0.01, 1.01, r'$R(Q,t)$=%.3g' % R, transform=ax.transAxes)
                #ax.axhline(np.average(up))
                #ax.axhline(np.average(dn))
        #plt.xlabel(r'ephase current [A]')

        #plt.legend(loc='best')
        #plt.grid(True)
        fig.suptitle(r'%s | %s | $\lambda$=%.2g$\AA$ $Q$=%.3f$\AA^{-1}$ $\tau$=%.3gns' %
                     (sample, base, lam0/ANGSTROM, q0,tau0))

    if center_only and ntaus>1:
        #      figr.suptitle(r'%s | %s' % (sample, base))
        #fig0.subplots_adjust(wspace=0.25)
    if center_only:
        #print(miny, maxy)
        for ax in fig0.get_axes():
            ax.set_ylim(bottom=miny*0.8, top=maxy*1.2)
        if ntaus<=1:
            return
        ax  = fig0.add_subplot(nxtau,nytau,ntaus)
        res = np.asarray(resolution)

        ax.plot(res[:,0],res[:,1], '.--')
        ax.set_xscale('log')
        ax.set_yscale('log')
@@ -410,7 +426,6 @@ def echo_plot(hdfile, iecho=0, **kwargs):
        top=max(1.1,max(res[:,1])))
        ax.grid(True)


def setup_logger(loglevel):
    "setup logger"
    #normalize loglevel 0=ERR, 1=WARN, 2=INFO, 3=DEBUG