forked from nextgis/qgis_molusce
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweightofevidencewidget.py
More file actions
202 lines (171 loc) · 7.47 KB
/
weightofevidencewidget.py
File metadata and controls
202 lines (171 loc) · 7.47 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
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
# -*- coding: utf-8 -*-
#******************************************************************************
#
# MOLUSCE
# ---------------------------------------------------------
# Modules for Land Use Change Simulations
#
# Copyright (C) 2012-2013 NextGIS (info@nextgis.org)
#
# This source 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.
#
# This code is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# A copy of the GNU General Public License is available on the World Wide Web
# at <http://www.gnu.org/licenses/>. You can also obtain it by writing
# to the Free Software Foundation, 51 Franklin Street, Suite 500 Boston,
# MA 02110-1335 USA.
#
#******************************************************************************
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from algorithms.models.area_analysis.manager import AreaAnalyst
from algorithms.models.woe.manager import WoeManager, WoeManagerError
from algorithms import dataprovider
import spinboxdelegate
from ui.ui_weightofevidencewidgetbase import Ui_Widget
import molusceutils as utils
class WeightOfEvidenceWidget(QWidget, Ui_Widget):
def __init__(self, plugin, parent=None):
QWidget.__init__(self, parent)
self.setupUi(self)
self.plugin = plugin
self.inputs = plugin.inputs
self.settings = QSettings("NextGIS", "MOLUSCE")
self.btnTrainModel.clicked.connect(self.trainModel)
self.btnResetBins.clicked.connect(self.__resetBins)
self.manageGui()
def manageGui(self):
if not utils.checkFactors(self.inputs):
QMessageBox.warning(self.plugin,
self.tr("Missed input data"),
self.tr("Factors rasters is not set. Please specify them and try again")
)
return
if not utils.checkChangeMap(self.inputs):
QMessageBox.warning(self.plugin,
self.tr("Missed input data"),
self.tr("Change map raster is not set. Please create it try again")
)
return
self.tblReclass.clearContents()
self.delegate = spinboxdelegate.SpinBoxDelegate(self.tblReclass.model(), minRange=2, maxRange=dataprovider.MAX_CATEGORIES)
row = 0
for k, v in self.inputs["factors"].iteritems():
v.denormalize() # Denormalize the factor's bands if they are normalized
for b in xrange(1, v.getBandsCount()+1):
if v.isCountinues(b):
self.tblReclass.insertRow(row)
if v.getBandsCount()>1:
name = u"%s (band %s)" % (utils.getLayerById(k).name(), unicode(b))
else:
name = u"%s" % (utils.getLayerById(k).name(), )
stat = v.getBandStat(b)
for n, item_data in enumerate([name, (u"%f" % (stat["min"], )), (u"%f" % (stat["max"])), u"" , u"" ]):
item = QTableWidgetItem(item_data)
if n < 3:
item.setFlags(item.flags() ^ Qt.ItemIsEditable)
self.tblReclass.setItem(row, n, item)
row += 1
rowCount = row
self.tblReclass.setItemDelegateForColumn(3, self.delegate)
for row in range(rowCount):
# Set 2 bins as default value
self.tblReclass.setItem(row, 3, QTableWidgetItem(u'2'))
self.tblReclass.resizeRowsToContents()
self.tblReclass.resizeColumnsToContents()
def trainModel(self):
if not utils.checkInputRasters(self.inputs):
QMessageBox.warning(self.plugin,
self.tr("Missed input data"),
self.tr("Initial or final raster is not set. Please specify input data and try again")
)
return
if not utils.checkFactors(self.inputs):
QMessageBox.warning(self.plugin,
self.tr("Missed input data"),
self.tr("Factors rasters is not set. Please specify them and try again")
)
return
myBins = self.__getBins()
self.plugin.logMessage(self.tr("Init WoE model"))
try:
model = WoeManager(self.inputs["factors"].values(), self.plugin.analyst, bins=myBins)
except WoeManagerError as err:
QMessageBox.warning(self.plugin,
self.tr("Initialization error"),
err.msg
)
return
self.inputs["model"] = model
if not model.checkBins():
QMessageBox.warning(self.plugin,
self.tr("Wrong ranges"),
self.tr("Ranges are not correctly specified. Please specify them and try again (use space as separator)")
)
return
model.moveToThread(self.plugin.workThread)
self.plugin.workThread.started.connect(model.train)
model.updateProgress.connect(self.plugin.showProgress)
model.rangeChanged.connect(self.plugin.setProgressRange)
model.errorReport.connect(self.plugin.logErrorReport)
model.processFinished.connect(self.__trainFinished)
model.processFinished.connect(self.plugin.workThread.quit)
self.plugin.workThread.start()
def __trainFinished(self):
model = self.inputs["model"]
self.plugin.workThread.started.disconnect(model.train)
model.updateProgress.disconnect(self.plugin.showProgress)
model.rangeChanged.connect(self.plugin.setProgressRange)
model.processFinished.disconnect(self.__trainFinished)
model.processFinished.disconnect(self.plugin.workThread.quit)
model.errorReport.disconnect(self.plugin.logErrorReport)
self.plugin.restoreProgressState()
self.plugin.logMessage(self.tr("WoE model trained"))
self.pteWeightsInform.appendPlainText( unicode(model.weightsToText()) )
def __getBins(self):
bins = dict()
n = 0
for k, v in self.inputs["factors"].iteritems():
lst = []
for b in xrange(v.getBandsCount()):
lst.append(None)
if v.isCountinues(b+1):
if v.getBandsCount()>1:
name = u"%s (band %s)" % (utils.getLayerById(k).name(), unicode(b+1))
else:
name = u"%s" % (utils.getLayerById(k).name(), )
items = self.tblReclass.findItems(name, Qt.MatchExactly)
idx = self.tblReclass.indexFromItem(items[0])
reclassList = self.tblReclass.item(idx.row(), 4).text()
try:
lst[b] = [int(j) for j in reclassList.split(" ")]
except ValueError:
QMessageBox.warning(self.plugin,
self.tr("Wrong ranges"),
self.tr("Ranges are not correctly specified. Please specify them and try again (use space as separator)")
)
return {}
bins[n] = lst
n += 1
return bins
def __resetBins(self):
for row in xrange(self.tblReclass.rowCount()):
try:
rangeMin = float(self.tblReclass.item(row, 1).text())
rangeMax = float(self.tblReclass.item(row, 2).text())
intervals = int(float(self.tblReclass.item(row, 3).text()))
except ValueError:
continue
delta = (rangeMax - rangeMin)/intervals
item = [unicode( int(rangeMin + delta*(i)) ) for i in range(1,intervals)]
item = u" ".join(item)
item = QTableWidgetItem(item)
self.tblReclass.setItem(row, 4, item)