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

removed extra python libraries

parent 5bfe8bf9
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line

__all__ = ['plot_sqt', 'plot_sqt_bin', 'plot_fits', 'plot_maps']

from .drspine import plot_sqt , plot_sqt_bin
from .fits    import plot_fits, plot_maps
+0 −131
Original line number Diff line number Diff line
#!/usr/bin/env python
"""
Python interface to DrSPINE
"""

import sys, os
import ast
import numpy as np
import matplotlib.pyplot as plt

from collections import OrderedDict
from itertools import ifilter

# unbuffered output
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

NS = 1e-9
ANGSTROEM = 1e-10

def get_color(color):
    "Get consistent colors for plotting"
    import colorsys
    while True:
        for hue in range(color):
            hue = 1. * hue / float(color)
            col = [int(x) for x in colorsys.hsv_to_rgb(hue, 0.667, 230)]
            yield "#{0:02x}{1:02x}{2:02x}".format(*col)

def average(arr, axis=None):
    marr = np.ma.masked_array(arr,np.isnan(arr))
    mavg = np.mean(marr,axis=axis)
    return mavg.filled(np.nan)

def _evaluate(value):
    "evaluate value"
    try:
        value = ast.literal_eval(value) # convert to numbers
    except (ValueError, SyntaxError):
        pass
    return value

def read_datreat(filename):
    """
    Read datreat type file produced by .e.g by echodet

    Returns list of (metadata, data) pairs
    """
    result = []             # result
    data   = []             # data
    meta   = OrderedDict()  # meta data
    with open(filename, 'r') as fdesc:
        lines = iter([ line.strip() for line in fdesc.readlines() ])
        try:
            while True:
                # -------- meta section ------------
                meta['master'] = next(lines)
                meta['info'  ] = next(lines)
                while next(lines)!='parameter':
                    continue
                for line in ifilter(lambda line: len(line)>0, lines):
                    if line=='values':
                        break
                    token = line.split()
                    key   = token[0]
                    value  = " ".join(token[1:])
                    meta[key] = _evaluate(value)
                # -------- data section ------------
                dataheader=False
                for line in ifilter(lambda line: len(line)>0, lines):
                    if line == '#eod' or line == '#nxt':
                        result.append((meta, np.asarray(data).T))
                        data = []
                        meta = OrderedDict()
                        break
                    try:
                        data.append([float(x) for x in line.split()])
                    except ValueError:
                        if not dataheader:
                            meta['dataheader']=line
                            dataheader=True
        except StopIteration:
            pass
    return result



def plot_sqt(filename):
    """
    Plot S(Q,tau) from a datreat type file

    filename - datreat type file
    """
    result = read_datreat(filename)
    acolor = get_color(len(result))

    plt.figure(figsize=(12,9))
    for meta, data in result:
        color = next(acolor)
        if not len(data):
            continue
        label = r"Q=(%.3f $\pm$ %.3f) $\AA^{-1}$" % (meta['q']*ANGSTROEM, meta['q_var']*ANGSTROEM)
        plt.errorbar(data[0], data[1], yerr=abs(data[2]), xerr=abs(data[4]), color=color, fmt='s-', label=label)

    plt.legend(loc='best')
    plt.xscale('log')
    plt.yscale('linear')
    plt.xlabel(r'$\tau$ [ns]')
    plt.ylabel(r'S(Q,$\tau$)')
    plt.grid(True, which='both')


def plot_sqt_bin(filename):
    """
    Plot Q,tau binning
    """
    # 0,1,2:  itau, ix, iy
    # 3, 4 :  tau_mean, q_mean
    # 5, 6 :  sqt, e_sqt
    # 7, 8 :  udratio, e_udratio (res)
    # 9,10 :  udratio, e_udratio (sig)
    #11,12 :  chisq (res), chisq (sig)

    s = np.loadtxt(filename, delimiter=',')

    plt.figure(figsize=(12,9))
    plt.plot(s[:,3], s[:,4], 'k.')

    plt.xscale('log')
    plt.grid(True, which='both')
    plt.xlabel(r'$\tau$ [ns]')
    plt.ylabel(r'Q [$\AA$]')
