pol_histo.py
3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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