pol_histo.py 3.83 KB
title = "Histogram"
tip = "density distribution on a map"
onein = 1

import numpy as np
import scipy.stats as ss
from scipy import optimize, signal

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 _

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):
            s = StringItem('s', default="string")
            i = IntItem('i', default=0, max=100, min=0)
            a = FloatItem('a', default=1.)
            b = BoolItem("bool", default=True)
            choice = ChoiceItem("Unit", ("Degree", "Arcmin", "Arcsec"), default=2)
        name = title.replace(" ", "")
        if args == {}:
           param = FuncParam(_(title), "description")
        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 hist(self, data, lh, x0, txt):
        try: 
             self.parent.N += 1
        except:
             self.parent.N = 1
             self.parent.fig = pyplot.figure("Intensity distribution")
             self.Nanz = len(self.parent._get_selected_rows())
             self.ix = max(1, int(np.sqrt(self.Nanz+0.25)-0.5 + 0.99))
             self.iy = int(float(self.Nanz)/float(self.ix) + 0.99)
        pyplot.subplot(self.ix, self.iy, self.parent.N)
        if txt != "": pyplot.legend()
        pyplot.hist(data, title=txt, color='blue')
        pyplot.xlabel("Intensity")
        pyplot.ylabel("#bins")
        pyplot.zlabel("distribution")
        if self.parent.N == self.Nanz: pyplot.show(mainloop=False)

    def plot(self, xy, lh, x0, txt):
        x = xy[1][:lh]
        y = xy[0][:lh]
        try: 
             self.parent.N += 1
        except:
             self.parent.N = 1
             self.parent.fig = pyplot.figure("Intensity distribution")
             self.Nanz = len(self.parent._get_selected_rows())
             self.ix = max(1, int(np.sqrt(self.Nanz+0.25)-0.5 + 0.99))
             self.iy = int(float(self.Nanz)/float(self.ix) + 0.99)
        pyplot.subplot(self.ix, self.iy, self.parent.N)
        if txt != "": pyplot.legend()
        pyplot.plot(x, y, "b-", label=txt)
        pyplot.xlabel("Intensity")
        pyplot.ylabel("#bins")
        pyplot.zlabel("distribution")
        if self.parent.N == self.Nanz: pyplot.show(mainloop=False)

    def function(self, m, p):
        hdata = m.data
        ny, nx = hdata.shape
	mask = ~np.isnan(hdata)
	hdata = hdata[mask]
        med = max(5*np.median(hdata), 5*abs(hdata.min()))
        bins = min(int(np.sqrt(nx*ny)), 128)
        hdata = np.where(abs(hdata) < med, hdata, med)
        hist, bin_edges = np.histogram(hdata, bins=bins)
        dbin = bin_edges[1] - bin_edges[0]
        #hist /= dbin
        bin_edges += dbin/2.0
        hist = hist[1:]
        bin_edges = bin_edges[1:-1]
        #bin_edges = bin_edges[:-1]
        lh = len(hist)
        for i in range(len(hist)):
            if abs(bin_edges[i]) < med: lh = i
        hmax = 0.0
        imax = 0
        for i in range(lh):
            if hist[i] > imax:
               hmax = bin_edges[i]
               imax = hist[i]
        if "EXTNAME" in m.header: txt = m.header["EXTNAME"].replace("MAP-", "")
        elif "MAPTYPE" in m.header: txt = m.header["MAPTYPE"]
        else: txt = ""
        self.plot([hist, bin_edges], lh, hmax, txt)
        #self.hist(hdata, lh, hmax, txt)
        return [], p