+0 −328
Original line number Diff line number Diff line
#!/usr/bin/env python
from __future__ import print_function

import os, sys
import re, glob
import numpy as np

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, LinearLocator


_FLOAT    = r'[-+]?\d+\.?\d*(E[-+]?\d+)?'
_RE_FLOAT = re.compile(r'(%s|nan|infinity)' % _FLOAT, re.IGNORECASE)

GM = 1e-4 # Gauss-meter in Tesla-meter


def read_data(fname):
    meta = dict()
    data = None
    if not os.path.exists(fname):
        return meta, data
    fd = open(fname)
    firstline = fd.readline()
    if firstline.startswith('!') or firstline.startswith('#'):
        # first line is a version line
        meta['title'] = fd.readline() # <title>
    else:
        meta['title'] = firstline
    for _i in range(7):
        line   = fd.readline()
        tokens = line.split()
        record  = [ float(_x) for _x, _e in re.findall(_RE_FLOAT, line) ]
        meta[tokens[0][:-2]] = record[0]
    data  = np.loadtxt(fd, delimiter=',').T
    fd.close()
    return meta, data

def save_fig(datadir, prefix="", suffix=""):
    datadir = os.path.normpath(datadir)
    datadir = os.path.basename(datadir)
    outfile='%s-%s-%s.pdf' % (prefix, datadir, suffix)
    print("saving to %s" % outfile, end="")
    plt.savefig(outfile)

def plotfit(itau, pix, splot, datadir='./echodet_tmp', miny=None, maxy=None,
            xticks=True, yticks=True, mark_pixels=False):

    nud = -8 # n last points are "up/down" measurements

    lw   = 0.3 # linewidth
    msiz = 2   # markersize


    label =     label='%s-%s' % (itau,pix)

    dname = os.path.join(datadir,"%s.daref.%s" % (itau,pix)) # data
    fname = os.path.join(datadir,"%s.firef.%s" % (itau,pix)) # fit

    mdat, dat = read_data(dname)
    mfit, fit = read_data(fname)

    if dat is None:
        return

    title = mdat.get('title','')
    tau   = mdat.get('tau')

    title = '%s-tau= %s ns' % (title,tau*1e9)
#    print("title title title = %s" % title)  

    ave   = mfit.get('ave', 0)
    amp   = mfit.get('amp', 0)
    sym   = mfit.get('sym', 0) or mfit.get('pha', 0)

    axes  = plt.subplot(*splot)
    if mark_pixels:
        if fit is None:
            axes.set_axis_bgcolor('lightgray')
        elif np.isnan(amp) or amp<0:
            axes.set_axis_bgcolor('yellow')

#    plt.errorbar(dat[0], dat[1], yerr=dat[2], fmt='.', label=label, color='blue')
    plt.errorbar(dat[0], dat[1], yerr=dat[2], fmt='.', label=label, color='blue',markersize=msiz,linewidth=lw)
    
    if fit is not None:
        plt.plot    (fit[0], fit[1], ls='-', color='red',linewidth=lw)
#        plt.axvline (sym, color='green')
#        plt.axhline (ave, color='green')

        plt.axvline (sym, color='red',linewidth=lw)
        plt.axhline (ave, color='blue',linewidth=lw)

    if maxy:
        axes.set_ylim(ymin=miny, ymax=maxy)

    if not xticks:
        axes.set_xticklabels([])
    else:
        axes.xaxis.set_major_locator(LinearLocator(numticks=3))

    if not yticks:
        axes.set_yticklabels([])
    else:
        axes.xaxis.set_major_locator(LinearLocator(numticks=3))

    plt.text(0.07,0.95, label, ha='left', va='top',transform = axes.transAxes,color='gray',fontsize=6)
    plt.grid(True)

    #print "done"
    if pix%2:
        sys.stdout.write('.')
    miny = np.min(dat[1])
    maxy = np.max(dat[1])
    return dict(axes=axes, title=title, tau=tau, limits=(miny, maxy))

