-
Notifications
You must be signed in to change notification settings - Fork 1
/
completeness_wdg.py
205 lines (155 loc) · 5.9 KB
/
completeness_wdg.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name : GEM Modellers Toolkit plugin (GEM-MT)
Description : Analysing and Processing Earthquake Catalogue Data
Date : Jul 18, 2012
copyright : (C) 2012 by Giuseppe Sucameli (Faunalia)
email : brush.tyler@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from utils import Utils, LayerStyler
import numpy as np
class CompletenessWdg(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setupUi()
def setupUi(self):
layout = QVBoxLayout(self)
label = QLabel("Time window (years)", self)
layout.addWidget(label)
self.time_window = QLineEdit("1", self)
validator = QIntValidator(self)
validator.setBottom( 1 )
self.time_window.setValidator( validator )
layout.addWidget(self.time_window)
label = QLabel("Magnitude bin width", self)
layout.addWidget(label)
self.magn_bin_width = QLineEdit("0.2", self)
validator = QDoubleValidator(self)
validator.setBottom( 0.1 )
self.magn_bin_width.setValidator( validator )
layout.addWidget(self.magn_bin_width)
label = QLabel("Sensitivity", self)
layout.addWidget(label)
self.sensitivity = QLineEdit("0.1", self)
validator = QDoubleValidator(self)
validator.setBottom( 0.001 )
self.sensitivity.setValidator( validator )
layout.addWidget(self.sensitivity)
self.increment_lock = QCheckBox("Increment lock", self)
self.increment_lock.setChecked( True )
layout.addWidget(self.increment_lock)
spacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
layout.addItem(spacer)
self.toAsciiBtn = QPushButton("Save to file", self)
QObject.connect( self.toAsciiBtn, SIGNAL("clicked()"), self.toAscii )
layout.addWidget(self.toAsciiBtn)
self.plotBtn = QPushButton("Plot", self)
QObject.connect( self.plotBtn, SIGNAL("clicked()"), self.plot )
layout.addWidget(self.plotBtn)
self.setLayout(layout)
def stepp_completeness(self, matrix):
""" This method runs stepp completeness routines on the data.
@params:
**matrix** matrix with these columns in order:
year, magnitude
"""
# convert QVariant objects to proper values
def toYearPart(date):
""" convert QVariant date object to (year, month, day) tuple """
d = Utils.valueFromQVariant(date)
return d.year
# convert QVariant objects to proper values
year = np.vectorize(toYearPart)(matrix[:, 0])
magnitude = np.vectorize(lambda x: float(x) )(matrix[:, 1])
# get options
time_window = float(self.time_window.text())
magn_bin_width = float(self.magn_bin_width.text())
sensitivity = float(self.sensitivity.text())
increment_lock = self.increment_lock.isChecked()
# run now!
from .mtoolkit.scientific.completeness import stepp_analysis
return stepp_analysis(year, magnitude, magn_bin_width, time_window, sensitivity, increment_lock)
def requestData(self, fieldkeys):
data, panMap = [], {}
# ask for populating data and panMap objects
self.emit( SIGNAL("dataRequested"), data, panMap )
if len(data) <= 0:
return
# get indexes of fields required to execute the algorithm
indexes = []
for f in fieldkeys:
try:
indexes.append( panMap[f] )
except KeyError:
QMessageBox.warning(self, "Processing", u"Cannot find the field containing the %s. Such field is required to execute the selected algorithm." % f)
return
# convert input data to a matrix
data = np.array(data)
return data, panMap, indexes
def plot(self):
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
try:
dlg = self._plot()
if dlg is None:
return
finally:
QApplication.restoreOverrideCursor()
# plot clustered data!
dlg.show()
dlg.exec_()
dlg.deleteLater()
def _plot(self):
req = self.requestData( ['date', 'magnitude'] )
if req is None:
return
data, panMap, indexes = req
# run the algorithm
completeness_table = self.stepp_completeness( data[:, indexes] )
# create the plot dialog
from plot_wdg import ScatterPlotDlg
plot = CompletenessPlotDlg( parent=None, title="Magnitude completeness", labels=("Year", "Magnitude") )
plot.setData( completeness_table[:, 0], completeness_table[:, 1] )
return plot
def toAscii(self):
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
try:
completeness_table = self._toAscii()
if completeness_table is None:
return
finally:
QApplication.restoreOverrideCursor()
# store the output to ascii file
filename = QFileDialog.getSaveFileName( self, "Choose where to save the output", "", "ASCII file (*.txt)" )
if filename == "":
return
if filename[-4:] != ".txt":
filename += ".txt"
with open( unicode(filename), 'w' ) as fout:
fout.write( repr(completeness_table) )
def _toAscii(self):
req = self.requestData( ['date', 'magnitude'] )
if req is None:
return
data, panMap, indexes = req
# run the algorithm
completeness_table = self.stepp_completeness( data[:, indexes] )
return completeness_table
from plot_wdg import PlotDlg, PlotWdg
class CompletenessPlotDlg(PlotDlg):
def createPlot(self, *args, **kwargs):
return CompletenessPlotWdg(*args, **kwargs)
class CompletenessPlotWdg(PlotWdg):
def _plot(self):
self.axes.step( self.x, self.y )