base_RG_adjust_single.py 2.7 KB
title = "Adjust single"
tip = "corrects specific baselines only"
onein = True

import numpy as np

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 _

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):
            direct = ChoiceItem("direction", ("x", "y"), default=0)
            f = IntItem('first baseline (x/y)', default=0, max=10000, min=0)
            l = IntItem('last baseline (x/y)', default=0, max=10000, min=0)
            s1 = IntItem('from (left/lower)', default=0, max=10000, min=0)
            e1 = IntItem('until (left/lower)', default=3, max=10000, min=0)
            s2 = IntItem('to (right/upper)', default=0, max=10000, min=0)
            e2 = IntItem('until (right/upper)', default=0, max=10000, min=0)
            off1 = FloatItem('offset left/lower', default=0.)
            off2 = FloatItem('offset right/upper', default=0.)
        name = title.replace(" ", "")
        if args == {}:
            param = FuncParam(_(title), "(!) USE VIEW -> AXES:PIXEL TO DETERMINE YOUR INPUTS (!)\nSelect the direction of the baselines, the baselines to correct, and the pixels used to define the base-level on the left/lower and right/upper side. Counting starts at 0. Optionally an offset can be given for either end of the baseline.")
        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, m, p):
        if (p.f>p.l) or (p.s1>p.e1) or (p.s2>p.e2) or (p.e1>p.s2):
            self.Error("please check your inputs, something does not make sense here!")
            return [], None
        if p.direct==1:
            for bl in range(p.f,p.l+1):
                a,b=np.polyfit([p.s1,p.e2+1-p.s1],[np.median(m.data[p.s1:p.e1+1,bl])-p.off1,np.median(m.data[p.s2:p.e2+1,bl])-p.off2],1)
                baseline=np.polyval([a,b],range(0,len(m.data[bl,:])))
                m.data[:,bl]-=baseline
        else:
            for bl in range(p.f,p.l+1):
                a,b=np.polyfit([p.s1,p.e2+1-p.s1],[np.median(m.data[bl,p.s1:p.e1+1]-p.off1),np.median(m.data[bl,p.s2:p.e2+1]-p.off2)],1)
                baseline=np.polyval([a,b],range(0,len(m.data[bl,:])))
                m.data[bl,:]-=baseline
        return m, p