def plotspect(itau, pix=0, subpl=(211,212),datadir='./echodet_tmp'):

    fname = os.path.join(datadir,"%s.spect.%s" % (itau,pix))
    print("loading %s" % fname, end="")
    dat   = np.loadtxt(fname, skiprows=10, dtype='float').T
    print('done')

    cols, rows = dat.shape

    tbins = range(1,rows+1)
    lbins = dat[5]*1e10      #  Angstroems
    # check for overflow
    mon1 = dat[0]*20.0
    mon2 = dat[1]

    mon1[np.where(mon1<0)] += np.power(2.0,32)
    mon2[np.where(mon2<0)] += np.power(2.0,32)


    plt.subplot(subpl[0])
    plt.step(lbins, dat[2], where='mid', color='black', label='detector')
    plt.step(lbins, dat[3], where='mid', ls='--', color='black')
    plt.step(lbins, dat[4], where='mid', color='red' ,  lw=2, label='window')
    plt.xlabel('Wavelength/A')
    plt.ylabel('Counts')
    plt.legend(loc='best')
    plt.grid(True)

    plt.subplot(subpl[1])
    plt.step(tbins, mon1,  where='mid', label='monitor1*20')
    plt.step(tbins, mon2,  where='mid', label='monitor2')
    plt.xlabel('Time Channel')
    plt.ylabel('Counts')
    plt.legend(loc='best')
    plt.grid(True)

def plot_fits(datadir, tau=1, npix=8, mark_pixels=True, limy=(0.0,None), save=False):
    """
    Plot echo fits
    """
    print("plotting fits tau=%-s" % tau, end="")

    npix2 = npix**2
    rows, cols = npix, npix
    koff  = cols - rows + 1
    nplot = rows*cols
    center = (npix+1)*npix//2
    miny, maxy = limy

    plt.figure(figsize=(10,10))
    plt.clf()
    plt.subplots_adjust(hspace=0.1, wspace=0.1)

    tit=''
    for pix in range(npix2):
        irow = pix  % npix
        icol = pix // npix
        k    = cols*irow + icol + koff
        meta = plotfit(tau, pix+1, (rows, cols, k), miny=miny, maxy=maxy, datadir=datadir,
                       xticks=False, yticks=False, mark_pixels=mark_pixels)
        if pix==center and meta and meta.get('title'):
            tit = meta.get('title')
#    tit = tit.split()[0]
#    plt.suptitle("%s (%s echo[%s])" % (tit, datadir, tau), fontsize=14)
    plt.suptitle("%s " % tit, fontsize=12)
    #plt.gcf().set_size_inches(24, 18)

    if save:
        save_fig(datadir, "fits", "%02d" % tau)
    print(" done.")

def plot_maps(datadir, tau=1, npix=8, mark_pixels=False, limz=(0.0,None), what='sym', scale=1.0, save=False):
    """
    Plot maps
    """
    print("plotting maps tau=%-s" % tau, end="")
    npix2 = npix**2
    rows, cols = npix, npix
    koff  = cols - rows + 1
    nplot = rows*cols
    center = (npix+1)*npix/2
    minz, maxz = limz

    plt.figure(figsize=(10,10))
    plt.clf()
    #plt.subplots_adjust(hspace=0.1, wspace=0.1)

    tit=''
    d = np.empty((npix,npix), dtype='float')
    for pix in range(npix2):
        #irow = (npix2-pix-1) % npix
        #icol = (npix2-pix-1) / npix
        irow = pix %  npix
        icol = pix // npix
        k    = cols*irow + icol + koff

        fname = os.path.join(datadir,"%s.firef.%s" % (tau,pix+1)) # fit
        meta, data = read_data(fname)
        if meta.get('title'):
            tit = meta.get('title')
        d[irow,icol] = meta.get(what, None)

    plt.imshow(d/scale, extent=(0,npix, npix,0), interpolation='nearest')
    if maxz:
        plt.clim(minz, maxz)
    plt.colorbar()
    plt.suptitle("%s (%s/%s[%s])" % (tit, datadir, what, tau), fontsize=14)
    #plt.gcf().set_size_inches(24, 18)

    if save:
        save_fig(datadir, "maps", "%02d" % tau)
    print(" done.")


