forked from GeoDaCenter/geoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LowessParamDlg.cpp
292 lines (258 loc) · 10.4 KB
/
LowessParamDlg.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* GeoDa TM, Copyright (C) 2011-2015 by Luc Anselin - all rights reserved
*
* This file is part of GeoDa.
*
* GeoDa 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 3 of the License, or
* (at your option) any later version.
*
* GeoDa 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <wx/wx.h>
#include <boost/foreach.hpp>
#include <wx/textdlg.h>
#include <wx/valnum.h>
#include <wx/valtext.h>
#include <wx/xrc/xmlres.h>
#include <wx/settings.h>
#include "../GdaConst.h"
#include "../logger.h"
#include "../Project.h"
#include "../DialogTools/WebViewHelpWin.h"
#include "LowessParamDlg.h"
LowessParamFrame::LowessParamFrame(double f, int iter, double delta_factor, Project* project_)
: wxFrame((wxWindow*) 0, wxID_ANY, _("LOWESS Smoother Parameters"),
wxDefaultPosition, wxSize(400, -1), wxDEFAULT_FRAME_STYLE),
LowessParamObservable(f, iter, delta_factor),
project(project_)
{
wxLogMessage("Entering LowessParamFrame::LowessParamFrame");
wxPanel* panel = new wxPanel(this);
if (!wxSystemSettings::GetAppearance().IsDark()) {
panel->SetBackgroundColour(*wxWHITE);
SetBackgroundColour(*wxWHITE);
}
wxButton* help_btn = new wxButton(panel, XRCID("ID_HELP_BTN"), _("Help"),
wxDefaultPosition, wxDefaultSize,
wxBU_EXACTFIT);
wxButton* apply_btn = new wxButton(panel, XRCID("ID_APPLY_BTN"), _("Apply"),
wxDefaultPosition, wxDefaultSize,
wxBU_EXACTFIT);
wxButton* reset_defaults_btn = new wxButton(panel,
XRCID("ID_RESET_DEFAULTS_BTN"),
_("Reset"), wxDefaultPosition,
wxDefaultSize, wxBU_EXACTFIT);
wxStaticText* f_stat_t = new wxStaticText(panel, wxID_ANY, _("Bandwidth:"));
f_text = new wxTextCtrl(panel, XRCID("ID_F_TEXT"),
wxString::Format("%f", GetF()), wxDefaultPosition,
wxSize(100, -1), wxTE_PROCESS_ENTER);
f_text->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
Connect(XRCID("ID_F_TEXT"), wxEVT_TEXT,
wxCommandEventHandler(LowessParamFrame::OnFTextChange));
Connect(XRCID("ID_F_TEXT"), wxEVT_COMMAND_TEXT_ENTER,
wxCommandEventHandler(LowessParamFrame::OnApplyBtn));
wxStaticText* iter_stat_t = new wxStaticText(panel, wxID_ANY,
_("Iterations:"));
iter_text = new wxTextCtrl(panel, XRCID("ID_ITER_TEXT"),
wxString::Format("%d", GetIter()),
wxDefaultPosition, wxSize(100, -1),
wxTE_PROCESS_ENTER);
iter_text->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
Connect(XRCID("ID_ITER_TEXT"),
wxEVT_TEXT,
wxCommandEventHandler(LowessParamFrame::OnIterTextChange));
Connect(XRCID("ID_ITER_TEXT"), wxEVT_COMMAND_TEXT_ENTER,
wxCommandEventHandler(LowessParamFrame::OnApplyBtn));
wxStaticText* delta_factor_stat_t =
new wxStaticText(panel, wxID_ANY, _("Delta Factor:"));
delta_factor_text = new wxTextCtrl(panel,
XRCID("ID_DELTA_FACTOR_TEXT"),
wxString::Format("%.4f", GetDeltaFactor()),
wxDefaultPosition, wxSize(100, -1),
wxTE_PROCESS_ENTER);
delta_factor_text->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
Connect(XRCID("ID_DELTA_FACTOR_TEXT"),
wxEVT_TEXT,
wxCommandEventHandler(LowessParamFrame::OnDeltaFactorTextChange));
Connect(XRCID("ID_DELTA_FACTOR_TEXT"), wxEVT_COMMAND_TEXT_ENTER,
wxCommandEventHandler(LowessParamFrame::OnApplyBtn));
Connect(XRCID("ID_HELP_BTN"),
wxEVT_BUTTON,
wxCommandEventHandler(LowessParamFrame::OnHelpBtn));
Connect(XRCID("ID_APPLY_BTN"),
wxEVT_BUTTON,
wxCommandEventHandler(LowessParamFrame::OnApplyBtn));
Connect(XRCID("ID_RESET_DEFAULTS_BTN"),
wxEVT_BUTTON,
wxCommandEventHandler(LowessParamFrame::OnResetDefaultsBtn));
// Arrange above widgets in panel using sizers.
// Top level panel sizer will be panel_h_szr
// Below that will be panel_v_szr
// panel_v_szr will directly receive widgets
wxFlexGridSizer* fg_sizer = new wxFlexGridSizer(3, 2, 3, 3);
fg_sizer->Add(f_stat_t, 0, wxALIGN_CENTRE_VERTICAL | wxALIGN_RIGHT);
fg_sizer->Add(f_text, 0, wxALIGN_CENTRE_VERTICAL);
fg_sizer->Add(iter_stat_t, 0, wxALIGN_CENTRE_VERTICAL | wxALIGN_RIGHT);
fg_sizer->Add(iter_text, 0, wxALIGN_CENTRE_VERTICAL);
fg_sizer->Add(delta_factor_stat_t, 0, wxALIGN_CENTRE_VERTICAL|wxALIGN_RIGHT);
fg_sizer->Add(delta_factor_text, 0, wxALIGN_CENTRE_VERTICAL);
wxBoxSizer* btns_row1_h_szr = new wxBoxSizer(wxHORIZONTAL);
btns_row1_h_szr->Add(help_btn, 0, wxALIGN_CENTER_VERTICAL);
btns_row1_h_szr->AddSpacer(8);
btns_row1_h_szr->Add(reset_defaults_btn, 0, wxALIGN_CENTER_VERTICAL);
btns_row1_h_szr->AddSpacer(8);
btns_row1_h_szr->Add(apply_btn, 0, wxALIGN_CENTER_VERTICAL);
wxBoxSizer* panel_v_szr = new wxBoxSizer(wxVERTICAL);
panel_v_szr->Add(fg_sizer, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
panel_v_szr->AddSpacer(2);
panel_v_szr->Add(btns_row1_h_szr, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
wxBoxSizer* panel_h_szr = new wxBoxSizer(wxHORIZONTAL);
panel_h_szr->Add(panel_v_szr, 1, wxEXPAND);
panel->SetSizer(panel_h_szr);
// Top Sizer for Frame
wxBoxSizer* top_h_sizer = new wxBoxSizer(wxHORIZONTAL);
top_h_sizer->Add(panel, 1, wxEXPAND|wxALL, 8);
SetSizerAndFit(top_h_sizer);
Show(true);
wxLogMessage("Exiting LowessParamFrame::LowessParamFrame");
}
LowessParamFrame::~LowessParamFrame()
{
wxLogMessage("In LowessParamFrame::~LowessParamFrame");
notifyObserversOfClosing();
}
void LowessParamFrame::OnHelpBtn(wxCommandEvent& ev)
{
wxLogMessage("In LowessParamFrame::OnHelpBtn");
WebViewHelpWin* win = new WebViewHelpWin(project, GetHelpPageHtml(), NULL,
wxID_ANY, _("LOWESS Smoother Help"));
}
void LowessParamFrame::OnApplyBtn(wxCommandEvent& ev)
{
wxLogMessage("In LowessParamFrame::OnApplyBtn");
UpdateParamsFromFields();
notifyObservers();
}
void LowessParamFrame::OnResetDefaultsBtn(wxCommandEvent& ev)
{
wxLogMessage("In LowessParamFrame::OnResetDefaultsBtn");
f = Lowess::default_f;
iter = Lowess::default_iter;
delta_factor = Lowess::default_delta_factor;
f_text->ChangeValue(wxString::Format("%f", GetF()));
iter_text->ChangeValue(wxString::Format("%d", GetIter()));
delta_factor_text->ChangeValue(wxString::Format("%f", GetDeltaFactor()));
OnApplyBtn(ev);
}
void LowessParamFrame::OnFTextChange(wxCommandEvent& ev)
{
}
void LowessParamFrame::OnIterTextChange(wxCommandEvent& ev)
{
}
void LowessParamFrame::OnDeltaFactorTextChange(wxCommandEvent& event)
{
if (((wxKeyEvent&)event).GetKeyCode() == WXK_RETURN) {
wxCommandEvent ev;
OnApplyBtn(ev);
}
}
void LowessParamFrame::closeAndDeleteWhenEmpty()
{
Close(true);
}
void LowessParamFrame::UpdateParamsFromFields()
{
wxLogMessage("Entering LowessParamFrame::UpdateParamsFromFields");
Lowess temp_l(GetF(), GetIter(), GetDeltaFactor());
{
wxString s = f_text->GetValue();
double v;
if (s.ToDouble(&v))
temp_l.SetF(v);
f = temp_l.GetF();
wxString sf = wxString::Format("%f", GetF());
f_text->ChangeValue(sf);
}
{
wxString s = iter_text->GetValue();
long v;
if (s.ToLong(&v))
temp_l.SetIter((long) v);
iter = temp_l.GetIter();
wxString sf = wxString::Format("%d", GetIter());
iter_text->ChangeValue(sf);
}
{
wxString s = delta_factor_text->GetValue();
double v;
if (s.ToDouble(&v))
temp_l.SetDeltaFactor(v);
delta_factor = temp_l.GetDeltaFactor();
wxString sf = wxString::Format("%.4f", GetDeltaFactor());
delta_factor_text->ChangeValue(sf);
}
wxLogMessage("Exiting LowessParamFrame::UpdateParamsFromFields");
}
wxString LowessParamFrame::GetHelpPageHtml() const
{
wxString s;
s << "<!DOCTYPE html>";
s << "<html>";
s << "<head>";
s << " <style type=\"text/css\">";
s << " body {";
s << " font-family: \"Trebuchet MS\", Arial, Helvetica, sans-serif;";
s << " font-size: small;";
s << " }";
s << " h1 h2 {";
s << " font-family: \"Trebuchet MS\", Arial, Helvetica, sans-serif;";
s << " color: blue;";
s << " }";
s << "</style>";
s << "</head>";
s << "<body>";
s << "<h2>LOWESS Smoother Overview</h2>";
s << "<p>";
s << "The LOWESS smoother algorithm uses locally-weighted polynomial ";
s << "regression. This program is based on the function called ";
s << "“lowess” in the R stats package. An internet search ";
s << "for “lowess stats R” will bring up the relevent ";
s << "R manual page. The parameters documented below are respectively ";
s << "refered to as “f”, “iter” and ";
s << "“delta” in the R lowess manual page.";
s << "</p>";
s << "<h2>Bandwidth</h2>";
s << "<p>";
s << "The proportion of points in the plot that ";
s << "influence smoothing at each value. Larger values give ";
s << "more smoothness.";
s << "</p>";
s << "<h2>Iterations</h2>";
s << "<p>";
s << "The number of “robustifying” iterations which ";
s << "should be performed. ";
s << "Using smaller values of iter will speed up the smoothing.";
s << "</p>";
s << "<h2>Delta Factor</h2>";
s << "<p>";
s << "Delta Factor is multiplied by the range of values ";
s << "in the x-coordinates to produce the internal parameter delta. ";
s << "Small values of delta ";
s << "speed up computation: instead of computing the local polynomial ";
s << "fit at each data point it is not computed for points within ";
s << "delta of the last computed point, and linear interpolation is ";
s << "used to fill in the fitted values for the skipped points.";
s << "</p>";
s << "</body>";
s << "</html>";
return s;
}