misc_ttplot.py 2.3 KB
title = "TT-Plot"
tip = "displays and fits and scales two data sets"
onein = 2

import numpy as np

from guiqwt import pyplot
from guidata.qt.QtGui import QMessageBox
from guidata.dataset.datatypes import DataSet
from guidata.dataset.dataitems import (IntItem, StringItem, ChoiceItem, FloatItem, BoolItem)
from guiqwt.config import _

from nodfitting import correlate

def nint(x):
    if x > 0: return int(x+0.5)
    else: return int(x-0.5)

class NOD3_App:

    def __init__(self, parent):
        self.parent = parent
        self.parent.activateWindow()

    def Error(self, msg):
        QMessageBox.critical(self.parent.parent(), title,
                              _(u"Error:")+"\n%s" % str(msg))

    def compute_app(self, **args):
        class FuncParam(DataSet):
              fit = BoolItem("Fit:", default=True)
              sort = BoolItem("Sort:", default=False)
              #fabs = BoolItem("Absolut:", default=False)
              cut = FloatItem("Cut:", default=0.0)
        name = title.replace(" ", "")
        if args == {}:
           param = FuncParam(_(title), "Plots intensities of two maps")
        else:
           param = self.parent.ScriptParameter(name, args)
        # if no parameter needed set param to None. activate next line
        #param = None
        self.parent.compute_11(name, lambda m, p: self.function(m, p), param, onein) 

    def function(self, ms, p):
        data1 = ms[0].data
        data2 = ms[1].data
        if hasattr(p, 'fabs') and p.fabs:
           data1 = np.abs(data1)
           data2 = np.abs(data2)
        a, b, d1, d2 = correlate(data1, data2, fmax=p.cut, out=True, sort=p.sort)
        self.parent.fig = pyplot.figure("TT-plot")
        fit = str("data2 = %.3f + %.3f*data1" % (a, b))
        if p.sort:
           pyplot.plot(d1, d2, "g+", label="sorted data points")
        else:
           pyplot.plot(d1, d2, "g+", label="data points")
        if p.fit: 
           pyplot.legend(pos="TL")
        try:
           x = np.array([np.nanmin(d1), np.nanmax(d1)])
        except:
           self.Error("sorry, no solution possible")
           return [], p
        if p.fit: 
           pyplot.plot(x, a+b*x, "r-", label=fit)
        pyplot.xlabel("data #1")
        pyplot.ylabel("data #2")
        pyplot.zlabel("TT-plot")
        pyplot.show(mainloop=False)
        return [], p