def find_minmax(datadir, tau=None):
    if tau:
        datafiles = glob.glob(datadir+'/%s.daref.*' % tau)
    else:
        datafiles = glob.glob(datadir+'/*.daref.*')
    miny = np.finfo(np.float).max
    maxy = np.finfo(np.float).min
    for filename in datafiles:
        meta, data = read_data(filename)
        miny = min(miny, min(data[1]))
        maxy = max(maxy, max(data[1]))
    return miny, maxy

def get_info(datadir):
    # fixme
    datafiles = [ os.path.basename(_f) for _f in glob.glob(datadir+'/*.daref.*') ]
    taus = [ int(_f.split('.')[0]) for _f in datafiles ]
    pixs = [ int(_f.split('.')[2]) for _f in datafiles ]
    taus = sorted(set(taus))
    pixs = sorted(set(pixs))
    return taus, pixs




def main():
    import argparse
    parser = argparse.ArgumentParser(description='plot fits/maps')
    parser.set_defaults(datadir='.', tau=[],
                        npix=8, miny=0.0, maxy=0.0,
                        plot='fit', scale=1.0,
                        mark_pixels=False, do_show=True, do_save=False)

    parser.add_argument('tau', metavar='tau', nargs='*', type=int,
                        help='taus to display')

    parser.add_argument('--plot', '-P', dest='plot', metavar='plot',
                        help='plot action')
    parser.add_argument('--miny', '-y', dest='miny',    metavar='miny', type=float,
                     help='set min y (default 0.0)')
    parser.add_argument('--maxy', '-Y', dest='maxy',    metavar='maxy', type=float,
                        help='set max y (default auto)')
    parser.add_argument('--mark-pixels', '-M', dest='mark_pixels', action='store_true',
                        help='mark bad pixels')

    parser.add_argument('--npix', '-n', dest='npix',    metavar='npix', type=int,
                        help='set number of pixes (default %(default)s)')
    parser.add_argument('--scale','-s', dest='scale',    metavar='scale', type=float,
                        help='set scale for maps (default %(default)s)')

    parser.add_argument('--dir', '-D',  dest='datadir', metavar='dir',
                        help='set input dir (default %(default)s)')
    parser.add_argument('--no-show', '-N', dest='do_show', action='store_false',
                        help='do not show the plots')
    parser.add_argument('--save-fig','-o', dest='do_save', action='store_true',
                        help='save figure(s) to PDF')

    args = parser.parse_args()

    taus, pixs =  get_info(args.datadir)
    if not taus:
        parser.error("directory '%s' does not have any fit files" % args.datadir)

    taus = args.tau or taus

    # unbuffered output
    #sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)


    for tau in taus:
        if args.maxy<0:
            m1, m2 = find_minmax(args.datadir, tau)
            args.miny=m1
            args.maxy=m2

        if re.search('fit', args.plot):
            plot_fits(datadir=args.datadir,
                      tau=tau,
                      mark_pixels=args.mark_pixels,
                      npix=args.npix,
                      limy=(args.miny,args.maxy),
                      save=args.do_save)
        else:
            plot_maps(datadir=args.datadir,
                      tau=tau,
                      mark_pixels=args.mark_pixels,
                      npix=args.npix,
                      what=args.plot,
                      limz=(args.miny,args.maxy),
                      scale=args.scale,
                      save=args.do_save)

    if taus and args.do_show:
        plt.show()


#if __name__ == "__main__":
#    main()