maped_dummy_repair.py 2.12 KB
title = "Repair Dummies"
tip = "repair single dummies if possible"
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 Info(self, msg):
        QMessageBox.information(self.parent.parent(), title,
                              str(msg))

    def compute_app(self, **args):
        name = title.replace(" ", "")
        # 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 yintpn(self, a, b, c, d, y):
        if y == 0.0:
           return b
        e = b-a
        f = b-c
        g = e+f+f+d-c
        g = g*y-e-f-g
        g = g*y-a+c
        return 0.5*g*y+b

    def repair(self, data):
        rows, cols = data.shape
        #piy, pix = np.where(np.isnan(data[2:-2,2:-2]))
        piy, pix = np.where(np.isnan(data))
        notrepaired = 0
        for i in range(len(pix)):
          try:
            #y, x = piy[i]+2, pix[i]+2
            y, x = piy[i], pix[i]
            if y+1 < rows and not np.isnan(data[y-1, x]) and not np.isnan(data[y+1, x]): 
               data[y,x] = self.yintpn(data[y-2, x], data[y-1, x], data[y+1, x], data[y+2, x], 0.5)
            elif x+1 < cols and not np.isnan(data[y, x-1]) and not np.isnan(data[y, x+1]): 
               data[y,x] = self.yintpn(data[y, x-2], data[y, x-1], data[y, x+1], data[y, x+2], 0.5)
            else:
               notrepaired += 1
          except:
            notrepaired += 1
        return notrepaired 

    def function(self, m, p):
        n = self.repair(m.data)   
        n = self.repair(m.data)   
        if n > 0:
           self.Info(str("%d Dummies can't be repaired!" % n))
        return m, p